|
| 1 | +name: Live Project Sync |
| 2 | +on: |
| 3 | + issues: |
| 4 | + types: [opened, edited, reopened, closed] |
| 5 | + pull_request: |
| 6 | + types: [opened, edited, reopened, closed] |
| 7 | + |
| 8 | +jobs: |
| 9 | + online_sync: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + steps: |
| 12 | + - name: Propagate Item to Project Database |
| 13 | + env: |
| 14 | + GH_TOKEN: ${{ secrets.GLOBAL_PROJECT_TOKEN }} |
| 15 | + PROJECT_URL: ${{ vars.GLOBAL_PROJECT_URL }} |
| 16 | + ITEM_ID: ${{ github.event.issue.node_id || github.event.pull_request.node_id }} |
| 17 | + STATE: ${{ github.event.issue.state || github.event.pull_request.state }} |
| 18 | + REPO_NAME: ${{ github.event.repository.name }} |
| 19 | + GH_API_VERSION: "2026-03-10" |
| 20 | + PROJECT_ID: "PVT_kwHOAB7H7M4BZk7B" |
| 21 | + UPDATEAT_FIELD_ID: "PVTF_lAHOAB7H7M4BZk7BzhUmsLs" |
| 22 | + REPOMARK_FIELD_ID: "PVTSSF_lAHOAB7H7M4BZk7BzhUjAmA" |
| 23 | + run: | |
| 24 | + # 1. Extract the project number from your global variable URL (needed only for item-add) |
| 25 | + PROJECT_NUMBER="${PROJECT_URL##*/}" |
| 26 | + echo "🔄 Linking item to Project board #$PROJECT_NUMBER..." |
| 27 | +
|
| 28 | + # 2. Add the item using the native GitHub CLI project system |
| 29 | + ITEM_URL="${{ github.event.issue.html_url || github.event.pull_request.html_url }}" |
| 30 | + PRJ_ITEM_ID=$(gh project item-add "$PROJECT_NUMBER" --owner "@me" --url "$ITEM_URL" --format json --jq '.id') |
| 31 | + echo "✅ Item linked successfully. Project Item ID: $PRJ_ITEM_ID" |
| 32 | +
|
| 33 | + # 3. Handle the close event via refined ID extraction |
| 34 | + if [ "$STATE" = "closed" ]; then |
| 35 | + echo "🏁 Item is closed. Resolving structural field IDs..." |
| 36 | + |
| 37 | + # Fetch the field layout data structure using number for listing |
| 38 | + FIELD_DATA=$(gh project field-list "$PROJECT_NUMBER" --owner "@me" --format json) |
| 39 | + |
| 40 | + # Parse the status field and target option values |
| 41 | + STATUS_FIELD_ID=$(echo "$FIELD_DATA" | jq -r '.fields[] | select(.name == "Status") | .id') |
| 42 | + DONE_OPTION_ID=$(echo "$FIELD_DATA" | jq -r --arg f_id "$STATUS_FIELD_ID" '.fields[] | select(.id == $f_id) | .options[] | select(.name == "Done") | .id') |
| 43 | +
|
| 44 | + echo "DEBUG - Status Field ID: $STATUS_FIELD_ID" |
| 45 | + echo "DEBUG - Done Option ID: $DONE_OPTION_ID" |
| 46 | +
|
| 47 | + # Failsafe validation |
| 48 | + if [ "$STATUS_FIELD_ID" = "null" ] || [ "$DONE_OPTION_ID" = "null" ]; then |
| 49 | + echo "❌ Error: Could not resolve all required database metadata IDs." |
| 50 | + exit 1 |
| 51 | + fi |
| 52 | +
|
| 53 | + echo "Moving item to Done column..." |
| 54 | + gh project item-edit --id "$PRJ_ITEM_ID" --project-id "${{ env.PROJECT_ID }}" --field-id "$STATUS_FIELD_ID" --single-select-option-id "$DONE_OPTION_ID" |
| 55 | + fi |
| 56 | +
|
| 57 | + # 4. Format the real update timestamp from the GitHub event context |
| 58 | + RAW_TIME="${{ github.event.issue.updated_at || github.event.pull_request.updated_at }}" |
| 59 | + ISO_STRING=$(date -d "$RAW_TIME" +"%Y-%m-%d %H:%M") |
| 60 | + echo "⏰ Formatted issue timestamp: $ISO_STRING" |
| 61 | +
|
| 62 | + # 5. Write the timestamp string to your custom field using the --text parameter |
| 63 | + echo "Writing timestamp to project..." |
| 64 | + gh project item-edit --id "$PRJ_ITEM_ID" --project-id "${{ env.PROJECT_ID }}" --field-id "${{ env.UPDATEAT_FIELD_ID }}" --text "$ISO_STRING" |
| 65 | +
|
| 66 | + # 6. Determine the predefined Option ID for RepoMark based on the repository name |
| 67 | + echo "📦 Source repository name: $REPO_NAME" |
| 68 | + |
| 69 | + # Condition to assign a static option ID based on the repository name |
| 70 | + REPOMARK_OPTION_ID="5f088986" |
| 71 | +
|
| 72 | + # 7. Write the selected option value to the RepoMark field |
| 73 | + echo "Updating RepoMark field..." |
| 74 | + gh project item-edit --id "$PRJ_ITEM_ID" --project-id "${{ env.PROJECT_ID }}" --field-id "${{ env.REPOMARK_FIELD_ID }}" --single-select-option-id "$REPOMARK_OPTION_ID" |
0 commit comments