Skip to content

Commit

Permalink
refactor(pullRequest): use context to read PR labels
Browse files Browse the repository at this point in the history
  • Loading branch information
tagoro9 committed Feb 4, 2020
1 parent fd9f10d commit e603501
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 19 deletions.
14 changes: 3 additions & 11 deletions __tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('handlePullRequest', () => {
number: '1',
pull_request: {
number: 1,
labels: [],
title: 'test',
user: {
login: 'pr-creator',
Expand Down Expand Up @@ -831,11 +832,7 @@ describe('handlePullRequest', () => {
labels: ['test_label'],
} as any

client.issues = {
listLabelsOnIssue: jest
.fn()
.mockResolvedValue({ data: [{ name: 'some_label' }] }),
} as any
context.payload.pull_request.labels = [{ name: 'some_label' }]

await handler.handlePullRequest(client, context, config)

Expand All @@ -855,12 +852,7 @@ describe('handlePullRequest', () => {

const client = new github.GitHub('token')

client.issues = {
addAssignees: jest.fn().mockImplementation(async () => {}),
listLabelsOnIssue: jest
.fn()
.mockResolvedValue({ data: [{ name: 'some_label' }] }),
} as any
context.payload.pull_request.labels = [{ name: 'some_label' }]

client.pulls = {
createReviewRequest: jest.fn().mockImplementation(async () => {}),
Expand Down
2 changes: 1 addition & 1 deletion src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export async function handlePullRequest(
const pr = new PullRequest(client, context)

if (labels !== undefined && labels.length > 0) {
const hasLabels = await pr.hasAnyLabel(labels)
const hasLabels = pr.hasAnyLabel(labels)
if (!hasLabels) {
core.info(
'Skips the process to add reviewers/assignees since PR is not tagged with any of the labels'
Expand Down
12 changes: 5 additions & 7 deletions src/pull_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ export class PullRequest {
core.debug(JSON.stringify(result))
}

async hasAnyLabel(labels: string[]): Promise<boolean> {
const { owner, repo, number: pull_number } = this.context.issue
const pullRequestLabels = (await this.client.issues.listLabelsOnIssue({
issue_number: pull_number,
owner,
repo,
})).data
hasAnyLabel(labels: string[]): boolean {
if (!this.context.payload.pull_request) {
return false
}
const { labels: pullRequestLabels = [] } = this.context.payload.pull_request
return pullRequestLabels.some(label => labels.includes(label.name))
}
}

0 comments on commit e603501

Please sign in to comment.