|
1 | | -name: Project Sync |
| 1 | +name: Project Plan Sync |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | push: |
5 | | - branches: [ main, '**' ] |
6 | | - pull_request: |
7 | | - types: [opened, reopened, synchronize] |
8 | | - issues: |
9 | | - types: [opened] |
| 5 | + branches: [ main ] |
| 6 | + workflow_dispatch: |
10 | 7 |
|
11 | 8 | permissions: |
12 | 9 | contents: read |
13 | 10 | issues: write |
14 | 11 | pull-requests: read |
15 | 12 |
|
16 | 13 | env: |
17 | | - # Set these to match your environment. |
18 | | - GITHUB_USER: pynip |
19 | | - # Replace with the numeric project number for "Virtualization Studio ARM macOS" |
20 | | - PROJECT_NUMBER: 3 # Project number extracted from https://github.com/users/pynip/projects/3 |
21 | | - # Status names expected in the project's single-select Status field |
| 14 | + PROJECT_NUMBER: 3 |
22 | 15 | STATUS_TODO_NAME: "To do" |
23 | 16 | STATUS_INPROGRESS_NAME: "In Progress" |
24 | 17 | STATUS_DONE_NAME: "Done" |
25 | 18 |
|
26 | 19 | jobs: |
27 | | - add-new-issues: |
28 | | - name: Add newly opened issues to project (To do) |
29 | | - if: github.event_name == 'issues' |
| 20 | + sync-plan: |
30 | 21 | runs-on: ubuntu-latest |
31 | 22 | steps: |
32 | | - - name: Add to project |
33 | | - |
34 | | - with: |
35 | | - project-url: https://github.com/users/${{ env.GITHUB_USER }}/projects/${{ env.PROJECT_NUMBER }} |
36 | | - github-token: ${{ secrets.GITHUB_TOKEN }} |
37 | | - |
38 | | - sync-from-push: |
39 | | - name: Sync referenced issues from push commits |
40 | | - if: github.event_name == 'push' |
41 | | - runs-on: ubuntu-latest |
42 | | - steps: |
43 | | - - name: Extract issue numbers from commit messages |
44 | | - id: extract |
45 | | - run: | |
46 | | - issues=$(echo "${{ toJson(github.event.commits) }}" | jq -r '.[].message' | grep -Eo '#[0-9]+' | tr -d '#' | sort -u || true) |
47 | | - if [ -n "$issues" ]; then |
48 | | - echo "issues=$issues" >> $GITHUB_OUTPUT |
49 | | - else |
50 | | - echo "issues=" >> $GITHUB_OUTPUT |
51 | | - fi |
52 | | - - name: Add referenced issues to project (if any) |
53 | | - if: steps.extract.outputs.issues != '' |
54 | | - |
55 | | - with: |
56 | | - project-url: https://github.com/users/${{ env.GITHUB_USER }}/projects/${{ env.PROJECT_NUMBER }} |
57 | | - github-token: ${{ secrets.GITHUB_TOKEN }} |
58 | | - content-id: ${{ steps.extract.outputs.issues }} |
59 | | - continue-on-error: true |
60 | | - - name: Prepare status update script |
61 | | - if: steps.extract.outputs.issues != '' |
62 | | - uses: actions/setup-node@v4 |
63 | | - with: |
64 | | - node-version: '20' |
65 | | - - name: Update status for closed issues (Done) |
66 | | - if: steps.extract.outputs.issues != '' |
| 23 | + - uses: actions/checkout@v4 |
| 24 | + - name: Install deps (gh already available) |
67 | 25 | run: | |
68 | | - sudo apt-get update && sudo apt-get install -y jq |
69 | | - curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg |
70 | | - sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg |
71 | | - echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null |
72 | | - sudo apt-get update && sudo apt-get install gh -y |
73 | | - gh auth status || gh auth login --with-token <<<"${GITHUB_TOKEN}" |
74 | | - LOGIN=${{ env.GITHUB_USER }} |
75 | | - PROJECT_NUM=${{ env.PROJECT_NUMBER }} |
76 | | - ISSUES="${{ steps.extract.outputs.issues }}" |
77 | | - # Fetch project + status field metadata |
78 | | - projectQuery='query($login:String!,$number:Int!){ user(login:$login){ projectV2(number:$number){ id fields(first:50){ nodes { __typename ... on ProjectV2SingleSelectField { id name options { id name } } } } items(first:200){ nodes { id content { __typename ... on Issue { number id state } } } } } } }' |
79 | | - resp=$(gh api graphql -f query="$projectQuery" -F login="$LOGIN" -F number=$PROJECT_NUM) |
80 | | - projectId=$(echo "$resp" | jq -r '.data.user.projectV2.id') |
81 | | - statusFieldId=$(echo "$resp" | jq -r '.data.user.projectV2.fields.nodes[] | select(.name=="Status") | .id') |
82 | | - optDone=$(echo "$resp" | jq -r '.data.user.projectV2.fields.nodes[] | select(.name=="Status") | .options[] | select(.name=="'"${STATUS_DONE_NAME}"'") | .id') |
83 | | - optInProg=$(echo "$resp" | jq -r '.data.user.projectV2.fields.nodes[] | select(.name=="Status") | .options[] | select(.name=="'"${STATUS_INPROGRESS_NAME}"'") | .id') |
84 | | - for issue in $ISSUES; do |
85 | | - state=$(gh api repos/${{ github.repository }}/issues/$issue --jq '.state') |
86 | | - # Find item id in project |
87 | | - itemId=$(echo "$resp" | jq -r --arg n "$issue" '.data.user.projectV2.items.nodes[] | select(.content.number==($n|tonumber)) | .id') |
88 | | - steps: |
89 | | - - name: Sync issues via github-script |
90 | | - uses: actions/github-script@v7 |
91 | | - with: |
92 | | - script: | |
93 | | - const login = process.env.GITHUB_USER; |
94 | | - const projectNumber = parseInt(process.env.PROJECT_NUMBER, 10); |
95 | | - const statusTodo = process.env.STATUS_TODO_NAME; |
96 | | - const statusInProg = process.env.STATUS_INPROGRESS_NAME; |
97 | | - const statusDone = process.env.STATUS_DONE_NAME; |
98 | | - const commits = context.payload.commits || []; |
99 | | - const issueSet = new Set(); |
100 | | - for (const c of commits) { |
101 | | - const matches = (c.message.match(/#[0-9]+/g) || []); |
102 | | - for (const m of matches) issueSet.add(parseInt(m.substring(1),10)); |
103 | | - } |
104 | | - if (issueSet.size === 0) { core.info('No referenced issues in commit messages.'); return; } |
105 | | - core.info(`Referenced issues: ${[...issueSet].join(', ')}`); |
106 | | - const qProject = `query($login:String!,$number:Int!){ user(login:$login){ projectV2(number:$number){ id fields(first:50){ nodes { ... on ProjectV2SingleSelectField { id name options { id name } } } } items(first:200){ nodes { id content { ... on Issue { id number state } } } } } } }`; |
107 | | - const projectResp = await github.graphql(qProject, { login, number: projectNumber }); |
108 | | - const project = projectResp.user.projectV2; if(!project) { core.setFailed('Project not found'); return; } |
109 | | - const projectId = project.id; |
110 | | - // Find Status field |
111 | | - const statusField = project.fields.nodes.find(f=>f && f.name==='Status'); |
112 | | - if(!statusField) { core.setFailed('Status field not found'); return; } |
113 | | - const optMap = {}; if(statusField.options){ for(const o of statusField.options){ optMap[o.name]=o.id; } } |
114 | | - const ensureIssueNodeId = async (issueNumber)=>{ |
115 | | - const qIssue = `query($owner:String!,$repo:String!,$num:Int!){ repository(owner:$owner,name:$repo){ issue(number:$num){ id number state } } }`; |
116 | | - const ir = await github.graphql(qIssue,{ owner: context.repo.owner, repo: context.repo.repo, num: issueNumber }); |
117 | | - return ir.repository.issue; |
118 | | - }; |
119 | | - const existingItems = new Map(); |
120 | | - for(const item of project.items.nodes){ if(item.content && item.content.number) existingItems.set(item.content.number, item); } |
121 | | - const mAdd = `mutation($project:ID!,$content:ID!){ addProjectV2ItemById(input:{projectId:$project contentId:$content}){ item { id } } }`; |
122 | | - const mUpdate = `mutation($project:ID!,$item:ID!,$field:ID!,$option:String!){ updateProjectV2ItemFieldValue(input:{projectId:$project,itemId:$item,fieldId:$field,value:{singleSelectOptionId:$option}}){ projectV2Item { id } } }`; |
123 | | - for(const issueNumber of issueSet){ |
124 | | - let item = existingItems.get(issueNumber); |
125 | | - let issueNode = null; |
126 | | - if(!item){ |
127 | | - issueNode = await ensureIssueNodeId(issueNumber); |
128 | | - if(!issueNode){ core.warning(`Issue #${issueNumber} not found`); continue; } |
129 | | - try { await github.graphql(mAdd,{ project: projectId, content: issueNode.id }); core.info(`Added issue #${issueNumber} to project`); } catch(e){ core.warning(`Add failed for #${issueNumber}: ${e.message}`); } |
130 | | - } else { |
131 | | - // Retrieve issue state |
132 | | - issueNode = { id: item.content.id, number: item.content.number, state: item.content.state }; |
133 | | - } |
134 | | - if(!issueNode){ issueNode = await ensureIssueNodeId(issueNumber); } |
135 | | - const desired = issueNode.state === 'CLOSED' ? statusDone : statusInProg; |
136 | | - if(!optMap[desired]) { core.warning(`Desired status ${desired} option missing`); continue; } |
137 | | - // Need item id; refresh to capture newly added items when necessary |
138 | | - if(!item){ |
139 | | - const refresh = await github.graphql(qProject,{ login, number: projectNumber }); |
140 | | - for(const it of refresh.user.projectV2.items.nodes){ if(it.content && it.content.number) existingItems.set(it.content.number, it); } |
141 | | - item = existingItems.get(issueNumber); |
142 | | - } |
143 | | - if(item){ |
144 | | - try { await github.graphql(mUpdate,{ project: projectId, item: item.id, field: statusField.id, option: optMap[desired] }); core.info(`Set #${issueNumber} -> ${desired}`); } catch(e){ core.warning(`Status update failed for #${issueNumber}: ${e.message}`); } |
145 | | - } |
146 | | - } |
147 | | - core.info('Sync complete.'); |
148 | | - resp=$(gh api graphql -f query="$projectQuery" -F login="$LOGIN" -F number=$PROJECT_NUM) |
149 | | - itemId=$(echo "$resp" | jq -r --arg n "$issue" '.data.user.projectV2.items.nodes[] | select(.content.number==($n|tonumber)) | .id') |
150 | | - fi |
151 | | - gh api graphql -f query="$updateMutation" -f project=$projectId -f item=$itemId -f field=$statusFieldId -f option=$optInProg >/dev/null || true |
152 | | - done |
153 | | -
|
154 | | - finalize-merged: |
155 | | - name: Mark closed issues Done after merge |
156 | | - if: github.event_name == 'pull_request' && github.event.pull_request.merged == true |
157 | | - runs-on: ubuntu-latest |
158 | | - steps: |
159 | | - - name: Extract issues from PR body/title |
160 | | - id: issues |
161 | | - run: | |
162 | | - body="${{ github.event.pull_request.body }}\n${{ github.event.pull_request.title }}" |
163 | | - issues=$(echo "$body" | grep -Eo '#[0-9]+' | tr -d '#' | sort -u || true) |
164 | | - echo "issues=$issues" >> $GITHUB_OUTPUT |
165 | | - - name: Install gh + jq |
166 | | - if: steps.issues.outputs.issues != '' |
167 | | - run: | |
168 | | - sudo apt-get update && sudo apt-get install -y jq |
169 | | - curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg |
170 | | - sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg |
171 | | - echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null |
172 | | - sudo apt-get update && sudo apt-get install gh -y |
173 | | - - name: Set Status=Done |
174 | | - if: steps.issues.outputs.issues != '' |
| 26 | + pip install pyyaml || true |
| 27 | + - name: Sync project plan |
175 | 28 | env: |
176 | | - ISSUES: ${{ steps.issues.outputs.issues }} |
177 | | - run: | |
178 | | - gh auth status || gh auth login --with-token <<<"${GITHUB_TOKEN}" |
179 | | - LOGIN=${{ env.GITHUB_USER }} |
180 | | - PROJECT_NUM=${{ env.PROJECT_NUMBER }} |
181 | | - projectQuery='query($login:String!,$number:Int!){ user(login:$login){ projectV2(number:$number){ id fields(first:50){ nodes { __typename ... on ProjectV2SingleSelectField { id name options { id name } } } } items(first:200){ nodes { id content { __typename ... on Issue { number id state } } } } } } }' |
182 | | - resp=$(gh api graphql -f query="$projectQuery" -F login="$LOGIN" -F number=$PROJECT_NUM) |
183 | | - projectId=$(echo "$resp" | jq -r '.data.user.projectV2.id') |
184 | | - statusFieldId=$(echo "$resp" | jq -r '.data.user.projectV2.fields.nodes[] | select(.name=="Status") | .id') |
185 | | - optDone=$(echo "$resp" | jq -r '.data.user.projectV2.fields.nodes[] | select(.name=="Status") | .options[] | select(.name=="'"${STATUS_DONE_NAME}"'") | .id') |
186 | | - updateMutation='mutation($project:ID!,$item:ID!,$field:ID!,$option: String!){ updateProjectV2ItemFieldValue(input:{projectId:$project,itemId:$item,fieldId:$field,value:{singleSelectOptionId:$option}}){ projectV2Item { id } } }' |
187 | | - for issue in $ISSUES; do |
188 | | - itemId=$(echo "$resp" | jq -r --arg n "$issue" '.data.user.projectV2.items.nodes[] | select(.content.number==($n|tonumber)) | .id') |
189 | | - if [ -n "$itemId" ]; then |
190 | | - gh api graphql -f query="$updateMutation" -f project=$projectId -f item=$itemId -f field=$statusFieldId -f option=$optDone >/dev/null || true |
191 | | - fi |
192 | | - done |
193 | | -
|
194 | | - notes: |
195 | | - name: Guidance (no-op) |
196 | | - runs-on: ubuntu-latest |
197 | | - steps: |
198 | | - - name: Display guidance |
| 29 | + GITHUB_REPOSITORY: ${{ github.repository }} |
| 30 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 31 | + PROJECT_OWNER: ${{ github.repository_owner }} |
| 32 | + PROJECT_NUMBER: ${{ env.PROJECT_NUMBER }} |
| 33 | + STATUS_TODO_NAME: ${{ env.STATUS_TODO_NAME }} |
| 34 | + STATUS_INPROGRESS_NAME: ${{ env.STATUS_INPROGRESS_NAME }} |
| 35 | + STATUS_DONE_NAME: ${{ env.STATUS_DONE_NAME }} |
199 | 36 | run: | |
200 | | - echo "Project Sync workflow installed. IMPORTANT:" |
201 | | - echo "1. Update PROJECT_NUMBER in the workflow to match your project's number." |
202 | | - echo "2. Ensure the project has a 'Status' single-select field with options: To do, In Progress, Done." |
203 | | - echo "3. (Optional) Configure built-in project workflows: On item added -> Status=To do; On item closed -> Status=Done." |
| 37 | + python3 scripts/sync_project_plan.py |
0 commit comments