Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions .github/workflows/close-parent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
name: Auto-Close Parent Issue

on:
issues:
types: [closed]

jobs:
auto-close-parent:
runs-on: ubuntu-latest
permissions:
issues: write
steps:
- name: Check and Close Parent Issue
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Get the closed issue's number
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding set -e at the start of the run block to ensure the script exits immediately if a command fails. This would help avoid unpredictable behavior on API errors.

This comment was generated because it violated a code review rule: mrule_OR1S8PRRHcvbdFib.

issue_number=${{ github.event.issue.number }}
owner=${{ github.repository_owner }}
repo=$(echo "${{ github.repository }}" | cut -d'/' -f2)

# Retrieve the issue ID
issue_id=$(gh api graphql -H "GraphQL-Features: sub_issues" -f query='
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be beneficial to add error handling for the gh api graphql calls. For example, verify that the response contains the expected data before proceeding, to handle API errors gracefully.

This comment was generated because it violated a code review rule: mrule_OR1S8PRRHcvbdFib.

query($owner: String!, $repo: String!, $number: Int!) {
repository(owner: $owner, name: $repo) {
issue(number: $number) {
id
}
}
}' -F owner="$owner" -F repo="$repo" -F number="$issue_number" --jq '.data.repository.issue.id')

# Retrieve the parent issue
parent_data=$(gh api graphql -H "GraphQL-Features: sub_issues" -f query='
query($issueId: ID!) {
node(id: $issueId) {
... on Issue {
parent {
id
number
}
}
}
}' -F issueId="$issue_id")

parent_id=$(echo "$parent_data" | jq -r '.data.node.parent.id')
parent_number=$(echo "$parent_data" | jq -r '.data.node.parent.number')

if [ "$parent_id" = "null" ]; then
echo "No parent issue found."
exit 0
fi

# Retrieve sub-issues of the parent
sub_issues=$(gh api graphql -H "GraphQL-Features: sub_issues" -f query='
query($parentId: ID!) {
node(id: $parentId) {
... on Issue {
subIssues(first: 100) {
nodes {
number
state
}
}
}
}
}' -F parentId="$parent_id")

# Check if all sub-issues are closed
all_closed=true
echo "$sub_issues" | jq -c '.data.node.subIssues.nodes[]' | while read -r issue; do
state=$(echo "$issue" | jq -r '.state')
if [ "$state" != "CLOSED" ]; then
all_closed=false
break
fi
done

if [ "$all_closed" = true ]; then
# Close the parent issue
gh issue close "$parent_number" --repo "$owner/$repo" --comment "Auto-closing parent issue as all sub-issues are closed."
else
echo "Not all sub-issues are closed."
fi