forked from github/safe-settings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariables.js
More file actions
196 lines (185 loc) · 6.21 KB
/
variables.js
File metadata and controls
196 lines (185 loc) · 6.21 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
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
const _ = require('lodash')
const Diffable = require('./diffable')
module.exports = class Variables extends Diffable {
constructor (...args) {
super(...args)
if (this.entries) {
// Force all names to uppercase to avoid comparison issues.
this.entries.forEach((variable) => {
variable.name = variable.name.toUpperCase()
})
}
}
/**
* Look-up existing variables for a given repository
*
* @see {@link https://docs.github.com/en/rest/actions/variables?apiVersion=2022-11-28#list-repository-variables} list repository variables
* @returns {Array.<object>} Returns a list of variables that exist in a repository
*/
async find () {
this.log.debug(`Finding repo vars for ${this.repo.owner}/${this.repo.repo}`)
const { data: { variables } } = await this.github.request('GET /repos/:org/:repo/actions/variables', {
org: this.repo.owner,
repo: this.repo.repo
})
return variables
}
/**
* Compare the existing variables with what we've defined as code
*
* @param {Array.<object>} existing Existing variables defined in the repository
* @param {Array.<object>} variables Variables that we have defined as code
*
* @returns {object} The results of a list comparison
*/
getChanged (existing, variables = []) {
const result =
JSON.stringify(
existing.sort((x1, x2) => {
return x1.name.toUpperCase().localeCompare(x2.name.toUpperCase())
})
) !==
JSON.stringify(
variables.sort((x1, x2) => {
return x1.name.toUpperCase().localeCompare(x2.name.toUpperCase())
})
)
return result
}
/**
* Compare existing variables with what's defined
*
* @param {Object} existing The existing entries in GitHub
* @param {Object} attrs The entries defined as code
*
* @returns
*/
comparator (existing, attrs) {
return existing.name === attrs.name
}
/**
* Return a list of changed entries
*
* @param {Object} existing The existing entries in GitHub
* @param {Object} attrs The entries defined as code
*
* @returns
*/
changed (existing, attrs) {
return this.getChanged(_.castArray(existing), _.castArray(attrs))
}
/**
* Update an existing variable if the value has changed
*
* @param {Array.<object>} existing Existing variables defined in the repository
* @param {Array.<object>} variables Variables that we have defined as code
*
* @see {@link https://docs.github.com/en/rest/actions/variables?apiVersion=2022-11-28#update-a-repository-variable} update a repository variable
* @returns
*/
async update (existing, variables = []) {
this.log.debug(`Updating a repo var existing params ${JSON.stringify(existing)} and new ${JSON.stringify(variables)}`)
existing = _.castArray(existing)
variables = _.castArray(variables)
const changed = this.getChanged(existing, variables)
if (changed) {
let existingVariables = [...existing]
for (const variable of variables) {
const existingVariable = existingVariables.find((_var) => _var.name === variable.name)
if (existingVariable) {
existingVariables = existingVariables.filter((_var) => _var.name !== variable.name)
if (existingVariable.value !== variable.value) {
await this.github
.request('PATCH /repos/:org/:repo/actions/variables/:variable_name', {
org: this.repo.owner,
repo: this.repo.repo,
variable_name: variable.name.toUpperCase(),
value: variable.value.toString()
})
.then((res) => {
return res
})
.catch((e) => {
this.logError(e)
})
}
} else {
await this.github
.request('POST /repos/:org/:repo/actions/variables', {
org: this.repo.owner,
repo: this.repo.repo,
name: variable.name.toUpperCase(),
value: variable.value.toString()
})
.then((res) => {
return res
})
.catch((e) => {
this.logError(e)
})
}
}
for (const variable of existingVariables) {
await this.github
.request('DELETE /repos/:org/:repo/actions/variables/:variable_name', {
org: this.repo.owner,
repo: this.repo.repo,
variable_name: variable.name.toUpperCase()
})
.then((res) => {
return res
})
.catch((e) => {
this.logError(e)
})
}
}
}
/**
* Add a new variable to a given repository
*
* @param {object} variable The variable to add, with name and value
*
* @see {@link https://docs.github.com/en/rest/actions/variables?apiVersion=2022-11-28#create-a-repository-variable} create a repository variable
* @returns
*/
async add (variable) {
this.log.debug(`Adding a repo var with the params ${JSON.stringify(variable)}`)
await this.github
.request('POST /repos/:org/:repo/actions/variables', {
org: this.repo.owner,
repo: this.repo.repo,
name: variable.name,
value: variable.value.toString()
})
.then((res) => {
return res
})
.catch((e) => {
this.logError(e)
})
}
/**
* Remove variables that aren't defined as code
*
* @param {String} existing Name of the existing variable to remove
*
* @see {@link https://docs.github.com/en/rest/actions/variables?apiVersion=2022-11-28#delete-a-repository-variable} delete a repository variable
* @returns
*/
async remove (existing) {
this.log.debug(`Removing a repo var with the params ${JSON.stringify(existing)}`)
await this.github
.request('DELETE /repos/:org/:repo/actions/variables/:variable_name', {
org: this.repo.owner,
repo: this.repo.repo,
variable_name: existing.name
})
.then((res) => {
return res
})
.catch((e) => {
this.logError(e)
})
}
}