|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io/ioutil" |
| 9 | + "log" |
| 10 | + "net/http" |
| 11 | + "os" |
| 12 | + "strconv" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "github.com/google/go-github/github" |
| 16 | + "golang.org/x/oauth2" |
| 17 | +) |
| 18 | + |
| 19 | +type IssueName struct { |
| 20 | + Name string `json:"name"` |
| 21 | +} |
| 22 | + |
| 23 | +type IssueValue struct { |
| 24 | + Value string `json:"value"` |
| 25 | +} |
| 26 | + |
| 27 | +type IssueKey struct { |
| 28 | + Key string `json:"key"` |
| 29 | +} |
| 30 | + |
| 31 | +type IssueFields struct { |
| 32 | + Project IssueKey `json:"project"` |
| 33 | + Summary string `json:"summary"` |
| 34 | + Description string `json:"description"` |
| 35 | + MyTeam IssueValue `json:"customfield_14803"` |
| 36 | + IssueType IssueName `json:"issuetype"` |
| 37 | + Components []Component `json:"components"` |
| 38 | +} |
| 39 | + |
| 40 | +type Component struct { |
| 41 | + Name string `json:"name"` |
| 42 | +} |
| 43 | + |
| 44 | +type InternalIssue struct { |
| 45 | + Fields IssueFields `json:"fields"` |
| 46 | +} |
| 47 | + |
| 48 | +type IssueCreationResponse struct { |
| 49 | + ID string `json:"id"` |
| 50 | + Key string `json:"key"` |
| 51 | + Self string `json:"self"` |
| 52 | +} |
| 53 | + |
| 54 | +func main() { |
| 55 | + ctx := context.Background() |
| 56 | + if len(os.Args) < 2 { |
| 57 | + log.Fatalf("Usage: sync-github-issue-to-jira <GitHub issue number>\n") |
| 58 | + } |
| 59 | + iss := os.Args[1] |
| 60 | + issueNumber, err := strconv.Atoi(iss) |
| 61 | + if err != nil { |
| 62 | + log.Fatalf("error parsing issue %q as a number: %s", iss, err) |
| 63 | + } |
| 64 | + |
| 65 | + githubRepositoryOwner := os.Getenv("GITHUB_OWNER") |
| 66 | + githubRepositoryName := os.Getenv("GITHUB_REPO") |
| 67 | + githubAccessToken := os.Getenv("GITHUB_TOKEN") |
| 68 | + jiraHostname := os.Getenv("JIRA_HOSTNAME") |
| 69 | + jiraAuthToken := os.Getenv("JIRA_AUTH_TOKEN") |
| 70 | + accessClientID := os.Getenv("CF_ACCESS_CLIENT_ID") |
| 71 | + accessClientSecret := os.Getenv("CF_ACCESS_CLIENT_SECRET") |
| 72 | + |
| 73 | + if githubRepositoryOwner == "" { |
| 74 | + log.Fatal("GITHUB_OWNER not set") |
| 75 | + } |
| 76 | + |
| 77 | + if githubRepositoryName == "" { |
| 78 | + log.Fatal("GITHUB_REPO not set") |
| 79 | + } |
| 80 | + |
| 81 | + if githubAccessToken == "" { |
| 82 | + log.Fatal("GITHUB_TOKEN not set") |
| 83 | + } |
| 84 | + |
| 85 | + if jiraHostname == "" { |
| 86 | + log.Fatal("JIRA_HOSTNAME not set") |
| 87 | + } |
| 88 | + |
| 89 | + if jiraAuthToken == "" { |
| 90 | + log.Fatal("JIRA_AUTH_TOKEN not set") |
| 91 | + } |
| 92 | + |
| 93 | + if accessClientID == "" { |
| 94 | + log.Fatal("CF_ACCESS_CLIENT_ID not set") |
| 95 | + } |
| 96 | + |
| 97 | + if accessClientSecret == "" { |
| 98 | + log.Fatal("CF_ACCESS_CLIENT_SECRET not set") |
| 99 | + } |
| 100 | + |
| 101 | + ts := oauth2.StaticTokenSource( |
| 102 | + &oauth2.Token{AccessToken: githubAccessToken}, |
| 103 | + ) |
| 104 | + tc := oauth2.NewClient(ctx, ts) |
| 105 | + |
| 106 | + client := github.NewClient(tc) |
| 107 | + |
| 108 | + issue, _, err := client.Issues.Get(ctx, githubRepositoryOwner, githubRepositoryName, issueNumber) |
| 109 | + if err != nil { |
| 110 | + log.Fatalf("error retrieving issue %s/%s#%d: %s", githubRepositoryOwner, githubRepositoryName, issueNumber, err) |
| 111 | + } |
| 112 | + |
| 113 | + newIssue := InternalIssue{Fields: IssueFields{ |
| 114 | + Project: IssueKey{Key: "PCX"}, |
| 115 | + Summary: *issue.Title, |
| 116 | + Description: jirafyBodyMarkdown(issue), |
| 117 | + MyTeam: IssueValue{Value: "Product Management"}, |
| 118 | + IssueType: IssueName{Name: "Task"}, |
| 119 | + Components: []Component{{Name: "Other (Unknown)"}}, |
| 120 | + }} |
| 121 | + |
| 122 | + res, err := json.Marshal(newIssue) |
| 123 | + if err != nil { |
| 124 | + fmt.Println(err) |
| 125 | + } |
| 126 | + |
| 127 | + url := fmt.Sprintf("https://%s/rest/api/latest/issue/", jiraHostname) |
| 128 | + req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(res)) |
| 129 | + if err != nil { |
| 130 | + log.Fatalf("failed to build HTTP request: %s", err) |
| 131 | + } |
| 132 | + |
| 133 | + req.Header.Set("authorization", "Basic "+jiraAuthToken) |
| 134 | + req.Header.Set("cf-access-client-id", accessClientID) |
| 135 | + req.Header.Set("cf-access-client-secret", accessClientSecret) |
| 136 | + req.Header.Set("content-type", "application/json") |
| 137 | + |
| 138 | + httpClient := &http.Client{} |
| 139 | + resp, err := httpClient.Do(req) |
| 140 | + if err != nil { |
| 141 | + panic(err) |
| 142 | + } |
| 143 | + defer resp.Body.Close() |
| 144 | + |
| 145 | + body, err := ioutil.ReadAll(resp.Body) |
| 146 | + if err != nil { |
| 147 | + log.Fatalf("failed to read response body: %s", err) |
| 148 | + } |
| 149 | + |
| 150 | + var createdIssue IssueCreationResponse |
| 151 | + json.Unmarshal([]byte(body), &createdIssue) |
| 152 | + |
| 153 | + if resp.StatusCode != http.StatusCreated { |
| 154 | + fmt.Println(fmt.Sprintf("failed to create new JIRA issue")) |
| 155 | + os.Exit(1) |
| 156 | + } |
| 157 | + |
| 158 | + fmt.Println(fmt.Sprintf("successfully created internal JIRA issue: %s", createdIssue.Key)) |
| 159 | + |
| 160 | + os.Exit(0) |
| 161 | +} |
| 162 | + |
| 163 | +// jirafyBodyMarkdown takes GitHub markdown and makes it palatable for JIRA |
| 164 | +// with reasonable formatting. |
| 165 | +func jirafyBodyMarkdown(issue *github.Issue) string { |
| 166 | + output := "GitHub issue: " + *issue.HTMLURL + "\n\n---\n\n" |
| 167 | + |
| 168 | + output += *issue.Body |
| 169 | + output = strings.ReplaceAll(output, "###", "h3.") |
| 170 | + |
| 171 | + return output |
| 172 | +} |
0 commit comments