Skip to content

Commit 1d86969

Browse files
authored
Merge pull request #9 from diegolnasc/features/refactor
refactor: service and structure + pull request overview
2 parents 22e0ae7 + 7ebb2fd commit 1d86969

13 files changed

+306
-175
lines changed

build/config.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ layout:
99
users:
1010
- diegolnasc
1111
pullRequest:
12+
enableOverview: true
13+
overViewCommand: run overview
1214
approveCommand: lgtm
1315
runTestSuiteCommand: run testsuite
1416
mergeCommand: merge

cmd/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"flag"
45
"log"
56
"net/http"
67

@@ -15,7 +16,9 @@ const (
1516
)
1617

1718
func main() {
18-
startProvider(Provider("github"))
19+
provider := flag.String("provider", "github", "Provider to run")
20+
flag.Parse()
21+
startProvider(Provider(*provider))
1922
err := http.ListenAndServe(":3000", nil)
2023
if err != nil {
2124
log.Panic(err)

go.mod

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ module github.com/diegolnasc/gotcha
33
go 1.17
44

55
require (
6-
github.com/bradleyfalzon/ghinstallation v1.1.1
6+
github.com/bradleyfalzon/ghinstallation/v2 v2.0.3
77
github.com/go-playground/webhooks/v6 v6.0.0-rc.1
88
github.com/google/go-github/v41 v41.0.0
99
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
1010
)
1111

1212
require (
13-
github.com/davecgh/go-spew v1.1.1 // indirect
14-
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
15-
github.com/google/go-github/v29 v29.0.2 // indirect
13+
github.com/golang-jwt/jwt/v4 v4.0.0 // indirect
14+
github.com/google/go-github/v39 v39.0.0 // indirect
1615
github.com/google/go-querystring v1.1.0 // indirect
1716
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
1817
)

go.sum

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
github.com/bradleyfalzon/ghinstallation v1.1.1 h1:pmBXkxgM1WeF8QYvDLT5kuQiHMcmf+X015GI0KM/E3I=
2-
github.com/bradleyfalzon/ghinstallation v1.1.1/go.mod h1:vyCmHTciHx/uuyN82Zc3rXN3X2KTK8nUTCrTMwAhcug=
1+
github.com/bradleyfalzon/ghinstallation/v2 v2.0.3 h1:ywF/8q+GVpvlsEuvRb1SGSDQDUxntW1d4kFu/9q/YAE=
2+
github.com/bradleyfalzon/ghinstallation/v2 v2.0.3/go.mod h1:tlgi+JWCXnKFx/Y4WtnDbZEINo31N5bcvnCoqieefmk=
3+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
34
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4-
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
5-
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6-
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
7-
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
85
github.com/go-playground/webhooks/v6 v6.0.0-rc.1 h1:U78wIkdcCGzMw38BGw2eep542XRsUr3/kGDSjmFFELU=
96
github.com/go-playground/webhooks/v6 v6.0.0-rc.1/go.mod h1:GCocmfMtpJdkEOM1uG9p2nXzg1kY5X/LtvQgtPHUaaA=
107
github.com/gogits/go-gogs-client v0.0.0-20200905025246-8bb8a50cb355/go.mod h1:cY2AIrMgHm6oOHmR7jY+9TtjzSjQ3iG7tURJG3Y6XH0=
8+
github.com/golang-jwt/jwt/v4 v4.0.0 h1:RAqyYixv1p7uEnocuy8P1nru5wprCh/MH2BIlW5z5/o=
9+
github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
1110
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1211
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
13-
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
1412
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
1513
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
1614
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
17-
github.com/google/go-github/v29 v29.0.2 h1:opYN6Wc7DOz7Ku3Oh4l7prmkOMwEcQxpFtxdU8N8Pts=
18-
github.com/google/go-github/v29 v29.0.2/go.mod h1:CHKiKKPHJ0REzfwc14QMklvtHwCveD0PxlMjLlzAM5E=
15+
github.com/google/go-github/v39 v39.0.0 h1:pygGA5ySwxEez1N39GnDauD0PaWWuGgayudyZAc941s=
16+
github.com/google/go-github/v39 v39.0.0/go.mod h1:C1s8C5aCC9L+JXIYpJM5GYytdX52vC1bLvHEF1IhBrE=
1917
github.com/google/go-github/v41 v41.0.0 h1:HseJrM2JFf2vfiZJ8anY2hqBjdfY1Vlj/K27ueww4gg=
2018
github.com/google/go-github/v41 v41.0.0/go.mod h1:XgmCA5H323A9rtgExdTcnDkcqp6S30AVACCBDOonIxg=
21-
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
2219
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
2320
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
2421
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -29,7 +26,6 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
2926
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
3027
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ=
3128
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
32-
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
3329
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
3430
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
3531
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
@@ -42,7 +38,6 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
4238
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
4339
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
4440
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
45-
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
4641
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
4742
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
4843
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

pkg/config/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ type Repository struct {
3636
}
3737

3838
type PullRequest struct {
39+
EnableOverview bool `yaml:"enableOverview"`
40+
OverViewCommand string `yaml:"overViewCommand"`
3941
ApproveCommand string `yaml:"approveCommand"`
4042
RunTestSuiteCommand string `yaml:"runTestSuiteCommand"`
4143
MergeCommand string `yaml:"mergeCommand"`

pkg/github/check.go

+25-23
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,36 @@ import (
99
v41 "github.com/google/go-github/v41/github"
1010
)
1111

12+
type CheckService service
13+
1214
type checkRunResult struct {
1315
title string
1416
passed bool
1517
body string
1618
}
1719

18-
func (w *Worker) processCheckRun(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload) {
20+
func (s *CheckService) processCheckRun(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload) {
1921
if p.Action == "created" {
20-
result, overralResults := w.runTestSuite(owner, pullRequest, p)
21-
w.updateCheckRunStatus(owner, pullRequest, p, overralResults)
22-
w.printResults(owner, pullRequest, p, result, overralResults)
22+
result, overralResults := s.runTestSuite(owner, pullRequest, p)
23+
s.updateCheckRunStatus(owner, pullRequest, p, overralResults)
24+
s.printResults(owner, pullRequest, p, result, overralResults)
2325
}
2426
}
2527

26-
func (w *Worker) runTestSuite(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload) ([]*checkRunResult, bool) {
28+
func (s *CheckService) runTestSuite(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload) ([]*checkRunResult, bool) {
2729
var results []*checkRunResult
2830
overralResults := true
29-
if len(w.Config.Layout.PullRequest.TestSuite.NamePattern) > 0 {
30-
results = append(results, w.isNamePatternValid(owner, pullRequest, p))
31+
if len(s.w.Config.Layout.PullRequest.TestSuite.NamePattern) > 0 {
32+
results = append(results, s.isNamePatternValid(owner, pullRequest, p))
3133
}
32-
if w.Config.Layout.PullRequest.TestSuite.Reviewers {
33-
results = append(results, w.hasReviewers(owner, pullRequest, p))
34+
if s.w.Config.Layout.PullRequest.TestSuite.Reviewers {
35+
results = append(results, s.hasReviewers(owner, pullRequest, p))
3436
}
35-
if w.Config.Layout.PullRequest.TestSuite.Assignees {
36-
results = append(results, w.hasAssignees(owner, pullRequest, p))
37+
if s.w.Config.Layout.PullRequest.TestSuite.Assignees {
38+
results = append(results, s.hasAssignees(owner, pullRequest, p))
3739
}
38-
if w.Config.Layout.PullRequest.TestSuite.Labels {
39-
results = append(results, w.hasLabels(owner, pullRequest, p))
40+
if s.w.Config.Layout.PullRequest.TestSuite.Labels {
41+
results = append(results, s.hasLabels(owner, pullRequest, p))
4042
}
4143
for _, r := range results {
4244
if !r.passed {
@@ -47,11 +49,11 @@ func (w *Worker) runTestSuite(owner *string, pullRequest *v41.PullRequest, p *gh
4749
return results, overralResults
4850
}
4951

50-
func (w *Worker) isNamePatternValid(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload) *checkRunResult {
51-
match, _ := regexp.MatchString(w.Config.Layout.PullRequest.TestSuite.NamePattern, *pullRequest.Title)
52+
func (s *CheckService) isNamePatternValid(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload) *checkRunResult {
53+
match, _ := regexp.MatchString(s.w.Config.Layout.PullRequest.TestSuite.NamePattern, *pullRequest.Title)
5254
var body string
5355
if !match {
54-
body = fmt.Sprintf("The pull request format should be: [%s]", w.Config.Layout.PullRequest.TestSuite.NamePattern)
56+
body = fmt.Sprintf("The pull request format should be: [%s]", s.w.Config.Layout.PullRequest.TestSuite.NamePattern)
5557
}
5658
return &checkRunResult{
5759
title: "Pull request pattern",
@@ -60,7 +62,7 @@ func (w *Worker) isNamePatternValid(owner *string, pullRequest *v41.PullRequest,
6062
}
6163
}
6264

63-
func (w *Worker) hasReviewers(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload) *checkRunResult {
65+
func (s *CheckService) hasReviewers(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload) *checkRunResult {
6466
if len(pullRequest.RequestedReviewers) == 0 {
6567
return &checkRunResult{
6668
title: "Reviewers",
@@ -74,7 +76,7 @@ func (w *Worker) hasReviewers(owner *string, pullRequest *v41.PullRequest, p *gh
7476
}
7577
}
7678

77-
func (w *Worker) hasAssignees(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload) *checkRunResult {
79+
func (s *CheckService) hasAssignees(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload) *checkRunResult {
7880
if len(pullRequest.Assignees) == 0 {
7981
return &checkRunResult{
8082
title: "Assignees",
@@ -88,7 +90,7 @@ func (w *Worker) hasAssignees(owner *string, pullRequest *v41.PullRequest, p *gh
8890
}
8991
}
9092

91-
func (w *Worker) hasLabels(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload) *checkRunResult {
93+
func (s *CheckService) hasLabels(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload) *checkRunResult {
9294
if len(pullRequest.Labels) == 0 {
9395
return &checkRunResult{
9496
title: "Labels",
@@ -102,20 +104,20 @@ func (w *Worker) hasLabels(owner *string, pullRequest *v41.PullRequest, p *ghweb
102104
}
103105
}
104106

105-
func (w *Worker) updateCheckRunStatus(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload, overralResults bool) {
107+
func (s *CheckService) updateCheckRunStatus(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload, overralResults bool) {
106108
var conclusion string
107109
if overralResults {
108110
conclusion = "success"
109111
} else {
110112
conclusion = "failure"
111113
}
112-
w.UpdateCheckRun(*owner, p.Repository.Name, p.CheckRun.ID, v41.UpdateCheckRunOptions{
114+
s.w.UpdateCheckRun(*owner, p.Repository.Name, p.CheckRun.ID, v41.UpdateCheckRunOptions{
113115
Status: v41.String("completed"),
114116
Conclusion: &conclusion,
115117
})
116118
}
117119

118-
func (w *Worker) printResults(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload, results []*checkRunResult, overralResults bool) {
120+
func (s *CheckService) printResults(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload, results []*checkRunResult, overralResults bool) {
119121
var title string
120122
var body bytes.Buffer
121123
if overralResults {
@@ -137,7 +139,7 @@ func (w *Worker) printResults(owner *string, pullRequest *v41.PullRequest, p *gh
137139
body.WriteString(fmt.Sprintf("[:heavy_exclamation_mark:] **%s** &#8594; %s <br/>", r.title, r.body))
138140
}
139141
}
140-
w.PullRequestCreateComment(*owner, p.Repository.Name, *pullRequest.Number, v41.IssueComment{
142+
s.w.IssueCreateComment(*owner, p.Repository.Name, *pullRequest.Number, v41.IssueComment{
141143
Body: v41.String(body.String()),
142144
})
143145
}

0 commit comments

Comments
 (0)