-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Add support for repository variables #635
Open
primetheus
wants to merge
1
commit into
github:main-enterprise
Choose a base branch
from
primetheus:primetheus/repo-variables
base: main-enterprise
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+282
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
/* eslint-disable no-unused-expressions */ | ||
/* eslint-disable quotes */ | ||
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 | ||
*/ | ||
find () { | ||
const result = async () => { | ||
return await this.github | ||
.request('GET /repos/:org/:repo/actions/variables', { | ||
org: this.repo.owner, | ||
repo: this.repo.repo | ||
}) | ||
.then((res) => { | ||
return res | ||
}) | ||
.catch((e) => { | ||
this.logError(e) | ||
}) | ||
} | ||
|
||
return result.data.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) => { | ||
x1.name.toUpperCase() - x2.name.toUpperCase() | ||
}) | ||
) !== | ||
JSON.stringify( | ||
variables.sort((x1, x2) => { | ||
x1.name.toUpperCase() - 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 = []) { | ||
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) { | ||
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) { | ||
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
variables: | ||
- name: MY_VAR_1 | ||
permission: batman | ||
- name: MY_VAR_2 | ||
permission: superman |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const { when } = require('jest-when'); | ||
const Variables = require('../../../../lib/plugins/variables'); | ||
|
||
describe('Variables', () => { | ||
let github; | ||
const org = 'bkeepers'; | ||
const repo = 'test'; | ||
|
||
function fillVariables(variables = []) { | ||
return variables; | ||
} | ||
|
||
beforeAll(() => { | ||
github = { | ||
request: jest.fn().mockReturnValue(Promise.resolve(true)), | ||
}; | ||
}); | ||
|
||
it('sync', () => { | ||
const plugin = new Variables(undefined, github, { owner: org, repo }, [{ name: 'test', value: 'test' }], { | ||
debug() {}, | ||
}); | ||
|
||
when(github.request) | ||
.calledWith('GET /repos/:org/:repo/actions/variables', { org, repo }) | ||
.mockResolvedValue({ | ||
data: { | ||
variables: [ | ||
fillVariables({ | ||
variables: [], | ||
}), | ||
], | ||
}, | ||
}); | ||
|
||
['variables'].forEach(() => { | ||
when(github.request) | ||
.calledWith('GET /repos/:org/:repo/actions/variables', { org, repo }) | ||
.mockResolvedValue({ | ||
data: { | ||
variables: [], | ||
}, | ||
}); | ||
}); | ||
|
||
when(github.request).calledWith('POST /repos/:org/:repo/actions/variables').mockResolvedValue({}); | ||
|
||
return plugin.sync().then(() => { | ||
expect(github.request).toHaveBeenCalledWith('GET /repos/:org/:repo/actions/variables', { org, repo }); | ||
|
||
['variables'].forEach(() => { | ||
expect(github.request).toHaveBeenCalledWith('GET /repos/:org/:repo/actions/variables', { org, repo }); | ||
}); | ||
|
||
expect(github.request).toHaveBeenCalledWith( | ||
'POST /repos/:org/:repo/actions/variables', | ||
expect.objectContaining({ | ||
org, | ||
repo, | ||
name: 'TEST', | ||
value: 'test', | ||
}) | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code was failing the tests, perhaps the find method could be rewritten as