Skip to content

Commit 1d036e4

Browse files
committed
Fixup the get-highest-priority GHA job.
- Add missing permission - Fail gracefully - Slight optimization.
1 parent 20fc6bd commit 1d036e4

1 file changed

Lines changed: 39 additions & 25 deletions

File tree

.github/workflows/pr_issue_status_automation.yml

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ jobs:
6565

6666
get-highest-priority:
6767
# This job gets the highest priority from linked issues
68-
if: ${{ github.event.pull_request.state == 'open' && needs.get-project-id.outputs.ITEM_PROJECT_ID != '' }}
69-
needs: get-project-id
68+
if: ${{ github.event.pull_request.state == 'open' }}
7069
runs-on: ubuntu-latest
7170
permissions:
7271
contents: read
7372
pull-requests: read
73+
projects: read
7474
outputs:
7575
highest-priority: ${{ steps.get-priority.outputs.highest-priority }}
7676
steps:
@@ -79,29 +79,25 @@ jobs:
7979
env:
8080
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
8181
PROJECT_ID: "PVT_kwDOAp2shc4AA8lR"
82-
ITEM_PROJECT_ID: "${{ needs.get-project-id.outputs.ITEM_PROJECT_ID }}"
82+
PR_NODE_ID: "${{ github.event.pull_request.node_id }}"
8383
run: |
84-
# GraphQL query to get linked issues and their priority field values
84+
# GraphQL query to get linked issues and their priority field values from projects
8585
query='
86-
query($itemId: ID!) {
87-
node(id: $itemId) {
88-
... on ProjectV2Item {
89-
content {
90-
... on PullRequest {
91-
closingIssuesReferences(first: 100) {
86+
query($prId: ID!) {
87+
node(id: $prId) {
88+
... on PullRequest {
89+
closingIssuesReferences(first: 100) {
90+
nodes {
91+
id
92+
projectItems(first: 100) {
9293
nodes {
9394
id
94-
projectItems(first: 100) {
95-
nodes {
96-
id
97-
project {
98-
id
99-
}
100-
fieldValueByName(name: "Priority") {
101-
... on ProjectV2ItemFieldSingleSelectValue {
102-
name
103-
}
104-
}
95+
project {
96+
id
97+
}
98+
fieldValueByName(name: "Priority") {
99+
... on ProjectV2ItemFieldSingleSelectValue {
100+
name
105101
}
106102
}
107103
}
@@ -112,12 +108,25 @@ jobs:
112108
}
113109
}'
114110
115-
# Execute query
116-
response=$(gh api graphql -f query="$query" -f itemId="$ITEM_PROJECT_ID")
111+
# Execute query (fail gracefully if permissions are insufficient)
112+
if ! response=$(gh api graphql -f query="$query" -f prId="$PR_NODE_ID" 2>&1); then
113+
echo "Warning: Failed to query project data (possibly insufficient permissions)"
114+
echo "Error: $response"
115+
echo "highest-priority=" >> "$GITHUB_OUTPUT"
116+
exit 0
117+
fi
118+
119+
# Check for GraphQL errors
120+
if echo "$response" | jq -e '.errors' > /dev/null 2>&1; then
121+
echo "Warning: GraphQL query returned errors"
122+
echo "$response" | jq '.errors'
123+
echo "highest-priority=" >> "$GITHUB_OUTPUT"
124+
exit 0
125+
fi
117126
118127
# Extract priorities from linked issues that are in the same project
119128
priorities=$(echo "$response" | jq -r '
120-
.data.node.content.closingIssuesReferences.nodes[]?.projectItems.nodes[]? |
129+
.data.node.closingIssuesReferences.nodes[]?.projectItems.nodes[]? |
121130
select(.project.id == env.PROJECT_ID) |
122131
.fieldValueByName.name // empty
123132
')
@@ -146,7 +155,12 @@ jobs:
146155
done <<< "$priorities"
147156
148157
echo "highest-priority=$highest_priority" >> "$GITHUB_OUTPUT"
149-
echo "Found highest priority: $highest_priority"
158+
159+
if [[ -z "$highest_priority" ]]; then
160+
echo "No priority found on linked issues (or no linked issues found)"
161+
else
162+
echo "Found highest priority: $highest_priority"
163+
fi
150164
151165
update-priority:
152166
# This job sets the PR priority to the highest priority of linked issues

0 commit comments

Comments
 (0)