|
1 | | -const _ = require('lodash') |
2 | 1 | const Diffable = require('./diffable') |
| 2 | +const NopCommand = require('../nopcommand') |
3 | 3 |
|
4 | 4 | module.exports = class Variables extends Diffable { |
5 | 5 | constructor (...args) { |
6 | 6 | super(...args) |
7 | 7 |
|
8 | 8 | if (this.entries) { |
9 | | - // Force all names to uppercase to avoid comparison issues. |
10 | 9 | this.entries.forEach((variable) => { |
11 | 10 | variable.name = variable.name.toUpperCase() |
12 | 11 | }) |
13 | 12 | } |
14 | 13 | } |
15 | 14 |
|
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 () { |
23 | 16 | 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', { |
25 | 18 | org: this.repo.owner, |
26 | 19 | 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 }))) |
52 | 21 | } |
53 | 22 |
|
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 | | - */ |
62 | 23 | comparator (existing, attrs) { |
63 | 24 | return existing.name === attrs.name |
64 | 25 | } |
65 | 26 |
|
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 | | - */ |
74 | 27 | changed (existing, attrs) { |
75 | | - return this.getChanged(_.castArray(existing), _.castArray(attrs)) |
| 28 | + return existing.value !== attrs.value |
76 | 29 | } |
77 | 30 |
|
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 | + ]) |
145 | 36 | } |
| 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 | + }) |
146 | 43 | } |
147 | 44 |
|
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 | + }) |
171 | 57 | } |
172 | 58 |
|
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 | + }) |
195 | 70 | } |
196 | 71 | } |
0 commit comments