-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghaction.go
144 lines (128 loc) · 3.28 KB
/
ghaction.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"
"github.com/google/go-github/v52/github"
githubactions "github.com/sethvargo/go-githubactions"
"golang.org/x/oauth2"
)
type ghAction struct {
gh *github.Client
action *githubactions.Action
inputs *inputs
pr_labels []*github.Label
path string
}
type inputs struct {
pr_number int
gh_user string
gh_repo string
depth int
include string
exclude string
}
func (s *ghAction) setup() {
a := githubactions.New()
event := os.Getenv("GITHUB_EVENT_NAME")
if event != "pull_request" {
a.Warningf("This action is designed to work with 'pull_request' event. Current event: %s", event)
}
// collect GitHub action inputs
i := &inputs{}
var err error
i.pr_number, _ = strconv.Atoi(strings.Split(os.Getenv("GITHUB_REF_NAME"), "/")[0])
// Not all GitHub events would contain ref with PR id. Fall back on input if PR number can't be identified.
if i.pr_number == 0 {
a.Warningf("Failed to identify PR number from GITHUB_REF_NAME. Trying to check inputs.")
i.pr_number, _ = strconv.Atoi(a.GetInput("pr_number"))
}
r := strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")
i.gh_user = r[0]
i.gh_repo = r[1]
// depth is optional, if not provided set to 1
d := a.GetInput("depth")
if len(d) == 0 {
d = "1"
}
i.depth, err = strconv.Atoi(d)
if err != nil {
a.Errorf("Failed to get path depth: %v", err)
}
if i.depth > 4 && i.depth < 1 {
a.Warningf("Path depth has an unreasonable value: %d", i.depth)
}
i.include = a.GetInput("include")
i.exclude = a.GetInput("exclude")
// GitHub authenticated client
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: a.GetInput("token")})
s.gh = github.NewClient(oauth2.NewClient(context.Background(), ts))
a.Infof("GitHub repository: %s", r)
a.Infof("Pull request Number: %d", i.pr_number)
// DEBUG
// a.Infof("ENV: %+v", os.Environ())
s.action = a
s.inputs = i
}
func (s *ghAction) getChangedFiles() []*github.CommitFile {
files, _, err := s.gh.PullRequests.ListFiles(
context.Background(),
s.inputs.gh_user,
s.inputs.gh_repo,
s.inputs.pr_number,
&github.ListOptions{},
)
if err != nil {
s.action.Fatalf("Failed getting PR files: %+v\n", err)
}
return files
}
func (s *ghAction) getIssueLabels() {
issue, _, err := s.gh.Issues.Get(
context.Background(),
s.inputs.gh_user,
s.inputs.gh_repo,
s.inputs.pr_number,
)
if err != nil {
s.action.Fatalf("Failed getting PR object: %+v", err)
}
s.pr_labels = issue.Labels
fmt.Printf("Pull Requiest labels: %+v\n", issue.Labels)
}
func (s *ghAction) getCurrentPathLabel() *github.Label {
for _, l := range s.pr_labels {
if strings.HasPrefix(*l.Name, identifier) {
return l
}
}
return nil
}
func (s *ghAction) addLabel(n string) {
_, _, err := s.gh.Issues.AddLabelsToIssue(
context.Background(),
s.inputs.gh_user,
s.inputs.gh_repo,
s.inputs.pr_number,
[]string{n},
)
if err != nil {
s.action.Warningf("Failed adding label: %+v", err)
}
fmt.Printf("Added PR label %s\n", n)
}
func (s *ghAction) rmLabel(n string) {
_, err := s.gh.Issues.RemoveLabelForIssue(
context.Background(),
s.inputs.gh_user,
s.inputs.gh_repo,
s.inputs.pr_number,
n,
)
if err != nil {
s.action.Warningf("Failed removing label: %+v", err)
}
fmt.Printf("Deleted PR label %s\n", n)
}