-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
105 lines (95 loc) · 2.64 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
package main
import (
"context"
"errors"
"io/ioutil"
"os"
"github.com/google/go-github/v55/github"
"github.com/hashicorp/go-multierror"
"github.com/sethvargo/go-githubactions"
"golang.org/x/oauth2"
)
func main() {
c := &config{
githubToken: githubactions.GetInput("GITHUB_TOKEN"),
label: githubactions.GetInput("label"),
table: githubactions.GetInput("table"),
partition: githubactions.GetInput("partition"),
bucket: githubactions.GetInput("bucket"),
lock: githubactions.GetInput("lock"),
}
err := c.Validate()
if err != nil {
githubactions.Fatalf("failed to validate input: %+v", err)
}
var uriLocker URILocker
var initErr error
if c.bucket == "" {
uriLocker, initErr = NewDynamoURILocker(c.table, c.partition, c.lock)
} else {
uriLocker, initErr = NewGCSLocker(c.bucket, c.lock)
}
if initErr != nil {
githubactions.Fatalf("failed to initialize: %+v", err)
}
event, err := ioutil.ReadFile(os.Getenv("GITHUB_EVENT_PATH"))
if err != nil {
githubactions.Fatalf("Couldn't read event: %+v", err)
}
ctx := context.Background()
client := c.githubClient(ctx)
labelMutex := &LabelMutex{
context: ctx,
issuesClient: client.Issues,
pullRequestsClient: client.PullRequests,
uriLocker: uriLocker,
label: c.label,
event: event,
eventName: os.Getenv("GITHUB_EVENT_NAME"),
}
err = labelMutex.process()
if err != nil {
githubactions.Fatalf("error while processing event: %+v", err)
}
output := labelMutex.output()
for k, v := range output {
githubactions.SetOutput(k, v)
}
}
type config struct {
githubToken string
label string
table string
partition string
bucket string
lock string
}
func (c *config) Validate() error {
var resultErr *multierror.Error
if c.githubToken == "" {
resultErr = multierror.Append(resultErr, errors.New("input 'GITHUB_TOKEN' missing"))
}
if c.label == "" {
resultErr = multierror.Append(resultErr, errors.New("input 'label' missing"))
}
if c.table == "" && c.bucket == "" {
resultErr = multierror.Append(resultErr, errors.New("either 'table' or 'bucket' is required"))
}
if c.partition == "" {
c.partition = c.bucket
}
if c.partition == "" {
resultErr = multierror.Append(resultErr, errors.New("input 'partition' missing"))
}
if c.lock == "" {
resultErr = multierror.Append(resultErr, errors.New("input 'lock' missing"))
}
return resultErr.ErrorOrNil()
}
func (c *config) githubClient(ctx context.Context) *github.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: c.githubToken},
)
tc := oauth2.NewClient(ctx, ts)
return github.NewClient(tc)
}