-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathcommenter.go
134 lines (114 loc) · 3.31 KB
/
commenter.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/owenrumney/go-github-pr-commenter/commenter"
)
func main() {
fmt.Println("Starting the github commenter...")
token := os.Getenv("INPUT_GITHUB_TOKEN")
if len(token) == 0 {
fail("the INPUT_GITHUB_TOKEN has not been set")
}
githubRepository := os.Getenv("GITHUB_REPOSITORY")
split := strings.Split(githubRepository, "/")
if len(split) != 2 {
fail(fmt.Sprintf("Expected value for split not found. Expected 2 in %v", split))
}
owner := split[0]
repo := split[1]
prNo, err := extractPullRequestNumber()
if err != nil {
fmt.Println("Not a PR, nothing to comment on, exiting...")
return
}
c, err := commenter.NewCommenter(token, owner, repo, prNo)
if err != nil {
fail(fmt.Sprintf("failed to create a new commenter. %s", err.Error()))
}
results, err := loadResultsFile()
if err != nil {
fail(fmt.Sprintf("failed to load results. %s", err.Error()))
}
if len(results) == 0 {
fmt.Println("No issues found.")
os.Exit(0)
}
var errMessages []string
workspacePath := fmt.Sprintf("%s/", os.Getenv("GITHUB_WORKSPACE"))
var validCommentWritten bool
for _, result := range results {
result.Range.Filename = strings.ReplaceAll(result.Range.Filename, workspacePath, "")
comment := generateErrorMessage(result)
err := c.WriteMultiLineComment(result.Range.Filename, comment, result.Range.StartLine, result.Range.EndLine)
if err != nil {
// don't error if its simply that the comments aren't valid for the PR
switch err.(type) {
case commenter.CommentAlreadyWrittenError:
fmt.Println("Comment already written so not writing")
validCommentWritten = true
case commenter.CommentNotValidError:
fmt.Printf("%s .... not writing as not part of the current PR\n", result.Description)
continue
default:
errMessages = append(errMessages, err.Error())
}
} else {
validCommentWritten = true
fmt.Printf("Commenting for %s to %s:%d:%d\n", result.Description, result.Range.Filename, result.Range.StartLine, result.Range.EndLine)
}
}
if len(errMessages) > 0 {
fmt.Printf("There were %d errors:\n", len(errMessages))
for _, err := range errMessages {
fmt.Println(err)
}
os.Exit(1)
}
if validCommentWritten || len(errMessages) > 0 {
if softFail, ok := os.LookupEnv("INPUT_SOFT_FAIL_COMMENTER"); ok && strings.ToLower(softFail) == "true" {
return
}
os.Exit(1)
}
}
func generateErrorMessage(result result) string {
return fmt.Sprintf(`tfsec check %s failed.
Description: %s
Severity: %s
For more information, see:
%s`,
result.RuleID, result.Description, result.Severity, formatUrls(result.Links))
}
func extractPullRequestNumber() (int, error) {
file, err := ioutil.ReadFile("/github/workflow/event.json")
if err != nil {
return -1, err
}
var data interface{}
err = json.Unmarshal(file, &data)
if err != nil {
return -1, err
}
payload := data.(map[string]interface{})
prNumber, err := strconv.Atoi(fmt.Sprintf("%v", payload["number"]))
if err != nil {
return 0, fmt.Errorf("not a valid PR")
}
return prNumber, nil
}
func formatUrls(urls []string) string {
urlList := ""
for _, url := range urls {
urlList += fmt.Sprintf("- %s\n", url)
}
return urlList
}
func fail(err string) {
fmt.Printf("The commenter failed with the following error:\n%s\n", err)
os.Exit(-1)
}