This repository has been archived by the owner on Feb 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgithub.go
144 lines (124 loc) · 3.43 KB
/
github.go
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
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
OpenSince 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.OpenSince == actionItem.Day:
actionTaken = actionItem
takeAction = true
break
case currentPr.OpenSince > actionItem.Day && actionItem.Last:
// Last action
actionTaken = actionItem
takeAction = true
}
}
if !takeAction {
fmt.Printf("[skipping] \n")
return
}
if arguments["--enable"].(bool) {
commentOnPr(currentPr, actionTaken.Message)
switch actionTaken.Action {
case "close":
closePr(currentPr)
default:
fmt.Printf("[%s]\n", actionTaken.Action)
}
} else {
fmt.Printf("[%s 'DRY MODE']\n", actionTaken.Action)
}
}
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 {
currentPr := prType{
client: client,
ctx: ctx,
owner: owner,
repo: repo,
OpenSince: daysSincePRCreated(openPullRequest.CreatedAt),
GHpr: openPullRequest,
}
fmt.Printf("Repo %s/%s/#%d user '%s' open since '%d' days : ", owner, repo, *openPullRequest.Number, *openPullRequest.User.Login, currentPr.OpenSince)
// take action if any
actions(currentPr)
}
}
func daysSincePRCreated(CreatedAt *time.Time) int {
duration := time.Since(*CreatedAt)
if !runConfig.OnlyWorkdays {
return int(duration.Hours() / HOURSINADAY)
}
return workdaysBetweenDates(*CreatedAt, 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.OpenSince), -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)
}