-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CI bot #1402
Merged
Merged
Add CI bot #1402
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# bot | ||
|
||
This GitHub Action parses commands from pull request comments and executes them. | ||
|
||
Only authorized users (members and owners of this repository) are able to execute commands. | ||
|
||
Commands look like: | ||
``` | ||
/echo hello world | ||
``` | ||
|
||
Multiple commands can be included in a comment, one per line; but each command must be unique. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
name: "Bot" | ||
description: "🤖 beep boop" | ||
runs: | ||
using: "composite" | ||
steps: | ||
- uses: "actions/checkout@v3" | ||
- uses: "actions/github-script@v6" | ||
with: | ||
script: | | ||
const crypto = require('crypto'); | ||
const uuid = crypto.randomUUID(); | ||
const bot = require('./.github/actions/bot/index.js'); | ||
await bot(core, github, context, uuid); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// this script cannot require/import, because it's called by actions/github-script. | ||
// any dependencies must be passed in the inline script in action.yaml | ||
|
||
async function bot(core, github, context, uuid) { | ||
const payload = context.payload; | ||
|
||
if (!payload.comment) { | ||
console.log("No comment found in payload"); | ||
return; | ||
} | ||
console.log("Comment found in payload"); | ||
|
||
const author = payload.comment.user.login; | ||
const authorized = ["OWNER", "MEMBER"].includes(payload.comment.author_association); | ||
if (!authorized) { | ||
console.log(`Comment author is not authorized: ${author}`); | ||
return; | ||
} | ||
console.log(`Comment author is authorized: ${author}`); | ||
|
||
const commands = parseCommands(uuid, payload, payload.comment.body); | ||
if (commands.length === 0) { | ||
console.log("No commands found in comment body"); | ||
return; | ||
} | ||
const uniqueCommands = [...new Set(commands.map(command => typeof command))]; | ||
if (uniqueCommands.length != commands.length) { | ||
console.log("Duplicate commands found in comment body"); | ||
return; | ||
} | ||
console.log(commands.length + " command(s) found in comment body"); | ||
|
||
for (const command of commands) { | ||
const reply = await command.run(author, github); | ||
if (typeof reply === 'string') { | ||
github.rest.issues.createComment({ | ||
owner: payload.repository.owner.login, | ||
repo: payload.repository.name, | ||
issue_number: payload.issue.number, | ||
body: reply | ||
}); | ||
} else if (reply) { | ||
console.log(`Command returned: ${reply}`); | ||
} else { | ||
console.log("Command did not return a reply"); | ||
} | ||
} | ||
} | ||
|
||
// parseCommands splits the comment body into lines and parses each line as a command. | ||
function parseCommands(uuid, payload, commentBody) { | ||
const commands = []; | ||
if (!commentBody) { | ||
return commands; | ||
} | ||
const lines = commentBody.split(/\r?\n/); | ||
for (const line of lines) { | ||
const command = parseCommand(uuid, payload, line); | ||
if (command) { | ||
commands.push(command); | ||
} | ||
} | ||
return commands | ||
} | ||
|
||
// parseCommand parses a line as a command. | ||
// The format of a command is `/NAME ARGS...`. | ||
// Leading and trailing spaces are ignored. | ||
function parseCommand(uuid, payload, line) { | ||
const command = line.trim().match(/^\/([a-z\-]+)(?:\s+(.+))?$/); | ||
if (command) { | ||
return buildCommand(uuid, payload, command[1], command[2]); | ||
} | ||
return null; | ||
} | ||
|
||
// buildCommand builds a command from a name and arguments. | ||
function buildCommand(uuid, payload, name, args) { | ||
switch (name) { | ||
case "echo": | ||
return new EchoCommand(uuid, payload, args); | ||
case "ci": | ||
return new CICommand(uuid, payload, args); | ||
default: | ||
console.log(`Unknown command: ${name}`); | ||
return null; | ||
} | ||
} | ||
|
||
class EchoCommand { | ||
constructor(uuid, payload, args) { | ||
this.phrase = args ? args : "echo"; | ||
} | ||
|
||
run(author) { | ||
return `@${author} *${this.phrase}*`; | ||
} | ||
} | ||
|
||
class CICommand { | ||
constructor(uuid, payload, args) { | ||
this.repository_owner = payload.repository.owner.login; | ||
this.repository_name = payload.repository.name; | ||
this.pr_number = payload.issue.number; | ||
this.comment_url = payload.comment.html_url; | ||
this.uuid = uuid; | ||
this.goal = "test"; | ||
// "test" goal, which executes all CI stages, is the default when no goal is specified | ||
if (args != null && args != "") { | ||
this.goal = args; | ||
} | ||
} | ||
|
||
async run(author, github) { | ||
const pr = await github.rest.pulls.get({ | ||
owner: this.repository_owner, | ||
repo: this.repository_name, | ||
pull_number: this.pr_number | ||
}); | ||
const mergeable = pr.data.mergeable; | ||
switch (mergeable) { | ||
case true: | ||
break; | ||
case false: | ||
case null: | ||
return `@${author} this PR is not currently mergeable, you'll need to rebase it first.`; | ||
default: | ||
throw new Error(`Unknown mergeable value: ${mergeable}`); | ||
} | ||
const inputs = { | ||
uuid: this.uuid, | ||
pr_number: this.pr_number.toString(), | ||
git_sha: pr.data.merge_commit_sha, | ||
goal: this.goal, | ||
requester: author, | ||
comment_url: this.comment_url | ||
}; | ||
console.log(`Dispatching workflow with inputs: ${JSON.stringify(inputs)}`); | ||
await github.rest.actions.createWorkflowDispatch({ | ||
owner: this.repository_owner, | ||
repo: this.repository_name, | ||
workflow_id: 'ci-manual.yaml', | ||
ref: 'master', | ||
inputs: inputs | ||
}); | ||
return null; | ||
} | ||
} | ||
|
||
|
||
module.exports = async (core, github, context, uuid) => { | ||
bot(core, github, context, uuid).catch((error) => { | ||
core.setFailed(error); | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: maybe a short line to document the behavior
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍 I intend to add a
/help
command in the short term as well