Skip to content

Commit 2171eb9

Browse files
authored
feat: merge implementation (#6)
1 parent dfb55c9 commit 2171eb9

File tree

7 files changed

+61
-8
lines changed

7 files changed

+61
-8
lines changed

Dockerfile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM golang:alpine AS builder
22

3-
ARG BINARY_FOLDER=bin
4-
ARG BINARY_NAME=gotcha
3+
ENV BINARY_FOLDER=bin
4+
ENV BINARY_NAME=gotcha
55
ARG GOOS=linux
66
ARG GOARCH=amd64
77

@@ -11,4 +11,4 @@ RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*
1111
RUN go fmt ./cmd/ ./pkg/config/ ./pkg/github/
1212
RUN CGO_ENABLED=0 GOOS=${GOOS} GOARCH=${GOARCH} go build -ldflags="-s -w" -o ${BINARY_FOLDER}/${BINARY_NAME} ./cmd/main.go
1313
EXPOSE 3000
14-
ENTRYPOINT ["./${BINARY_FOLDER}/${BINARY_NAME}"]
14+
ENTRYPOINT "${BINARY_FOLDER}/${BINARY_NAME}"

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Next, enable the following permissions:
3232
- Repository → Checks
3333
- Repository → Issues
3434
- Repository → Pull requests
35+
- Repository → Contents
3536
- Events → Check run
3637
- Events → Issue comment
3738
- Events → Issues
@@ -58,7 +59,9 @@ In the *build* folder, you will find a file called **config.yaml** which is inte
5859
| administration &#8594; permission | Users who are allowed to execute commands such as pull request approval, test re-runs and others. <br/> The permissions here are one level above the *repositories* item, so they overlap the others. | list(string) | None |
5960
| administration &#8594; permission &#8594; repositories | Users with permissions on certain repositories. | list(object) | None |
6061
| pullRequest &#8594; approveCommand | Command for approval of the PR by *Gotcha*. | string | lgtm |
61-
| pullRequest &#8594; reRunTestSuiteCommand | Command to re-run the test suite. | string | rerun testsuite |
62+
| pullRequest &#8594; runTestSuiteCommand | Command to re-run the test suite. | string | run testsuite |
63+
| pullRequest &#8594; mergeCommand | Command for merge of the PR by *Gotcha*. | string | merge |
64+
| pullRequest &#8594; mergeAndDeleteCommand | Command for merge and delete ref branch of the PR by *Gotcha*. | string | merge and delete |
6265
| pullRequest &#8594; testSuite &#8594; namePattern | Format (regex) that pull request name must follow.<br/> Default value is based on [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/). | string(regex) | (?P<type>feat&#124;fix&#124;refactor&#124;style&#124;docs&#124;build)(?P<separator>:) (?P<body>.+)' |
6366
| pullRequest &#8594; testSuite &#8594; reviewers | Need to have reviewers on the pull request. | bool | false |
6467
| pullRequest &#8594; testSuite &#8594; assignees | Need to have assignees on the pull request. | bool | true |

build/config.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ layout:
1010
- diegolnasc
1111
pullRequest:
1212
approveCommand: lgtm
13-
reRunTestSuiteCommand: rerun testsuite
13+
runTestSuiteCommand: run testsuite
14+
mergeCommand: merge
15+
mergeAndDeleteCommand: merge and delete
1416
testSuite:
1517
namePattern: (?P<type>feat|fix|refactor|style|docs|build)(?P<separator>:) (?P<body>.+)
1618
reviewers: false

pkg/config/config.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ type Repository struct {
3737

3838
type PullRequest struct {
3939
ApproveCommand string `yaml:"approveCommand"`
40-
ReRunTestSuiteCommand string `yaml:"reRunTestSuiteCommand"`
40+
RunTestSuiteCommand string `yaml:"runTestSuiteCommand"`
41+
MergeCommand string `yaml:"mergeCommand"`
42+
MergeAndDeleteCommand string `yaml:"mergeAndDeleteCommand"`
4143
TestSuite TestSuite `yaml:"testSuite"`
4244
}
4345

pkg/github/controller.go

+24
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,27 @@ func (w *Worker) GetPullRequest(owner string, repo string, number int) (*v41.Pul
7070
}
7171
return resp, err
7272
}
73+
74+
func (w *Worker) MergePullRequest(owner string, repo string, number int, commitMessage string, options v41.PullRequestOptions) (*v41.PullRequestMergeResult, error) {
75+
resp, _, err := w.Client.PullRequests.Merge(context.TODO(), owner, repo, number, commitMessage, &options)
76+
if err != nil {
77+
log.Printf("error merging pull request: %v\n", err)
78+
}
79+
return resp, err
80+
}
81+
82+
func (w *Worker) GetRef(owner string, repo string, ref string) (*v41.Reference, error) {
83+
resp, _, err := w.Client.Git.GetRef(context.TODO(), owner, repo, ref)
84+
if err != nil {
85+
log.Printf("error getting ref: %v\n", err)
86+
}
87+
return resp, err
88+
}
89+
90+
func (w *Worker) DeleteRef(owner string, repo string, ref string) (*v41.Response, error) {
91+
resp, err := w.Client.Git.DeleteRef(context.TODO(), owner, repo, ref)
92+
if err != nil {
93+
log.Printf("error deleting ref: %v\n", err)
94+
}
95+
return resp, err
96+
}

pkg/github/issue.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212

1313
func (w *Worker) processIssueComment(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.IssueCommentPayload) {
1414
w.approve(owner, pullRequest, p)
15+
w.merge(owner, pullRequest, p)
16+
w.mergeAndDelete(owner, pullRequest, p)
1517
w.reRunLaboratoryTest(owner, pullRequest, p)
1618
}
1719

@@ -30,9 +32,29 @@ func (w *Worker) approve(owner *string, pullRequest *v41.PullRequest, p *ghwebho
3032
}
3133
}
3234

