From b7bd8341aa651da60c4e5bad17614853bb05c063 Mon Sep 17 00:00:00 2001 From: Marc Littlemore Date: Thu, 4 Sep 2025 12:31:28 +0100 Subject: [PATCH] chore: Retrieve correct commits for OpenAPI docs sync (#19179) --- .github/workflows/sync-public-api-docs.yml | 67 ++++++++++++++++------ 1 file changed, 48 insertions(+), 19 deletions(-) diff --git a/.github/workflows/sync-public-api-docs.yml b/.github/workflows/sync-public-api-docs.yml index 1119e4c0eb..6e505c7847 100644 --- a/.github/workflows/sync-public-api-docs.yml +++ b/.github/workflows/sync-public-api-docs.yml @@ -22,10 +22,13 @@ jobs: runs-on: ubuntu-latest permissions: contents: read + actions: read # Needed for `gh` to read the workflow history steps: - name: Checkout Main n8n Repository uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 # Fetch all history for git log - name: Setup PNPM uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0 @@ -82,28 +85,54 @@ jobs: echo "Copying 'packages/cli/dist/public-api/v1/openapi.yml' to '${DOCS_TARGET_PATH}'" cp packages/cli/dist/public-api/v1/openapi.yml "${DOCS_TARGET_PATH}" - - name: Extract PR number from commit message + - name: Find Relevant Source Commits + id: find_source_commits if: steps.verify_file.outputs.file_exists == 'true' - id: extract_pr + env: + GH_TOKEN: ${{ github.token }} run: | - COMMIT_MESSAGE=$(git log -1 --pretty=%s) - echo "Commit message: $COMMIT_MESSAGE" - - # Extract PR number from commit message (format: "title (#1234)") - PR_NUMBER=$(echo "$COMMIT_MESSAGE" | grep -om1E '\(#[0-9]+\)' | grep -oE '[0-9]+' || echo "") - - if [ -n "$PR_NUMBER" ]; then - echo "Found PR number: $PR_NUMBER" - echo "pr_number=$PR_NUMBER" >> "$GITHUB_OUTPUT" - echo "pr_url=https://github.com/${{ github.repository }}/pull/$PR_NUMBER" >> "$GITHUB_OUTPUT" - echo "source_link=Source PR: [$PR_NUMBER](https://github.com/${{ github.repository }}/pull/$PR_NUMBER)" >> "$GITHUB_OUTPUT" - else - echo "No PR number found in commit message, using commit link" - echo "pr_number=" >> "$GITHUB_OUTPUT" - echo "pr_url=" >> "$GITHUB_OUTPUT" - echo "source_link=Source commit: [${{ github.sha }}](https://github.com/${{ github.repository }}/commit/${{ github.sha }})" >> "$GITHUB_OUTPUT" + echo "Finding last successful workflow run..." + LAST_SUCCESS_SHA=$(gh run list \ + --workflow "${{ github.workflow }}" \ + --branch "${{ github.ref_name }}" \ + --status "success" \ + --json "headSha" \ + --jq '.[0].headSha // empty' \ + -L 1) + + RELEVANT_COMMITS="" + if [[ -n "$LAST_SUCCESS_SHA" ]]; then + echo "Last successful commit: $LAST_SUCCESS_SHA" + echo "Current commit: ${{ github.sha }}" + + # Find all commits between the last success and now that touched the relevant files + # NOTE: The pathspecs here mirror the workflow's 'paths' trigger for precision + RELEVANT_COMMITS=$(git log --pretty=format:"- [%h](https://github.com/${{ github.repository }}/commit/%H) %s" $LAST_SUCCESS_SHA..${{ github.sha }} -- \ + 'packages/cli/src/public-api/**/*.css' \ + 'packages/cli/src/public-api/**/*.yaml' \ + 'packages/cli/src/public-api/**/*.yml') fi + if [[ -z "$RELEVANT_COMMITS" ]]; then + if [[ -z "$LAST_SUCCESS_SHA" ]]; then + echo "No previous successful run found. Using current commit as source." + else + echo "No commits touching source files found. Linking to trigger commit (likely a dependency update)." + fi + FULL_SHA="${{ github.sha }}" + SHORT_SHA="${FULL_SHA:0:7}" + SOURCE_LINK="Source commit: [$SHORT_SHA](https://github.com/${{ github.repository }}/commit/$FULL_SHA)" + else + echo "Found relevant source commits:" + echo "$RELEVANT_COMMITS" + # Build the string manually to avoid heredoc capturing leading whitespace from YAML + SOURCE_LINK="Source commit(s):"$'\n'"$RELEVANT_COMMITS" + fi + + echo "source_link<> "$GITHUB_OUTPUT" + echo "$SOURCE_LINK" >> "$GITHUB_OUTPUT" + echo "EOF" >> "$GITHUB_OUTPUT" + - name: Create PR in Docs Repo if: steps.verify_file.outputs.file_exists == 'true' @@ -127,6 +156,6 @@ jobs: Automated update of the Public API OpenAPI YAML schema. This PR was generated by a GitHub Action in the [${{ github.repository }} repository](https://github.com/${{ github.repository }}). - ${{ steps.extract_pr.outputs.source_link }} + ${{ steps.find_source_commits.outputs.source_link }} Please review the changes and merge if appropriate.