Skip to content

Commit d78ef9a

Browse files
authored
Merge branch 'main-enterprise' into bug/archived-repo
2 parents da756ee + db9d78c commit d78ef9a

4 files changed

Lines changed: 211 additions & 212 deletions

File tree

lib/plugins/variables.js

Lines changed: 39 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -1,196 +1,71 @@
1-
const _ = require('lodash')
21
const Diffable = require('./diffable')
2+
const NopCommand = require('../nopcommand')
33

44
module.exports = class Variables extends Diffable {
55
constructor (...args) {
66
super(...args)
77

88
if (this.entries) {
9-
// Force all names to uppercase to avoid comparison issues.
109
this.entries.forEach((variable) => {
1110
variable.name = variable.name.toUpperCase()
1211
})
1312
}
1413
}
1514

16-
/**
17-
* Look-up existing variables for a given repository
18-
*
19-
* @see {@link https://docs.github.com/en/rest/actions/variables?apiVersion=2026-03-10#list-repository-variables} list repository variables
20-
* @returns {Array.<object>} Returns a list of variables that exist in a repository
21-
*/
22-
async find () {
15+
find () {
2316
this.log.debug(`Finding repo vars for ${this.repo.owner}/${this.repo.repo}`)
24-
const { data: { variables } } = await this.github.request('GET /repos/:org/:repo/actions/variables', {
17+
return this.github.request('GET /repos/:org/:repo/actions/variables', {
2518
org: this.repo.owner,
2619
repo: this.repo.repo
27-
})
28-
return variables
29-
}
30-
31-
/**
32-
* Compare the existing variables with what we've defined as code
33-
*
34-
* @param {Array.<object>} existing Existing variables defined in the repository
35-
* @param {Array.<object>} variables Variables that we have defined as code
36-
*
37-
* @returns {object} The results of a list comparison
38-
*/
39-
getChanged (existing, variables = []) {
40-
const result =
41-
JSON.stringify(
42-
existing.sort((x1, x2) => {
43-
return x1.name.toUpperCase().localeCompare(x2.name.toUpperCase())
44-
})
45-
) !==
46-
JSON.stringify(
47-
variables.sort((x1, x2) => {
48-
return x1.name.toUpperCase().localeCompare(x2.name.toUpperCase())
49-
})
50-
)
51-
return result
20+
}).then(({ data: { variables } }) => variables.map(({ name, value }) => ({ name, value })))
5221
}
5322

54-
/**
55-
* Compare existing variables with what's defined
56-
*
57-
* @param {Object} existing The existing entries in GitHub
58-
* @param {Object} attrs The entries defined as code
59-
*
60-
* @returns
61-
*/
6223
comparator (existing, attrs) {
6324
return existing.name === attrs.name
6425
}
6526

66-
/**
67-
* Return a list of changed entries
68-
*
69-
* @param {Object} existing The existing entries in GitHub
70-
* @param {Object} attrs The entries defined as code
71-
*
72-
* @returns
73-
*/
7427
changed (existing, attrs) {
75-
return this.getChanged(_.castArray(existing), _.castArray(attrs))
28+
return existing.value !== attrs.value
7629
}
7730

78-
/**
79-
* Update an existing variable if the value has changed
80-
*
81-
* @param {Array.<object>} existing Existing variables defined in the repository
82-
* @param {Array.<object>} variables Variables that we have defined as code
83-
*
84-
* @see {@link https://docs.github.com/en/rest/actions/variables?apiVersion=2026-03-10#update-a-repository-variable} update a repository variable
85-
* @returns
86-
*/
87-
async update (existing, variables = []) {
88-
this.log.debug(`Updating a repo var existing params ${JSON.stringify(existing)} and new ${JSON.stringify(variables)}`)
89-
existing = _.castArray(existing)
90-
variables = _.castArray(variables)
91-
const changed = this.getChanged(existing, variables)
92-
93-
if (changed) {
94-
let existingVariables = [...existing]
95-
for (const variable of variables) {
96-
const existingVariable = existingVariables.find((_var) => _var.name === variable.name)
97-
if (existingVariable) {
98-
existingVariables = existingVariables.filter((_var) => _var.name !== variable.name)
99-
if (existingVariable.value !== variable.value) {
100-
await this.github
101-
.request('PATCH /repos/:org/:repo/actions/variables/:variable_name', {
102-
org: this.repo.owner,
103-
repo: this.repo.repo,
104-
variable_name: variable.name.toUpperCase(),
105-
value: variable.value.toString()
106-
})
107-
.then((res) => {
108-
return res
109-
})
110-
.catch((e) => {
111-
this.logError(e)
112-
})
113-
}
114-
} else {
115-
await this.github
116-
.request('POST /repos/:org/:repo/actions/variables', {
117-
org: this.repo.owner,
118-
repo: this.repo.repo,
119-
name: variable.name.toUpperCase(),
120-
value: variable.value.toString()
121-
})
122-
.then((res) => {
123-
return res
124-
})
125-
.catch((e) => {
126-
this.logError(e)
127-
})
128-
}
129-
}
130-
131-
for (const variable of existingVariables) {
132-
await this.github
133-
.request('DELETE /repos/:org/:repo/actions/variables/:variable_name', {
134-
org: this.repo.owner,
135-
repo: this.repo.repo,
136-
variable_name: variable.name.toUpperCase()
137-
})
138-
.then((res) => {
139-
return res
140-
})
141-
.catch((e) => {
142-
this.logError(e)
143-
})
144-
}
31+
update (existing, attrs) {
32+
if (this.nop) {
33+
return Promise.resolve([
34+
new NopCommand(this.constructor.name, this.repo, null, `Update variable ${attrs.name}`)
35+
])
14536
}
37+
return this.github.request('PATCH /repos/:org/:repo/actions/variables/:variable_name', {
38+
org: this.repo.owner,
39+
repo: this.repo.repo,
40+
variable_name: attrs.name.toUpperCase(),
41+
value: attrs.value.toString()
42+
})
14643
}
14744

148-
/**
149-
* Add a new variable to a given repository
150-
*
151-
* @param {object} variable The variable to add, with name and value
152-
*
153-
* @see {@link https://docs.github.com/en/rest/actions/variables?apiVersion=2026-03-10#create-a-repository-variable} create a repository variable
154-
* @returns
155-
*/
156-
async add (variable) {
157-
this.log.debug(`Adding a repo var with the params ${JSON.stringify(variable)}`)
158-
await this.github
159-
.request('POST /repos/:org/:repo/actions/variables', {
160-
org: this.repo.owner,
161-
repo: this.repo.repo,
162-
name: variable.name,
163-
value: variable.value.toString()
164-
})
165-
.then((res) => {
166-
return res
167-
})
168-
.catch((e) => {
169-
this.logError(e)
170-
})
45+
add (attrs) {
46+
if (this.nop) {
47+
return Promise.resolve([
48+
new NopCommand(this.constructor.name, this.repo, null, `Add variable ${attrs.name}`)
49+
])
50+
}
51+
return this.github.request('POST /repos/:org/:repo/actions/variables', {
52+
org: this.repo.owner,
53+
repo: this.repo.repo,
54+
name: attrs.name.toUpperCase(),
55+
value: attrs.value.toString()
56+
})
17157
}
17258

173-
/**
174-
* Remove variables that aren't defined as code
175-
*
176-
* @param {String} existing Name of the existing variable to remove
177-
*
178-
* @see {@link https://docs.github.com/en/rest/actions/variables?apiVersion=2026-03-10#delete-a-repository-variable} delete a repository variable
179-
* @returns
180-
*/
181-
async remove (existing) {
182-
this.log.debug(`Removing a repo var with the params ${JSON.stringify(existing)}`)
183-
await this.github
184-
.request('DELETE /repos/:org/:repo/actions/variables/:variable_name', {
185-
org: this.repo.owner,
186-
repo: this.repo.repo,
187-
variable_name: existing.name
188-
})
189-
.then((res) => {
190-
return res
191-
})
192-
.catch((e) => {
193-
this.logError(e)
194-
})
59+
remove (existing) {
60+
if (this.nop) {
61+
return Promise.resolve([
62+
new NopCommand(this.constructor.name, this.repo, null, `Remove variable ${existing.name}`)
63+
])
64+
}
65+
return this.github.request('DELETE /repos/:org/:repo/actions/variables/:variable_name', {
66+
org: this.repo.owner,
67+
repo: this.repo.repo,
68+
variable_name: existing.name.toUpperCase()
69+
})
19570
}
19671
}

package-lock.json

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"deepmerge": "^4.3.1",
3131
"eta": "^3.5.0",
3232
"js-yaml": "^4.1.0",
33-
"lodash": "^4.17.21",
33+
"lodash": "^4.18.1",
3434
"minimatch": "^10.2.1",
3535
"node-cron": "^4.2.1",
3636
"octokit": "^5.0.2",

0 commit comments

Comments
 (0)