-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (114 loc) · 3.33 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
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/google/go-github/v52/github"
githubactions "github.com/sethvargo/go-githubactions"
)
const identifier = "tf_path"
func main() {
a := &ghAction{}
a.setup()
a.getIssueLabels()
files := a.getChangedFiles()
a.path = identifyPath(cleanDirPath(files, a.inputs, a.action), a.action)
fmt.Printf("Identified project path: %s\n", a.path)
// Set the path as the action output
// Example: ${{ steps.STEP_ID.outputs.tf_path }}
a.action.SetOutput(identifier, a.path)
label := fmt.Sprintf("%s: %s", identifier, a.path)
currentLabel := a.getCurrentPathLabel()
if currentLabel == nil {
fmt.Printf("No lable for path is set for the Pull request.\n")
a.addLabel(label)
} else if label != *currentLabel.Name {
fmt.Printf("Pull Request path label needs to be updated.\n")
a.rmLabel(*currentLabel.Name)
a.addLabel(label)
} else {
fmt.Printf("Pull Request path label is confirmed.\n")
}
}
func identifyPath(ps []string, a *githubactions.Action) string {
a.Infof("Valid paths under witch changes are made in this PR: %+q", ps)
switch {
case len(ps) == 0:
a.Warningf("NO valid paths were found.\n")
// Not erroring, decision can be made in the next steps of the action
return "undefined"
case len(ps) == 1:
return ps[0]
default:
a.Warningf("More then one potential project paths found.")
a.Warningf("Returning the first match.")
return ps[0]
}
}
func cleanDirPath(f []*github.CommitFile, i *inputs, a *githubactions.Action) []string {
var paths []string
for _, f := range f {
dir := filepath.Dir(*f.Filename)
a.Infof("Validating change for %s", dir)
// Ignore files in the root of repository, directories that start with a '.', and directories not valid acording to the desired depth
if !strings.HasPrefix(dir, ".") {
ds := strings.Split(dir, string(os.PathSeparator))
switch {
case len(ds) == i.depth:
paths = append(paths, dir)
a.Infof("Adding %s to futher validation.", dir)
case len(ds) < i.depth:
a.Warningf("Project path depth is longer, %s path isn't valid", dir)
case len(ds) > i.depth:
// Cut directory path to desired depth
p := strings.Join(ds[:i.depth], string(os.PathSeparator))
paths = append(paths, p)
a.Infof("Adding %s to futher validation. Actual change path is %s", p, ds)
}
}
}
return matchRegex(removeDuplicateValues(paths), i.include, i.exclude)
}
func matchRegex(paths []string, include, exclude string) []string {
if len(include) == 0 && len(exclude) == 0 {
fmt.Println("No regex defined, skipping matching.")
return paths
}
var p []string
if len(include) > 0 {
for _, d := range paths {
r, _ := regexp.MatchString(include, d)
if r {
fmt.Printf("Matched include %s regex for path %s\n", include, d)
p = append(p, d)
}
}
}
var px []string
if len(exclude) > 0 {
for _, d := range p {
e, _ := regexp.MatchString(exclude, d)
if !e {
fmt.Printf("Not matched exclude %s regex for path %s. \n", include, d)
px = append(px, d)
}
}
} else {
return p
}
return px
}
func removeDuplicateValues(ps []string) []string {
keys := make(map[string]bool)
res := []string{}
for _, p := range ps {
if _, ok := keys[p]; !ok {
keys[p] = true
res = append(res, p)
}
}
fmt.Printf("Found and removed %d duplicates\n", len(ps)-len(res))
return res
}