From ee71cd74cf74e28dac22c3cdf2792a5b7fd37d2e Mon Sep 17 00:00:00 2001 From: Nicholas Dudfield Date: Tue, 25 Nov 2025 08:09:02 +0700 Subject: [PATCH] refactor: use GitHub API instead of git fetch for PR commit messages --- .../xahau-ga-get-commit-message/action.yml | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/.github/actions/xahau-ga-get-commit-message/action.yml b/.github/actions/xahau-ga-get-commit-message/action.yml index f82da45e0..2a379eb1d 100644 --- a/.github/actions/xahau-ga-get-commit-message/action.yml +++ b/.github/actions/xahau-ga-get-commit-message/action.yml @@ -19,13 +19,17 @@ runs: steps: - name: Get commit message and set environment variable shell: python + env: + GH_TOKEN: ${{ github.token }} run: | + import json import os - import subprocess import secrets + import urllib.request event_name = "${{ inputs.event-name }}" pr_head_sha = "${{ inputs.pr-head-sha }}" + repository = "${{ github.repository }}" print("==========================================") print("Setting XAHAU_GA_COMMIT_MSG environment variable") @@ -36,24 +40,21 @@ runs: # For push events, use the input directly message = """${{ inputs.head-commit-message }}""" print("Source: workflow input (github.event.head_commit.message)") - elif event_name == 'pull_request': - # For PR events, fetch the specific SHA - print(f"Source: git show {pr_head_sha} (fetching PR head commit)") - - # Fetch the PR head commit - subprocess.run( - ['git', 'fetch', 'origin', pr_head_sha], - check=True - ) - - # Get commit message from the fetched SHA - result = subprocess.run( - ['git', 'show', '-s', '--format=%B', pr_head_sha], - capture_output=True, - text=True, - check=True - ) - message = result.stdout.strip() + elif event_name == 'pull_request' and pr_head_sha: + # For PR events, fetch via GitHub API + print(f"Source: GitHub API (fetching commit {pr_head_sha})") + try: + url = f"https://api.github.com/repos/{repository}/commits/{pr_head_sha}" + req = urllib.request.Request(url, headers={ + "Accept": "application/vnd.github.v3+json", + "Authorization": f"Bearer {os.environ.get('GH_TOKEN', '')}" + }) + with urllib.request.urlopen(req) as response: + data = json.load(response) + message = data["commit"]["message"] + except Exception as e: + print(f"Failed to fetch commit message: {e}") + message = "" else: message = "" print(f"Warning: Unknown event type: {event_name}")