-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (125 loc) · 4.07 KB
/
main.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"
"log"
"net/http"
"runtime"
"strconv"
"strings"
"time"
"github.com/bradleyfalzon/ghinstallation"
"github.com/google/go-github/v31/github"
"github.com/slack-go/slack"
)
var slackAPI *slack.Client
var config = Config{}
func arrayHasOverlap(list1 []string, list2 []string) bool {
for _, a := range list1 {
for _, b := range list2 {
if a == b {
return true
}
}
}
return false
}
func main() {
configure(&config)
itr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, config.GitHub.AppID, config.GitHub.InstallationID, config.GitHub.PrivateKey)
if err != nil {
panic(err)
}
timeUntilUnassigned := time.Hour * 24 * time.Duration(config.Unassign.DaysUntilUnassign)
timeUntilNotified := time.Hour * 24 * time.Duration(config.Unassign.DaysUntilWarning)
gh := github.NewClient(&http.Client{Transport: itr})
slackAPI = slack.New(config.Slack.Token)
if err != nil {
panic(err)
}
opts := &github.IssueListByRepoOptions{Assignee: "*", State: "open"}
for _, repo := range config.GitHub.Repos {
issues, _, err := gh.Issues.ListByRepo(context.Background(), config.GitHub.Organization, repo, opts)
if err != nil {
logError("[FATAL] Getting issues", err)
panic(err)
}
for _, issue := range issues {
labels := []string{}
for _, label := range issue.Labels {
labels = append(labels, *label.Name)
}
if arrayHasOverlap(config.GitHub.IgnoreLabels, labels) {
continue
}
assignees := []string{}
for _, assignee := range issue.Assignees {
assignees = append(assignees, *assignee.Login)
}
if issue.UpdatedAt.Add(timeUntilUnassigned).Before(time.Now()) {
// the issue hasn't been updated in a week
message := "Hi! I've removed @" + strings.Join(assignees, ", @") + " from this issue due to a lack of activity. If this is a mistake, feel free to reassign yourself. If you would like to work on this issue, feel free to assign yourself."
_, _, err = gh.Issues.RemoveAssignees(context.Background(), config.GitHub.Organization, repo, *issue.Number, assignees)
if err != nil {
logError("Removing issue assignees", err)
continue
}
_, _, err = gh.Issues.CreateComment(context.Background(), config.GitHub.Organization, repo, *issue.Number, &github.IssueComment{
Body: github.String(message),
})
// fmt.Println(message)
if err != nil {
logError("Creating issue comment", err)
}
} else if issue.UpdatedAt.Add(timeUntilNotified).Before(time.Now()) {
for _, assignee := range assignees {
message := "Hi! This is a warning that due to a lack of activity on <" + *issue.HTMLURL + "|" + config.GitHub.Organization + "/" + repo + "#" + strconv.Itoa(*issue.Number) + ">, you will be unassigned soon. Comment on the issue if you want to stay assigned (A simple \"bump\" will suffice)."
_, _, _, err := slackAPI.SendMessage(config.Users[assignee], slack.MsgOptionText(message, false))
if err != nil {
logError("Posting message to slack", err)
}
}
}
}
}
}
func logError(desc string, err error) {
buf := make([]byte, 1<<16)
stackSize := runtime.Stack(buf, false)
stackTrace := string(buf[0:stackSize])
log.Println("======================================")
log.Printf("Error occurred while '%s'!", desc)
errDesc := ""
if err != nil {
errDesc = err.Error()
} else {
errDesc = "(err == nil)"
}
log.Println(errDesc)
log.Println(stackTrace)
log.Println("======================================")
title := fmt.Sprintf("An error occurred - %s", desc)
_, _, _, postingErr := slackAPI.SendMessage(config.Slack.ErrlogChannel, slack.MsgOptionAttachments(slack.Attachment{
Fallback: title,
Color: "danger",
Title: title,
Text: "```" + stackTrace + "```",
MarkdownIn: []string{"fields"},
Fields: []slack.AttachmentField{
{
Title: "Host",
Value: "PlanBot :robot_face:",
Short: true,
},
{
Title: "Message",
Value: err.Error(),
Short: true,
},
},
}))
if postingErr != nil {
log.Println("An error occurred while reporting the previous error to Slack")
log.Println(postingErr)
}
}