-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathcustom_properties.js
More file actions
102 lines (86 loc) · 2.75 KB
/
custom_properties.js
File metadata and controls
102 lines (86 loc) · 2.75 KB
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
const Diffable = require('./diffable')
const NopCommand = require('../nopcommand')
module.exports = class CustomProperties extends Diffable {
constructor (...args) {
super(...args)
if (this.entries) {
this.normalizeEntries()
}
}
// Force all names to lowercase to avoid comparison issues.
normalizeEntries () {
this.entries = this.entries
.filter(({ name }) => name != null)
.map(({ name, value }) => ({
name: name.toLowerCase(),
value
}))
}
async find () {
const { owner, repo } = this.repo
const repoFullName = `${owner}/${repo}`
this.log.debug(`Getting all custom properties for the repo ${repoFullName}`)
const customProperties = await this.github.paginate(
this.github.rest.repos.getCustomPropertiesValues,
{
owner,
repo,
per_page: 100
}
)
this.log.debug(`Found ${customProperties.length} custom properties`)
return this.normalize(customProperties)
}
// Force all names to lowercase to avoid comparison issues.
normalize (properties) {
return properties
.filter(({ property_name: propertyName }) => propertyName != null)
.map(({ property_name: propertyName, value }) => ({
name: propertyName.toLowerCase(),
value
}))
}
comparator (existing, attrs) {
return existing.name === attrs.name
}
changed (existing, attrs) {
return attrs.value !== existing.value
}
async update ({ name }, { value }) {
return this.modifyProperty('Update', { name, value })
}
async add ({ name, value }) {
return this.modifyProperty('Create', { name, value })
}
// Custom Properties on repository does not support deletion, so we set the value to null
async remove ({ name }) {
return this.modifyProperty('Delete', { name, value: null })
}
async modifyProperty (operation, { name, value }) {
const { owner, repo } = this.repo
const repoFullName = `${owner}/${repo}`
const params = {
owner,
repo,
properties: [{
property_name: name,
value
}]
}
if (this.nop) {
return new NopCommand(
this.constructor.name,
this.repo,
this.github.rest.repos.createOrUpdateCustomPropertiesValues.endpoint(params),
`${operation} Custom Property`
)
}
try {
this.log.debug(`${operation} Custom Property "${name}" for the repo ${repoFullName}`)
await this.github.rest.repos.createOrUpdateCustomPropertiesValues(params)
this.log.debug(`Successfully ${operation.toLowerCase()}d Custom Property "${name}" for the repo ${repoFullName}`)
} catch (e) {
this.logError(`Error during ${operation} Custom Property "${name}" for the repo ${repoFullName}: ${e.message || e}`)
}
}
}