Flujos de trabajo de GitHub Actions
Este artículo demuestra cómo utilizar la API de GraphQL y las GitHub Actions para agregar una solicitud de cambios a un proyecto organizacional. En los flujos de trabajo de ejemplo, cuando la solicitud de cambios se marca como "lista para revisión", se agrega una tarea nueva al proyecto con un campo de "Estado" configurado en "Pendiente" y se agrega la fecha actual a un campo personalizado de "Fecha en la que se publicó".
Puedes copiar uno de los siguientes flujos de trabajo y modificarlo de acuerdo con lo descrito en la siguiente tabla para que satisfaga tus necesidades.
Un proyecto puede abarcar repositorios múltiples, pero un flujo de trabajo es específico par aun repositorio. Agrega el flujo de trabajo a cada repositorio del que quieras que realice un seguimiento el proyecto. Para obtener más información sobre cómo crear archivos de flujo de trabajo, consulta "Guía de inicio rápido para GitHub Actions".
Este artículo asume que tienes un entendimiento básico de las GitHub Actions. Para más información sobre GitHub Actions, consulta "Documentación de GitHub Actions".
Para obtener más información sobre otros cambios que puedes realizar en el proyecto mediante la API, consulta "Uso de la API para administrar instancias de Projects".
También puedes usar el flujo de trabajo actions/add-to-project, que mantiene GitHub y agregará la incidencia o solicitud de incorporación de cambios actual al proyecto especificado. Para más información, consulta el repositorio actions/add-to-project y el archivo LÉAME.
Note
GITHUB_TOKEN
tiene como ámbito el nivel de repositorio y no puede acceder a projects. Para acceder a projects, puedes crear una GitHub App (recomendado para proyectos de la organización) o un personal access token (recomendado para proyectos de usuario). A continuación se muestran los ejemplos de flujo de trabajo para ambos acercamientos.
Flujo de trabajo ejemplo autenticándose con una GitHub App
Para más información sobre la autenticación en un flujo de trabajo de GitHub Actions con una GitHub App, consulta "Realización de solicitudes de API autenticadas con una aplicación de GitHub en un flujo de trabajo de Acciones de GitHub".
-
Crea una GitHub App o elige una GitHub App existente que le pertenezca a tu organización. Para obtener más información, vea «Registro de una instancia de GitHub App».
-
Dale a tu GitHub App permisos de lectura y escritura para los proyectos organizacionales. En este ejemplo concreto, tu GitHub App también necesitará permisos de lectura para las PR del repositorio y las incidencias del repositorio. Para obtener más información, vea «Modificación del registro de una instancia de GitHub App».
Note
Puedes controlar el permiso de la aplicación para los proyectos de la organización y para los proyectos del repositorio. Debes otorgar permisos de lectura y escritura de proyectos organizacionales; los permisos de lectura y escritura en los proyectos de repositorio no serán suficientes.
-
Instala la GitHub App en tu organización. Instálala para todos los repositorios a los cuales necesita acceso tu proyecto. Para obtener más información, vea «Instalación de tu propia instancia de GitHub App».
-
Almacene la ID de su GitHub App como una variable de configuración en su repositorio u organización. En el flujo de trabajo siguiente, reemplace
APP_ID
por el nombre de la variable de configuración. Puedes encontrar tu ID de app en la página de ajustes de tu app o mediante la API de la misma. Para obtener más información, vea «Puntos de conexión de la API de REST para las aplicaciones». Para obtener más información sobre las variables de configuración, consulta "Almacenamiento de información en variables". -
Generar una llave privada para tu app. Almacena el contenido del archivo resultante como secreto en tu repositorio u organización. (Almacene todo el contenido del archivo, incluido el contenido de
-----BEGIN RSA PRIVATE KEY-----
y-----END RSA PRIVATE KEY-----
). En el flujo de trabajo siguiente, reemplaceAPP_PEM
por el nombre del secreto. Para obtener más información, vea «Administración de claves privadas para aplicaciones de GitHub». Para obtener más información sobre cómo almacenar secretos, consulta "Uso de secretos en Acciones de GitHub". -
En el flujo de trabajo siguiente, reemplace
YOUR_ORGANIZATION
por el nombre de la organización. Por ejemplo:octo-org
. ReemplaceYOUR_PROJECT_NUMBER
por el número del proyecto. Para encontrar un número de proyecto, revisa su URL. Por ejemplo,https://github.com/orgs/octo-org/projects/5
tiene "5" como número de proyecto. Para que este ejemplo concreto funcione, el proyecto también debe tener un campo de fecha "Fecha de publicación".
# name: Add PR to project # This workflow runs whenever a pull request in the repository is marked as "ready for review". on: pull_request: types: - ready_for_review jobs: track_pr: runs-on: ubuntu-latest steps: # Uses the [actions/create-github-app-token](https://github.com/marketplace/actions/create-github-app-token) action to generate an installation access token for your app from the app ID and private key. The installation access token is accessed later in the workflow as `${{ steps.generate-token.outputs.token }}`. # # Replace `APP_ID` with the name of the configuration variable that contains your app ID. # # Replace `APP_PEM` with the name of the secret that contains your app private key. - name: Generate token id: generate-token uses: actions/create-github-app-token@v1 with: app-id: ${{ vars.APP_ID }} private-key: ${{ secrets.APP_PEM }} # Sets environment variables for this step. # # 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. - name: Get project data env: GH_TOKEN: ${{ steps.generate-token.outputs.token }} ORGANIZATION: YOUR_ORGANIZATION PROJECT_NUMBER: YOUR_PROJECT_NUMBER # Uses [GitHub CLI](https://cli.github.com/manual/) to query the API for the ID of the project and return the name and ID of the first 20 fields in the project. `fields` returns a union and the query uses inline fragments (`... on`) to return information about any `ProjectV2Field` and `ProjectV2SingleSelectField` fields. The response is stored in a file called `project_data.json`. run: | gh api graphql -f query=' query($org: String!, $number: Int!) { organization(login: $org){ projectV2(number: $number) { id fields(first:20) { nodes { ... on ProjectV2Field { id name } ... on ProjectV2SingleSelectField { id name options { id name } } } } } } }' -f org=$ORGANIZATION -F number=$PROJECT_NUMBER > 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: # # - To get the ID of a field called `Team`, add `echo 'TEAM_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Team") | .id' project_data.json) >> $GITHUB_ENV`. # - To get the ID of an option called `Octoteam` for the `Team` single select field, add `echo 'OCTOTEAM_OPTION_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Team") |.options[] | select(.name=="Octoteam") |.id' project_data.json) >> $GITHUB_ENV`. # # **Note:** This workflow assumes that you have a project with a single select field called "Status" that includes an option called "Todo" and a date field called "Date posted". You must modify this section to match the fields that are present in your table. echo 'PROJECT_ID='$(jq '.data.organization.projectV2.id' project_data.json) >> $GITHUB_ENV echo 'DATE_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Date posted") | .id' project_data.json) >> $GITHUB_ENV echo 'STATUS_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Status") | .id' project_data.json) >> $GITHUB_ENV echo 'TODO_OPTION_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Status") | .options[] | select(.name=="Todo") |.id' project_data.json) >> $GITHUB_ENV # Sets environment variables for this step. `GH_TOKEN` is the token generated in the first step. `PR_ID` is the ID of the pull request that triggered this workflow. - name: Add PR to project env: GH_TOKEN: ${{ steps.generate-token.outputs.token }} PR_ID: ${{ github.event.pull_request.node_id }} # Uses [GitHub CLI](https://cli.github.com/manual/) 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. run: | item_id="$( gh api graphql -f query=' mutation($project:ID!, $pr:ID!) { addProjectV2ItemById(input: {projectId: $project, contentId: $pr}) { item { id } } }' -f project=$PROJECT_ID -f pr=$PR_ID --jq '.data.addProjectV2ItemById.item.id')" # Stores the ID of the created item as an environment variable. echo 'ITEM_ID='$item_id >> $GITHUB_ENV # Saves the current date as an environment variable in `yyyy-mm-dd` format. - name: Get date run: echo "DATE=$(date +"%Y-%m-%d")" >> $GITHUB_ENV # Sets environment variables for this step. `GH_TOKEN` is the token generated in the first step. - name: Set fields env: GH_TOKEN: ${{ steps.generate-token.outputs.token }} # Sets the value of the `Status` field to `Todo`. Sets the value of the `Date posted` field. run: | gh api graphql -f query=' mutation ( $project: ID! $item: ID! $status_field: ID! $status_value: String! $date_field: ID! $date_value: Date! ) { set_status: updateProjectV2ItemFieldValue(input: { projectId: $project itemId: $item fieldId: $status_field value: { singleSelectOptionId: $status_value } }) { projectV2Item { id } } set_date_posted: updateProjectV2ItemFieldValue(input: { projectId: $project itemId: $item fieldId: $date_field value: { date: $date_value } }) { projectV2Item { 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
name: Add PR to project
on:
pull_request:
types:
- ready_for_review
jobs:
track_pr:
runs-on: ubuntu-latest
steps:
This workflow runs whenever a pull request in the repository is marked as "ready for review".
- name: Generate token
id: generate-token
uses: actions/create-github-app-token@v1
with:
app-id: ${{ vars.APP_ID }}
private-key: ${{ secrets.APP_PEM }}
Uses the actions/create-github-app-token action to generate an installation access token for your app from the app ID and private key. The installation access token is accessed later in the workflow as ${{ steps.generate-token.outputs.token }}
.
Replace APP_ID
with the name of the configuration variable that contains your app ID.
Replace APP_PEM
with the name of the secret that contains your app private key.
- name: Get project data
env:
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
ORGANIZATION: YOUR_ORGANIZATION
PROJECT_NUMBER: YOUR_PROJECT_NUMBER
Sets environment variables for this step.
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.
run: |
gh api graphql -f query='
query($org: String!, $number: Int!) {
organization(login: $org){
projectV2(number: $number) {
id
fields(first:20) {
nodes {
... on ProjectV2Field {
id
name
}
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
}
}
}' -f org=$ORGANIZATION -F number=$PROJECT_NUMBER > project_data.json
Uses GitHub CLI to query the API for the ID of the project and return the name and ID of the first 20 fields in the project. fields
returns a union and the query uses inline fragments (... on
) to return information about any ProjectV2Field
and ProjectV2SingleSelectField
fields. The response is stored in a file called project_data.json
.
echo 'PROJECT_ID='$(jq '.data.organization.projectV2.id' project_data.json) >> $GITHUB_ENV
echo 'DATE_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Date posted") | .id' project_data.json) >> $GITHUB_ENV
echo 'STATUS_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Status") | .id' project_data.json) >> $GITHUB_ENV
echo 'TODO_OPTION_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Status") | .options[] | select(.name=="Todo") |.id' project_data.json) >> $GITHUB_ENV
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:
- To get the ID of a field called
Team
, addecho 'TEAM_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Team") | .id' project_data.json) >> $GITHUB_ENV
. - To get the ID of an option called
Octoteam
for theTeam
single select field, addecho 'OCTOTEAM_OPTION_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Team") |.options[] | select(.name=="Octoteam") |.id' project_data.json) >> $GITHUB_ENV
.
Note: This workflow assumes that you have a project with a single select field called "Status" that includes an option called "Todo" and a date field called "Date posted". You must modify this section to match the fields that are present in your table.
- name: Add PR to project
env:
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
PR_ID: ${{ github.event.pull_request.node_id }}
Sets environment variables for this step. GH_TOKEN
is the token generated in the first step. PR_ID
is the ID of the pull request that triggered this workflow.
run: |
item_id="$( gh api graphql -f query='
mutation($project:ID!, $pr:ID!) {
addProjectV2ItemById(input: {projectId: $project, contentId: $pr}) {
item {
id
}
}
}' -f project=$PROJECT_ID -f pr=$PR_ID --jq '.data.addProjectV2ItemById.item.id')"
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.
echo 'ITEM_ID='$item_id >> $GITHUB_ENV
Stores the ID of the created item as an environment variable.
- name: Get date
run: echo "DATE=$(date +"%Y-%m-%d")" >> $GITHUB_ENV
Saves the current date as an environment variable in yyyy-mm-dd
format.
- name: Set fields
env:
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
Sets environment variables for this step. GH_TOKEN
is the token generated in the first step.
run: |
gh api graphql -f query='
mutation (
$project: ID!
$item: ID!
$status_field: ID!
$status_value: String!
$date_field: ID!
$date_value: Date!
) {
set_status: updateProjectV2ItemFieldValue(input: {
projectId: $project
itemId: $item
fieldId: $status_field
value: {
singleSelectOptionId: $status_value
}
}) {
projectV2Item {
id
}
}
set_date_posted: updateProjectV2ItemFieldValue(input: {
projectId: $project
itemId: $item
fieldId: $date_field
value: {
date: $date_value
}
}) {
projectV2Item {
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
Sets the value of the Status
field to Todo
. Sets the value of the Date posted
field.
#
name: Add PR to project
# This workflow runs whenever a pull request in the repository is marked as "ready for review".
on:
pull_request:
types:
- ready_for_review
jobs:
track_pr:
runs-on: ubuntu-latest
steps:
# Uses the [actions/create-github-app-token](https://github.com/marketplace/actions/create-github-app-token) action to generate an installation access token for your app from the app ID and private key. The installation access token is accessed later in the workflow as `${{ steps.generate-token.outputs.token }}`.
#
# Replace `APP_ID` with the name of the configuration variable that contains your app ID.
#
# Replace `APP_PEM` with the name of the secret that contains your app private key.
- name: Generate token
id: generate-token
uses: actions/create-github-app-token@v1
with:
app-id: ${{ vars.APP_ID }}
private-key: ${{ secrets.APP_PEM }}
# Sets environment variables for this step.
#
# 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.
- name: Get project data
env:
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
ORGANIZATION: YOUR_ORGANIZATION
PROJECT_NUMBER: YOUR_PROJECT_NUMBER
# Uses [GitHub CLI](https://cli.github.com/manual/) to query the API for the ID of the project and return the name and ID of the first 20 fields in the project. `fields` returns a union and the query uses inline fragments (`... on`) to return information about any `ProjectV2Field` and `ProjectV2SingleSelectField` fields. The response is stored in a file called `project_data.json`.
run: |
gh api graphql -f query='
query($org: String!, $number: Int!) {
organization(login: $org){
projectV2(number: $number) {
id
fields(first:20) {
nodes {
... on ProjectV2Field {
id
name
}
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
}
}
}' -f org=$ORGANIZATION -F number=$PROJECT_NUMBER > 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:
#
# - To get the ID of a field called `Team`, add `echo 'TEAM_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Team") | .id' project_data.json) >> $GITHUB_ENV`.
# - To get the ID of an option called `Octoteam` for the `Team` single select field, add `echo 'OCTOTEAM_OPTION_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Team") |.options[] | select(.name=="Octoteam") |.id' project_data.json) >> $GITHUB_ENV`.
#
# **Note:** This workflow assumes that you have a project with a single select field called "Status" that includes an option called "Todo" and a date field called "Date posted". You must modify this section to match the fields that are present in your table.
echo 'PROJECT_ID='$(jq '.data.organization.projectV2.id' project_data.json) >> $GITHUB_ENV
echo 'DATE_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Date posted") | .id' project_data.json) >> $GITHUB_ENV
echo 'STATUS_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Status") | .id' project_data.json) >> $GITHUB_ENV
echo 'TODO_OPTION_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Status") | .options[] | select(.name=="Todo") |.id' project_data.json) >> $GITHUB_ENV
# Sets environment variables for this step. `GH_TOKEN` is the token generated in the first step. `PR_ID` is the ID of the pull request that triggered this workflow.
- name: Add PR to project
env:
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
PR_ID: ${{ github.event.pull_request.node_id }}
# Uses [GitHub CLI](https://cli.github.com/manual/) 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.
run: |
item_id="$( gh api graphql -f query='
mutation($project:ID!, $pr:ID!) {
addProjectV2ItemById(input: {projectId: $project, contentId: $pr}) {
item {
id
}
}
}' -f project=$PROJECT_ID -f pr=$PR_ID --jq '.data.addProjectV2ItemById.item.id')"
# Stores the ID of the created item as an environment variable.
echo 'ITEM_ID='$item_id >> $GITHUB_ENV
# Saves the current date as an environment variable in `yyyy-mm-dd` format.
- name: Get date
run: echo "DATE=$(date +"%Y-%m-%d")" >> $GITHUB_ENV
# Sets environment variables for this step. `GH_TOKEN` is the token generated in the first step.
- name: Set fields
env:
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
# Sets the value of the `Status` field to `Todo`. Sets the value of the `Date posted` field.
run: |
gh api graphql -f query='
mutation (
$project: ID!
$item: ID!
$status_field: ID!
$status_value: String!
$date_field: ID!
$date_value: Date!
) {
set_status: updateProjectV2ItemFieldValue(input: {
projectId: $project
itemId: $item
fieldId: $status_field
value: {
singleSelectOptionId: $status_value
}
}) {
projectV2Item {
id
}
}
set_date_posted: updateProjectV2ItemFieldValue(input: {
projectId: $project
itemId: $item
fieldId: $date_field
value: {
date: $date_value
}
}) {
projectV2Item {
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
Flujo de trabajo de ejemplo de autenticación con una personal access token
- Crea una personal access token (classic) con los ámbitos
project
yrepo
. Para obtener más información, vea «Administración de tokens de acceso personal». - Guarda el personal access token como un secreto en el repositorio u organización.
- En el flujo de trabajo siguiente, reemplace
YOUR_TOKEN
por el nombre del secreto. ReemplaceYOUR_ORGANIZATION
por el nombre de la organización. Por ejemplo,octo-org
. ReemplaceYOUR_PROJECT_NUMBER
por el número del proyecto. Para encontrar un número de proyecto, revisa su URL. Por ejemplo,https://github.com/orgs/octo-org/projects/5
tiene "5" como número de proyecto.
# This workflow runs whenever a pull request in the repository is marked as "ready for review". name: Add PR to project on: pull_request: types: - ready_for_review jobs: track_pr: runs-on: ubuntu-latest steps: # Sets environment variables for this step. # # If you are using a personal access token, replace `YOUR_TOKEN` with the name of the secret that contains your personal access token. # # 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. - name: Get project data env: GH_TOKEN: ${{ secrets.YOUR_TOKEN }} ORGANIZATION: YOUR_ORGANIZATION PROJECT_NUMBER: YOUR_PROJECT_NUMBER # Uses [GitHub CLI](https://cli.github.com/manual/) to query the API for the ID of the project and return the name and ID of the first 20 fields in the project. `fields` returns a union and the query uses inline fragments (`... on`) to return information about any `ProjectV2Field` and `ProjectV2SingleSelectField` fields. The response is stored in a file called `project_data.json`. run: | gh api graphql -f query=' query($org: String!, $number: Int!) { organization(login: $org){ projectV2(number: $number) { id fields(first:20) { nodes { ... on ProjectV2Field { id name } ... on ProjectV2SingleSelectField { id name options { id name } } } } } } }' -f org=$ORGANIZATION -F number=$PROJECT_NUMBER > 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: # # - To get the ID of a field called `Team`, add `echo 'TEAM_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Team") | .id' project_data.json) >> $GITHUB_ENV`. # - To get the ID of an option called `Octoteam` for the `Team` single select field, add `echo 'OCTOTEAM_OPTION_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Team") |.options[] | select(.name=="Octoteam") |.id' project_data.json) >> $GITHUB_ENV`. # # **Note:** This workflow assumes that you have a project with a single select field called "Status" that includes an option called "Todo" and a date field called "Date posted". You must modify this section to match the fields that are present in your table. echo 'PROJECT_ID='$(jq '.data.organization.projectV2.id' project_data.json) >> $GITHUB_ENV echo 'DATE_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Date posted") | .id' project_data.json) >> $GITHUB_ENV echo 'STATUS_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Status") | .id' project_data.json) >> $GITHUB_ENV echo 'TODO_OPTION_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Status") | .options[] | select(.name=="Todo") |.id' project_data.json) >> $GITHUB_ENV # Sets environment variables for this step. Replace `YOUR_TOKEN` with the name of the secret that contains your personal access token. - name: Add PR to project env: GH_TOKEN: ${{ secrets.YOUR_TOKEN }} PR_ID: ${{ github.event.pull_request.node_id }} # Uses [GitHub CLI](https://cli.github.com/manual/) 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. run: | item_id="$( gh api graphql -f query=' mutation($project:ID!, $pr:ID!) { addProjectV2ItemById(input: {projectId: $project, contentId: $pr}) { item { id } } }' -f project=$PROJECT_ID -f pr=$PR_ID --jq '.data.addProjectV2ItemById.item.id')" # Stores the ID of the created item as an environment variable. echo 'ITEM_ID='$item_id >> $GITHUB_ENV # Saves the current date as an environment variable in `yyyy-mm-dd` format. - name: Get date run: echo "DATE=$(date +"%Y-%m-%d")" >> $GITHUB_ENV # Sets environment variables for this step. Replace `YOUR_TOKEN` with the name of the secret that contains your personal access token. - name: Set fields env: GH_TOKEN: ${{ secrets.YOUR_TOKEN }} # Sets the value of the `Status` field to `Todo`. Sets the value of the `Date posted` field. run: | gh api graphql -f query=' mutation ( $project: ID! $item: ID! $status_field: ID! $status_value: String! $date_field: ID! $date_value: Date! ) { set_status: updateProjectV2ItemFieldValue(input: { projectId: $project itemId: $item fieldId: $status_field value: { singleSelectOptionId: $status_value } }) { projectV2Item { id } } set_date_posted: updateProjectV2ItemFieldValue(input: { projectId: $project itemId: $item fieldId: $date_field value: { date: $date_value } }) { projectV2Item { 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
name: Add PR to project
on:
pull_request:
types:
- ready_for_review
jobs:
track_pr:
runs-on: ubuntu-latest
steps:
This workflow runs whenever a pull request in the repository is marked as "ready for review".
- name: Get project data
env:
GH_TOKEN: ${{ secrets.YOUR_TOKEN }}
ORGANIZATION: YOUR_ORGANIZATION
PROJECT_NUMBER: YOUR_PROJECT_NUMBER
Sets environment variables for this step.
If you are using a personal access token, replace YOUR_TOKEN
with the name of the secret that contains your personal access token.
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.
run: |
gh api graphql -f query='
query($org: String!, $number: Int!) {
organization(login: $org){
projectV2(number: $number) {
id
fields(first:20) {
nodes {
... on ProjectV2Field {
id
name
}
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
}
}
}' -f org=$ORGANIZATION -F number=$PROJECT_NUMBER > project_data.json
Uses GitHub CLI to query the API for the ID of the project and return the name and ID of the first 20 fields in the project. fields
returns a union and the query uses inline fragments (... on
) to return information about any ProjectV2Field
and ProjectV2SingleSelectField
fields. The response is stored in a file called project_data.json
.
echo 'PROJECT_ID='$(jq '.data.organization.projectV2.id' project_data.json) >> $GITHUB_ENV
echo 'DATE_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Date posted") | .id' project_data.json) >> $GITHUB_ENV
echo 'STATUS_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Status") | .id' project_data.json) >> $GITHUB_ENV
echo 'TODO_OPTION_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Status") | .options[] | select(.name=="Todo") |.id' project_data.json) >> $GITHUB_ENV
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:
- To get the ID of a field called
Team
, addecho 'TEAM_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Team") | .id' project_data.json) >> $GITHUB_ENV
. - To get the ID of an option called
Octoteam
for theTeam
single select field, addecho 'OCTOTEAM_OPTION_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Team") |.options[] | select(.name=="Octoteam") |.id' project_data.json) >> $GITHUB_ENV
.
Note: This workflow assumes that you have a project with a single select field called "Status" that includes an option called "Todo" and a date field called "Date posted". You must modify this section to match the fields that are present in your table.
- name: Add PR to project
env:
GH_TOKEN: ${{ secrets.YOUR_TOKEN }}
PR_ID: ${{ github.event.pull_request.node_id }}
Sets environment variables for this step. Replace YOUR_TOKEN
with the name of the secret that contains your personal access token.
run: |
item_id="$( gh api graphql -f query='
mutation($project:ID!, $pr:ID!) {
addProjectV2ItemById(input: {projectId: $project, contentId: $pr}) {
item {
id
}
}
}' -f project=$PROJECT_ID -f pr=$PR_ID --jq '.data.addProjectV2ItemById.item.id')"
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.
echo 'ITEM_ID='$item_id >> $GITHUB_ENV
Stores the ID of the created item as an environment variable.
- name: Get date
run: echo "DATE=$(date +"%Y-%m-%d")" >> $GITHUB_ENV
Saves the current date as an environment variable in yyyy-mm-dd
format.
- name: Set fields
env:
GH_TOKEN: ${{ secrets.YOUR_TOKEN }}
Sets environment variables for this step. Replace YOUR_TOKEN
with the name of the secret that contains your personal access token.
run: |
gh api graphql -f query='
mutation (
$project: ID!
$item: ID!
$status_field: ID!
$status_value: String!
$date_field: ID!
$date_value: Date!
) {
set_status: updateProjectV2ItemFieldValue(input: {
projectId: $project
itemId: $item
fieldId: $status_field
value: {
singleSelectOptionId: $status_value
}
}) {
projectV2Item {
id
}
}
set_date_posted: updateProjectV2ItemFieldValue(input: {
projectId: $project
itemId: $item
fieldId: $date_field
value: {
date: $date_value
}
}) {
projectV2Item {
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
Sets the value of the Status
field to Todo
. Sets the value of the Date posted
field.
# This workflow runs whenever a pull request in the repository is marked as "ready for review".
name: Add PR to project
on:
pull_request:
types:
- ready_for_review
jobs:
track_pr:
runs-on: ubuntu-latest
steps:
# Sets environment variables for this step.
#
# If you are using a personal access token, replace `YOUR_TOKEN` with the name of the secret that contains your personal access token.
#
# 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.
- name: Get project data
env:
GH_TOKEN: ${{ secrets.YOUR_TOKEN }}
ORGANIZATION: YOUR_ORGANIZATION
PROJECT_NUMBER: YOUR_PROJECT_NUMBER
# Uses [GitHub CLI](https://cli.github.com/manual/) to query the API for the ID of the project and return the name and ID of the first 20 fields in the project. `fields` returns a union and the query uses inline fragments (`... on`) to return information about any `ProjectV2Field` and `ProjectV2SingleSelectField` fields. The response is stored in a file called `project_data.json`.
run: |
gh api graphql -f query='
query($org: String!, $number: Int!) {
organization(login: $org){
projectV2(number: $number) {
id
fields(first:20) {
nodes {
... on ProjectV2Field {
id
name
}
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
}
}
}' -f org=$ORGANIZATION -F number=$PROJECT_NUMBER > 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:
#
# - To get the ID of a field called `Team`, add `echo 'TEAM_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Team") | .id' project_data.json) >> $GITHUB_ENV`.
# - To get the ID of an option called `Octoteam` for the `Team` single select field, add `echo 'OCTOTEAM_OPTION_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Team") |.options[] | select(.name=="Octoteam") |.id' project_data.json) >> $GITHUB_ENV`.
#
# **Note:** This workflow assumes that you have a project with a single select field called "Status" that includes an option called "Todo" and a date field called "Date posted". You must modify this section to match the fields that are present in your table.
echo 'PROJECT_ID='$(jq '.data.organization.projectV2.id' project_data.json) >> $GITHUB_ENV
echo 'DATE_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Date posted") | .id' project_data.json) >> $GITHUB_ENV
echo 'STATUS_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Status") | .id' project_data.json) >> $GITHUB_ENV
echo 'TODO_OPTION_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Status") | .options[] | select(.name=="Todo") |.id' project_data.json) >> $GITHUB_ENV
# Sets environment variables for this step. Replace `YOUR_TOKEN` with the name of the secret that contains your personal access token.
- name: Add PR to project
env:
GH_TOKEN: ${{ secrets.YOUR_TOKEN }}
PR_ID: ${{ github.event.pull_request.node_id }}
# Uses [GitHub CLI](https://cli.github.com/manual/) 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.
run: |
item_id="$( gh api graphql -f query='
mutation($project:ID!, $pr:ID!) {
addProjectV2ItemById(input: {projectId: $project, contentId: $pr}) {
item {
id
}
}
}' -f project=$PROJECT_ID -f pr=$PR_ID --jq '.data.addProjectV2ItemById.item.id')"
# Stores the ID of the created item as an environment variable.
echo 'ITEM_ID='$item_id >> $GITHUB_ENV
# Saves the current date as an environment variable in `yyyy-mm-dd` format.
- name: Get date
run: echo "DATE=$(date +"%Y-%m-%d")" >> $GITHUB_ENV
# Sets environment variables for this step. Replace `YOUR_TOKEN` with the name of the secret that contains your personal access token.
- name: Set fields
env:
GH_TOKEN: ${{ secrets.YOUR_TOKEN }}
# Sets the value of the `Status` field to `Todo`. Sets the value of the `Date posted` field.
run: |
gh api graphql -f query='
mutation (
$project: ID!
$item: ID!
$status_field: ID!
$status_value: String!
$date_field: ID!
$date_value: Date!
) {
set_status: updateProjectV2ItemFieldValue(input: {
projectId: $project
itemId: $item
fieldId: $status_field
value: {
singleSelectOptionId: $status_value
}
}) {
projectV2Item {
id
}
}
set_date_posted: updateProjectV2ItemFieldValue(input: {
projectId: $project
itemId: $item
fieldId: $date_field
value: {
date: $date_value
}
}) {
projectV2Item {
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