Skip to content

Commit 1d695ca

Browse files
committed
CONT-1219 : fail ci for puppetlabs members if no label
1 parent e2cbb8b commit 1d695ca

File tree

5 files changed

+64
-14
lines changed

5 files changed

+64
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This is a basic action that will label issues and pull request with a given labe
1010
| label_color | false | The color of the label. If the label already exists in the repository, this setting will have no effect. | 5319E7 |
1111
| 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 |
1212
| 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` |
13+
| fail_if_member | false | Pipeline will fail, if the user is member of specified organisations and no label has been added manually. | false |
1314
| token | true | A token with enough privilege to view org memberships and repo content. | `N/A` |
1415

1516
## Security

__tests__/github.test.ts

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,48 @@ describe('With a new github client', () => {
160160
)
161161
})
162162

163-
test('hasLabel returns true if the label already exists on the issue or pr', () => {
164-
const result = client.hasLabel('community')
165-
expect(result).toBe(true)
163+
test('getLabels returns true if the label already exists on the issue or pr', () => {
164+
const result = client.getLabels()
165+
expect(result).toEqual(['community'])
166+
})
167+
})
168+
169+
describe('With a new github client without label', () => {
170+
let client: GitHubClient
171+
172+
beforeAll(() => {
173+
process.env['GITHUB_PATH'] = ''
174+
175+
Object.defineProperty(github, 'context', {
176+
value: {
177+
eventName: 'issues',
178+
repo: {
179+
owner: 'puppetlabs',
180+
repo: 'iac'
181+
},
182+
issue: {
183+
number: 331
184+
},
185+
payload: {
186+
action: 'opened',
187+
sender: {
188+
login: 'dependabot[bot]'
189+
},
190+
issue: {
191+
labels: [
192+
]
193+
}
194+
}
195+
}
196+
})
197+
198+
client = new GitHubClient('1234')
199+
console.log('::stop-commands::stoptoken')
200+
process.stdout.write = jest.fn()
166201
})
167202

168-
test('hasLabel returns false if the label does not exist on the issue or pr', () => {
169-
const result = client.hasLabel('test')
203+
test('getLabels returns false if the label does not exist on the issue or pr', () => {
204+
const result = client.getLabels()
170205
expect(result).toBe(false)
171206
})
172207
})

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ inputs:
1717
logins_to_ignore:
1818
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.'
1919
required: false
20+
fail_if_member:
21+
description: 'Pipeline will fail, if the user is member of specified organisations and no label has been added manually.'
22+
default: 'false'
23+
required: false
2024
token:
2125
description: 'The GitHub token to use for authentication.'
2226
required: true

src/github.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class GitHubClient {
8888
}
8989

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

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

106-
return labels.some((l) => l.name === name)
106+
if (labels.length > 0) {
107+
return labels.map((label) => label.name)
108+
}
109+
110+
return false
107111
}
108112

109113
/* Adds the label to the issue/pull request. It will attempt to create the label

src/main.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,27 @@ export async function run(): Promise<void> {
1111
const labelName = core.getInput('label_name', {required: true})
1212
const labelColor = core.getInput('label_color', {required: true})
1313
const loginsToIgnore = core.getInput('logins_to_ignore', {required: false})
14+
const failIfMember = core.getInput('fail_if_member', {required: false})
1415
const orgs = getOrgMembershipList()
1516
const token = core.getInput('token', {required: true})
16-
1717
const client = new GitHubClient(token)
18-
19-
if (client.hasLabel(labelName)) {
20-
core.info(`The '${labelName}' label is already present on this issue! 🙌`)
21-
return
22-
}
18+
const labels = client.getLabels()
19+
const missingCommunityLabel = labels && !labels.includes(labelName)
2320

2421
if (
2522
(await client.checkOrgMembership(orgs)) ||
2623
client.isExcludedLogin(loginsToIgnore)
2724
) {
28-
core.info("Looks like this issue doesn't need labeling! 👍")
25+
if (failIfMember === 'true' && !labels) {
26+
core.setFailed('The PR is missing a label!')
27+
} else {
28+
core.info("Looks like this issue doesn't need labeling! 👍")
29+
}
30+
return
31+
}
32+
33+
if (missingCommunityLabel) {
34+
core.setFailed('Community PR must have a label name community')
2935
return
3036
}
3137

0 commit comments

Comments
 (0)