Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Create labels when repo is added to installation #15

Merged
merged 8 commits into from
Dec 21, 2018
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
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const issueLabeled = require('./src/issue/labeled')
const pullLabeled = require('./src/pull/labeled')
const closed = require('./src/issue/closed')
const commentDeleted = require('./src/comment/deleted')
const installationAdded = require('./src/installation/added')

const { CLOSE, MERGE } = require('./src/constants')

Expand Down Expand Up @@ -72,7 +73,9 @@ module.exports = async (robot, queue = setup()) => {
// Kill job when issue/pull is closed
robot.on(['issues.closed', 'pull_request.closed'], closed(queue))

robot.on(['issue_comment.deleted'], commentDeleted(queue))
robot.on('issue_comment.deleted', commentDeleted(queue))

robot.on(['installation_repositories.added', 'installation.created'], installationAdded(robot))

// For more information on building apps:
// https://probot.github.io/docs/
Expand Down
12 changes: 12 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,15 @@ exports.getPullRequest = function getPullRequest(github, data) {
})
.then(_ => _.data)
}

exports.createLabel = function createLabel(github, data) {
const { owner, repo, name, color, description } = data

return github.issues.createLabel({
owner,
repo,
name,
color,
description
})
}
25 changes: 25 additions & 0 deletions src/installation/added.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { createLabel } = require('../api')

module.exports = robot => async context => {
try {
const github = await robot.auth(context.payload.installation.id)

const repos = context.payload.repositories_added || context.payload.repositories

const promises = repos.map(({ name: repo }) => {
const data = {
owner: context.payload.installation.account.login,
repo,
name: 'merge when passing',
color: 'FF851B',
description: 'Merge the PR once all status checks have passed'
}

return createLabel(github, data)
})

return Promise.all(promises)
} catch (e) {
robot.log.error(e)
}
}
19 changes: 19 additions & 0 deletions test/fixtures/added.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = ({
repositories_added = [
{
name: 'ranger'
}
]
}) => ({
name: 'installation_repositories',
payload: {
action: 'added',
installation: {
id: 533899,
account: {
login: 'ranger'
}
},
repositories_added
}
})
19 changes: 19 additions & 0 deletions test/fixtures/created.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = ({
repositories = [
{
name: 'ranger'
}
]
}) => ({
name: 'installation',
payload: {
action: 'created',
installation: {
id: 533899,
account: {
login: 'ranger'
}
},
repositories
}
})
50 changes: 46 additions & 4 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const app = require('..')

const payload = require('./fixtures/labeled')
const commentPayload = require('./fixtures/comment')
const addedPayload = require('./fixtures/added')
const createdPayload = require('./fixtures/created')

const wait = (delay = 0) => new Promise(resolve => setTimeout(resolve, delay))

class MockJob {
Expand Down Expand Up @@ -115,6 +118,7 @@ describe('Bot', () => {
github = {
issues: {
createComment: jest.fn(),
createLabel: jest.fn(),
update: jest.fn((_, data) => Promise.resolve({ data }))
},
pullRequests: {
Expand Down Expand Up @@ -195,8 +199,8 @@ describe('Bot', () => {
action: 'close'
}
expect(queue.createJob).toHaveBeenCalledWith(data)
expect(queue.jobs[Object.keys(queue.jobs).slice(-1)[0]].id).toBe('mfix22:test-issue-bot:7:close')
expect(queue.jobs[Object.keys(queue.jobs).slice(-1)[0]].data).toEqual(data)
expect(queue.jobs[Object.keys(queue.jobs)[0]].id).toBe('mfix22:test-issue-bot:7:close')
expect(queue.jobs[Object.keys(queue.jobs)[0]].data).toEqual(data)
})

test('Will remove the job if an issue is closed', async () => {
Expand Down Expand Up @@ -263,8 +267,8 @@ describe('Bot', () => {
}

expect(queue.createJob).toHaveBeenCalledWith(data)
expect(queue.jobs[Object.keys(queue.jobs).slice(-1)[0]].id).toBe('mfix22:test-issue-bot:7:merge')
expect(queue.jobs[Object.keys(queue.jobs).slice(-1)[0]].data).toEqual(data)
expect(queue.jobs[Object.keys(queue.jobs)[0]].id).toBe('mfix22:test-issue-bot:7:merge')
expect(queue.jobs[Object.keys(queue.jobs)[0]].data).toEqual(data)

await wait(2)

Expand Down Expand Up @@ -358,6 +362,44 @@ describe('Bot', () => {
})
})

describe('installation', () => {
test('Will take action when repos are added', async () => {
const repositories_added = [{ name: 'ranger-0' }, { name: 'ranger-1' }]

await robot.receive(addedPayload({ repositories_added }))

const data = repositories_added.map(({ name: repo }) => [
{
owner: 'ranger',
repo,
name: 'merge when passing',
color: 'FF851B',
description: 'Merge the PR once all status checks have passed'
}
])

expect(github.issues.createLabel.mock.calls).toEqual(data)
})

test('Will take action when an installation is created', async () => {
const repositories = [{ name: 'ranger-0' }, { name: 'ranger-1' }]

await robot.receive(createdPayload({ repositories }))

const data = repositories.map(({ name: repo }) => [
{
owner: 'ranger',
repo,
name: 'merge when passing',
color: 'FF851B',
description: 'Merge the PR once all status checks have passed'
}
])

expect(github.issues.createLabel.mock.calls).toEqual(data)
})
})

describe('billing', () => {
beforeEach(() => {
robot.log.error = jest.fn()
Expand Down