Skip to content

Commit

Permalink
chore: omit unused circle variables that cause contributor PR issues
Browse files Browse the repository at this point in the history
  • Loading branch information
AtofStryker committed Jun 2, 2023
1 parent 60df86d commit 083d821
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
14 changes: 12 additions & 2 deletions scripts/circle-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,18 @@ async function checkCanaries () {

const circleEnv = await readCircleEnv()

if (Object.keys(circleEnv).length === 0) {
return console.warn('CircleCI env empty, assuming this is a contributor PR. Not checking for canary variables.')
// if the config contains only CIRCLE_OIDC_TOKEN, CIRCLE_OIDC_TOKEN_V2, CIRCLE_PLUGIN_TEST, treat the config as if it were empty
const containsOnlyAllowedEnvs = () => {
const circleEnvKeys = Object.keys(circleEnv)

return circleEnvKeys.length === 0 || (circleEnvKeys.length === 3 &&
circleEnvKeys.includes('CIRCLE_OIDC_TOKEN') &&
circleEnvKeys.includes('CIRCLE_OIDC_TOKEN_V2') &&
circleEnvKeys.includes('CIRCLE_PLUGIN_TEST'))
}

if (containsOnlyAllowedEnvs()) {
return console.warn('CircleCI env empty or contains only allowed envs, assuming this is a contributor PR. Not checking for canary variables.')
}

if (!circleEnv.MAIN_CANARY) throw new Error('Missing MAIN_CANARY.')
Expand Down
50 changes: 50 additions & 0 deletions scripts/unit/circle-env-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,56 @@ describe('circle-env', () => {
}
})
})

context('with circleEnv plus only omitted keys', () => {
it('passes', async () => {
sinon.stub(fs, 'readFile')
.withArgs('/foo.json').resolves(JSON.stringify({
Dispatched: { TaskInfo: { Environment: {
CIRCLE_OIDC_TOKEN: 'foo',
CIRCLE_OIDC_TOKEN_V2: 'bar',
CIRCLE_PLUGIN_TEST: 'baz',
} } },
}))

sinon.spy(console, 'warn')
await _checkCanaries()
expect(console.warn).to.be.calledWith('CircleCI env empty or contains only allowed envs, assuming this is a contributor PR. Not checking for canary variables.')
})

it('also passes', async () => {
sinon.stub(fs, 'readFile')
.withArgs('/foo.json').resolves(JSON.stringify({
Dispatched: { TaskInfo: { Environment: {
CIRCLE_OIDC_TOKEN: 'foo',
CIRCLE_OIDC_TOKEN_V2: 'bar',
CIRCLE_PLUGIN_TEST: 'baz',
MAIN_CANARY: 'qux',
CONTEXT_CANARY: 'quux',
} } },
}))

await _checkCanaries()
})

it('fails', async () => {
sinon.stub(fs, 'readFile')
.withArgs('/foo.json').resolves(JSON.stringify({
Dispatched: { TaskInfo: { Environment: {
CIRCLE_OIDC_TOKEN: 'foo',
CIRCLE_OIDC_TOKEN_V2: 'bar',
CIRCLE_PLUGIN_TEST: 'baz',
SOME_OTHER_VAR: 'quux',
} } },
}))

try {
await _checkCanaries()
} catch (e) {
expect(e.message).to.equal('Missing MAIN_CANARY.')
}
})
})
})

it('passes with canaries', async () => {
Expand Down

0 comments on commit 083d821

Please sign in to comment.