name: GitHub Release Issue Commenter on: release: types: - published workflow_dispatch: inputs: tag: description: 'Tag (e.g. "v1.0.0")' required: true type: string concurrency: group: ${{ github.workflow }}-${{ inputs.tag || github.event.release.tag_name }} cancel-in-progress: true env: tag: ${{ inputs.tag || github.event.release.tag_name }} jobs: comment: permissions: contents: read pull-requests: read issues: write runs-on: ubuntu-latest steps: - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5 with: fetch-depth: 0 fetch-tags: true - shell: bash env: GH_TOKEN: ${{ github.token }} run: | REPO="${GITHUB_REPOSITORY}" CURR_TAG="${{ env.tag }}" CURR_PUBLISHED="$(gh release view "${CURR_TAG}" --json publishedAt --jq '.publishedAt')" RELEASE_HTML_URL="$(gh release view "${CURR_TAG}" --json url --jq '.url')" # Determine release tag range PREV_TAG="$(gh release list \ --limit 100 \ --json tagName,publishedAt,isDraft \ --jq "[.[] | select(.isDraft == false) | select(.publishedAt != null) | select(.tagName != \"${CURR_TAG}\") | select(.publishedAt < \"${CURR_PUBLISHED}\")] | sort_by(.publishedAt) | last? | .tagName // empty")" if [ -n "${PREV_TAG}" ]; then echo "Previous release tag: ${PREV_TAG}" RANGE="${PREV_TAG}..${CURR_TAG}" else echo "No previous release found; scanning all commits reachable by ${CURR_TAG}" RANGE="${CURR_TAG}" exit 1 fi # Get the commits from the tag range mapfile -t COMMITS < <(git rev-list "${RANGE}") echo "Found ${#COMMITS[@]} commits in range ${RANGE}:" for sha in "${COMMITS[@]}"; do echo " - ${sha}" done # Get the PRs associated with the commits declare -A PRS_SET=() for sha in "${COMMITS[@]}"; do PRS_FOR_COMMIT=$(gh api \ -H "Accept: application/vnd.github+json" \ "/repos/${REPO}/commits/${sha}/pulls" \ --jq '.[].number' 2>/dev/null || true) if [ -n "${PRS_FOR_COMMIT}" ]; then while IFS= read -r pr; do [ -n "$pr" ] && PRS_SET["$pr"]=1 done <<< "${PRS_FOR_COMMIT}" fi done if [ ${#PRS_SET[@]} -eq 0 ]; then echo "No pull requests found in this release range." exit 0 fi # Print PR numbers (sorted) for debugging mapfile -t PRS_SORTED < <(printf '%s\n' "${!PRS_SET[@]}" | sort -n) echo "Found ${#PRS_SORTED[@]} unique PRs associated with the commits:" for pr in "${PRS_SORTED[@]}"; do echo " - #${pr}" done # Get the issues resolved by the PRs declare -A ISSUES_SET=() for pr in "${PRS_SORTED[@]}"; do ISSUE_NUMBERS=$(gh pr view "${pr}" --json closingIssuesReferences --jq '.closingIssuesReferences[].number' 2>/dev/null || true) if [ -n "${ISSUE_NUMBERS}" ]; then while IFS= read -r issue; do [ -n "$issue" ] && ISSUES_SET["$issue"]=1 done <<< "${ISSUE_NUMBERS}" fi done if [ ${#ISSUES_SET[@]} -eq 0 ]; then echo "No issues were resolved by PRs in this release." exit 0 fi mapfile -t ISSUES_SORTED < <(printf '%s\n' "${!ISSUES_SET[@]}" | sort -n) echo "Found ${#ISSUES_SORTED[@]} unique issues resolved by the PRs:" for issue in "${ISSUES_SORTED[@]}"; do echo " - #${issue}" done # Comment on each issue COMMENT_BODY="$( echo "## :rocket: GitHub Release" echo "This issue was released in: [${CURR_TAG}](${RELEASE_HTML_URL})" echo "" echo "_Comment generated by the [${{ github.workflow }}](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}/attempts/${{ github.run_attempt }}) workflow._" )" for issue in "${ISSUES_SORTED[@]}"; do echo "Processing issue #${issue}..." ALREADY_COMMENTED=$(gh issue view "${issue}" --json comments --jq \ '[.comments[].body | contains("comment generated by")] | any' 2>/dev/null || echo "false") if [ "${ALREADY_COMMENTED}" = "true" ]; then echo " - Comment for ${CURR_TAG} already exists on #${issue}; skipping." continue fi gh issue comment "${issue}" --body "${COMMENT_BODY}" echo " - Commented on #${issue}" done echo "Done."