-
Notifications
You must be signed in to change notification settings - Fork 0
186 lines (155 loc) · 6.55 KB
/
Copy pathmove-linked-issue.yml
File metadata and controls
186 lines (155 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
name: Move Linked Issues to Project Column
on:
workflow_call:
inputs:
pr_number:
required: true
type: number
project_column_name:
required: true
type: string
project_name_prefix:
required: true
type: string
secrets:
gh_token:
required: true
jobs:
move-linked-issues:
runs-on: ubuntu-latest
steps:
- name: Checkout the repository
uses: actions/checkout@v2
- name: Install GitHub CLI
run: |
curl -sSL https://github.com/cli/cli/releases/latest/download/gh_$(curl -sSL https://api.github.com/repos/cli/cli/releases/latest | jq -r .tag_name | sed 's/^v//')_linux_amd64.tar.gz -o gh.tar.gz
tar -xzf gh.tar.gz
sudo cp gh_*/bin/gh /usr/local/bin/
gh --version
- name: Authenticate gh CLI
run: gh auth status
env:
GH_TOKEN: ${{ secrets.gh_token }}
- name: Get linked issue numbers
id: get_issues
run: |
PR_ID=$(gh pr view ${{ inputs.pr_number }} --json id -q .id)
QUERY='query($id: ID!) { node(id: $id) { ... on PullRequest { closingIssuesReferences(first: 10) { nodes { number id } } } } }'
gh api graphql -f id="$PR_ID" -f query="$QUERY" > pr_issues.json
jq -r '.data.node.closingIssuesReferences.nodes[] | "\(.number),\(.id)"' pr_issues.json > issues.csv
echo "issue_list<<EOF" >> $GITHUB_OUTPUT
cat issues.csv >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{ secrets.gh_token }}
- name: Find the latest matching project v2
id: project
run: |
PREFIX="${{ inputs.project_name_prefix }}"
gh api graphql -f query='{
organization(login: "TransactionProcessing") {
projectsV2(first: 100) {
nodes {
id title url createdAt
}
}
}
}' > all_projects.json
jq --arg prefix "$PREFIX" '.data.organization.projectsV2.nodes | map(select(.title | startswith($prefix))) | sort_by(.createdAt) | reverse | .[0] | { title, id }' all_projects.json > latest_project.json
echo "project_id=$(jq -r '.id' latest_project.json)" >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{ secrets.gh_token }}
- name: Get "Status" field ID and option ID
id: status_field
run: |
PROJECT_ID=${{ steps.project.outputs.project_id }}
FIELD_NAME="Status"
STATUS_OPTION_NAME="${{ inputs.project_column_name }}"
gh api graphql -f query='query($projectId: ID!) {
node(id: $projectId) {
... on ProjectV2 {
fields(first: 100) {
nodes {
... on ProjectV2SingleSelectField {
id name options { id name }
}
}
}
}
}
}' -f projectId="$PROJECT_ID" > fields.json
FIELD_ID=$(jq -r '.data.node.fields.nodes[] | select(.name == "'$FIELD_NAME'") | .id' fields.json)
OPTION_ID=$(jq -r '.data.node.fields.nodes[] | select(.name == "'$FIELD_NAME'") | .options[] | select(.name == "'$STATUS_OPTION_NAME'") | .id' fields.json)
if [ -z "$FIELD_ID" ] || [ -z "$OPTION_ID" ]; then
echo "Could not find field or option IDs"
exit 1
fi
echo "field_id=$FIELD_ID" >> $GITHUB_OUTPUT
echo "option_id=$OPTION_ID" >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{ secrets.gh_token }}
- name: Debug GH token access
run: |
echo "Testing token..."
gh api user || echo "❌ Token cannot access /user"
gh api graphql -f query='query { viewer { login } }' || echo "❌ Token cannot query GraphQL viewer"
env:
GH_TOKEN: ${{ secrets.gh_token }}
- name: Map issues to project item IDs (paginated)
id: map_issues
run: |
PROJECT_ID="${{ steps.project.outputs.project_id }}"
ISSUE_IDS=$(cut -d',' -f2 < issues.csv)
echo "🔄 Mapping issues to project items using pagination..."
> item_ids.csv
END_CURSOR=""
HAS_NEXT_PAGE=true
while [ "$HAS_NEXT_PAGE" = true ]; do
# Build inline query
if [ -z "$END_CURSOR" ]; then
QUERY="query { node(id:\"$PROJECT_ID\") { ... on ProjectV2 { items(first:100) { nodes { id content { ... on Issue { id } } } pageInfo { endCursor hasNextPage } } } } }"
else
QUERY="query { node(id:\"$PROJECT_ID\") { ... on ProjectV2 { items(first:100, after:\"$END_CURSOR\") { nodes { id content { ... on Issue { id } } } pageInfo { endCursor hasNextPage } } } } }"
fi
RESPONSE=$(gh api graphql -f query="$QUERY")
# Map items
echo "$RESPONSE" | jq -c '.data.node.items.nodes[]' | while read -r item; do
ISSUE_ID=$(echo "$item" | jq -r '.content.id')
ITEM_ID=$(echo "$item" | jq -r '.id')
if echo "$ISSUE_IDS" | grep -q "$ISSUE_ID"; then
echo "$ISSUE_ID,$ITEM_ID" >> item_ids.csv
fi
done
HAS_NEXT_PAGE=$(echo "$RESPONSE" | jq -r '.data.node.items.pageInfo.hasNextPage')
END_CURSOR=$(echo "$RESPONSE" | jq -r '.data.node.items.pageInfo.endCursor')
done
echo "🧩 Mapped issues to project items:"
cat item_ids.csv
env:
GH_TOKEN: ${{ secrets.gh_token }}
- name: Set Status field to selected value
run: |
FIELD_ID="${{ steps.status_field.outputs.field_id }}"
OPTION_ID="${{ steps.status_field.outputs.option_id }}"
PROJECT_ID="${{ steps.project.outputs.project_id }}"
echo "🔄 Updating status field for project items..."
while IFS=',' read -r ISSUE_ID ITEM_ID; do
echo "🔧 Updating item $ITEM_ID (Issue ID: $ISSUE_ID)..."
gh api graphql -f query="
mutation {
updateProjectV2ItemFieldValue(input: {
projectId: \"$PROJECT_ID\",
itemId: \"$ITEM_ID\",
fieldId: \"$FIELD_ID\",
value: {
singleSelectOptionId: \"$OPTION_ID\"
}
}) {
projectV2Item {
id
}
}
}"
done < item_ids.csv
env:
GH_TOKEN: ${{ secrets.gh_token }}