Skip to content
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 command to automate issue assignment #1746

Merged
merged 1 commit into from
Sep 13, 2024
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
75 changes: 75 additions & 0 deletions .github/scripts/radius-bot.js
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],
});
}
24 changes: 24 additions & 0 deletions .github/workflows/radius-bot.yaml
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})