Skip to content
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This is a basic action that will label issues and pull request with a given labe
| label_color | false | The color of the label. If the label already exists in the repository, this setting will have no effect. | 5319E7 |
| org_membership | false | Contributions from users that are not members of the specified organisations will be labeled with the configured label. The value can be a single organisation or a comma-separated list of organisations. | puppetlabs |
| logins_to_ignore | false | Contributions from the specified users will not be labeled by this action. The value can be a single login or a comma-separated list of logins. | `N/A` |
| fail_if_member | false | Pipeline will fail, if the user is member of specified organisations and no label has been added manually. | false |
| token | true | A token with enough privilege to view org memberships and repo content. | `N/A` |

## Security
Expand Down
45 changes: 40 additions & 5 deletions __tests__/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,48 @@ describe('With a new github client', () => {
)
})

test('hasLabel returns true if the label already exists on the issue or pr', () => {
const result = client.hasLabel('community')
expect(result).toBe(true)
test('getLabels returns true if the label already exists on the issue or pr', () => {
const result = client.getLabels()
expect(result).toEqual(['community'])
})
})

describe('With a new github client without label', () => {
let client: GitHubClient

beforeAll(() => {
process.env['GITHUB_PATH'] = ''

Object.defineProperty(github, 'context', {
value: {
eventName: 'issues',
repo: {
owner: 'puppetlabs',
repo: 'iac'
},
issue: {
number: 331
},
payload: {
action: 'opened',
sender: {
login: 'dependabot[bot]'
},
issue: {
labels: [
]
}
}
}
})

client = new GitHubClient('1234')
console.log('::stop-commands::stoptoken')
process.stdout.write = jest.fn()
})

test('hasLabel returns false if the label does not exist on the issue or pr', () => {
const result = client.hasLabel('test')
test('getLabels returns false if the label does not exist on the issue or pr', () => {
const result = client.getLabels()
expect(result).toBe(false)
})
})
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ inputs:
logins_to_ignore:
description: 'Contributions from the specified users will not be labeled by this action. The value can be a single login or a comma-separated list of logins.'
required: false
fail_if_member:
description: 'Pipeline will fail, if the user is member of specified organisations and no label has been added manually.'
default: 'false'
required: false
token:
description: 'The GitHub token to use for authentication.'
required: true
Expand Down
8 changes: 6 additions & 2 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class GitHubClient {
}

// Checks if the issue already has the label
hasLabel(name: string): boolean {
getLabels(): string[] | false {
let labels!: Label[]

if (github.context.eventName === 'issues') {
Expand All @@ -103,7 +103,11 @@ export class GitHubClient {
return false
}

return labels.some((l) => l.name === name)
if (labels.length > 0) {
return labels.map((label) => label.name)
}

return false
}

/* Adds the label to the issue/pull request. It will attempt to create the label
Expand Down
20 changes: 13 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,27 @@ export async function run(): Promise<void> {
const labelName = core.getInput('label_name', {required: true})
const labelColor = core.getInput('label_color', {required: true})
const loginsToIgnore = core.getInput('logins_to_ignore', {required: false})
const failIfMember = core.getInput('fail_if_member', {required: false})
const orgs = getOrgMembershipList()
const token = core.getInput('token', {required: true})

const client = new GitHubClient(token)

if (client.hasLabel(labelName)) {
core.info(`The '${labelName}' label is already present on this issue! 🙌`)
return
}
const labels = client.getLabels()
const missingCommunityLabel = labels && !labels.includes(labelName)

if (
(await client.checkOrgMembership(orgs)) ||
client.isExcludedLogin(loginsToIgnore)
) {
core.info("Looks like this issue doesn't need labeling! 👍")
if (failIfMember === 'true' && !labels) {
core.setFailed('The PR is missing a label!')
} else {
core.info("Looks like this issue doesn't need labeling! 👍")
}
return
}

if (missingCommunityLabel) {
core.setFailed('Community PRs must be labelled')
return
}

Expand Down