This repository was archived by the owner on Sep 6, 2025. It is now read-only.
forked from ahelal/ci-bully
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub.go
More file actions
178 lines (152 loc) · 4.5 KB
/
github.go
File metadata and controls
178 lines (152 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
type prType struct {
client *github.Client
ctx *context.Context
owner string
repo string
inactiveDays int
CloseOn int
GHpr *github.PullRequest
}
const (
HOURSINADAY = 24.0
)
func actions(currentPr prType) {
var takeAction bool
var actionTaken action
for _, actionItem := range runConfig.Actions {
switch {
case currentPr.inactiveDays == actionItem.Day:
actionTaken = actionItem
takeAction = true
break
case currentPr.inactiveDays > actionItem.Day && actionItem.Last:
// Last action
actionTaken = actionItem
takeAction = true
}
}
if !takeAction {
fmt.Printf("[skipping] \n")
return
}
currentPr.CloseOn = remainingDaysUntilClose(currentPr)
if arguments["--enable"].(bool) {
commentOnPr(currentPr, actionTaken.Message)
if actionTaken.Action == "close" {
closePr(currentPr)
}
fmt.Printf("[%s]\n", actionTaken.Action)
} else {
fmt.Printf("[%s 'DRY MODE']\n", actionTaken.Action)
}
}
func remainingDaysUntilClose(pullRequest prType) int {
for _, actionItem := range runConfig.Actions {
if actionItem.Action == "close" {
return actionItem.Day - pullRequest.inactiveDays
}
}
return 0
}
func checkOpenPRs(ctx *context.Context, client *github.Client, owner string, repo string) {
openPullRequests, _, err := client.PullRequests.List(*ctx, owner, repo, nil)
if err != nil {
fmt.Printf("Error %s\n", err)
os.Exit(1)
}
for _, openPullRequest := range openPullRequests {
lastDate, err := lastActionDate(ctx, client, owner, repo, openPullRequest)
if err != nil {
fmt.Printf("Error %s\n", err)
os.Exit(1)
}
currentPr := prType{
client: client,
ctx: ctx,
owner: owner,
repo: repo,
inactiveDays: daysSince(lastDate),
GHpr: openPullRequest,
}
fmt.Printf("Repo %s/%s/#%d user '%s' open since '%d' days : ", owner, repo, *openPullRequest.Number, *openPullRequest.User.Login, currentPr.inactiveDays)
// take action if any
actions(currentPr)
}
}
func lastActionDate(ctx *context.Context, client *github.Client, owner string, repo string, pullRequest *github.PullRequest) (*time.Time, error) {
if !runConfig.DaysSinceLastCommit {
return pullRequest.CreatedAt, nil
}
return lastCommitDate(ctx, client, owner, repo, pullRequest)
}
func lastCommitDate(ctx *context.Context, client *github.Client, owner string, repo string, pullRequest *github.PullRequest) (*time.Time, error) {
commitsList, _, err := client.PullRequests.ListCommits(*ctx, owner, repo, pullRequest.GetNumber(), nil)
if err != nil {
return nil, err
}
var lastDate time.Time
for _, commit := range commitsList {
lastDate = commit.Commit.Committer.GetDate()
}
return &lastDate, nil
}
func daysSince(date *time.Time) int {
if !runConfig.OnlyWorkdays {
return int(time.Since(*date).Hours() / HOURSINADAY)
}
return workdaysBetweenDates(*date, time.Now())
}
// workdaysBetweenDates calculates the workdays between two dates.
func workdaysBetweenDates(t1, t2 time.Time) int {
if t2.Before(t1) {
t1, t2 = t2, t1
}
days := 0
for {
if t1.After(t2) || t1.Equal(t2) {
return days
}
if t1.Weekday() != time.Saturday && t1.Weekday() != time.Sunday {
days++
}
t1 = t1.Add(time.Hour * HOURSINADAY)
}
return days
}
func loopOverRepos() {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: runConfig.Token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
for _, repo := range runConfig.Repos {
owner := strings.Split(repo, "/")[0]
repo := strings.Split(repo, "/")[1]
checkOpenPRs(&ctx, client, owner, repo)
}
}
func commentOnPr(pr prType, message string) {
message = strings.Replace(message, "_USER_", *pr.GHpr.User.Login, -1)
message = strings.Replace(message, "_SINCE_", strconv.Itoa(pr.inactiveDays), -1)
message = strings.Replace(message, "_TILL_", strconv.Itoa(pr.CloseOn), -1)
commentMsg := &github.IssueComment{Body: &message}
_, _, err := pr.client.Issues.CreateComment(*pr.ctx, pr.owner, pr.repo, *pr.GHpr.Number, commentMsg)
checkError(fmt.Sprintf("Error failed to comment on %s/%s #%d", pr.owner, pr.repo, *pr.GHpr.Number), err)
}
func closePr(pr prType) {
*pr.GHpr.State = "closed"
_, _, err := pr.client.PullRequests.Edit(*pr.ctx, pr.owner, pr.repo, *pr.GHpr.Number, pr.GHpr)
checkError(fmt.Sprintf("Error failed to close PR %s/%s #%d", pr.owner, pr.repo, *pr.GHpr.Number), err)
}