Skip to content

Commit

Permalink
fix: handle unapproved pull requests (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
0daryo authored Dec 28, 2021
1 parent d1356ca commit 7f08958
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ comment: ${{ github.event.comment.body }}
merge_method: 'merge'
mergers: 'comma separeted github usernames. every user is allowed if not specified'
```

## Note
**Setting Branch protection rules is recommended to avoid unexpected merge of pull requests.**
20 changes: 19 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"log"
"regexp"
"text/template"
"time"

Expand Down Expand Up @@ -42,7 +43,7 @@ func main() {
defer f()
client := newGHClient(e.GithubToken)
if err := client.merge(ctx, e.Owner, e.Repo, e.PRNumber, e.MergeMethod); err != nil {
if serr := client.sendMsg(ctx, e.Owner, e.Repo, e.PRNumber, err.Error()); serr != nil {
if serr := client.sendMsg(ctx, e.Owner, e.Repo, e.PRNumber, errMsg(err)); serr != nil {
log.Fatalf("failed to send message: %v original: %v", serr, err)
}
log.Fatal(err.Error())
Expand Down Expand Up @@ -155,3 +156,20 @@ Labels:
{{- end -}}
{{- end -}}
`))

var (
needApproveRegexp = regexp.MustCompile("At least ([0-9]+) approving review is required by reviewers with write access")
)

// errMsg returns error message to post from error.
// Especially handing error from github. go-github does not have error type for some cases.
func errMsg(err error) string {
if err == nil {
return "Succeeded!"
}
ss := needApproveRegexp.FindStringSubmatch(err.Error())
if len(ss) == 2 {
return fmt.Sprintf("Need %s approving review", ss[1])
}
return err.Error()
}
39 changes: 39 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"testing"

"github.com/google/go-github/github"
Expand Down Expand Up @@ -145,3 +146,41 @@ func Test_validateEnv(t *testing.T) {
})
}
}

func Test_errMsg(t *testing.T) {
type args struct {
err error
}
tests := []struct {
name string
args args
want string
}{
{
name: "ok",
args: args{},
want: "Succeeded!",
},
{
name: "need approval",
args: args{
err: errors.New("failed to merge pull request: PUT https://api.github.com/repos/abema/github-actions-merger/pulls/1/merge: 405 At least 2 approving review is required by reviewers with write access. []"),
},
want: "Need 2 approving review",
},
{
name: "internal server error",
args: args{
err: errors.New("internal server error"),
},
want: "internal server error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := errMsg(tt.args.err); got != tt.want {
t.Errorf("errMsg() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 7f08958

Please sign in to comment.