-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathrulesets.js
208 lines (196 loc) · 7.25 KB
/
rulesets.js
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const Diffable = require('./diffable')
const NopCommand = require('../nopcommand')
const MergeDeep = require('../mergeDeep')
const ignorableFields = []
const version = {
'X-GitHub-Api-Version': '2022-11-28'
}
module.exports = class Rulesets extends Diffable {
constructor (nop, github, repo, entries, log, errors, scope) {
super(nop, github, repo, entries, log, errors)
this.github = github
this.repo = repo
this.rulesets = entries
this.log = log
this.nop = nop
this.scope = scope || 'repo'
}
// Find all Rulesets for this org
find () {
if (this.scope === 'org') {
this.log.debug(`Getting all rulesets for the org ${this.repo.owner}`)
const listOptions = this.github.request.endpoint.merge('GET /orgs/{org}/rulesets', {
org: this.repo.owner,
headers: version
})
this.log.debug(listOptions)
return this.github.paginate(listOptions)
.then(res => {
const rulesets = res.map(ruleset => {
const getOptions = this.github.request.endpoint.merge('GET /orgs/{org}/rulesets/{id}', {
org: this.repo.owner,
id: ruleset.id,
headers: version
})
return this.github.paginate(getOptions)
})
return Promise.all(rulesets).then(res => {
return res ? res.flat(1) : []
})
}).catch(e => {
return this.handleError(e, [])
})
} else {
this.log.debug(`Getting all rulesets for the repo ${this.repo}`)
const listOptions = this.github.request.endpoint.merge('GET /repos/{owner}/{repo}/rulesets', {
owner: this.repo.owner,
repo: this.repo.repo,
headers: version
})
this.log(listOptions)
return this.github.paginate(listOptions)
.then(res => {
const rulesets = res
.filter(ruleset => ruleset.source_type === 'Repository')
.map(ruleset => {
const getOptions = this.github.request.endpoint.merge('GET /repos/{owner}/{repo}/rulesets/{id}', {
owner: this.repo.owner,
repo: this.repo.repo,
id: ruleset.id,
headers: version
})
return this.github.paginate(getOptions)
})
return Promise.all(rulesets).then(res => {
return res ? res.flat(1) : []
})
}).catch(e => {
return this.handleError(e, [])
})
}
}
isEmpty (maybeEmpty) {
return (maybeEmpty === null) || Object.keys(maybeEmpty).length === 0
}
comparator (existing, attrs) {
return existing.name === attrs.name
}
changed (existing, attrs) {
const mergeDeep = new MergeDeep(this.log, this.github, ignorableFields)
const merged = mergeDeep.compareDeep(existing, attrs)
return merged.hasChanges
}
update (existing, attrs) {
const parms = this.wrapAttrs(Object.assign({ id: existing.id }, attrs))
if (this.scope === 'org') {
if (this.nop) {
return Promise.resolve([
new NopCommand(this.constructor.name, this.repo, this.github.request.endpoint('PUT /orgs/{org}/rulesets/{id}', parms), 'Update Ruleset')
])
}
this.log.debug(`Updating Ruleset with the following values ${JSON.stringify(parms, null, 2)}`)
return this.github.request('PUT /orgs/{org}/rulesets/{id}', parms).then(res => {
this.log(`Ruleset updated successfully ${JSON.stringify(res.url)}`)
return res
}).catch(e => {
return this.handleError(e)
})
} else {
if (this.nop) {
return Promise.resolve([
new NopCommand(this.constructor.name, this.repo, this.github.request.endpoint('PUT /repos/{owner}/{repo}/rulesets/{id}', parms), 'Update Ruleset')
])
}
this.log.debug(`Updating Ruleset with the following values ${JSON.stringify(parms, null, 2)}`)
return this.github.request('PUT /repos/{owner}/{repo}/rulesets/{id}', parms).then(res => {
this.log(`Ruleset updated successfully ${JSON.stringify(res.url)}`)
return res
}).catch(e => {
return this.handleError(e)
})
}
}
add (attrs) {
if (this.scope === 'org') {
if (this.nop) {
return Promise.resolve([
new NopCommand(this.constructor.name, this.repo, this.github.request.endpoint('POST /orgs/{org}/rulesets', this.wrapAttrs(attrs)), 'Create Ruleset')
])
}
this.log.debug(`Creating Rulesets with the following values ${JSON.stringify(attrs, null, 2)}`)
return this.github.request('POST /orgs/{org}/rulesets', this.wrapAttrs(attrs)).then(res => {
this.log(`Ruleset created successfully ${JSON.stringify(res.url)}`)
return res
}).catch(e => {
return this.handleError(e)
})
} else {
if (this.nop) {
return Promise.resolve([
new NopCommand(this.constructor.name, this.repo, this.github.request.endpoint('POST /repos/{owner}/{repo}/rulesets', this.wrapAttrs(attrs)), 'Create Ruleset')
])
}
this.log.debug(`Creating Rulesets with the following values ${JSON.stringify(attrs, null, 2)}`)
return this.github.request('POST /repos/{owner}/{repo}/rulesets', this.wrapAttrs(attrs)).then(res => {
this.log(`Ruleset created successfully ${JSON.stringify(res.url)}`)
return res
}).catch(e => {
return this.handleError(e)
})
}
}
remove (existing) {
const parms = this.wrapAttrs(Object.assign({ id: existing.id }))
if (this.scope === 'org') {
if (this.nop) {
return Promise.resolve([
new NopCommand(this.constructor.name, this.repo, this.github.request.endpoint('DELETE /orgs/{org}/rulesets/{id}', parms), 'Delete Ruleset')
])
}
this.log.debug(`Deleting Ruleset with the following values ${JSON.stringify(parms, null, 2)}`)
return this.github.request('DELETE /orgs/{org}/rulesets/{id}', parms).then(res => {
this.log(`Ruleset deleted successfully ${JSON.stringify(res.url)}`)
return res
}).catch(e => {
return this.handleError(e)
})
} else {
if (this.nop) {
return Promise.resolve([
new NopCommand(this.constructor.name, this.repo, this.github.request.endpoint('DELETE /repos/{owner}/{repo}/rulesets/{id}', parms), 'Delete Ruleset')
])
}
this.log.debug(`Deleting Ruleset with the following values ${JSON.stringify(parms, null, 2)}`)
return this.github.request('DELETE /repos/{owner}/{repo}/rulesets/{id}', parms).then(res => {
this.log(`Ruleset deleted successfully ${JSON.stringify(res.url)}`)
return res
}).catch(e => {
if (e.status === 404) {
return
}
return this.handleError(e)
})
}
}
wrapAttrs (attrs) {
if (this.scope === 'org') {
return Object.assign({}, attrs, {
org: this.repo.owner,
headers: version
})
} else {
return Object.assign({}, attrs, {
owner: this.repo.owner,
repo: this.repo.repo,
headers: version
})
}
}
handleError (e, returnValue) {
this.logError(e)
if (this.nop) {
return Promise.resolve([(new NopCommand(this.constructor.name, this.repo, null, `error: ${e}`, 'ERROR'))])
}
return Promise.resolve(returnValue)
}
}