Skip to content

Commit b43bfa3

Browse files
committed
feat(scripts): add pagination status update script
Enhance `update_issue_status.sh` by implementing pagination for project item and field retrieval. This change ensures the script can process projects with more than 100 items or fields.
1 parent b5a4c2c commit b43bfa3

File tree

1 file changed

+67
-22
lines changed

1 file changed

+67
-22
lines changed

scripts/update_issue_status.sh

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ echo "Projects associated with the issue: $PROJECT_NUMBERS"
8080
for PROJECT_NUMBER in $PROJECT_NUMBERS; do
8181
echo "Processing Project #$PROJECT_NUMBER"
8282

83-
# Get item ID
84-
ITEM_ID=$(gh api graphql -F org="$OWNER" -F projectNumber="$PROJECT_NUMBER" -f query='
85-
query($org: String! , $projectNumber: Int!) {
83+
# Get item ID with pagination
84+
ITEMS_QUERY='
85+
query($org: String!, $projectNumber: Int!, $cursor: String) {
8686
organization(login: $org) {
8787
projectV2(number: $projectNumber) {
88-
items(first: 100) {
88+
items(first: 100, after: $cursor) {
8989
nodes {
9090
id
9191
content {
@@ -94,10 +94,33 @@ for PROJECT_NUMBER in $PROJECT_NUMBERS; do
9494
}
9595
}
9696
}
97+
pageInfo {
98+
hasNextPage
99+
endCursor
100+
}
97101
}
98102
}
99103
}
100-
}' --jq '.data.organization.projectV2.items.nodes[] | select(.content.id=="'$ISSUE_NODE_ID'") | .id')
104+
}'
105+
106+
ITEM_ID=""
107+
CURSOR=""
108+
109+
while true; do
110+
RESPONSE=$(gh api graphql -F org="$OWNER" -F projectNumber="$PROJECT_NUMBER" -F cursor="$CURSOR" -f query="$ITEMS_QUERY")
111+
ITEM_ID=$(echo "$RESPONSE" | jq -r --arg NODE_ID "$ISSUE_NODE_ID" '.data.organization.projectV2.items.nodes[] | select(.content.id==$NODE_ID) | .id')
112+
113+
if [ ! -z "$ITEM_ID" ]; then
114+
break
115+
fi
116+
117+
HAS_NEXT_PAGE=$(echo "$RESPONSE" | jq -r '.data.organization.projectV2.items.pageInfo.hasNextPage')
118+
if [ "$HAS_NEXT_PAGE" = "false" ]; then
119+
break
120+
fi
121+
122+
CURSOR=$(echo "$RESPONSE" | jq -r '.data.organization.projectV2.items.pageInfo.endCursor')
123+
done
101124

102125
if [ -z "$ITEM_ID" ]; then
103126
echo "Warning: Item ID not found in Project #$PROJECT_NUMBER."
@@ -106,23 +129,45 @@ for PROJECT_NUMBER in $PROJECT_NUMBERS; do
106129

107130
echo "Item ID in Project: $ITEM_ID"
108131

109-
# Get Status field ID
110-
STATUS_FIELD_ID=$(gh api graphql -F org="$OWNER" -F projectNumber="$PROJECT_NUMBER" -f query='
111-
query($org: String!, $projectNumber: Int!) {
132+
# Get Status field ID with pagination
133+
FIELDS_QUERY='
134+
query($org: String!, $projectNumber: Int!, $cursor: String) {
112135
organization(login: $org) {
113136
projectV2(number: $projectNumber) {
114-
fields(first: 100) {
137+
fields(first: 100, after: $cursor) {
115138
nodes {
116139
... on ProjectV2FieldCommon {
117140
id
118141
name
119142
}
120143
}
144+
pageInfo {
145+
hasNextPage
146+
endCursor
147+
}
121148
}
122149
}
123150
}
124-
}' --jq '.data.organization.projectV2.fields.nodes[] | select(.name=="Status") | .id')
151+
}'
152+
153+
STATUS_FIELD_ID=""
154+
CURSOR=""
155+
156+
while true; do
157+
RESPONSE=$(gh api graphql -F org="$OWNER" -F projectNumber="$PROJECT_NUMBER" -F cursor="$CURSOR" -f query="$FIELDS_QUERY")
158+
STATUS_FIELD_ID=$(echo "$RESPONSE" | jq -r '.data.organization.projectV2.fields.nodes[] | select(.name=="Status") | .id')
125159

160+
if [ ! -z "$STATUS_FIELD_ID" ]; then
161+
break
162+
fi
163+
164+
HAS_NEXT_PAGE=$(echo "$RESPONSE" | jq -r '.data.organization.projectV2.fields.pageInfo.hasNextPage')
165+
if [ "$HAS_NEXT_PAGE" = "false" ]; then
166+
break
167+
fi
168+
169+
CURSOR=$(echo "$RESPONSE" | jq -r '.data.organization.projectV2.fields.pageInfo.endCursor')
170+
done
126171

127172
if [ -z "$STATUS_FIELD_ID" ]; then
128173
echo "Warning: 'Status' field not found in Project #$PROJECT_NUMBER."
@@ -132,25 +177,25 @@ for PROJECT_NUMBER in $PROJECT_NUMBERS; do
132177
echo "Status Field ID: $STATUS_FIELD_ID"
133178

134179
# Get Status option ID
135-
STATUS_OPTION_ID=$(gh api graphql -F org="$OWNER" -F projectNumber="$PROJECT_NUMBER" -F fieldId="$STATUS_FIELD_ID" -f query='
180+
OPTIONS_QUERY='
136181
query($org: String!, $projectNumber: Int!) {
137182
organization(login: $org) {
138183
projectV2(number: $projectNumber) {
139-
fields(first: 100) {
140-
nodes {
141-
... on ProjectV2SingleSelectField {
184+
field(name: "Status") {
185+
... on ProjectV2SingleSelectField {
186+
options {
142187
id
143188
name
144-
options {
145-
id
146-
name
147-
}
148189
}
149190
}
150191
}
151192
}
152193
}
153-
}' --jq ".data.organization.projectV2.fields.nodes[] | select(.name==\"Status\") | .options[] | select(.name==\"$NEW_STATUS\") | .id")
194+
}'
195+
196+
STATUS_OPTION_ID=""
197+
RESPONSE=$(gh api graphql -F org="$OWNER" -F projectNumber="$PROJECT_NUMBER" -f query="$OPTIONS_QUERY")
198+
STATUS_OPTION_ID=$(echo "$RESPONSE" | jq -r --arg STATUS "$NEW_STATUS" '.data.organization.projectV2.field.options[] | select(.name==$STATUS) | .id')
154199

155200
if [ -z "$STATUS_OPTION_ID" ]; then
156201
echo "Warning: Status option '$NEW_STATUS' not found in Project #$PROJECT_NUMBER."
@@ -163,9 +208,9 @@ for PROJECT_NUMBER in $PROJECT_NUMBERS; do
163208
PROJECT_ID=$(gh api graphql -F org="$OWNER" -F projectNumber="$PROJECT_NUMBER" -f query='
164209
query($org: String!, $projectNumber: Int!) {
165210
organization(login: $org) {
166-
projectV2(number: $projectNumber) {
167-
id
168-
}
211+
projectV2(number: $projectNumber) {
212+
id
213+
}
169214
}
170215
}' --jq '.data.organization.projectV2.id')
171216

0 commit comments

Comments
 (0)