Skip to content

Commit

Permalink
Improve duplicate handling logic
Browse files Browse the repository at this point in the history
- Fix pointer bug
- Logic: oldest issue is original, rest is duplicate instead of using newest one as original
- Better logging
  • Loading branch information
Nicolas committed Jul 28, 2022
1 parent ef96b5e commit 0bc087d
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"net/http"
"os"
"strings"
"time"

jira "github.com/andygrunwald/go-jira"
Expand Down Expand Up @@ -123,6 +124,28 @@ func handlePostWebhook(w http.ResponseWriter, r *http.Request) {
return
}

// in case of duplicate issues, update the oldest (probably original) one, ignore rest as duplicates
var oldestExistingIssue jira.Issue
var duplicateIssueKeys []string
for i, existingIssue := range existingIssues {
if i == 0 {
oldestExistingIssue = existingIssue
continue
}
tCurrent := time.Time(existingIssue.Fields.Created)
tOldest := time.Time(oldestExistingIssue.Fields.Created)
if tCurrent.Before(tOldest) {
duplicateIssueKeys = append(duplicateIssueKeys, oldestExistingIssue.Key)
oldestExistingIssue = existingIssue
} else {
duplicateIssueKeys = append(duplicateIssueKeys, existingIssue.Key)
}
}
if len(duplicateIssueKeys) > 0 {
logger.Printf("Ignoring the following possible duplicate issues in favor of older issue %v: %v", oldestExistingIssue.Key,
strings.Join(duplicateIssueKeys, ", "))
}

// This seems hacky, but is taken from the official examples
// https://github.com/andygrunwald/go-jira/blob/47d27a76e84da43f6e27e1cd0f930e6763dc79d7/examples/addlabel/main.go
// There is also a jiraClient.Issue.Update() method, but it panics and does not provide a usage example
Expand All @@ -132,31 +155,15 @@ func handlePostWebhook(w http.ResponseWriter, r *http.Request) {
type issueUpdate struct {
Summary []summaryUpdate `json:"summary" structs:"summary"`
}

var latestExistingIssue *jira.Issue = nil
for _, existingIssue := range existingIssues {
if latestExistingIssue == nil {
latestExistingIssue = &existingIssue
continue
}
tCurrent := time.Time(existingIssue.Fields.Created)
tLatest := time.Time(latestExistingIssue.Fields.Created)
if tCurrent.After(tLatest) {
logger.Printf("Ignoring older issue for same software %v\n", latestExistingIssue.Key)
latestExistingIssue = &existingIssue
} else {
logger.Printf("Ignoring older issue for same software %v\n", existingIssue.Key)
}
}
previousSummary := latestExistingIssue.Fields.Summary
previousSummary := oldestExistingIssue.Fields.Summary
updates := map[string]any{
"update": issueUpdate{
Summary: []summaryUpdate{
{Set: release.IssueSummary()},
},
},
}
resp, err = jiraClient.Issue.UpdateIssue(latestExistingIssue.ID, updates)
resp, err = jiraClient.Issue.UpdateIssue(oldestExistingIssue.ID, updates)
if err != nil {
body, readErr := io.ReadAll(resp.Body)
errCtx := errors.New("error response from Jira when updating issue")
Expand Down

0 comments on commit 0bc087d

Please sign in to comment.