Skip to content

Commit 5ca9c17

Browse files
committed
CONT-1219 : fail ci for puppetlabs members if no labe
1 parent e2cbb8b commit 5ca9c17

File tree

5 files changed

+60
-11
lines changed

5 files changed

+60
-11
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,10 @@ 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') {
95-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
9695
labels = (this.payload as IssuesEvent).issue.labels!
9796
} else if (
9897
github.context.eventName === 'pull_request' ||
@@ -103,7 +102,11 @@ export class GitHubClient {
103102
return false
104103
}
105104

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

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

src/main.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ 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+
const labels = client.getLabels()
1819

19-
if (client.hasLabel(labelName)) {
20+
if (labels && labels.includes(labelName)) {
21+
// "community" label exists
2022
core.info(`The '${labelName}' label is already present on this issue! 🙌`)
2123
return
2224
}
@@ -25,7 +27,11 @@ export async function run(): Promise<void> {
2527
(await client.checkOrgMembership(orgs)) ||
2628
client.isExcludedLogin(loginsToIgnore)
2729
) {
28-
core.info("Looks like this issue doesn't need labeling! 👍")
30+
if (failIfMember === 'true' && !labels) {
31+
core.setFailed('The PR does not have a label, please add one!')
32+
} else {
33+
core.info("Looks like this issue doesn't need labeling! 👍")
34+
}
2935
return
3036
}
3137

0 commit comments

Comments
 (0)