Skip to content
This repository has been archived by the owner on Sep 21, 2023. It is now read-only.

Update JIRA issues from matching GitHub issues #4

Merged
merged 1 commit into from
Jul 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 73 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,79 @@ func compareIssues(ghClient github.Client, jiraClient jira.Client) error {
return nil
}

func updateIssue(ghIssue github.Issue, jIssue jira.Issue) error {
log.Debugf("Updating JIRA issue %s with GitHub issue %d", jIssue.ID, *ghIssue.ID)
func updateIssue(ghIssue github.Issue, jIssue jira.Issue, client jira.Client) error {
log.Debugf("Updating JIRA %s with GitHub #%d", jIssue.Key, *ghIssue.Number)

anyDifferent := false

fields := jira.IssueFields{}
fields.Unknowns = map[string]interface{}{}

if *ghIssue.Title != jIssue.Fields.Summary {
anyDifferent = true
fields.Summary = *ghIssue.Title
}

if *ghIssue.Body != jIssue.Fields.Description {
anyDifferent = true
fields.Description = *ghIssue.Body
}

key := fmt.Sprintf("customfield_%s", ghStatusFieldID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should create a constant for the customFieldTemplateString = "customefield_%s" to avoid errors going forward since this is used to heavily.

field, err := jIssue.Fields.Unknowns.String(key)
if err != nil || *ghIssue.State != field {
anyDifferent = true
fields.Unknowns[key] = *ghIssue.State
}

key = fmt.Sprintf("customfield_%s", ghReporterFieldID)
field, err = jIssue.Fields.Unknowns.String(key)
if err != nil || *ghIssue.User.Login != field {
anyDifferent = true
fields.Unknowns[key] = *ghIssue.User.Login
}

labels := make([]string, len(ghIssue.Labels))
for i, l := range ghIssue.Labels {
labels[i] = *l.Name
}

key = fmt.Sprintf("customfield_%s", ghLabelsFieldID)
field, err = jIssue.Fields.Unknowns.String(key)
if err != nil || strings.Join(labels, ",") != field {
anyDifferent = true
fields.Unknowns[key] = strings.Join(labels, ",")
}

if !anyDifferent {
log.Debugf("JIRA issue %s is already up to date!", jIssue.Key)
return nil
}

key = fmt.Sprintf("customfield_%s", isLastUpdateFieldID)
fields.Unknowns[key] = time.Now().Format("2006-01-02T15:04:05-0700")

fields.Type = jIssue.Fields.Type
fields.Summary = jIssue.Fields.Summary

issue := &jira.Issue{
Fields: &fields,
Key: jIssue.Key,
ID: jIssue.ID,
}

issue, res, err := client.Issue.Update(issue)

if err != nil {
log.Errorf("Error updating JIRA issue %s: %s", jIssue.Key, err)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
log.Debugf("Error body: %s", body)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more descriptive error please, e.g. error reading body from jira update issue request

return err
}

log.Debugf("Successfully updated JIRA issue %s!", jIssue.Key)

return nil
}

Expand Down