[#199] Get project and column id in update-issue-status.yml #9
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: Update Issue Status and Project | |
on: | |
pull_request: | |
types: [closed] | |
jobs: | |
check-pr-for-issue: | |
runs-on: ubuntu-latest | |
outputs: | |
issue-number: ${{ steps.check-issue.outputs.issue-number }} | |
steps: | |
- name: Get PR Title and Body | |
id: get-pr-data | |
run: | | |
echo "PR Title: ${{ github.event.pull_request.title }}" | |
echo "PR Body: ${{ github.event.pull_request.body }}" | |
echo "::set-output name=pr-title::${{ github.event.pull_request.title }}" | |
echo "::set-output name=pr-body::${{ github.event.pull_request.body }}" | |
- name: Check for Issue Number in PR Title or Body | |
id: check-issue | |
run: | | |
# Look for issue numbers in PR title or body | |
issue_number=$(echo "${{ steps.get-pr-data.outputs.pr-title }} ${{ steps.get-pr-data.outputs.pr-body }}" | grep -oP '#([0-9]+)' | head -n 1 | sed 's/#//') | |
if [[ -z "$issue_number" ]]; then | |
echo "No issue number found." | |
echo "::set-output name=issue-number::none" | |
else | |
echo "Found issue number: $issue_number" | |
echo "::set-output name=issue-number::$issue_number" | |
fi | |
update-status: | |
needs: check-pr-for-issue | |
runs-on: ubuntu-latest | |
if: needs.check-pr-for-issue.outputs.issue-number != 'none' # Only run if issue number is found | |
steps: | |
- name: Update Issue Status to "Done" | |
uses: peter-evans/create-or-update-comment@v2 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
issue-number: ${{ needs.check-pr-for-issue.outputs.issue-number }} | |
body: "Status: Done" | |
- name: Move Issue Card to 'Done' Column in Project | |
run: | | |
# Ensure the issue number is properly assigned | |
issue_number=${{ needs.check-pr-for-issue.outputs.issue-number }} | |
echo "Moving issue #$issue_number to 'Done' column in the project..." | |
# Get the list of project cards associated with the issue | |
project_cards=$(curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-s "https://api.github.com/repos/${{ github.repository }}/projects/columns/cards?issue=$issue_number") | |
# Debug: Print the project cards JSON to check the structure | |
echo "Project Cards: $project_cards" | |
# Check if project cards exist and extract the first card ID | |
project_card_id=$(echo "$project_cards" | jq -r '.[0].id') | |
if [ -z "$project_card_id" ]; then | |
echo "No project card found for issue #$issue_number" | |
exit 1 | |
fi | |
echo "Found project card ID: $project_card_id" | |
# Dynamically get the project ID by listing the projects in the repository | |
project_name="My Project" # Replace this with the name of your project | |
project_id=$(curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-s "https://api.github.com/repos/${{ github.repository }}/projects" | \ | |
jq -r ".[] | select(.name == \"$project_name\") | .id") | |
if [ -z "$project_id" ]; then | |
echo "Project '$project_name' not found in the repository." | |
exit 1 | |
fi | |
echo "Found project ID: $project_id" | |
# Dynamically get the 'Done' column ID by listing the columns in the project | |
done_column_id=$(curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-s "https://api.github.com/repos/${{ github.repository }}/projects/${project_id}/columns" | \ | |
jq -r '.[] | select(.name == "Done") | .id') | |
if [ -z "$done_column_id" ]; then | |
echo "Done column not found. Please ensure the column exists in the project." | |
exit 1 | |
fi | |
echo "Found 'Done' column ID: $done_column_id" | |
# Move the project card to the 'Done' column | |
curl -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-d '{"column_id": "'$done_column_id'"}' \ | |
"https://api.github.com/projects/columns/cards/$project_card_id/moves" | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |