func (PingCIMunger) MungePullRequest(client *github.Client, pr *github.PullRequest, issue *github.Issue, commits []github.RepositoryCommit, events []github.IssueEvent, opts opts.MungeOptions) { if !HasLabel(issue.Labels, "lgtm") { return } if pr.Mergeable == nil || !*pr.Mergeable { glog.Infof("skipping CI check for %d since mergeable is nil or false", *pr.Number) return } status, err := github_util.GetStatus(client, opts.Org, opts.Project, *pr.Number, []string{"Shippable", "continuous-integration/travis-ci/pr"}) if err != nil { glog.Errorf("unexpected error getting status: %v", err) return } if status == "incomplete" { if opts.Dryrun { glog.Infof("would have pinged CI for %d", pr.Number) return } glog.V(2).Infof("status is incomplete, closing and re-opening") msg := "Continuous integration appears to have missed, closing and re-opening to trigger it" if _, _, err := client.Issues.CreateComment(opts.Org, opts.Project, *pr.Number, &github.IssueComment{Body: &msg}); err != nil { glog.Errorf("failed to create comment: %v", err) } state := "closed" pr.State = &state if _, _, err := client.PullRequests.Edit(opts.Org, opts.Project, *pr.Number, pr); err != nil { glog.Errorf("Failed to close pr %d: %v", *pr.Number, err) return } time.Sleep(5 * time.Second) state = "open" pr.State = &state // Try pretty hard to re-open, since it's pretty bad if we accidentally leave a PR closed for tries := 0; tries < 10; tries++ { if _, _, err := client.PullRequests.Edit(opts.Org, opts.Project, *pr.Number, pr); err == nil { break } else { glog.Errorf("failed to re-open pr %d: %v", *pr.Number, err) } time.Sleep(5 * time.Second) } } }
func (config *GithubConfig) ClosePR(pr *github.PullRequest) error { if config.DryRun { glog.Infof("Would have closed PR# %d but --dry-run was set", *pr.Number) return nil } state := "closed" pr.State = &state if _, _, err := config.client.PullRequests.Edit(config.Org, config.Project, *pr.Number, pr); err != nil { glog.Errorf("Failed to close pr %d: %v", *pr.Number, err) return err } return nil }
// OpenPR will attempt to open the given PR. func (config *GithubConfig) OpenPR(pr *github.PullRequest, numTries int) error { if config.DryRun { glog.Infof("Would have openned PR# %d but --dry-run was set", *pr.Number) return nil } var err error state := "open" pr.State = &state // Try pretty hard to re-open, since it's pretty bad if we accidentally leave a PR closed for tries := 0; tries < numTries; tries++ { if _, _, err = config.client.PullRequests.Edit(config.Org, config.Project, *pr.Number, pr); err == nil { return nil } glog.Warningf("failed to re-open pr %d: %v", *pr.Number, err) time.Sleep(5 * time.Second) } if err != nil { glog.Errorf("failed to re-open pr %d after %d tries, giving up: %v", *pr.Number, numTries, err) } return err }
func (p *PullRequestCloser) closePullRequest(owner string, repo string, pull github.PullRequest, comment string, label string) error { issueComment := &github.IssueComment{Body: github.String(comment)} _, _, err := p.client.Issues.CreateComment(owner, repo, *pull.Number, issueComment) if err != nil { return err } if label != "" { _, _, err = p.client.Issues.AddLabelsToIssue(owner, repo, *pull.Number, []string{label}) if err != nil { return err } } pull.State = github.String("closed") _, _, err = p.client.PullRequests.Edit(owner, repo, *pull.Number, &pull) if err != nil { return err } return nil }