Skip to content

Commit

Permalink
Adding to code to support repository-settings#711
Browse files Browse the repository at this point in the history
  • Loading branch information
ctoestreich committed Feb 5, 2024
1 parent 9ec4069 commit 1d49f40
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 37 deletions.
25 changes: 25 additions & 0 deletions lib/plugins/environments.js
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
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

0 comments on commit 1d49f40

Please sign in to comment.