-
Notifications
You must be signed in to change notification settings - Fork 12
/
permission.go
164 lines (133 loc) · 3.16 KB
/
permission.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"encoding/base64"
"path/filepath"
"strings"
"github.com/opensourceways/community-robot-lib/giteeclient"
"github.com/opensourceways/repo-file-cache/models"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/yaml"
)
const ownerFile = "OWNERS"
func (bot *robot) hasPermission(
commenter string,
pr giteeclient.PRInfo,
cfg *botConfig,
log *logrus.Entry,
) (bool, error) {
commenter = strings.ToLower(commenter)
p, err := bot.cli.GetUserPermissionsOfRepo(pr.Org, pr.Repo, commenter)
if err != nil {
return false, err
}
if p.Permission == "admin" || p.Permission == "write" {
return true, nil
}
if bot.isRepoOwners(commenter, pr, log) {
return true, nil
}
if cfg.CheckPermissionBasedOnSigOwners {
return bot.isOwnerOfSig(commenter, pr, cfg, log)
}
return false, nil
}
func (bot *robot) isRepoOwners(
commenter string,
pr giteeclient.PRInfo,
log *logrus.Entry,
) bool {
v, err := bot.cli.GetPathContent(pr.Org, pr.Repo, ownerFile, pr.BaseRef)
if err != nil {
log.Errorf(
"get file:%s/%s/%s:%s, err:%s",
pr.Org, pr.Repo, pr.BaseRef, ownerFile, err.Error(),
)
return false
}
o := decodeOwnerFile(v.Content, log)
return o.Has(commenter)
}
func (bot *robot) isOwnerOfSig(
commenter string,
pr giteeclient.PRInfo,
cfg *botConfig,
log *logrus.Entry,
) (bool, error) {
changes, err := bot.cli.GetPullRequestChanges(pr.Org, pr.Repo, pr.Number)
if err != nil || len(changes) == 0 {
return false, err
}
pathes := sets.NewString()
for _, file := range changes {
if !cfg.regSigDir.MatchString(file.Filename) {
return false, nil
}
pathes.Insert(filepath.Dir(file.Filename))
}
files, err := bot.getSigOwnerFiles(pr.Org, pr.Repo, pr.BaseRef, log)
if err != nil {
return false, err
}
for _, v := range files.Files {
p := v.Path.Dir()
if !pathes.Has(p) {
continue
}
if o := decodeOwnerFile(v.Content, log); !o.Has(commenter) {
return false, nil
}
pathes.Delete(p)
if len(pathes) == 0 {
return true, nil
}
}
return false, nil
}
func (bot *robot) getSigOwnerFiles(org, repo, branch string, log *logrus.Entry) (models.FilesInfo, error) {
files, err := bot.cacheCli.GetFiles(
models.Branch{
Platform: "gitee",
Org: org,
Repo: repo,
Branch: branch,
},
ownerFile, false,
)
if err != nil {
return models.FilesInfo{}, err
}
if len(files.Files) == 0 {
log.WithFields(
logrus.Fields{
"org": org,
"repo": repo,
"branch": branch,
},
).Infof("there is not %s file stored in cache.", ownerFile)
}
return files, nil
}
func decodeOwnerFile(content string, log *logrus.Entry) sets.String {
owners := sets.NewString()
c, err := base64.StdEncoding.DecodeString(content)
if err != nil {
log.WithError(err).Error("decode file")
return owners
}
var m struct {
Maintainers []string `yaml:"maintainers"`
Committers []string `yaml:"committers"`
}
if err = yaml.Unmarshal(c, &m); err != nil {
log.WithError(err).Error("code yaml file")
return owners
}
for _, v := range m.Maintainers {
owners.Insert(strings.ToLower(v))
}
for _, v := range m.Committers {
owners.Insert(strings.ToLower(v))
}
return owners
}