Skip to content

Commit

Permalink
Get git hash url from gh actions + inject actions url by default
Browse files Browse the repository at this point in the history
  • Loading branch information
jbusecke authored Apr 30, 2024
1 parent f599f6f commit 84837e8
Showing 1 changed file with 51 additions and 17 deletions.
68 changes: 51 additions & 17 deletions leap_data_management_utils/data_management_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,58 @@

yaml = YAML(typ='safe')

def get_github_actions_url() -> str:
"""Return the url of the gh action run"""
if os.getenv('GITHUB_ACTIONS') == 'true':
print('Running from within GH actions')
server_url = os.getenv('GITHUB_SERVER_URL')
repository = os.getenv('GITHUB_REPOSITORY')
run_id = os.getenv('GITHUB_RUN_ID')
commit_hash = os.getenv('GITHUB_SHA')

if server_url and repository and run_id and commit_hash:
return f"{server_url}/{repository}/actions/runs/{run_id}"
else:
print("One or more environment variables are missing.")
return "none"

def get_github_commit_url() -> Optional[str]:
def get_github_commit_url() -> str:
"""Get the GitHub commit URL for the current commit"""
# Get GitHub Server URL
github_server_url = 'https://github.com'

# Get the repository's remote origin URL
try:
repo_origin_url = subprocess.check_output(
['git', 'config', '--get', 'remote.origin.url'], text=True
).strip()

# Extract the repository path from the remote URL
repository_path = repo_origin_url.split('github.com/')[-1].replace('.git', '')

# Get the current commit SHA
commit_sha = subprocess.check_output(['git', 'rev-parse', 'HEAD'], text=True).strip()

# Construct the GitHub commit URL
git_url_hash = f'{github_server_url}/{repository_path}/commit/{commit_sha}'


# check if this is running from within a github action
if os.getenv('GITHUB_ACTIONS') == 'true':
print('Running from within GH actions')
server_url = os.getenv('GITHUB_SERVER_URL')
repository = os.getenv('GITHUB_REPOSITORY')
run_id = os.getenv('GITHUB_RUN_ID')
commit_hash = os.getenv('GITHUB_SHA')

if server_url and repository and run_id and commit_hash:
git_url_hash = f"{server_url}/{repository}/commit/{commit_hash}"
else:
print("Could not construct git_url_hash. One or more environment variables are missing.")
git_url_hash = "none"

else:
#TODO: If the above fails, maybe still try this? Even though that would be a really rare case?
print('Fallback: Calling git via subprocess')
github_server_url = 'https://github.com'
# Get the repository's remote origin URL
try:
repo_origin_url = subprocess.check_output(
['git', 'config', '--get', 'remote.origin.url'], text=True
).strip()

# Extract the repository path from the remote URL
repository_path = repo_origin_url.split('github.com/')[-1].replace('.git', '')

# Get the current commit SHA
commit_sha = subprocess.check_output(['git', 'rev-parse', 'HEAD'], text=True).strip()

# Construct the GitHub commit URL
git_url_hash = f'{github_server_url}/{repository_path}/commit/{commit_sha}'

# Output the GitHub commit URL
return git_url_hash
Expand Down Expand Up @@ -197,9 +229,11 @@ def __post_init__(self):

if self.add_provenance:
git_url_hash = get_github_commit_url()
gh_actions_url = get_github_actions_url()
timestamp = datetime.now(timezone.utc).isoformat()
provenance_dict = {
'pangeo_forge_build_git_hash': git_url_hash,
'pangeo_forge_gh_actions_url': gh_actions_url
'pangeo_forge_build_timestamp': timestamp,
}
self.inject_attrs.update(provenance_dict)
Expand Down

0 comments on commit 84837e8

Please sign in to comment.