Skip to content

Approve changelog PRs using GH reviews #7979

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

Merged
merged 1 commit into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions .github/workflows/changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:

runs-on: ubuntu-latest

container: eu.gcr.io/gitpod-core-dev/dev/changelog:0.0.34
container: eu.gcr.io/gitpod-core-dev/dev/changelog:0.0.36

steps:
- uses: actions/checkout@v2
Expand All @@ -24,10 +24,11 @@ jobs:
git add CHANGELOG.md
git commit -m "[changelog] updated changelog"
git push origin $PR_BRANCH
/app/changelog pr -t $TOKEN -o gitpod-io -r gitpod -b $GITHUB_REF -H $PR_BRANCH
/app/changelog pr -t $TOKEN -a $APPROVAL_TOKEN -o gitpod-io -r gitpod -b $GITHUB_REF -H $PR_BRANCH
fi
env:
GITHUB_USER: roboquat
GITHUB_EMAIL: roboquat@gitpod.io
TOKEN: ${{ secrets.ROBOQUAT_AUTOMATIC_CHANGELOG }}
APPROVAL_TOKEN: ${{ secrets.GITOPS_BOT_PR_APPROVAL_TOKEN }}
shell: bash
35 changes: 28 additions & 7 deletions dev/changelog/pullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import (
)

type PullRequestOptions struct {
Title string
Body string
Token string
Org string
Repo string
BaseBranch string
HeadBranch string
Title string
Body string
Token string
ApprovalToken string
Org string
Repo string
BaseBranch string
HeadBranch string
}

var prOpts = &PullRequestOptions{}
Expand All @@ -33,6 +34,9 @@ var pullRequestCommand = &cobra.Command{
Short: "Creates a PR to update the changelog.",
Run: func(c *cobra.Command, args []string) {
client := NewClient(prOpts.Token)
// PRs can't be approved by the author of the PR. Thus we need two clients.
// One for creating the PR and one for approving it.
approvalClient := NewClient(prOpts.ApprovalToken)
context := context.Background()
newPr := &github.NewPullRequest{
Title: &prOpts.Title,
Expand Down Expand Up @@ -100,13 +104,30 @@ var pullRequestCommand = &cobra.Command{
l += *label.Name
}
logger.WithField("labels", l).WithField("pr", pr.Number).Info("PR labels successfully added")

retries = 0
for {
retries++
if retries > 60 {
logger.WithError(err).Fatal("Timeout trying to approve PR")
}
event := "APPROVE"
_, _, err := approvalClient.PullRequests.CreateReview(context, prOpts.Org, prOpts.Repo, *pr.Number, &github.PullRequestReviewRequest{Event: &event})
if err != nil {
logger.WithError(err).Error("Error approving PR. Trying again in a bit.")
time.Sleep(time.Second)
} else {
break
}
}
},
}

func init() {
// Setup prFlags before the command is initialized
prFlags := pullRequestCommand.PersistentFlags()
prFlags.StringVarP(&prOpts.Token, "token", "t", prOpts.Token, "a GitHub personal API token to perform authenticated requests")
prFlags.StringVarP(&prOpts.ApprovalToken, "approval-token", "a", prOpts.Token, "a GitHub personal API token to perform PR approval")
prFlags.StringVarP(&prOpts.Org, "org", "o", prOpts.Org, "the github organization")
prFlags.StringVarP(&prOpts.Repo, "repo", "r", prOpts.Repo, "the github repository name")
prFlags.StringVarP(&prOpts.HeadBranch, "head", "H", "main", "the head branch for pull requests")
Expand Down