Skip to content

Commit 0293902

Browse files
committed
refactor: use actions/add-to-project instead of raw GraphQL
1 parent 752d1a8 commit 0293902

2 files changed

Lines changed: 14 additions & 56 deletions

File tree

workflows/issue-project-sync/README.md

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
# Issue Project Sync Workflow
22

3-
A reusable composite action that automatically adds issues to [cloudoperators project #9](https://github.com/orgs/cloudoperators/projects/9) when the `backlog` label is applied.
3+
A reusable composite action that automatically adds issues to [cloudoperators project #9](https://github.com/orgs/cloudoperators/projects/9) when the `backlog` label is applied. Uses the official [actions/add-to-project](https://github.com/actions/add-to-project) action under the hood.
44

55
## Usage
66

7-
Create `.github/workflows/issue-project-sync.yml` in your repository:
7+
Create `.github/workflows/issue-project-sync.yaml` in your repository:
88

99
```yaml
1010
name: Issue Project Sync
1111
on:
1212
issues:
1313
types: [labeled]
1414

15+
permissions:
16+
issues: read
17+
1518
jobs:
1619
sync:
1720
if: github.event.label.name == 'backlog'
@@ -26,7 +29,7 @@ jobs:
2629
2730
1. Triggered when any label is added to an issue
2831
2. Filters to only run when the `backlog` label is applied
29-
3. Uses the GitHub GraphQL API to add the issue to the organization project
32+
3. Uses `actions/add-to-project` to add the issue to the organization project
3033

3134
## Prerequisites
3235

@@ -61,14 +64,4 @@ gh label create "backlog" --color "0e8a16" --description "Ready for sprint plann
6164
| Input | Required | Default | Description |
6265
|---|---|---|---|
6366
| `GH_TOKEN` | **Yes** | — | GitHub token with `project` scope (org-level secret) |
64-
| `PROJECT_NUMBER` | No | `9` | The GitHub project number to add issues to |
65-
| `ORG` | No | `cloudoperators` | The GitHub organization owning the project |
66-
67-
## How it works
68-
69-
The action uses the GitHub GraphQL API to:
70-
71-
1. Look up the project node ID from the org and project number
72-
2. Add the issue (by its node ID) to the project using `addProjectV2ItemById`
73-
74-
This replaces the built-in GitHub Project "Auto-add" UI workflow with a code-based approach that can be version-controlled and applied consistently across repositories.
67+
| `PROJECT_URL` | No | `https://github.com/orgs/cloudoperators/projects/9` | Full URL of the GitHub project |

workflows/issue-project-sync/action.yaml

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,16 @@ inputs:
55
GH_TOKEN:
66
description: 'GitHub token with project write scope. Must be an org-level secret with project and repo permissions.'
77
required: true
8-
PROJECT_NUMBER:
9-
description: 'The GitHub project number to add issues to'
8+
PROJECT_URL:
9+
description: 'The full URL of the GitHub project to add issues to'
1010
required: false
11-
default: '9'
12-
ORG:
13-
description: 'The GitHub organization owning the project'
14-
required: false
15-
default: 'cloudoperators'
11+
default: 'https://github.com/orgs/cloudoperators/projects/9'
1612

1713
runs:
1814
using: "composite"
1915
steps:
2016
- name: Add issue to project
21-
shell: bash
22-
env:
23-
GH_TOKEN: ${{ inputs.GH_TOKEN }}
24-
ISSUE_NODE_ID: ${{ github.event.issue.node_id }}
25-
PROJECT_NUMBER: ${{ inputs.PROJECT_NUMBER }}
26-
ORG: ${{ inputs.ORG }}
27-
run: |
28-
# Get the project node ID
29-
PROJECT_ID=$(gh api graphql -f query='
30-
query($org: String!, $number: Int!) {
31-
organization(login: $org) {
32-
projectV2(number: $number) {
33-
id
34-
}
35-
}
36-
}' -f org="$ORG" -F number="$PROJECT_NUMBER" --jq '.data.organization.projectV2.id')
37-
38-
if [ -z "$PROJECT_ID" ] || [ "$PROJECT_ID" = "null" ]; then
39-
echo "ERROR: Could not find project #${PROJECT_NUMBER} in org ${ORG}"
40-
exit 1
41-
fi
42-
43-
echo "Adding issue to project ${PROJECT_ID}..."
44-
45-
# Add the issue to the project
46-
gh api graphql -f query='
47-
mutation($project: ID!, $issue: ID!) {
48-
addProjectV2ItemById(input: {projectId: $project, contentId: $issue}) {
49-
item {
50-
id
51-
}
52-
}
53-
}' -f project="$PROJECT_ID" -f issue="$ISSUE_NODE_ID"
54-
55-
echo "✅ Issue added to project #${PROJECT_NUMBER} successfully."
17+
uses: actions/add-to-project@v1.0.2
18+
with:
19+
project-url: ${{ inputs.PROJECT_URL }}
20+
github-token: ${{ inputs.GH_TOKEN }}

0 commit comments

Comments
 (0)