-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeowners-policy.go
87 lines (71 loc) · 1.8 KB
/
codeowners-policy.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
package cmd
import (
"context"
"fmt"
"os"
"github.com/google/go-github/v50/github"
)
// What the codeowners file should look like
type codeownersPolicy struct {
Content string `yaml:"content"`
Tags []string `yaml:"tags"`
}
// Does the work to check codeowners policy against a repository and branch
func auditCodeownersPolicy(policy codeownersPolicy, repo *wardenRepo, client *github.Client, branch string) auditResults {
var results auditResults
if !tagsMatched(policy.Tags, repo.Tags()) {
return nil
}
file, _, _, err := client.Repositories.GetContents(context.Background(), repo.Owner, repo.Name, ".github/CODEOWNERS", &github.RepositoryContentGetOptions{Ref: branch})
if err != nil {
switch err.(type) {
case *github.ErrorResponse:
if err.(*github.ErrorResponse).Response != nil && err.(*github.ErrorResponse).Response.StatusCode == 404 {
results.add(
repo,
RESULT_ERROR,
ERR_CO_MISSING,
)
return results
} else {
fmt.Fprintf(os.Stderr, err.Error())
return nil
}
default:
fmt.Fprintf(os.Stderr, err.Error())
return nil
}
}
content, err := file.GetContent()
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
return nil
}
// check if the files match
if policy.Content != content {
results.add(
repo,
RESULT_ERROR,
ERR_CO_DIFFERENT,
)
}
// check for codeowners syntax errors
coErrs, _, err := client.Repositories.GetCodeownersErrors(context.Background(), repo.Owner, repo.Name)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
return nil
}
if len(coErrs.Errors) > 0 {
var suggestions []string
for _, coErr := range coErrs.Errors {
suggestions = append(suggestions, " > "+coErr.GetSuggestion())
}
results.add(
repo,
RESULT_ERROR,
ERR_CO_SYNTAX,
suggestions,
)
}
return results
}