Skip to content
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

Adding environment variables support #860

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/plugins/environments.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ module.exports = class Environments extends Diffable {
)
)
}
if (attrs.variables) {
// perhaps this should be retrieved on init so that repo context is more broadly hydrated and not just for this one call
const repository = await this.getRepository(this.repo.owner, this.repo.repo)
if (repository && repository.id) {
await Promise.all(
Object.entries(attrs.variables).map(([name, value]) =>
this.github.request('POST /repositories/:repository_id/environments/:environment_name/variables', {
repository_id: repository.id,
environment_name: attrs.name,
name,
value
})
))
}
}
}

remove (existing) {
Expand Down Expand Up @@ -151,6 +166,16 @@ module.exports = class Environments extends Diffable {
return branchPolicies
}

async getRepository (org, repo) {
const {
data
} = await this.github.request('GET /repos/:org/:repo', {
org,
repo
})
return data
}

toParams (existing, attrs) {
const deploymentBranchPolicy = attrs.deployment_branch_policy
? this.shouldUseProtectedBranches(
Expand Down
49 changes: 12 additions & 37 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions test/unit/lib/plugins/environments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ describe('Environments', () => {
let github
const org = 'bkeepers'
const repo = 'test'
const repoId = 12345

function configure (config) {
return new Environments(github, { owner: org, repo }, config)
Expand Down Expand Up @@ -42,6 +43,10 @@ describe('Environments', () => {
],
deployment_branch_policy: {
custom_branches: ['dev/*', 'dev-*']
},
variables: {
FOO: 'bar',
FIZZ: 'buzz'
}
},
{
Expand All @@ -66,6 +71,23 @@ describe('Environments', () => {
{ name: 'Different-case', wait_timer: 0 }
])

when(github.request)
.calledWith('GET /repos/:org/:repo', {
org,
repo
}).mockResolvedValue({
data: {
id: repoId,
node_id: 'A_bcdefghijk',
name: repo,
full_name: `${org}/${repo}`,
private: true,
owner: {
login: org
}
}
})

when(github.request)
.calledWith('GET /repos/:org/:repo/environments', { org, repo })
.mockResolvedValue({
Expand Down Expand Up @@ -215,6 +237,26 @@ describe('Environments', () => {
}
})

expect(github.request).toHaveBeenCalledWith(
'POST /repositories/:repository_id/environments/:environment_name/variables',
{
repoId,
environment_name: 'new-environment',
name: 'FOO',
value: 'bar'
}
)

expect(github.request).toHaveBeenCalledWith(
'POST /repositories/:repository_id/environments/:environment_name/variables',
{
repoId,
environment_name: 'new-environment',
name: 'FIZZ',
value: 'buzz'
}
)

expect(github.request).toHaveBeenCalledWith('DELETE /repos/:org/:repo/environments/:environment_name', {
org,
repo,
Expand Down