This repository shares a few scripts that together provide an answer to the following question:
How the hell can I set up GitHub so that whenever a new issue is created in any of my repositories (or a subset thereof), it is automatically added to a project‽
GitHub does not support this functionality natively, hence the need for these scripts.
Caution
These scripts were developed for personal use and are shared in the hope that they might be useful to others. They have not been thoroughly tested in all environments or scenarios. Users should review the scripts and test them carefully before running them in a production environment. Contributions, bug fixes, and improvements are welcome—see Contributing below.
Before using these scripts, ensure you have the following installed and configured:
- GitHub CLI (
gh):- Installation instructions: cli.github.com
- Authenticate with
gh auth login. You'll need permissions to read repository data, read/write project data, and potentiallyworkflowscope if you're usingadd-issues-to-project-batchto manage workflow files (this scope is needed for the PAT used by the workflow if it modifies workflow files or requires elevated permissions).
jq:- A lightweight and flexible command-line JSON processor.
- Installation instructions: stedolan.github.io/jq/download/
- Bash Shell: These scripts are written for Bash.
- The shebang (first line, e.g.,
#!/opt/homebrew/bin/bashor#!/bin/bash) in the scripts might need adjustment based on your system's Bash location.#!/usr/bin/env bashis often a more portable option.
- The shebang (first line, e.g.,
- Git: Required by
batch-deploy-add-new-issues-workflow.shfor cloning repositories.
- Configure
config.sh:- Open
config.shin a text editor and replace all placeholder values (e.g.,YOUR_GITHUB_OWNER,YOUR_PROJECT_NUMBER,YOUR_GITHUB_PAT,YOUR_EXAMPLE_REPO_1,YOUR_PATH_TO/add-issues-to-project.yml) with your actual data.
- Open
- Make scripts executable:
chmod +x *.sh. - Run the scripts: e.g.,
bash ./automate-github-secrets.sh.
Part 1: Initial setup (for automating new issues)
This part focuses on configuring GitHub Actions to automatically add newly created issues to your project. These steps are typically done only once.
-
automate-github-secrets.sh- Purpose: Securely provides the necessary GitHub Personal Access Token (PAT) to your repositories. This PAT allows the GitHub Action (deployed in the next step) to add issues to your project.
- Action:
- Generate a GitHub Personal Access Token (PAT classic):
- Go to GitHub > Your Profile (top right) > Settings > Developer settings (bottom left) > Personal access tokens > Tokens (classic).
- Click "Generate new token" and select "Generate new token (classic)".
- Give your token a descriptive name (e.g., "multi-repo-project-automation").
- Set an expiration date.
- Required scopes:
repo: Full control of private repositories (needed to add workflow files, set secrets in target repos, list repos if processing all).project: Read and write projects (to allow theactions/add-to-projectaction to manage project items).workflow: Update GitHub Action workflows (needed by theactions/add-to-projectworkflow if it needs to make changes that require this scope, or if you are deploying workflows that modify other workflows).
- Click "Generate token".
- Copy the token immediately. You will not be able to see it again.
- Ensure
OWNER_NAME,PAT_VALUE, and optionallyREPO_LISTare correctly set inconfig.shas per its comments. - Run the script:
bash ./automate-github-secrets.sh.
- Generate a GitHub Personal Access Token (PAT classic):
- Notes:
- Security: Be very careful with your PAT. Once you've run this script to set the secrets in your repositories, you might want to clear the
PAT_VALUEfrom the script file or delete the script if you don't need to run it again soon, to avoid accidentally exposing the PAT if the script is shared or committed. - The secret name
ADD_TO_PROJECT_PATis a common convention for theactions/add-to-projectaction, but can be changed if your workflow uses a different secret name (you'd need to change it in this script and in your workflow YAML).
- Security: Be very careful with your PAT. Once you've run this script to set the secrets in your repositories, you might want to clear the
-
batch-deploy-add-new-issues-workflow.sh(using the providedadd-issues-to-project.yml)- Purpose: Deploys the GitHub Actions workflow (
add-issues-to-project.yml) to all specified repositories. This workflow will automatically add any newly created issues in these repositories to your designated project. - Action:
- Locate the
add-issues-to-project.ymlfile provided in this automation scripts repository. - Customize it: Open this YAML file. You must change the
project-urlto point to your project.- For an organization project:
https://github.com/orgs/YOUR_ORG_NAME/projects/YOUR_PROJECT_NUMBER - For a user project:
https://github.com/users/YOUR_USER_NAME/projects/YOUR_PROJECT_NUMBER
- For an organization project:
- By default, this action will add all issues to the project board, regardless of their status, tag, etc. If you want to filter issues based on specific criteria (e.g., only open issues), you can modify the script accordingly.
- Save this customized
add-issues-to-project.ymlfile somewhere on your local system. - Ensure
OWNER_NAME,WORKFLOW_FILE_PATH, and optionallyREPO_LISTare correctly set inconfig.sh. TheWORKFLOW_FILE_PATHshould be the correct path to your customizedadd-issues-to-project.yml. - Run the script:
bash ./batch-deploy-add-new-issues-workflow.sh.
- Locate the
- Notes:
- This script performs
git clone,git commit, andgit pushoperations. Ensure yourghCLI is authenticated with rights to push to these repositories, or that your Git credential manager is set up. - The commit message is predefined in the script.
- It creates a temporary directory for cloning, which is removed afterward.
- If the workflow file in the repository is identical to the one being deployed, it will skip committing and pushing for that repository.
- This script performs
- Result: After these two steps, any new issues created in the configured repositories will be automatically added to your GitHub project by the GitHub Action.
- Purpose: Deploys the GitHub Actions workflow (
Part 2 (optional): Handling existing issues (one-time population)
The GitHub Action set up above only works for newly created issues. If you have existing issues in your repositories that you want to add to the project, use the following scripts.
-
add-all-existing-issues-to-project.sh- Purpose: Adds all existing issues (both open and closed) from specified repositories to the project.
- Action:
- Ensure
OWNER_NAME,PROJECT_NUMBER, and optionallyREPO_LISTare correctly set inconfig.sh. - Run the script:
bash ./add-all-existing-issues-to-project.sh. This can take a significant amount of time.
- Ensure
- Notes:
- It includes
sleepcommands to help manage GitHub API rate limits. If you hit rate limits, you might need to wait (often an hour) and resume, possibly by commenting out already processed repositories fromREPO_LIST. - The script will skip issues that are already in the project.
- It includes
-
categorize-project-items.sh- Purpose: Updates the status of items in your project based on the current state (open/closed) of their linked issues. Can operate on all items in the project or be filtered to items from specific repositories. The script assumes your project's status field is named "Status".
- Action:
- Ensure
OWNER_NAME,PROJECT_NUMBER,OPEN_ISSUE_STATUS,CLOSED_ISSUE_STATUS, and optionallyREPO_LISTare correctly set inconfig.sh. - Run the script:
bash ./categorize-project-items.sh.
- Ensure
- Notes:
- This script can also be run periodically to ensure project item statuses reflect the actual issue states if they are changed manually or by other processes outside the project board.
- It helps synchronize the project board status with the actual status of linked issues.
- It fetches project field and option IDs dynamically. If field or option names are incorrect, or if the script cannot find them, it will fail with an error message.
- Rate Limits: The GitHub API has rate limits (typically 5000 requests per hour for authenticated
ghusers). Scripts that iterate over many items or repositories (likeadd-all-issues-to-project) can hit these limits.- The scripts include
sleepcommands to mitigate this. - If you hit a limit, you'll usually need to wait an hour for it to reset.
- For
add-all-existing-issues-to-project.sh, you can comment out already processed repositories in theREPO_LISTand restart the script.
- The scripts include
- Permissions: Ensure your
ghCLI is authenticated with sufficient permissions for the operations being performed (reading repos, reading/writing projects, writing secrets, pushing code). Forbatch-deploy-add-new-issues-workflow.sh, ensure your Git setup allows pushing to the target repositories (e.g., via HTTPS with a PAT or SSH keys). jq: command not found: Installjq.gh: command not found: Install the GitHub CLI.- Script execution permission denied: Use
chmod +x ./script-name.sh. No such file or directoryfor workflow file: Double-check theWORKFLOW_FILE_PATHinbatch-deploy-add-new-issues-workflow.sh.- Git clone/push issues: Ensure your Git credentials are set up correctly and you have push access to the repositories.
Feel free to fork this repository, suggest improvements, or submit pull requests. If you find bugs or have feature requests, please open an issue.
This project is licensed under the MIT License - see the LICENSE.md file for details.