From abc9fcb5c8cfa248d9d4c222e8e613fc669904f8 Mon Sep 17 00:00:00 2001 From: Artem Goncharov Date: Wed, 8 Oct 2025 13:22:06 +0200 Subject: [PATCH] chore(ci): And hopefully the final one --- .github/workflows/propose_osc_changes.yml | 84 ++++++++++++++++------- 1 file changed, 59 insertions(+), 25 deletions(-) diff --git a/.github/workflows/propose_osc_changes.yml b/.github/workflows/propose_osc_changes.yml index 5e01fe5e..ff1c3a8f 100644 --- a/.github/workflows/propose_osc_changes.yml +++ b/.github/workflows/propose_osc_changes.yml @@ -8,7 +8,7 @@ permissions: contents: read jobs: - generate: + propose-osc-pr: if: github.event.pull_request.merged == true runs-on: ubuntu-latest steps: @@ -50,29 +50,63 @@ jobs: uv run openstack-codegenerator --work-dir wrk --target rust-tui --metadata metadata/identity_metadata.yaml --service identity --resource ${resource} done; - - name: Set variables - id: vars - run: | - echo "branch_name=${GITHUB_HEAD_REF}" >> $GITHUB_OUTPUT - echo "pr_number=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT - - - name: Get commit message from merged PR - id: commit - env: - GH_TOKEN: ${{ github.token }} - PR_NUMBER: ${{ steps.vars.outputs.pr_number }} - REPO: ${{ github.repository }} - run: | - # Fetch all commits for the PR - RESPONSE=$(curl -s -H "Authorization: token $GH_TOKEN" \ - "https://api.github.com/repos/$REPO/pulls/$PR_NUMBER/commits") - - # Extract last commit message using jq - COMMIT_MESSAGE=$(echo "$RESPONSE" | jq -r '.[-1].commit.message') - - echo "commit_message<> $GITHUB_OUTPUT - echo "$COMMIT_MESSAGE" >> $GITHUB_OUTPUT - echo "EOF" >> $GITHUB_OUTPUT + - name: Set PR variables + id: vars + run: | + echo "pr_number=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT + echo "branch_name=${{ github.event.pull_request.head.ref }}" >> $GITHUB_OUTPUT + echo "merge_sha=${{ github.event.pull_request.merge_commit_sha }}" >> $GITHUB_OUTPUT + + - name: Resolve best commit message for the merged PR + id: commit + env: + GITHUB_TOKEN: ${{ github.token }} + PR_NUMBER: ${{ steps.vars.outputs.pr_number }} + REPO: ${{ github.repository }} + MERGE_SHA: ${{ steps.vars.outputs.merge_sha }} + run: | + set -euo pipefail + # helper to emit multi-line output + set_output() { + echo "commit_message<<'EOF'" >> $GITHUB_OUTPUT + printf "%s\n" "$1" >> $GITHUB_OUTPUT + echo "EOF" >> $GITHUB_OUTPUT + } + + COMMIT_MSG="" + # 1) If merge_commit_sha exists, try to fetch that commit (works for merge & squash) + if [ -n "$MERGE_SHA" ] && [ "$MERGE_SHA" != "null" ]; then + echo "Attempting to fetch merge commit message for SHA: $MERGE_SHA" + RESP=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ + "https://api.github.com/repos/$REPO/commits/$MERGE_SHA") + COMMIT_MSG=$(echo "$RESP" | jq -r '.commit.message // empty' | sed 's/\r$//') + fi + + # 2) Fallback: collect all commits on the PR and join their messages + if [ -z "$COMMIT_MSG" ]; then + echo "Falling back to collecting PR commits..." + RESP=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ + "https://api.github.com/repos/$REPO/pulls/$PR_NUMBER/commits") + COMMIT_MSG=$(echo "$RESP" | jq -r 'map(.commit.message) | join("\n\n---\n\n")' | sed 's/\r$//') + fi + + # 3) Fallback: use PR title and body + if [ -z "$COMMIT_MSG" ]; then + echo "Falling back to PR title/body..." + RESP=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ + "https://api.github.com/repos/$REPO/pulls/$PR_NUMBER") + TITLE=$(echo "$RESP" | jq -r '.title // empty') BODY=$(echo "$RESP" | jq -r '.body // empty') + if [ -n "$TITLE" ] || [ -n "$BODY" ]; then + COMMIT_MSG="$TITLE"$'\n\n'"$BODY" + fi + fi + + # 4) Final fallback + if [ -z "$COMMIT_MSG" ]; then + COMMIT_MSG="Automated update from source repo (PR #$PR_NUMBER)" + fi + + set_output "$COMMIT_MSG" - name: Clone target repository run: | @@ -112,5 +146,5 @@ jobs: --repo gtema/openstack \ --head "${{ steps.vars.outputs.branch_name }}" \ --base main \ - --title "${{ steps.vars.commit.commit_message }}" \ + --title "${{ steps.commit.commit_message }}" \ --body "Automated PR created after merging '${{ steps.vars.outputs.branch_name }}' in the gtema/keystone repo."