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

Create JIRA issues from unmatched GitHub issues #2

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
53 changes: 53 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/url"
"os"
"strings"
Expand All @@ -29,6 +30,8 @@ var (
ghIDFieldID string // The customfield ID of the GitHub ID field in JIRA
ghNumFieldID string // The customfield ID of the GitHub Number field in JIRA
ghLabelsFieldID string // The customfield ID of the GitHub Labels field in JIRA
ghStatusFieldID string // The customfield ID of the GitHub Status field in JIRA
ghReporterFieldID string // The customfield ID of the GitHub Reporter field in JIRA
isLastUpdateFieldID string // The customfield ID of the Last Issue-Sync Update field in JIRA

project jira.Project
Expand Down Expand Up @@ -223,6 +226,10 @@ func getFieldIDs(client jira.Client) error {
ghNumFieldID = fmt.Sprint(field.Schema.CustomID)
case "GitHub Labels":
ghLabelsFieldID = fmt.Sprint(field.Schema.CustomID)
case "GitHub Status":
ghStatusFieldID = fmt.Sprint(field.Schema.CustomID)
case "GitHub Reporter":
ghReporterFieldID = fmt.Sprint(field.Schema.CustomID)
case "Last Issue-Sync Update":
isLastUpdateFieldID = fmt.Sprint(field.Schema.CustomID)
}
Expand All @@ -234,6 +241,10 @@ func getFieldIDs(client jira.Client) error {
return errors.New("Could not find ID of 'GitHub Number' custom field. Check that it is named correctly.")
} else if ghLabelsFieldID == "" {
return errors.New("Could not find ID of 'Github Labels' custom field. Check that it is named correctly.")
} else if ghStatusFieldID == "" {
return errors.New("Could not find ID of 'Github Status' custom field. Check that it is named correctly.")
} else if ghReporterFieldID == "" {
return errors.New("Could not find ID of 'Github Reporter' custom field. Check that it is named correctly.")
} else if isLastUpdateFieldID == "" {
return errors.New("Could not find ID of 'Last Issue-Sync Update' custom field. Check that it is named correctly.")
}
Expand Down Expand Up @@ -308,6 +319,48 @@ func updateIssue(ghIssue github.Issue, jIssue jira.Issue) error {

func createIssue(issue github.Issue, client jira.Client) error {
log.Debugf("Creating JIRA issue based on GitHub issue #%d", *issue.Number)

fields := jira.IssueFields{
Type: jira.IssueType{
Name: "Task", // TODO: Determine issue type
},
Project: project,
Summary: *issue.Title,
Description: *issue.Body,
Unknowns: map[string]interface{}{},
}

key := fmt.Sprintf("customfield_%s", ghIDFieldID)
fields.Unknowns[key] = *issue.ID
key = fmt.Sprintf("customfield_%s", ghNumFieldID)
fields.Unknowns[key] = *issue.Number
key = fmt.Sprintf("customfield_%s", ghStatusFieldID)
fields.Unknowns[key] = *issue.State
key = fmt.Sprintf("customfield_%s", ghReporterFieldID)
fields.Unknowns[key] = issue.User.GetLogin()
key = fmt.Sprintf("customfield_%s", ghLabelsFieldID)
strs := make([]string, len(issue.Labels))
for i, v := range issue.Labels {
strs[i] = *v.Name
}
fields.Unknowns[key] = strings.Join(strs, ",")
key = fmt.Sprintf("customfield_%s", isLastUpdateFieldID)
fields.Unknowns[key] = time.Now().Format("2006-01-02T15:04:05-0700")

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

jIssue, res, err := client.Issue.Create(jIssue)
if err != nil {
log.Errorf("Error creating JIRA issue: %s", err)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
log.Debugf("Error body: %s", body)
return err
}

log.Debugf("Created JIRA issue #%s!", jIssue.ID)
return nil
}

Expand Down