Create A Github Action That Runs Every Day And Checks All Pull Requests
Introduction
In this article, we will explore how to create a GitHub Action that runs every day and checks all pull requests. This action will be responsible for monitoring pull requests, adding comments when changes are requested, and closing pull requests after 7 days. We will use GitHub Actions, a powerful tool for automating tasks on GitHub, to achieve this.
Prerequisites
Before we begin, make sure you have the following:
- A GitHub account with a repository
- Basic knowledge of GitHub Actions and YAML syntax
- Familiarity with GitHub's API and webhooks
Step 1: Create a New GitHub Action
To create a new GitHub Action, navigate to your repository's settings and click on "Actions" in the left-hand menu. Click on "New workflow" and give your action a name, such as "Daily PR Checker".
Step 2: Write the YAML Script
In the "Actions" tab, click on the three dots next to your new workflow and select "Edit YAML". This will open the YAML script editor. We will write the script to check all pull requests, add comments when changes are requested, and close pull requests after 7 days.
name: Daily PR Checker
on:
schedule:
- cron: 0 0 * * *
jobs:
check-prs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Get pull requests
run: |
curl -X GET \
https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls?state=open \
-H 'Authorization: Bearer ${GITHUB_TOKEN}' \
-H 'Content-Type: application/json'
- name: Loop through pull requests
run: |
for pr in $(curl -X GET \
https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls?state=open \
-H 'Authorization: Bearer ${GITHUB_TOKEN}' \
-H 'Content-Type: application/json'); do
id=$(echo $pr | jq -r '.id')
title=$(echo $pr | jq -r '.title')
changes_requested=$(echo $pr | jq -r '.requested_changes')
days_since_requested=$(date -d "$(echo $pr | jq -r '.updated_at')" +%s) - $(date -d "$(date -d "$(echo $pr | jq -r '.updated_at')" -d -24h)" +%s)
if [ $days_since_requested -gt 86400 ]; then
echo "Changes requested for $title for more than 24 hours"
curl -X POST \
https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${id}/comments \
-H 'Authorization: Bearer ${GITHUB_TOKEN}' \
-H 'Content-Type: application/json' \
-d '{"body": "Changes requested for more than 24 hours"}'
fi
if [ $days_since_requested -gt 604800 ]; then
echo "Closing pull request $title"
curl -X PATCH \
https://api.github.com/repos/${GITHUB_REPOSITORY}/s/${id} \
-H 'Authorization: Bearer ${GITHUB_TOKEN}' \
-H 'Content-Type: application/json' \
-d '{"state": "closed"}'
curl -X PATCH \
https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${id} \
-H 'Authorization: Bearer ${GITHUB_TOKEN}' \
-H 'Content-Type: application/json' \
-d '{"assignees": []}'
fi
done
Step 3: Configure the GitHub Token
To use the GitHub API, we need to configure a GitHub token. Go to your repository's settings and click on "Actions" in the left-hand menu. Click on "Secrets" and add a new secret called "GITHUB_TOKEN". This token will be used to authenticate with the GitHub API.
Step 4: Save and Trigger the Action
Save the YAML script and trigger the action by clicking on the "Run workflow" button. The action will run every day at midnight and check all pull requests.
Conclusion
In this article, we created a GitHub Action that runs every day and checks all pull requests. The action adds comments when changes are requested and closes pull requests after 7 days. We used GitHub Actions, a powerful tool for automating tasks on GitHub, to achieve this. With this action, you can ensure that your pull requests are reviewed and closed in a timely manner.
Troubleshooting
If you encounter any issues with the action, make sure to check the following:
- The GitHub token is correctly configured
- The YAML script is correctly formatted
- The action is triggered correctly
Future Improvements
In the future, we can improve the action by:
- Adding more features, such as sending notifications when pull requests are closed
- Improving the script to handle edge cases, such as pull requests with no changes requested
- Using a more robust authentication method, such as OAuth
References
- GitHub Actions documentation: https://docs.github.com/en/actions
- GitHub API documentation: https://docs.github.com/en/rest
- YAML syntax documentation: https://yaml.org/
GitHub Action Daily PR Checker: Q&A =====================================
Introduction
In our previous article, we created a GitHub Action that runs every day and checks all pull requests. The action adds comments when changes are requested and closes pull requests after 7 days. In this article, we will answer some frequently asked questions about the GitHub Action Daily PR Checker.
Q: What is the purpose of the GitHub Action Daily PR Checker?
A: The purpose of the GitHub Action Daily PR Checker is to automate the process of checking all pull requests in a repository and adding comments when changes are requested. It also closes pull requests after 7 days.
Q: How does the GitHub Action Daily PR Checker work?
A: The GitHub Action Daily PR Checker uses a YAML script to check all pull requests in a repository. It uses the GitHub API to retrieve the list of pull requests and then loops through each pull request to check if changes are requested. If changes are requested, it adds a comment to the pull request. If the pull request is older than 7 days, it closes the pull request.
Q: What are the benefits of using the GitHub Action Daily PR Checker?
A: The benefits of using the GitHub Action Daily PR Checker include:
- Automating the process of checking all pull requests in a repository
- Adding comments when changes are requested
- Closing pull requests after 7 days
- Reducing the workload of developers and reviewers
- Improving the overall quality of pull requests
Q: How do I configure the GitHub Action Daily PR Checker?
A: To configure the GitHub Action Daily PR Checker, you need to:
- Create a new GitHub Action in your repository
- Write a YAML script to check all pull requests in your repository
- Configure a GitHub token to authenticate with the GitHub API
- Save and trigger the action
Q: What are the system requirements for the GitHub Action Daily PR Checker?
A: The system requirements for the GitHub Action Daily PR Checker are:
- A GitHub account with a repository
- Basic knowledge of GitHub Actions and YAML syntax
- Familiarity with GitHub's API and webhooks
Q: Can I customize the GitHub Action Daily PR Checker to fit my needs?
A: Yes, you can customize the GitHub Action Daily PR Checker to fit your needs. You can modify the YAML script to add or remove features, or to change the behavior of the action.
Q: What are some common issues that I may encounter with the GitHub Action Daily PR Checker?
A: Some common issues that you may encounter with the GitHub Action Daily PR Checker include:
- The GitHub token is not correctly configured
- The YAML script is not correctly formatted
- The action is not triggered correctly
Q: How do I troubleshoot issues with the GitHub Action Daily PR Checker?
A: To troubleshoot issues with the GitHub Action Daily PR Checker, you can:
- Check the GitHub token is correctly configured
- Check the YAML script is correctly formatted
- Check the action is triggered correctly
- Check the GitHub API documentation for any errors or issues
Conclusion
In this article, we answered some asked questions about the GitHub Action Daily PR Checker. We hope that this article has been helpful in understanding how to use the GitHub Action Daily PR Checker and troubleshooting common issues.
References
- GitHub Actions documentation: https://docs.github.com/en/actions
- GitHub API documentation: https://docs.github.com/en/rest
- YAML syntax documentation: https://yaml.org/