35+
func (w *Worker) merge(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.IssueCommentPayload) {
36+
if pullRequest != nil {
37+
if strings.EqualFold(p.Comment.Body, w.Config.Layout.PullRequest.MergeCommand) {
38+
w.MergePullRequest(*owner, p.Repository.Name, *pullRequest.Number, "", v41.PullRequestOptions{})
39+
}
40+
}
41+
}
42+
43+
func (w *Worker) mergeAndDelete(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.IssueCommentPayload) {
44+
if pullRequest != nil {
45+
if strings.EqualFold(p.Comment.Body, w.Config.Layout.PullRequest.MergeAndDeleteCommand) {
46+
if _, err := w.MergePullRequest(*owner, p.Repository.Name, *pullRequest.Number, "", v41.PullRequestOptions{}); err == nil {
47+
if *pullRequest.Base.Repo.Name == *pullRequest.Head.Repo.Name {
48+
w.DeleteRef(*owner, p.Repository.Name, fmt.Sprintf("heads/%s", *pullRequest.Head.Ref))
49+
}
50+
}
51+
}
52+
}
53+
}
54+
3355
func (w *Worker) reRunLaboratoryTest(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.IssueCommentPayload) {
3456
if pullRequest != nil {
35-
if strings.EqualFold(p.Comment.Body, w.Config.Layout.PullRequest.ReRunTestSuiteCommand) {
57+
if strings.EqualFold(p.Comment.Body, w.Config.Layout.PullRequest.RunTestSuiteCommand) {
3658
pullRequestNumber := strconv.Itoa(int(*pullRequest.Number))
3759
w.CreateCheckRun(*owner, p.Repository.Name, v41.CreateCheckRunOptions{
3860
Name: "Laboratory test",

pkg/github/worker.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (w *Worker) processIssueCommentEvent(p *ghwebhooks.IssueCommentPayload) {
1818
var pullRequest *v41.PullRequest
1919
owner, _ := getOwner(&w.Config)
2020
if len(p.Issue.PullRequest.HTMLURL) > 0 {
21-
if pullRequestNumber, err := strconv.Atoi(strings.Split(p.Issue.PullRequest.HTMLURL, "/")[6]); err != nil {
21+
if pullRequestNumber, err := strconv.Atoi(strings.Split(p.Issue.PullRequest.HTMLURL, "/")[6]); err == nil {
2222
pullRequest, err = w.GetPullRequest(*owner, p.Repository.Name, pullRequestNumber)
2323
if err != nil {
2424
return

0 commit comments

Comments
 (0)