Skip to content

Instantly share code, notes, and snippets.

@arilivigni
Created April 25, 2023 12:47
Show Gist options
  • Save arilivigni/f68108be2474947b846b99dde6a920d5 to your computer and use it in GitHub Desktop.
Save arilivigni/f68108be2474947b846b99dde6a920d5 to your computer and use it in GitHub Desktop.
GitHub Action that uses the gh project cli extension to get info about a project from the command line
name: Add a new GitHub Project card linked to a GitHub issue to the specified project column
on:
issues:
types: [ opened, labeled, unlabeled ]
env:
GITHUB_ORG: ${{ github.repository_owner }}
GITHUB_PROJECT_NAME: "Continuous-Devops-GitHub-App-MasterBlaster"
GITHUB_EVENT: ${{ toJson(github.event) }}
GITHUB_ISSUE: ${{ toJson(github.event.issue) }}
GITHUB_ISSUE_URL: ${{ github.event.issue.html_url }}
GITHUB_ISSUE_NUM: ${{ github.event.issue.number }}
GITHUB_LABELS: ${{ toJson(github.event.issue.labels) }}
jobs:
github-actions-automate-projects:
runs-on: ubuntu-latest
steps:
- name: "Install `gh` CLI extension for `projects`"
run: |
gh extension install github/gh-projects
env:
GITHUB_TOKEN: ${{ secrets.GHPROJECT_TOKEN }}
- name: "Get project and field details, Number, ID, Field=Status, and Option IDs"
run: |
GITHUB_PROJECT_NUM=$(gh projects list --org=$GITHUB_ORG --format=json| jq ".projects[] | select(.title|test(\"$GITHUB_PROJECT_NAME\")) | .number")
GITHUB_PROJECT_ID=$(gh projects list --org=$GITHUB_ORG --format=json| jq -r '.projects[] | select(.title|test("Continuous.")) | .id')
GITHUB_ITEM_ID=$(gh projects item-list $GITHUB_PROJECT_NUM --org=$GITHUB_ORG --format=json | jq -r ".items[] | select(.content.number == $GITHUB_ISSUE_NUM) | .id")
GITHUB_STATUS_ID=$(gh projects field-list $GITHUB_PROJECT_NUM --org=$GITHUB_ORG --format=json | jq -r '.fields[] | select(.name|test("Status")) | .id')
GITHUB_BACKLOG_ID=$(gh projects field-list $GITHUB_PROJECT_NUM --org=$GITHUB_ORG --format=json | jq -r '.fields[] | select(.name|test("Status")) | .options[] | select(.name|test(".Backlog")) | .id')
GITHUB_NEW_ID=$(gh projects field-list $GITHUB_PROJECT_NUM --org=$GITHUB_ORG --format=json | jq -r '.fields[] | select(.name|test("Status")) | .options[] | select(.name|test(".New")) | .id')
echo "GITHUB_PROJECT_NUM=$GITHUB_PROJECT_NUM" >> $GITHUB_ENV
echo "GITHUB_PROJECT_ID=$GITHUB_PROJECT_ID" >> $GITHUB_ENV
echo "GITHUB_ITEM_ID=$GITHUB_ITEM_ID" >> $GITHUB_ENV
echo "GITHUB_STATUS_ID=$GITHUB_STATUS_ID" >> $GITHUB_ENV
echo "GITHUB_BACKLOG_ID=$GITHUB_BACKLOG_ID" >> $GITHUB_ENV
echo "GITHUB_NEW_ID=$GITHUB_NEW_ID" >> $GITHUB_ENV
env:
GITHUB_TOKEN: ${{ secrets.GHPROJECT_TOKEN }}
- name: Echo "GitHub Project env variables"
run: |
echo "GitHub organization: $GITHUB_ORG"
echo "GitHub Project Name: $GITHUB_PROJECT_NAME"
echo "GitHub Project Number: $GITHUB_PROJECT_NUM"
echo "GitHub Project id: $GITHUB_PROJECT_ID"
echo "GitHub item id: $GITHUB_ITEM_ID"
echo "GitHub Status id: $GITHUB_STATUS_ID"
echo "GitHub Backlog Status id: $GITHUB_BACKLOG_ID"
echo "GitHub New Status id: $GITHUB_NEW_ID"
echo "GitHub github.event"
echo "$GITHUB_EVENT"
echo "GitHub github.event.issue"
echo "$GITHUB_ISSUE"
echo "GitHub github.event.issue.labels on issue $GITHUB_ISSUE_URL"
echo "$GITHUB_LABELS"
echo "GitHub github.event.issue.number is $GITHUB_ISSUE_NUM"
- name: "Assign issue in project ${{ env.GITHUB_PROJECT }} to Backlog column"
if: "contains(github.event.issue.labels.*.name, 'backlog')"
run: |
gh projects item-edit --id=$GITHUB_ITEM_ID --project-id=$GITHUB_PROJECT_ID --single-select-option-id=$GITHUB_BACKLOG_ID --field-id=$GITHUB_STATUS_ID
env:
GITHUB_TOKEN: ${{ secrets.GHPROJECT_TOKEN }}
- name: "Assign issue in project ${{ env.GITHUB_PROJECT }} to New column"
if: "!contains(github.event.issue.labels.*.name, 'backlog')"
run: |
gh projects item-edit --id=$GITHUB_ITEM_ID --project-id=$GITHUB_PROJECT_ID --single-select-option-id=$GITHUB_NEW_ID --field-id=$GITHUB_STATUS_ID
env:
GITHUB_TOKEN: ${{ secrets.GHPROJECT_TOKEN }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment