Пример #1
0
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)
}
Пример #2
0
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
}