Introduction
This article demonstrates how to use the GraphQL API and GitHub Actions to add a pull request to a project. When the pull request is marked as "ready for review", a new task is added to the project with a "Status" field set to "Todo", and the current date is added to a custom "Date posted" field.
This article assumes that you have a basic understanding of GitHub Actions. For more information about GitHub Actions, see "GitHub Actions."
Note: Projects (beta) is currently in limited public beta. For more information, see GitHub Issues.
Note: To access the projects (beta) API, you must pass the following header in addition to being part of the limited beta: GraphQL-Features: projects_next_graphql
.
Note: GITHUB_TOKEN
does not have the necessary scopes to access projects (beta). You must create a token with org:write
scope and save it as a secret in your repository or organization. In the following workflow, replace YOUR_TOKEN
with the name of the secret. For more information, see "Creating a personal access token."
Example workflow
You can copy this workflow and modify it as described in the table below to meet your needs. A project can span multiple repositories, but a workflow is specific to a repository. Add this workflow to each repository that you want your project to track. For more information about creating workflow files, see "Quickstart for GitHub Actions."
name: Add PR to project
on:
pull_request:
types:
- ready_for_review
jobs:
track_pr:
runs-on: ubuntu-latest
steps:
- name: Get project data
env:
GITHUB_TOKEN: ${{secrets.YOUR_TOKEN}}
ORGANIZATION: YOUR_ORGANIZATION
PROJECT_NUMBER: YOUR_PROJECT_NUMBER
run: |
gh api graphql --header 'GraphQL-Features: projects_next_graphql' -f query='
query($org: String!, $number: Int!) {
organization(login: $org){
projectNext(number: $number) {
id
fields(first:20) {
nodes {
id
name
settings
}
}
}
}
}' -f org=$ORGANIZATION -F number=$PROJECT_NUMBER > project_data.json
echo 'PROJECT_ID='$(jq '.data.organization.projectNext.id' project_data.json) >> $GITHUB_ENV
echo 'DATE_FIELD_ID='$(jq '.data.organization.projectNext.fields.nodes[] | select(.name== "Date posted") | .id' project_data.json) >> $GITHUB_ENV
echo 'STATUS_FIELD_ID='$(jq '.data.organization.projectNext.fields.nodes[] | select(.name== "Status") | .id' project_data.json) >> $GITHUB_ENV
echo 'TODO_OPTION_ID='$(jq '.data.organization.projectNext.fields.nodes[] | select(.name== "Status") |.settings | fromjson.options[] | select(.name=="Todo") |.id' project_data.json) >> $GITHUB_ENV
- name: Add PR to project
env:
GITHUB_TOKEN: ${{secrets.YOUR_TOKEN}}
PR_ID: ${{ github.event.pull_request.node_id }}
run: |
item_id="$( gh api graphql --header 'GraphQL-Features: projects_next_graphql' -f query='
mutation($project:ID!, $pr:ID!) {
addProjectNextItem(input: {projectId: $project, contentId: $pr}) {
projectNextItem {
id
}
}
}' -f project=$PROJECT_ID -f pr=$PR_ID --jq '.data.addProjectNextItem.projectNextItem.id')"
echo 'ITEM_ID='$item_id >> $GITHUB_ENV
- name: Get date
run: echo "DATE=$(date +"%Y-%m-%d")" >> $GITHUB_ENV
- name: Set fields
env:
GITHUB_TOKEN: ${{secrets.YOUR_TOKEN}}
run: |
gh api graphql --header 'GraphQL-Features: projects_next_graphql' -f query='
mutation (
$project: ID!
$item: ID!
$status_field: ID!
$status_value: String!
$date_field: ID!
$date_value: String!
) {
set_status: updateProjectNextItemField(input: {
projectId: $project
itemId: $item
fieldId: $status_field
value: $status_value
}) {
projectNextItem {
id
}
}
set_date_posted: updateProjectNextItemField(input: {
projectId: $project
itemId: $item
fieldId: $date_field
value: $date_value
}) {
projectNextItem {
id
}
}
}' -f project=$PROJECT_ID -f item=$ITEM_ID -f status_field=$STATUS_FIELD_ID -f status_value=${{ env.TODO_OPTION_ID }} -f date_field=$DATE_FIELD_ID -f date_value=$DATE --silent
The following table explains sections of the workflow and shows you how to adapt it for your own use.
|
This workflow runs whenever a pull request in the repository is marked as "ready for review". |
|
Sets environment variables for this step.
Create a token with org:write scope and save it as a secret in your repository or organization. Replace YOUR_TOKEN with the name of the secret.
Replace YOUR_ORGANIZATION with the name of your organization. For example, octo-org .
Replace YOUR_PROJECT_NUMBER with your project number. To find the project number, look at the project URL. For example, https://github.com/orgs/octo-org/projects/5 has a project number of 5.
|
|
Uses GitHub CLI to query the API for the ID of the project and for the ID, name, and settings for the first 20 fields in the project. The response is stored in a file called project_data.json .
|
|
Parses the response from the API query and stores the relevant IDs as environment variables. Modify this to get the ID for different fields or options. For example:
|
|
Sets environment variables for this step. GITHUB_TOKEN is described above. PR_ID is the ID of the pull request that triggered this workflow.
|
|
Uses GitHub CLI and the API to add the pull request that triggered this workflow to the project. The jq flag parses the response to get the ID of the created item.
|
|
Stores the ID of the created item as an environment variable. |
|
Saves the current date as an environment variable in yyyy-mm-dd format.
|
|
Sets environment variables for this step. GITHUB_TOKEN is described above.
|
|
Sets the value of the Status field to Todo . Sets the value of the Date posted field.
|
Next steps
For more information about other changes you can make to your project through the API, see "Using the API to manage projects." For more information about GitHub Actions, see "GitHub Actions."