-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to automate issue assignment (#1746)
Signed-off-by: karishma-chawla <[email protected]> Co-authored-by: karishma-chawla <[email protected]>
- Loading branch information
Showing
2 changed files
with
99 additions
and
0 deletions.
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,75 @@ | ||
/* | ||
Copyright 2023 The Radius Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
module.exports = async ({ github, context }) => { | ||
if (context.eventName === 'issue_comment' && context.payload.action === 'created') { | ||
try { | ||
await handleIssueCommentCreate({ github, context }); | ||
} catch (error) { | ||
console.log(`[handleIssueCommentCreate] unexpected error: ${error}`); | ||
} | ||
} | ||
} | ||
|
||
// Handle issue comment create event. | ||
async function handleIssueCommentCreate({ github, context }) { | ||
const payload = context.payload; | ||
const issue = context.issue; | ||
const isFromPulls = !!payload.issue.pull_request; | ||
const commentBody = payload.comment.body; | ||
const username = context.actor; | ||
|
||
if (!commentBody) { | ||
console.log('[handleIssueCommentCreate] comment body not found, exiting.'); | ||
return; | ||
} | ||
|
||
const commandParts = commentBody.split(/\s+/); | ||
const command = commandParts.shift(); | ||
|
||
switch (command) { | ||
case '/assign': | ||
await cmdAssign(github, issue, isFromPulls, username); | ||
break; | ||
default: | ||
console.log(`[handleIssueCommentCreate] command ${command} not found, exiting.`); | ||
break; | ||
} | ||
} | ||
|
||
/** | ||
* Assign issue to the user who commented. | ||
* @param {*} github GitHub object reference | ||
* @param {*} issue GitHub issue object | ||
* @param {*} isFromPulls is the workflow triggered by a pull request? | ||
* @param {*} username is the user who trigger the command | ||
*/ | ||
async function cmdAssign(github, issue, isFromPulls, username) { | ||
if (isFromPulls) { | ||
console.log('[cmdAssign] pull requests not supported, skipping command execution.'); | ||
return; | ||
} else if (issue.assignees && issue.assignees.length !== 0) { | ||
console.log('[cmdAssign] issue already has assignees, skipping command execution.'); | ||
return; | ||
} | ||
|
||
await github.rest.issues.addAssignees({ | ||
owner: issue.owner, | ||
repo: issue.repo, | ||
issue_number: issue.number, | ||
assignees: [username], | ||
}); | ||
} |
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,24 @@ | ||
name: radius-bot | ||
|
||
on: | ||
issue_comment: | ||
types: [created] | ||
|
||
jobs: | ||
radius-bot: | ||
name: Run Radius Bot script | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout Repository | ||
uses: actions/checkout@v4 | ||
with: | ||
sparse-checkout: | | ||
.github/scripts/radius-bot.js | ||
sparse-checkout-cone-mode: false | ||
- name: Comment analyzer | ||
uses: actions/github-script@v7 | ||
with: | ||
github-token: ${{ secrets.GH_RAD_CI_BOT_PAT }} | ||
script: | | ||
const script = require('./.github/scripts/radius-bot.js') | ||
await script({github, context}) |