Skip to content

Commit 6be8ddd

Browse files
authored
Merge pull request #4 from impossiblecloud/some-improvements
Add PR review checks
2 parents 0e39983 + 3859d07 commit 6be8ddd

File tree

4 files changed

+73
-11
lines changed

4 files changed

+73
-11
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,35 @@ Github app:
1616
- `GITHUB_APP_ID` - Github app ID
1717
- `GITHUB_INSTALLATION_ID` - installation ID (see URL when managing installation)
1818
- `GITHUB_APP_PRIVATE_KEY` - Github app private key PEM (sensitive)
19+
20+
## Config
21+
22+
Config example:
23+
24+
```yaml
25+
github_pr_notifications:
26+
27+
- gh_owner: my-org
28+
gh_repo: my-repo-1
29+
gh_pr_labels:
30+
- enhancement
31+
gh_pr_include_drafts: true
32+
gh_pr_ignore_approved: true
33+
gh_pr_ignore_changes_requested: true
34+
# Mon-Fri every 2 hours during business hours
35+
schedule: "CRON_TZ=Europe/Berlin 00 10-18/2 * * 1-5"
36+
notify:
37+
slack:
38+
channel_id: "ABC123000"
39+
message_header: ":warning: Please review at your earliest convenience @some-group-handle"
40+
41+
- gh_owner: my-org
42+
gh_repo: my-repo-2
43+
gh_pr_labels: []
44+
# Mon-Fri every 30 hours during business hours
45+
schedule: "CRON_TZ=Europe/Berlin */30 10-18 * * 1-5"
46+
notify:
47+
slack:
48+
channel_id: "DEF456000"
49+
message_header: ":warning: Please review at your earliest convenience @some-group-handle"
50+
```

fixtures/config.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ github_pr_notifications:
55
gh_pr_labels:
66
- enhancement
77
gh_pr_include_drafts: true
8+
gh_pr_ignore_approved: true
9+
gh_pr_ignore_changes_requested: true
810
# Mon-Fri every 2 hours during business hours
9-
schedule: "00 10,12,14,16,18 * * 1-5"
11+
schedule: "CRON_TZ=Europe/Berlin 00 10-18/2 * * 1-5"
1012
notify:
1113
slack:
1214
channel_id: "ABC123000"
@@ -15,9 +17,8 @@ github_pr_notifications:
1517
- gh_owner: my-org
1618
gh_repo: my-repo-2
1719
gh_pr_labels: []
18-
gh_pr_include_drafts: false
1920
# Mon-Fri every 30 hours during business hours
20-
schedule: "*/30 10-18 * * 1-5"
21+
schedule: "CRON_TZ=Europe/Berlin */30 10-18 * * 1-5"
2122
notify:
2223
slack:
2324
channel_id: "DEF456000"

internal/cfg/cfg.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ type Notification struct {
3131

3232
// PrNotification is a struct for a single GH repo PRs notifications
3333
type PrNotification struct {
34-
Owner string `yaml:"gh_owner"`
35-
Labels []string `yaml:"gh_pr_labels"`
36-
Repo string `yaml:"gh_repo"`
37-
Schedule string `yaml:"schedule"`
38-
IncludeDrafts bool `yaml:"gh_pr_include_drafts"`
39-
Notifications Notification `yaml:"notify"`
34+
Owner string `yaml:"gh_owner"`
35+
Labels []string `yaml:"gh_pr_labels"`
36+
Repo string `yaml:"gh_repo"`
37+
Schedule string `yaml:"schedule"`
38+
IncludeDrafts bool `yaml:"gh_pr_include_drafts"`
39+
IgnoreApproved bool `yaml:"gh_pr_ignore_approved"`
40+
IgnoreChangesRequested bool `yaml:"gh_pr_ignore_changes_requested"`
41+
Notifications Notification `yaml:"notify"`
4042
}
4143

4244
// LoadConfig loads config file

internal/gh/gh.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,36 @@ func (g *Github) GetPullRequests(prn cfg.PrNotification) ([]*github.PullRequest,
7777
if !prn.IncludeDrafts && *pr.Draft {
7878
continue
7979
}
80-
//glog.V(8).Infof("Got PR-%d %s: %s", *pr.Number, *pr.Title, *pr.State)
80+
glog.V(8).Infof("Checking PR-%d %q: %s", *pr.Number, *pr.Title, *pr.State)
81+
8182
if labelsMatched(pr.Labels, prn.Labels) {
82-
result = append(result, pr)
83+
addPR := true
84+
85+
// Ignore PRs that have APPROVED or CHANGES_REQUESTED reviews, if it's configured
86+
if prn.IgnoreApproved || prn.IgnoreChangesRequested {
87+
reviews, _, err := g.Client.PullRequests.ListReviews(context.Background(), prn.Owner, prn.Repo, *pr.Number, &github.ListOptions{})
88+
if err != nil {
89+
return result, err
90+
}
91+
for _, review := range reviews {
92+
glog.V(10).Infof("Review %s: %s", *review.HTMLURL, *review.State)
93+
if prn.IgnoreApproved && *review.State == "APPROVED" {
94+
glog.V(8).Infof("Skipping PR-%d: %q, because it's APPROVED", *pr.Number, *pr.Title)
95+
addPR = false
96+
break
97+
}
98+
if prn.IgnoreChangesRequested && *review.State == "CHANGES_REQUESTED" {
99+
glog.V(8).Infof("Skipping PR-%d: %q, because it's CHANGES_REQUESTED", *pr.Number, *pr.Title)
100+
addPR = false
101+
break
102+
}
103+
}
104+
}
105+
106+
if addPR {
107+
glog.V(8).Infof("Adding to notifications PR-%d: %q", *pr.Number, *pr.Title)
108+
result = append(result, pr)
109+
}
83110
}
84111
}
85112

0 commit comments

Comments
 (0)