Skip to content
This repository has been archived by the owner on Nov 27, 2020. It is now read-only.

WIP: New command syntax #6

Closed
wants to merge 2 commits into from
Closed
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
59 changes: 50 additions & 9 deletions __tests__/ensure.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,60 @@ beforeEach(() => {
robot.auth = () => Promise.resolve(github)
})

test('checking permission', async () => {
await robot.receive(events.pr_comment_created)
test('checking permission (old synatx)', async () => {
await robot.receive(events.pr_comment_created_old)
expect(github.repos.reviewUserPermissionLevel).toBeCalledWith(
expect.objectContaining({ username: 'user' })
)
})

test('processing plain issue comments', async () => {
await robot.receive(events.issue_comment_created)
test('checking permission (new syntax)', async () => {
await robot.receive(events.pr_comment_created_new)
expect(github.repos.reviewUserPermissionLevel).toBeCalledWith(
expect.objectContaining({ username: 'user' })
)
})

test('processing plain issue comments (old syntax)', async () => {
await robot.receive(events.issue_comment_created_old)
expect(github.issues.addLabels).not.toBeCalled()
})

test('processing plain issue comments (new syntax)', async () => {
await robot.receive(events.issue_comment_created_new)
expect(github.issues.addLabels).not.toBeCalled()
})

test('adding metadata', async () => {
await robot.receive(events.pr_comment_created)
test('adding metadata (old syntax)', async () => {
await robot.receive(events.pr_comment_created_old)
expect(metadata).toBeCalledWith(
expect.objectContaining({ payload: expect.any(Object) })
)
expect(metadata().set).toBeCalledWith('dependencies', expect.any(Array))
})

test('adding the marker', async () => {
await robot.receive(events.pr_comment_created)
test('adding metadata (new syntax)', async () => {
await robot.receive(events.pr_comment_created_new)
expect(metadata).toBeCalledWith(
expect.objectContaining({ payload: expect.any(Object) })
)
expect(metadata().set).toBeCalledWith('dependencies', expect.any(Array))
})

test('adding the marker (old syntax)', async () => {
await robot.receive(events.pr_comment_created_old)
expect(github.issues.addLabels).toBeCalledWith(
expect.objectContaining({
owner: 'user',
repo: 'test',
number: 1,
labels: expect.any(Array)
})
)
})

test('adding the marker (new syntax)', async () => {
await robot.receive(events.pr_comment_created_new)
expect(github.issues.addLabels).toBeCalledWith(
expect.objectContaining({
owner: 'user',
Expand All @@ -80,7 +112,7 @@ test('removing the marker', async () => {
})

test('running check after the update', async () => {
await robot.receive(events.pr_comment_created)
await robot.receive(events.pr_comment_created_old)
expect(check).toHaveBeenCalledWith(
expect.any(Object),
'user',
Expand All @@ -89,6 +121,15 @@ test('running check after the update', async () => {
[1, 2]
)

await robot.receive(events.pr_comment_created_new)
expect(check).toHaveBeenLastCalledWith(
expect.any(Object),
'user',
'test',
'123',
[1, 2]
)

await robot.receive(events.pr_comment_created_remove)
expect(check).toHaveBeenLastCalledWith(
expect.any(Object),
Expand Down
6 changes: 4 additions & 2 deletions __tests__/events/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Exports event JSONs as friendly names

module.exports = {
issue_comment_created: require('./issue_comment.created.json'),
issue_comment_created_old: require('./issue_comment.created-old.json'),
issue_comment_created_new: require('./issue_comment.created-new.json'),
issues_reopened: require('./issues.reopened.json'),
issues_closed: require('./issues.closed.json'),
pr_comment_created: require('./pr_comment.created.json'),
pr_comment_created_old: require('./pr_comment.created-old.json'),
pr_comment_created_new: require('./pr_comment.created-new.json'),
pr_comment_created_remove: require('./pr_comment.created-remove.json'),
pull_request_opened: require('./pull_request.opened.json'),
pull_request_reopened: require('./pull_request.reopened.json'),
Expand Down
25 changes: 25 additions & 0 deletions __tests__/events/issue_comment.created-new.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"event": "issue_comment",
"payload": {
"action": "created",
"issue": {
"number": 1,
"state": "open"
},
"comment": {
"user": {
"login": "user"
},
"body": "depends on #1 and #2"
},
"repository": {
"name": "test",
"owner": {
"login": "user"
}
},
"installation": {
"id": 1234
}
}
}
26 changes: 26 additions & 0 deletions __tests__/events/pr_comment.created-new.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"event": "issue_comment",
"payload": {
"action": "created",
"issue": {
"number": 1,
"state": "open",
"pull_request": {}
},
"comment": {
"user": {
"login": "user"
},
"body": "depending on #1 and #2 to be resolved first"
},
"repository": {
"name": "test",
"owner": {
"login": "user"
}
},
"installation": {
"id": 1234
}
}
}
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ const { join } = require('path')

// Packages
const command = require('probot-commands')
const { addCommand } = require('probot-issue-commands')

// Ours
const ensure = require('./lib/ensure')
const test = require('./lib/test')
const update = require('./lib/update')

module.exports = robot => {
// Ensures all dependencies are resolved before the PR can be merged
//
// Triggered when you write:
// /COMMAND arguments
// The new syntax (#3)
addCommand(robot, /depend(s|ed|ing)?( +on)?/, ensure)

// Depreated!
command(robot, 'depends', ensure)
command(robot, 'ensure', ensure)

Expand Down
10 changes: 8 additions & 2 deletions lib/ensure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const metadata = require('probot-metadata')
// Ours
const check = require('./check')

const ensure = async (context, command) => {
const ensure = async (context, issues) => {
const { github, payload } = context

// 1. Extract necessary info
Expand All @@ -27,7 +27,13 @@ const ensure = async (context, command) => {
if (!payload.issue.pull_request) return

// 4. Match issue numbers
const issues = (command.arguments.match(/#(\d+)(?=\s*)/g) || []).map(
// From slash command (Deprecated)
// TODO remove slash command support later
if (issues.arguments) {
issues = issues.arguments.match(/#(\d+)(?=\s*)/g) || []
}

issues = issues.map(
i => Number(i.slice(1)) // Removes '#' prefix
)

Expand Down
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"probot": "^3.0.0",
"probot-commands": "^1.0.0",
"probot-issue-commands": "^1.0.2",
"probot-metadata": "^1.0.0"
},
"devDependencies": {
Expand Down