mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
75 lines
2.8 KiB
YAML
75 lines
2.8 KiB
YAML
name: 'Get Commit Message'
|
|
description: 'Gets commit message for both push and pull_request events and sets XAHAU_GA_COMMIT_MSG env var'
|
|
|
|
inputs:
|
|
event-name:
|
|
description: 'The event name (push or pull_request)'
|
|
required: true
|
|
head-commit-message:
|
|
description: 'The head commit message (for push events)'
|
|
required: false
|
|
default: ''
|
|
pr-head-sha:
|
|
description: 'The PR head SHA (for pull_request events)'
|
|
required: false
|
|
default: ''
|
|
|
|
runs:
|
|
using: 'composite'
|
|
steps:
|
|
- name: Get commit message and set environment variable
|
|
shell: python
|
|
env:
|
|
GH_TOKEN: ${{ github.token }}
|
|
run: |
|
|
import json
|
|
import os
|
|
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")
|
|
print("==========================================")
|
|
print(f"Event: {event_name}")
|
|
|
|
if event_name == 'push':
|
|
# 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' 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}")
|
|
|
|
print(f"Commit message (first 100 chars): {message[:100]}")
|
|
|
|
# Write to GITHUB_ENV using heredoc with random delimiter (prevents injection attacks)
|
|
# See: https://securitylab.github.com/resources/github-actions-untrusted-input/
|
|
delimiter = f"EOF_{secrets.token_hex(16)}"
|
|
|
|
with open(os.environ['GITHUB_ENV'], 'a') as f:
|
|
f.write(f'XAHAU_GA_COMMIT_MSG<<{delimiter}\n')
|
|
f.write(message)
|
|
f.write(f'\n{delimiter}\n')
|
|
|
|
print(f"✓ XAHAU_GA_COMMIT_MSG set (available to all subsequent steps)")
|
|
print("==========================================")
|