Skip to content

Commit 34a74d3

Browse files
committed
Update changelog to approve PRs using GitHub
Fixes gitpod-io/ops#841
1 parent 3a2fb8b commit 34a74d3

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

.github/workflows/changelog.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010

1111
runs-on: ubuntu-latest
1212

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

1515
steps:
1616
- uses: actions/checkout@v2
@@ -24,10 +24,11 @@ jobs:
2424
git add CHANGELOG.md
2525
git commit -m "[changelog] updated changelog"
2626
git push origin $PR_BRANCH
27-
/app/changelog pr -t $TOKEN -o gitpod-io -r gitpod -b $GITHUB_REF -H $PR_BRANCH
27+
/app/changelog pr -t $TOKEN -a $APPROVAL_TOKEN -o gitpod-io -r gitpod -b $GITHUB_REF -H $PR_BRANCH
2828
fi
2929
env:
3030
GITHUB_USER: roboquat
3131
GITHUB_EMAIL: roboquat@gitpod.io
3232
TOKEN: ${{ secrets.ROBOQUAT_AUTOMATIC_CHANGELOG }}
33+
APPROVAL_TOKEN: ${{ secrets.GITOPS_BOT_PR_APPROVAL_TOKEN }}
3334
shell: bash

dev/changelog/pullrequest.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ import (
1616
)
1717

1818
type PullRequestOptions struct {
19-
Title string
20-
Body string
21-
Token string
22-
Org string
23-
Repo string
24-
BaseBranch string
25-
HeadBranch string
19+
Title string
20+
Body string
21+
Token string
22+
ApprovalToken string
23+
Org string
24+
Repo string
25+
BaseBranch string
26+
HeadBranch string
2627
}
2728

2829
var prOpts = &PullRequestOptions{}
@@ -33,6 +34,9 @@ var pullRequestCommand = &cobra.Command{
3334
Short: "Creates a PR to update the changelog.",
3435
Run: func(c *cobra.Command, args []string) {
3536
client := NewClient(prOpts.Token)
37+
// PRs can't be approved by the author of the PR. Thus we need two clients.
38+
// One for creating the PR and one for approving it.
39+
approvalClient := NewClient(prOpts.ApprovalToken)
3640
context := context.Background()
3741
newPr := &github.NewPullRequest{
3842
Title: &prOpts.Title,
@@ -100,13 +104,30 @@ var pullRequestCommand = &cobra.Command{
100104
l += *label.Name
101105
}
102106
logger.WithField("labels", l).WithField("pr", pr.Number).Info("PR labels successfully added")
107+
108+
retries = 0
109+
for {
110+
retries++
111+
if retries > 60 {
112+
logger.WithError(err).Fatal("Timeout trying to approve PR")
113+
}
114+
event := "APPROVE"
115+
_, _, err := approvalClient.PullRequests.CreateReview(context, prOpts.Org, prOpts.Repo, *pr.Number, &github.PullRequestReviewRequest{Event: &event})
116+
if err != nil {
117+
logger.WithError(err).Error("Error approving PR. Trying again in a bit.")
118+
time.Sleep(time.Second)
119+
} else {
120+
break
121+
}
122+
}
103123
},
104124
}
105125

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

0 commit comments

Comments
 (0)