Skip to content

Commit

Permalink
feat: add username input key to remove dependency on GitHub /user API
Browse files Browse the repository at this point in the history
  • Loading branch information
prototypicalpro committed Aug 14, 2020
1 parent f377ec1 commit f284c99
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 34 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ Currently this action uses the [prototypicalpro/repolinter](https://github.com/p
# Default: ${{ github.token }}
token: ''

# The username associated with the PAT given by the`token` field. Repolinter-action
# uses this value to determine which issues have been created by itself.
#
# Defaults to the username associated with the `GITHUB_TOKEN` provided by Github
# Actions.
#
# Default: github-actions
username: ''

# The repository name and owner, formatted like so: `owner/repository`.
# This input determines which repository repolinter-action will create
# an issue on, if that functionality is enabled.
Expand Down
31 changes: 6 additions & 25 deletions __tests__/createOrUpdateIssue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,19 +295,17 @@ describe('createOrUpdateIssue', () => {
const config: toolkit.CreateOrUpdateIssueOpts = {
owner: 'a-repo-owner',
repo: 'a-repo',
username: 'my-user',
issueName: 'Repolinter Issue',
issueContent: 'Some markdown content here',
labelName: 'repolinter',
labelColor: 'ff66ff'
}

test('creates a new issue and a label under our username', async () => {
const userScope = nock('https://api.github.com')
.get('/user')
.reply(200, {login: 'myuser'})
const findIssueScope = nock('https://api.github.com')
.get(`/repos/${config.owner}/${config.repo}/issues`)
.query(atLeastObject({creator: 'myuser'}))
.query(atLeastObject({creator: 'my-user'}))
.reply(200, [])
const getLabelScope = nock('https://api.github.com')
.get(`/repos/${config.owner}/${config.repo}/labels/${config.labelName}`)
Expand Down Expand Up @@ -336,20 +334,16 @@ describe('createOrUpdateIssue', () => {

expect(res).toBe(8)

userScope.done()
findIssueScope.done()
createLabelScope.done()
getLabelScope.done()
createIssueScope.done()
})

test('updates an existing issue under our username', async () => {
const userScope = nock('https://api.github.com')
.get('/user')
.reply(200, {login: 'myuser'})
const findIssueScope = nock('https://api.github.com')
.get(`/repos/${config.owner}/${config.repo}/issues`)
.query(atLeastObject({creator: 'myuser'}))
.query(atLeastObject({creator: 'my-user'}))
.reply(200, [{number: 7}])
const updateIssueScope = nock('https://api.github.com')
.patch(`/repos/${config.owner}/${config.repo}/issues/7`, {
Expand All @@ -360,18 +354,14 @@ describe('createOrUpdateIssue', () => {

expect(res).toBe(7)

userScope.done()
findIssueScope.done()
updateIssueScope.done()
})

test('creates a new issue and a label under our username and close the existing one if options.forceCreateIssue is set', async () => {
const userScope = nock('https://api.github.com')
.get('/user')
.reply(200, {login: 'myuser'})
const findIssueScope = nock('https://api.github.com')
.get(`/repos/${config.owner}/${config.repo}/issues`)
.query(atLeastObject({creator: 'myuser'}))
.query(atLeastObject({creator: 'my-user'}))
.reply(200, [{number: 7}])
const closeIssueScope = nock('https://api.github.com')
.patch(
Expand Down Expand Up @@ -411,7 +401,6 @@ describe('createOrUpdateIssue', () => {

expect(res).toBe(8)

userScope.done()
findIssueScope.done()
closeIssueScope.done()
createLabelScope.done()
Expand All @@ -420,12 +409,9 @@ describe('createOrUpdateIssue', () => {
})

test("doesn't do anything if options.shouldClose is set and there is no issue", async () => {
const userScope = nock('https://api.github.com')
.get('/user')
.reply(200, {login: 'myuser'})
const findIssueScope = nock('https://api.github.com')
.get(`/repos/${config.owner}/${config.repo}/issues`)
.query(atLeastObject({creator: 'myuser'}))
.query(atLeastObject({creator: 'my-user'}))
.reply(200, [])

const res = await createOrUpdateIssue(
Expand All @@ -435,17 +421,13 @@ describe('createOrUpdateIssue', () => {

expect(res).toBe(null)

userScope.done()
findIssueScope.done()
})

test('closes the issue if config.shouldClose is set', async () => {
const userScope = nock('https://api.github.com')
.get('/user')
.reply(200, {login: 'myuser'})
const findIssueScope = nock('https://api.github.com')
.get(`/repos/${config.owner}/${config.repo}/issues`)
.query(atLeastObject({creator: 'myuser'}))
.query(atLeastObject({creator: 'my-user'}))
.reply(200, [{number: 7}])
const closeIssueScope = nock('https://api.github.com')
.patch(
Expand All @@ -463,7 +445,6 @@ describe('createOrUpdateIssue', () => {

expect(res).toBe(7)

userScope.done()
findIssueScope.done()
closeIssueScope.done()
})
Expand Down
1 change: 1 addition & 0 deletions __tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function getBaseEnv(): NodeJS.ProcessEnv {
ret[getInputName(Inputs.OUTPUT_NAME)] = 'Open Source Policy Issues'
ret[getInputName(Inputs.LABEL_NAME)] = 'repolinter'
ret[getInputName(Inputs.LABEL_COLOR)] = 'fbca04'
ret[getInputName(Inputs.USERNAME)] = 'github-actions'
ret['GITHUB_ACTION'] = 'true'
return ret
}
Expand Down
19 changes: 15 additions & 4 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('main', () => {
process.env[getInputName(Inputs.OUTPUT_NAME)] = 'Open Source Policy Issues'
process.env[getInputName(Inputs.LABEL_NAME)] = 'repolinter'
process.env[getInputName(Inputs.LABEL_COLOR)] = 'fbca04'
process.env[getInputName(Inputs.USERNAME)] = 'my-user'
process.env['GITHUB_ACTION'] = 'true'
// disable STDOUT printing for now
spooledStdout = []
Expand Down Expand Up @@ -84,6 +85,20 @@ describe('main', () => {
expect(process.exitCode).not.toEqual(0)
})

test('throws when no username is supplied', async () => {
process.env[getInputName(Inputs.TOKEN)] = '2'
process.env[getInputName(Inputs.OUTPUT_TYPE)] = 'issue'
delete process.env[getInputName(Inputs.USERNAME)]

await run()
const outputs = getOutputs(spooledStdout)

// console.debug(out)
expect(outputs[Outputs.ERRORED]).toEqual('true')
expect(outputs[Outputs.PASSED]).toEqual('false')
expect(process.exitCode).not.toEqual(0)
})

test('throws when no output-type is supplied', async () => {
delete process.env[getInputName(Inputs.OUTPUT_TYPE)]

Expand Down Expand Up @@ -315,9 +330,6 @@ describe('main', () => {
repo: 'repolinter-action'
}
// mock stolen from createOrUpdateIssue.ts
const userScope = nock('https://api.github.com')
.get('/user')
.reply(200, {login: 'myuser'})
const findIssueScope = nock('https://api.github.com')
.get(`/repos/${config.owner}/${config.repo}/issues`)
.query(true)
Expand Down Expand Up @@ -352,7 +364,6 @@ describe('main', () => {
)
expect(process.exitCode).toEqual(0)

userScope.done()
findIssueScope.done()
updateIssueScope.done()
})
Expand Down
10 changes: 10 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@ inputs:
This token is optional and only required if this actions is configured to
output an issue (see output_type). This token must have the `public_repo`
scope for the current repository in order to work properly.
If you set this field to a custom token, you will also need to change
`username` to the username associated with the token provided.
[Learn more about creating and using encrypted secrets](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets)
default: ${{ github.token }}
username:
required: false
description: >
The username associated with the `token` field. Repolinter-action uses this value
to determine which issues have been created by itself. Defaults to the username
associated with the `GITHUB_TOKEN` provided by Github Actions.
default: github-actions
config_file:
required: false
description: >
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/createorUpdateIssue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Octokit from './getOctokit'
export interface CreateOrUpdateIssueOpts {
owner: string
repo: string
username: string
issueName: string
issueContent?: string
issueAssignee?: string
Expand Down Expand Up @@ -37,6 +38,7 @@ type Octo = InstanceType<typeof Octokit>
*
* @param options.owner The owner of the repository to create an issue on
* @param options.repo The repository to create the issue on
* @param options.username The username associated with the octokit instance
* @param options.issueContent The text content to use for the issue body (ex. the markdown output of repolinter).
* @param options.issueName The name to use for this issue
* @param options.issueAssignee The username to assign this issue to, falsey for no one.
Expand All @@ -56,12 +58,10 @@ export default async function createOrUpdateIssue(
// error check
if (options.forceCreateIssue && options.shouldClose)
throw new Error(`Both forceCreateIssue and shouldClose cannot be set!`)
// get the current username
const context = await client.users.getAuthenticated()
// attempt to find an issue created by Repolinter
const issue = await findRepolinterIssue(
client,
Object.assign({}, options, {selfUsername: context.data.login})
Object.assign({}, options, {selfUsername: options.username})
)
// if no issue exists and we should close the issue, exit and do nothing
if (!issue && options.shouldClose) {
Expand Down
1 change: 1 addition & 0 deletions src/inputs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const enum Inputs {
TOKEN = 'token',
USERNAME = 'username',
CONFIG_URL = 'config_url',
CONFIG_FILE = 'config_file',
REPO = 'repository',
Expand Down
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import createOrUpdateIssue from './createorUpdateIssue'
function getInputs(): {[key: string]: string} {
return {
TOKEN: core.getInput(Inputs.TOKEN),
USERNAME: core.getInput(Inputs.USERNAME, {required: true}),
CONFIG_URL: core.getInput(Inputs.CONFIG_URL),
CONFIG_FILE: core.getInput(Inputs.CONFIG_FILE),
REPO: core.getInput(Inputs.REPO, {required: true}),
Expand All @@ -29,6 +30,7 @@ export default async function run(disableRetry?: boolean): Promise<void> {
// get all inputs
const {
TOKEN,
USERNAME,
CONFIG_FILE,
CONFIG_URL,
REPO,
Expand Down Expand Up @@ -91,6 +93,7 @@ export default async function run(disableRetry?: boolean): Promise<void> {
await createOrUpdateIssue(octokit, {
owner,
repo,
username: USERNAME,
issueName: OUTPUT_NAME,
issueContent,
labelName: LABEL_NAME,
Expand Down

0 comments on commit f284c99

Please sign in to comment.