func sfTicketToGhIssue(ticket *sfapi.Ticket, category string) { labels := getPatchLabels(append(ticket.Labels, category, "sourceforge"), ticket.Status) mileStone := findMatchingMilestone(ticket) issueRequest := github.IssueRequest{ Title: &ticket.Summary, Body: createSFBody(ticket, category), Labels: &labels, // Assignee: &ticket.AssignedTo, // State: &statusText, } if mileStone > 0 { issueRequest.Milestone = &mileStone } issue, response, err := githubClient.Issues.Create(config.Github.UserName, ghRepo, &issueRequest) if err != nil { log.Fatal(err) } statusText := getStatusText(ticket) if statusText != *issue.State { issue, response, err = githubClient.Issues.Edit(config.Github.UserName, ghRepo, *issue.Number, &github.IssueRequest{ State: &statusText, }) if err != nil { log.Fatal(err) } } addCommentsToIssue(ticket, issue) debug("response", response) debug("issue", issue) printf("ticket %d moved to %d\n", ticket.TicketNum, *issue.Number) }
// Edit a github issue. // Edit the issue object before passing it to this method. func EditIssue(repo string, oldIssue *github.Issue) (*github.Issue, error) { s := strings.Split(repo, "/") issueNum := *oldIssue.Number issue := new(github.IssueRequest) if len(oldIssue.Labels) != 0 { labels := []string{oldIssue.Labels[0].String()} for i := 1; i < len(oldIssue.Labels); i++ { var label string label = oldIssue.Labels[i].String() labels = append(labels, label) } issue.Labels = &labels } issue.Title = oldIssue.Title issue.Body = oldIssue.Body if oldIssue.Assignee != nil { issue.Assignee = oldIssue.Assignee.Login } issue.State = oldIssue.State updatedIssue, _, err := client.Issues.Edit(s[0], s[1], issueNum, issue) if err == nil { _, err = Issues(repo) } else { GitLog.Println("Edit issue: ", err) } return updatedIssue, err }
// Create an issue on github. // Requires repo and title args, // rest are optinal arg and can be passed empty. // Make issue put milestone at 0 for no milestone. // BUG(butlerx) Issue Creation doesnt work in offile mode. func MakeIssue(repo, title, body, assignee string, milestone int, labels []string) (*github.Issue, error) { s := strings.Split(repo, "/") newIssue := new(github.Issue) state := "open" if milestone == 0 { issue := new(github.IssueRequest) issue.Title = &title issue.Body = &body issue.Labels = &labels issue.Assignee = &assignee issue.State = &state newIssue, _, err = client.Issues.Create(s[0], s[1], issue) } else { issue := github.IssueRequest{&title, &body, &labels, &assignee, &state, &milestone} newIssue, _, err = client.Issues.Create(s[0], s[1], &issue) } if err == nil { _, err = Issues(repo) return newIssue, err } else { return newIssue, err } }
func writeIssue(old *github.Issue, updated []byte, isBulk bool) (issue *github.Issue, err error) { var errbuf bytes.Buffer defer func() { if errbuf.Len() > 0 { err = errors.New(strings.TrimSpace(errbuf.String())) } }() sdata := string(updated) off := 0 var edit github.IssueRequest var addLabels, removeLabels []string for _, line := range strings.SplitAfter(sdata, "\n") { off += len(line) line = strings.TrimSpace(line) if line == "" { break } switch { case strings.HasPrefix(line, "#"): continue case strings.HasPrefix(line, "Title:"): edit.Title = diff(line, "Title:", getString(old.Title)) case strings.HasPrefix(line, "State:"): edit.State = diff(line, "State:", getString(old.State)) case strings.HasPrefix(line, "Assignee:"): edit.Assignee = diff(line, "Assignee:", getUserLogin(old.Assignee)) case strings.HasPrefix(line, "Closed:"): continue case strings.HasPrefix(line, "Labels:"): if isBulk { addLabels, removeLabels = diffList2(line, "Labels:", getLabelNames(old.Labels)) } else { edit.Labels = diffList(line, "Labels:", getLabelNames(old.Labels)) } case strings.HasPrefix(line, "Milestone:"): edit.Milestone = findMilestone(&errbuf, diff(line, "Milestone:", getMilestoneTitle(old.Milestone))) case strings.HasPrefix(line, "URL:"): continue default: fmt.Fprintf(&errbuf, "unknown summary line: %s\n", line) } } if errbuf.Len() > 0 { return nil, nil } if getInt(old.Number) == 0 { comment := strings.TrimSpace(sdata[off:]) edit.Body = &comment issue, _, err := client.Issues.Create(projectOwner, projectRepo, &edit) if err != nil { fmt.Fprintf(&errbuf, "error creating issue: %v\n", err) return nil, nil } return issue, nil } if getInt(old.Number) == -1 { // Asking to just sanity check the text parsing. return nil, nil } marker := "\nReported by " if isBulk { marker = bulkHeader } var comment string if i := strings.Index(sdata, marker); i >= off { comment = strings.TrimSpace(sdata[off:i]) } if comment == "<optional comment here>" { comment = "" } var failed bool var did []string if comment != "" { _, _, err := client.Issues.CreateComment(projectOwner, projectRepo, getInt(old.Number), &github.IssueComment{ Body: &comment, }) if err != nil { fmt.Fprintf(&errbuf, "error saving comment: %v\n", err) failed = true } else { did = append(did, "saved comment") } } if edit.Title != nil || edit.State != nil || edit.Assignee != nil || edit.Labels != nil || edit.Milestone != nil { _, _, err := client.Issues.Edit(projectOwner, projectRepo, getInt(old.Number), &edit) if err != nil { fmt.Fprintf(&errbuf, "error changing metadata: %v\n", err) failed = true } else { did = append(did, "updated metadata") } } if len(addLabels) > 0 { _, _, err := client.Issues.AddLabelsToIssue(projectOwner, projectRepo, getInt(old.Number), addLabels) if err != nil { fmt.Fprintf(&errbuf, "error adding labels: %v\n", err) failed = true } else { if len(addLabels) == 1 { did = append(did, "added label "+addLabels[0]) } else { did = append(did, "added labels") } } } if len(removeLabels) > 0 { for _, label := range removeLabels { _, err := client.Issues.RemoveLabelForIssue(projectOwner, projectRepo, getInt(old.Number), label) if err != nil { fmt.Fprintf(&errbuf, "error removing label %s: %v\n", label, err) failed = true } else { did = append(did, "removed label "+label) } } } if failed && len(did) > 0 { var buf bytes.Buffer fmt.Fprintf(&buf, "%s", did[0]) for i := 1; i < len(did)-1; i++ { fmt.Fprintf(&buf, ", %s", did[i]) } if len(did) >= 2 { if len(did) >= 3 { fmt.Fprintf(&buf, ",") } fmt.Fprintf(&buf, " and %s", did[len(did)-1]) } all := buf.Bytes() all[0] -= 'a' - 'A' fmt.Fprintf(&errbuf, "(%s successfully.)\n", all) } return }