Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for overriding base branch names #64

Merged
merged 6 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Differentiate between Github 422 errors
- Github returns HTTP status 422 for several different error states
- Now that we're introducing --base-branch-name, we need to
differentiate between the error states
  • Loading branch information
zackproser committed Jan 5, 2022
commit 3df49f1e544018e2fddbbd0eff1c2b8359872e71
31 changes: 21 additions & 10 deletions repository/repo-operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"strings"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
Expand Down Expand Up @@ -452,31 +453,41 @@ func openPullRequest(config *config.GitXargsConfig, repo *github.Repository, bra
pr, resp, err := config.GithubClient.PullRequests.Create(context.Background(), *repo.GetOwner().Login, repo.GetName(), newPR)

prErrorMessage := "Error opening pull request"
prDraftModeNotSupported := false

// Github's API will return HTTP status code 422 for several different errors
// Currently, there are two such errors that git-xargs is concerned with:
// 1. User passes the --draft flag, but the targeted repo does not support draft pull requests
// 2. User passes the --base-branch-name flag, specifying a branch that does not exist in the repo
if err != nil {
if resp.StatusCode == 422 {
// Update the error to be more RepoDoesntSupportDraftPullRequestsErra Draft PR
prErrorMessage = "Error opening pull request: draft PRs not supported for this repo. See https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests"
prDraftModeNotSupported = true
switch {
case strings.Contains(err.Error(), "Draft pull requests are not supported"):
prErrorMessage = "Error opening pull request: draft PRs not supported for this repo. See https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests"
config.Stats.TrackSingle(stats.RepoDoesntSupportDraftPullRequestsErr, repo)

case strings.Contains(err.Error(), "Field:base Code:invalid"): // TODO: implement error state detection for invalid base branch value
zackproser marked this conversation as resolved.
Show resolved Hide resolved
prErrorMessage = fmt.Sprintf("Error opening pull request: Base branch name: %s is invalid", config.BaseBranchName)
config.Stats.TrackSingle(stats.BaseBranchTargetInvalidErr, repo)

default:
config.Stats.TrackSingle(stats.PullRequestOpenErr, repo)
}
}

// If the Github reponse's status code is not 422, fallback to logging and tracking a generic pull request error
config.Stats.TrackSingle(stats.PullRequestOpenErr, repo)

logger.WithFields(logrus.Fields{
"Error": err,
"Head": branch,
"Base": repoDefaultBranch,
"Body": descriptionToUse,
}).Debug(prErrorMessage)

// Track pull request open failure
if prDraftModeNotSupported {
config.Stats.TrackSingle(stats.RepoDoesntSupportDraftPullRequestsErr, repo)
} else {
config.Stats.TrackSingle(stats.PullRequestOpenErr, repo)
}
return errors.WithStackTrace(err)
}

// There was no error opening the pull request
logger.WithFields(logrus.Fields{
"Pull Request URL": pr.GetHTMLURL(),
}).Debug("Successfully opened pull request")
Expand Down
3 changes: 3 additions & 0 deletions stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const (
RepoFlagSuppliedRepoMalformed types.Event = "repo-flag-supplied-repo-malformed"
// RepoDoesntSupportDraftPullRequestsErr denotes a repo that is incompatible with the submitted pull request configuration
RepoDoesntSupportDraftPullRequestsErr types.Event = "repo-not-compatible-with-pull-config"
// BaseBranchTargetInvalidErr denotes a repo that does not have the base branch specified by the user
BaseBranchTargetInvalidErr types.Event = "base-branch-target-invalid"
)

var allEvents = []types.AnnotatedEvent{
Expand Down Expand Up @@ -100,6 +102,7 @@ var allEvents = []types.AnnotatedEvent{
{Event: BranchRemoteDidntExistYet, Description: "Repos whose specified branches did not exist on the remote, and so were first created locally"},
{Event: RepoFlagSuppliedRepoMalformed, Description: "Repos passed via the --repo flag that were malformed (missing their Github org prefix?) and therefore unprocessable"},
{Event: RepoDoesntSupportDraftPullRequestsErr, Description: "Repos that do not support Draft PRs (--draft flag was passed)"},
{Event: BaseBranchTargetInvalidErr, Description: "Repos that did not have the branch specified by --base-branch-name"},
}

// RunStats will be a stats-tracker class that keeps score of which repos were touched, which were considered for update, which had branches made, PRs made, which were missing workflows or contexts, or had out of date workflows syntax values, etc
Expand Down