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
/
github.go
112 lines (98 loc) · 2.85 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
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
}
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 {
duration := time.Since(*openPullRequest.CreatedAt)
currentPr := prType{
client: client,
ctx: ctx,
owner: owner,
repo: repo,
OpenSince: int(duration.Hours() / 24.0),
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 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)
}