Preprocessing - Categorical Variables #7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Add Issues to project | |
# This workflow runs whenever a issue in the repository is marked as "opened". | |
on: | |
issues: | |
types: | |
- opened | |
jobs: | |
track_issue: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Generate token | |
id: generate_token | |
uses: actions/create-github-app-token@v1.7.0 | |
with: | |
app-id: ${{ secrets.DS_PROJECT_BOARD_APP_ID }} | |
private-key: ${{ secrets.DS_PROJECT_BOARD_APP_PEM }} | |
# Sets environment variables for this step. | |
- name: Get project data | |
env: | |
GH_TOKEN: ${{ steps.generate_token.outputs.token }} | |
ORGANIZATION: neuefische | |
PROJECT_NUMBER: 7 | |
# 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`. | |
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. | |
echo 'PROJECT_ID='$(jq '.data.organization.projectV2.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 (Ready)") |.id' project_data.json) >> $GITHUB_ENV | |
# Sets environment variables for this step. `GH_TOKEN` is the token generated in the first step. ISSUE_ID` is the ID of the issue that triggered this workflow. | |
- name: Add Issue to project | |
env: | |
GH_TOKEN: ${{ steps.generate_token.outputs.token }} | |
ISSUE_ID: ${{ github.event.issue.node_id }} | |
# Uses GitHub CLI and the API to add the issue 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!, $issue:ID!) { | |
addProjectV2ItemById(input: {projectId: $project, contentId: $issue}) { | |
item { | |
id | |
} | |
} | |
}' -f project=$PROJECT_ID -f issue=$ISSUE_ID --jq '.data.addProjectV2ItemById.item.id')" | |
# Stores the ID of the created item as an environment variable. | |
echo 'ITEM_ID='$item_id >> $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 `In Progress`. | |
run: | | |
gh api graphql -f query=' | |
mutation ( | |
$project: ID! | |
$item: ID! | |
$status_field: ID! | |
$status_value: String! | |
) { | |
set_status: updateProjectV2ItemFieldValue(input: { | |
projectId: $project | |
itemId: $item | |
fieldId: $status_field | |
value: { | |
singleSelectOptionId: $status_value | |
} | |
}) { | |
projectV2Item { | |
id | |
} | |
} | |
}' -f project=$PROJECT_ID -f item=$ITEM_ID -f status_field=$STATUS_FIELD_ID -f status_value=${{ env.TODO_OPTION_ID }} --silent |