Skip to content

Commit d25835d

Browse files
authored
Merge pull request #15 from diegolnasc/docs/update
docs: project documentation
2 parents 1d86969 + 21bb34d commit d25835d

File tree

10 files changed

+111
-45
lines changed

10 files changed

+111
-45
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<h2 align="center"> Gotcha Bot :robot: </h2>
22
<h2 align="center">A app to automate engineers tasks</h2>
33

4-
![License](https://img.shields.io/github/license/diegolnasc/gotcha?style=plastic)
5-
![go-version](https://img.shields.io/static/v1?label=golang&message=1.17&color=informational&style=plastic)
6-
[![gotcha release (latest)](https://img.shields.io/github/v/release/diegolnasc/gotcha?sort=semver&style=plastic)](https://github.com/diegolnasc/gotcha/releases)
4+
[![go-doc](https://godoc.org/github.com/diegolnasc/gotcha?status.svg)](https://godoc.org/github.com/diegolnasc/gotcha)
5+
[![go-report](https://goreportcard.com/badge/github.com/diegolnasc/gotcha)](https://goreportcard.com/report/github.com/diegolnasc/gotcha)
6+
![license](https://img.shields.io/github/license/diegolnasc/gotcha?style=plastic)
7+
[![gotcha-release(latest)](https://img.shields.io/github/v/release/diegolnasc/gotcha?sort=semver&style=plastic)](https://github.com/diegolnasc/gotcha/releases)
78

89
---
910
## What can :robot: do?

cmd/main.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1+
// Copyright 2021 Diego Lima. All rights reserved.
2+
3+
// Use of this source code is governed by a Apache license.
4+
// license that can be found in the LICENSE file.
15
package main
26

37
import (
48
"flag"
59
"log"
610
"net/http"
711

8-
config "github.com/diegolnasc/gotcha/pkg/config"
12+
"github.com/diegolnasc/gotcha/pkg/config"
913
github "github.com/diegolnasc/gotcha/pkg/github"
1014
)
1115

12-
type Provider string
16+
type provider string
1317

1418
const (
15-
GitHub Provider = "github"
19+
gitHub provider = "github"
1620
)
1721

1822
func main() {
19-
provider := flag.String("provider", "github", "Provider to run")
23+
p := flag.String("provider", "github", "Provider to run")
2024
flag.Parse()
21-
startProvider(Provider(*provider))
25+
startProvider(provider(*p))
2226
err := http.ListenAndServe(":3000", nil)
2327
if err != nil {
2428
log.Panic(err)
2529
}
2630
}
2731

28-
func startProvider(provider Provider) {
29-
switch provider {
30-
case GitHub:
32+
func startProvider(p provider) {
33+
switch p {
34+
case gitHub:
3135
config := &config.Settings{}
3236
config.ReadConf()
3337
worker := github.New(config)

pkg/config/config.go

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright 2021 Diego Lima. All rights reserved.
2+
3+
// Use of this source code is governed by a Apache license.
4+
// license that can be found in the LICENSE file.
15
package config
26

37
import (
@@ -7,34 +11,41 @@ import (
711
"gopkg.in/yaml.v3"
812
)
913

14+
// Settings represents the configuration and authorization aspects.
1015
type Settings struct {
1116
Layout Layout `yaml:"layout"`
1217
Github Github `yaml:"github"`
1318
}
1419

20+
// Layout represents permissions level and pull request functionalities.
1521
type Layout struct {
1622
Administration Administration `yaml:"administration"`
1723
PullRequest PullRequest `yaml:"pullRequest"`
1824
}
1925

26+
// Administration represents general permissions.
2027
type Administration struct {
2128
Permission Permission `yaml:"permission"`
2229
}
2330

31+
// Permission represents high level user's and repository permissions.
2432
type Permission struct {
2533
Users []string `yaml:"users"`
2634
Repositories []Repositories `yaml:"repositories"`
2735
}
2836

37+
// Repositories represents low level permissions in repositories.
2938
type Repositories struct {
3039
Repository Repository `yaml:"repository"`
3140
}
3241

42+
// Repository represents user permission in a single repository.
3343
type Repository struct {
3444
Name string `yaml:"name"`
3545
Users []string `yaml:"users"`
3646
}
3747

48+
// PullRequest represents commands and functionalities.
3849
type PullRequest struct {
3950
EnableOverview bool `yaml:"enableOverview"`
4051
OverViewCommand string `yaml:"overViewCommand"`
@@ -45,13 +56,15 @@ type PullRequest struct {
4556
TestSuite TestSuite `yaml:"testSuite"`
4657
}
4758

59+
// TestSuite represents configuration for the test cases.
4860
type TestSuite struct {
4961
NamePattern string `yaml:"namePattern"`
5062
Reviewers bool `yaml:"reviewers"`
5163
Assignees bool `yaml:"assignees"`
5264
Labels bool `yaml:"labels"`
5365
}
5466

67+
// Github represents github app owner configuration.
5568
type Github struct {
5669
AppID int `yaml:"appId"`
5770
Organization string `yaml:"organization"`
@@ -62,6 +75,7 @@ type Github struct {
6275
Events []string `yaml:"events"`
6376
}
6477

78+
// ReadConf initialize the configuration.
6579
func (c *Settings) ReadConf() {
6680
yamlFile, err := ioutil.ReadFile("build/config-local.yaml")
6781
if err != nil {

pkg/github/check.go

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright 2021 Diego Lima. All rights reserved.
2+
3+
// Use of this source code is governed by a Apache license.
4+
// license that can be found in the LICENSE file.
15
package github
26

37
import (
@@ -9,14 +13,17 @@ import (
913
v41 "github.com/google/go-github/v41/github"
1014
)
1115

16+
// CheckService handles communication with the checkrun/checksuite event.
1217
type CheckService service
1318

19+
// checkRunResult represents the result of a test case.
1420
type checkRunResult struct {
1521
title string
1622
passed bool
1723
body string
1824
}
1925

26+
// processCheckRun process the checkrun event payload.
2027
func (s *CheckService) processCheckRun(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload) {
2128
if p.Action == "created" {
2229
result, overralResults := s.runTestSuite(owner, pullRequest, p)
@@ -25,6 +32,7 @@ func (s *CheckService) processCheckRun(owner *string, pullRequest *v41.PullReque
2532
}
2633
}
2734

35+
// runTestSuite run the test suite.
2836
func (s *CheckService) runTestSuite(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload) ([]*checkRunResult, bool) {
2937
var results []*checkRunResult
3038
overralResults := true
@@ -49,6 +57,7 @@ func (s *CheckService) runTestSuite(owner *string, pullRequest *v41.PullRequest,
4957
return results, overralResults
5058
}
5159

60+
// isNamePatternValid check if the pull request name is valid.
5261
func (s *CheckService) isNamePatternValid(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload) *checkRunResult {
5362
match, _ := regexp.MatchString(s.w.Config.Layout.PullRequest.TestSuite.NamePattern, *pullRequest.Title)
5463
var body string
@@ -62,6 +71,7 @@ func (s *CheckService) isNamePatternValid(owner *string, pullRequest *v41.PullRe
6271
}
6372
}
6473

74+
// hasReviewers check if the pull request has reviewers.
6575
func (s *CheckService) hasReviewers(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload) *checkRunResult {
6676
if len(pullRequest.RequestedReviewers) == 0 {
6777
return &checkRunResult{
@@ -76,6 +86,7 @@ func (s *CheckService) hasReviewers(owner *string, pullRequest *v41.PullRequest,
7686
}
7787
}
7888

89+
// hasAssignees check if the pull request has assignees.
7990
func (s *CheckService) hasAssignees(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload) *checkRunResult {
8091
if len(pullRequest.Assignees) == 0 {
8192
return &checkRunResult{
@@ -90,6 +101,7 @@ func (s *CheckService) hasAssignees(owner *string, pullRequest *v41.PullRequest,
90101
}
91102
}
92103

104+
// hasLabels check if the pull request has labels.
93105
func (s *CheckService) hasLabels(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload) *checkRunResult {
94106
if len(pullRequest.Labels) == 0 {
95107
return &checkRunResult{
@@ -104,6 +116,7 @@ func (s *CheckService) hasLabels(owner *string, pullRequest *v41.PullRequest, p
104116
}
105117
}
106118

119+
// updateCheckRunStatus update the check run status in github.
107120
func (s *CheckService) updateCheckRunStatus(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload, overralResults bool) {
108121
var conclusion string
109122
if overralResults {
@@ -117,6 +130,7 @@ func (s *CheckService) updateCheckRunStatus(owner *string, pullRequest *v41.Pull
117130
})
118131
}
119132

133+
// printResults comments the result of the test suite on the pull request.
120134
func (s *CheckService) printResults(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.CheckRunPayload, results []*checkRunResult, overralResults bool) {
121135
var title string
122136
var body bytes.Buffer

pkg/github/common.go

+26-32
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright 2021 Diego Lima. All rights reserved.
2+
3+
// Use of this source code is governed by a Apache license.
4+
// license that can be found in the LICENSE file.
15
package github
26

37
import (
@@ -12,6 +16,7 @@ import (
1216
v41 "github.com/google/go-github/v41/github"
1317
)
1418

19+
// GetPullRequest get a pull request.
1520
func (w *Worker) GetPullRequest(owner string, repo string, number int) (*v41.PullRequest, error) {
1621
resp, _, err := w.Client.PullRequests.Get(context.TODO(), owner, repo, number)
1722
if err != nil {
@@ -20,6 +25,7 @@ func (w *Worker) GetPullRequest(owner string, repo string, number int) (*v41.Pul
2025
return resp, err
2126
}
2227

28+
// GetPullRequestFiles list files from a pull request.
2329
func (w *Worker) GetPullRequestFiles(owner string, repo string, number int, opts *v41.ListOptions) ([]*v41.CommitFile, error) {
2430
resp, _, err := w.Client.PullRequests.ListFiles(context.TODO(), owner, repo, number, opts)
2531
if err != nil {
@@ -28,6 +34,7 @@ func (w *Worker) GetPullRequestFiles(owner string, repo string, number int, opts
2834
return resp, err
2935
}
3036

37+
// MergePullRequest merge a pull request.
3138
func (w *Worker) MergePullRequest(owner string, repo string, number int, commitMessage string, options v41.PullRequestOptions) (*v41.PullRequestMergeResult, error) {
3239
resp, _, err := w.Client.PullRequests.Merge(context.TODO(), owner, repo, number, commitMessage, &options)
3340
if err != nil {
@@ -36,6 +43,7 @@ func (w *Worker) MergePullRequest(owner string, repo string, number int, commitM
3643
return resp, err
3744
}
3845

46+
// PullRequestCreateReview create a pull request review.
3947
func (w *Worker) PullRequestCreateReview(owner string, repo string, number int, review v41.PullRequestReviewRequest) (*v41.PullRequestReview, error) {
4048
resp, _, err := w.Client.PullRequests.CreateReview(context.TODO(), owner, repo, number, &review)
4149
if err != nil {
@@ -44,14 +52,7 @@ func (w *Worker) PullRequestCreateReview(owner string, repo string, number int,
4452
return resp, err
4553
}
4654

47-
func (w *Worker) PullRequestCreateCommentReview(owner string, repo string, number int, comment v41.PullRequestComment) (*v41.PullRequestComment, error) {
48-
resp, _, err := w.Client.PullRequests.CreateComment(context.TODO(), owner, repo, number, &comment)
49-
if err != nil {
50-
log.Printf("error creating pull request review comment: %v\n", err)
51-
}
52-
return resp, err
53-
}
54-
55+
// IssueCreateComment create an issue or pull request comment.
5556
func (w *Worker) IssueCreateComment(owner string, repo string, number int, comment v41.IssueComment) (*v41.IssueComment, error) {
5657
resp, _, err := w.Client.Issues.CreateComment(context.TODO(), owner, repo, number, &comment)
5758
if err != nil {
@@ -60,14 +61,16 @@ func (w *Worker) IssueCreateComment(owner string, repo string, number int, comme
6061
return resp, err
6162
}
6263

63-
func (w *Worker) IssueUpdateComment(owner string, repo string, commentId int, comment v41.IssueComment) (*v41.IssueComment, error) {
64-
resp, _, err := w.Client.Issues.EditComment(context.TODO(), owner, repo, int64(commentId), &comment)
64+
// IssueUpdateComment update an issue or pull request comment.
65+
func (w *Worker) IssueUpdateComment(owner string, repo string, commentID int, comment v41.IssueComment) (*v41.IssueComment, error) {
66+
resp, _, err := w.Client.Issues.EditComment(context.TODO(), owner, repo, int64(commentID), &comment)
6567
if err != nil {
6668
log.Printf("error updating issue comment: %v\n", err)
6769
}
6870
return resp, err
6971
}
7072

73+
// IssueListComments list the comments of an issue or pull request.
7174
func (w *Worker) IssueListComments(owner string, repo string, number int, opts *v41.IssueListCommentsOptions) ([]*v41.IssueComment, error) {
7275
resp, _, err := w.Client.Issues.ListComments(context.TODO(), owner, repo, number, opts)
7376
if err != nil {
@@ -76,30 +79,16 @@ func (w *Worker) IssueListComments(owner string, repo string, number int, opts *
7679
return resp, err
7780
}
7881

79-
func (w *Worker) PullRequestListReviewComments(owner string, repo string, number int, opts *v41.PullRequestListCommentsOptions) ([]*v41.PullRequestComment, error) {
80-
resp, _, err := w.Client.PullRequests.ListComments(context.TODO(), owner, repo, number, opts)
81-
if err != nil {
82-
log.Printf("error getting pull list of comments: %v\n", err)
83-
}
84-
return resp, err
85-
}
86-
87-
func (w *Worker) AddLabels(owner string, repo string, number int, labels []string) ([]*v41.Label, error) {
88-
resp, _, err := w.Client.Issues.AddLabelsToIssue(context.TODO(), owner, repo, number, labels)
89-
if err != nil {
90-
log.Printf("error adding labels: %v\n", err)
91-
}
92-
return resp, err
93-
}
94-
95-
func (w *Worker) GetCheckRun(owner string, repo string, checkrunId int64) (*v41.CheckRun, error) {
96-
resp, _, err := w.Client.Checks.GetCheckRun(context.TODO(), owner, repo, checkrunId)
82+
// GetCheckRun get a check run.
83+
func (w *Worker) GetCheckRun(owner string, repo string, checkrunID int64) (*v41.CheckRun, error) {
84+
resp, _, err := w.Client.Checks.GetCheckRun(context.TODO(), owner, repo, checkrunID)
9785
if err != nil {
9886
log.Printf("error creating checkrun: %v\n", err)
9987
}
10088
return resp, err
10189
}
10290

91+
// CreateCheckRun create a check run.
10392
func (w *Worker) CreateCheckRun(owner string, repo string, checkrun v41.CreateCheckRunOptions) (*v41.CheckRun, error) {
10493
resp, _, err := w.Client.Checks.CreateCheckRun(context.TODO(), owner, repo, checkrun)
10594
if err != nil {
@@ -108,14 +97,16 @@ func (w *Worker) CreateCheckRun(owner string, repo string, checkrun v41.CreateCh
10897
return resp, err
10998
}
11099

111-
func (w *Worker) UpdateCheckRun(owner string, repo string, checkRunId int64, checkRun v41.UpdateCheckRunOptions) (*v41.CheckRun, error) {
112-
resp, _, err := w.Client.Checks.UpdateCheckRun(context.TODO(), owner, repo, checkRunId, checkRun)
100+
// UpdateCheckRun update a check run.
101+
func (w *Worker) UpdateCheckRun(owner string, repo string, checkrunID int64, checkRun v41.UpdateCheckRunOptions) (*v41.CheckRun, error) {
102+
resp, _, err := w.Client.Checks.UpdateCheckRun(context.TODO(), owner, repo, checkrunID, checkRun)
113103
if err != nil {
114104
log.Printf("error updating checkrun: %v\n", err)
115105
}
116106
return resp, err
117107
}
118108

109+
// GetRef get a reference.
119110
func (w *Worker) GetRef(owner string, repo string, ref string) (*v41.Reference, error) {
120111
resp, _, err := w.Client.Git.GetRef(context.TODO(), owner, repo, ref)
121112
if err != nil {
@@ -124,6 +115,7 @@ func (w *Worker) GetRef(owner string, repo string, ref string) (*v41.Reference,
124115
return resp, err
125116
}
126117

118+
// DeleteRef delete a reference.
127119
func (w *Worker) DeleteRef(owner string, repo string, ref string) (*v41.Response, error) {
128120
resp, err := w.Client.Git.DeleteRef(context.TODO(), owner, repo, ref)
129121
if err != nil {
@@ -132,6 +124,7 @@ func (w *Worker) DeleteRef(owner string, repo string, ref string) (*v41.Response
132124
return resp, err
133125
}
134126

127+
// CreatePulllRequestOverviewComment create a pull request comment with the report (overview diff).
135128
func (w *Worker) CreatePulllRequestOverviewComment(owner *string, repo string, pullRequestNumber int) {
136129
if pullRequest, err := w.GetPullRequest(*owner, repo, pullRequestNumber); err == nil {
137130
currentIssueComment := w.GetPulllRequestOverviewComment(owner, repo, pullRequestNumber)
@@ -183,8 +176,9 @@ func (w *Worker) CreatePulllRequestOverviewComment(owner *string, repo string, p
183176
}
184177
}
185178

186-
func (w *Worker) GetPulllRequestOverviewComment(owner *string, repo string, pullrequestId int) *v41.IssueComment {
187-
if comments, err := w.IssueListComments(*owner, repo, pullrequestId, nil); err == nil {
179+
// GetPulllRequestOverviewComment get a pull request comment with the report (overview diff).
180+
func (w *Worker) GetPulllRequestOverviewComment(owner *string, repo string, pullrequestID int) *v41.IssueComment {
181+
if comments, err := w.IssueListComments(*owner, repo, pullrequestID, nil); err == nil {
188182
for _, comment := range comments {
189183
if strings.HasPrefix(*comment.Body, "\u003ch3 align=\"center\"\u003ePull request Overview :checkered_flag:") {
190184
if strings.EqualFold("Bot", *comment.User.Type) && strings.Contains(*comment.User.AvatarURL, strconv.Itoa(w.Config.Github.AppID)) {

0 commit comments

Comments
 (0)