|
6 | 6 |
|
7 | 7 | import json
|
8 | 8 | import os
|
| 9 | +import re |
9 | 10 | import requests
|
10 | 11 | import subprocess
|
11 | 12 | import sys
|
@@ -76,19 +77,34 @@ def get_version_info():
|
76 | 77 |
|
77 | 78 | if not version:
|
78 | 79 | # Get branch we are PR'ing into, if any.
|
79 |
| - branch = os.environ.get("GITHUB_BASE_REF", "").strip().replace("/", "_") |
| 80 | + # Works for pull_request actions. |
| 81 | + branch = os.environ.get("GITHUB_BASE_REF", "") |
| 82 | + if not branch: |
| 83 | + # Works for push actions (usually a PR merge). |
| 84 | + branch = os.environ.get("GITHUB_REF_NAME", "") |
80 | 85 | if not branch:
|
81 | 86 | branch = "no-branch"
|
| 87 | + # replace slashes with underscores to prevent path subdirs. |
| 88 | + branch = branch.strip().replace("/", "_") |
82 | 89 |
|
83 | 90 | # Get PR number, if any
|
84 |
| - pull_request_maybe = os.environ.get("PULL", "") |
85 |
| - if pull_request_maybe: |
86 |
| - pull_request_maybe = f"-PR{pull_request_maybe}" |
| 91 | + # PR jobs put the PR number in PULL. |
| 92 | + pull_request = os.environ.get("PULL", "") |
| 93 | + if not pull_request: |
| 94 | + # PR merge jobs put a commit message that includes the PR number in HEAD_COMMIT_MESSAGE. |
| 95 | + head_commit_message = os.environ.get("HEAD_COMMIT_MESSAGE", "") |
| 96 | + if head_commit_message: |
| 97 | + match = re.match(r"Merge pull request #(\d+) from", head_commit_message) |
| 98 | + if match: |
| 99 | + pull_request = match.group(1) |
| 100 | + |
| 101 | + if pull_request: |
| 102 | + pull_request = f"-PR{pull_request}" |
87 | 103 |
|
88 | 104 | date_stamp = date.today().strftime("%Y%m%d")
|
89 | 105 | short_sha = sha[:7]
|
90 | 106 | # Example: 20231121-8.2.x-PR9876-123abcd
|
91 |
| - version = f"{date_stamp}-{branch}{pull_request_maybe}-{short_sha}" |
| 107 | + version = f"{date_stamp}-{branch}{pull_request}-{short_sha}" |
92 | 108 |
|
93 | 109 | return sha, version
|
94 | 110 |
|
|
0 commit comments