Lab: Automatically Comment and Label Issues
Introduction
In this lab, you will create a workflow to automatically comment on new issues and add a label. This demonstrates how to use the github-script
action for interacting with GitHub’s REST API to enhance automation.
Estimated Duration: 20-30 minutes
Instructions
Step 1: Create the Workflow File
- Create a new workflow file in your repository under
.github/workflows/
namedauto-comment-on-issues.yml
. -
Copy the following content into the workflow file:
name: Misc - Auto Comment on New Issue on: issues: types: opened workflow_dispatch: jobs: thanks: runs-on: ubuntu-latest permissions: issues: write # Required to create comments steps: - uses: actions/github-script@v7 id: issue_script with: github-token: $ script: | const issue_number = context.issue.number; console.log(`issue_number: ${issue_number}`); const owner = context.repo.owner; const repo = context.repo.repo; // Lookup issue info const issue = await github.rest.issues.get({ repo, owner, issue_number }); console.log(`issue: ${issue}`); // Create comment thanking contributor const comment = await github.rest.issues.createComment({ repo, owner, issue_number, body: "Thanks for your contribution!" }); console.log(`comment id: ${comment.data.id}`); console.log(comment.data); // Auto-label the issue github.rest.issues.addLabels({ repo, owner, issue_number, labels: ['todo-review'] }); // Make comment id available to subsequent steps return comment.data.id; - run: echo 'comment id=$'
- Commit the file to the
main
branch.
Step 2: Trigger the Workflow
- Open the Issues tab in your repository.
- Create a new issue.
- Wait a few moments for the workflow to run.
- Navigate to the Actions tab and view the details of the workflow run.
Step 3: Review the Workflow Functionality
- Comment: The workflow should automatically add a comment to the newly created issue, thanking the contributor.
- Label: The workflow should label the issue with
todo-review
. - Logs: Review the workflow logs to see the GitHub API interactions. Look for the issue number, comment ID, and other information logged by the script.
Step 4: Understanding the Workflow
Triggers (on
)
issues
:- Listens for the
opened
event when a new issue is created.
- Listens for the
workflow_dispatch
:- Enables manual triggering of the workflow from the Actions tab.
Jobs (jobs
)
thanks
:- Runs on
ubuntu-latest
. - Uses the
actions/github-script
action to interact with GitHub’s REST API.
- Runs on
Steps
- Add Comment:
- The script fetches the issue number from the
context.issue
object. - It uses the GitHub REST API to create a comment on the issue, thanking the contributor.
- The script fetches the issue number from the
- Add Label:
- The script adds a label (
todo-review
) to the issue using the GitHub REST API.
- The script adds a label (
- Expose Comment ID:
- The script outputs the comment ID for potential use in subsequent steps.
Step 5: Optional Enhancements
-
Change the Comment Text:
- Modify the
body
field in the script to customize the comment text.
body: 'We appreciate your input! Stay tuned for a response.';
- Modify the
-
Add Multiple Labels:
- Add additional labels by updating the
labels
array.
labels: ['todo-review', 'needs-feedback'];
- Add additional labels by updating the
-
Trigger on Additional Events:
- Update the
on
section to include other issue-related events, such asreopened
.
on: issues: types: [opened, reopened]
- Update the
Step 6: Run the Workflow Manually
If you want to test the workflow manually without creating a new issue:
- Go to the Actions tab.
- Select the workflow named Misc - Auto Comment on New Issue.
- Click the Run workflow button.
- Provide an issue number for testing if needed.
Summary
In this lab, you:
- Created a workflow that listens for new issue events.
- Used the
github-script
action to comment on and label new issues. - Learned how to interact with the GitHub REST API through the GitHub Actions context.
This workflow is a practical example of automating repository management tasks with GitHub Actions.