-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
101 lines (82 loc) · 2.45 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
package main
import (
"context"
"github.com/google/go-github/v31/github"
"github.com/kelseyhightower/envconfig"
"github.com/posener/goaction"
"github.com/posener/goaction/actionutil"
"github.com/posener/goaction/log"
)
func main() {
if !goaction.CI {
log.Warnf("Not in GitHub Action mode, quitting...")
return
}
if goaction.Event != goaction.EventPullRequest {
log.Debugf("Not a pull request action, nothing to do here...")
return
}
var config Config
err := envconfig.Process("input", &config)
if err != nil {
log.Fatalf("Required configuration is missing: %s", err)
}
event, err := goaction.GetPullRequest()
if err != nil {
log.Fatalf("Error happened while getting event info: %s", err)
}
prSize := GetPrSize(config, event.PullRequest)
ctx := context.Background()
gh := actionutil.NewClientWithToken(ctx, config.GitHubToken)
_, _, err = gh.IssuesAddLabelsToIssue(ctx, goaction.PrNum(), []string{string(prSize)})
if err != nil {
log.Fatalf("Error happened while adding label: %s", err)
}
if prSize != XL {
log.Debugf("Pull request successfully labeled")
return
}
_, _, err = gh.PullRequestsCreateComment(ctx, goaction.PrNum(), &github.PullRequestComment{
Body: github.String(config.MessageIfXL),
})
if err != nil {
log.Fatalf("Error happened while adding comment: %s", err)
}
if config.FailIfXL {
log.Fatalf("PR size is XL, make it shorter, please!")
}
log.Debugf("Pull request successfully labeled")
}
func GetPrSize(config Config, pr *github.PullRequest) PrSize {
totalModifications := pr.GetAdditions() + pr.GetDeletions()
switch {
case totalModifications < config.XsMaxSize:
return XS
case totalModifications < config.SMaxSize:
return S
case totalModifications < config.MMaxSize:
return M
case totalModifications < config.LMaxSize:
return L
default:
return XL
}
}
type PrSize string
const (
XS PrSize = "size/xs"
S PrSize = "size/s"
M PrSize = "size/m"
L PrSize = "size/l"
XL PrSize = "size/xl"
)
// Config is the data structure used to define the action settings.
type Config struct {
GitHubToken string `envconfig:"GITHUB_TOKEN" required:"true"`
XsMaxSize int `envconfig:"XS_MAX_SIZE" default:"10"`
SMaxSize int `envconfig:"S_MAX_SIZE" default:"100"`
MMaxSize int `envconfig:"M_MAX_SIZE" default:"500"`
LMaxSize int `envconfig:"L_MAX_SIZE" default:"1000"`
FailIfXL bool `envconfig:"FAIL_IF_XL" default:"false"`
MessageIfXL string `envconfig:"MESSAGE_IF_XL" default:""`
}