datalad-clone-action/entrypoint.py
Michael Hanke 1078c7e3aa
All checks were successful
Exercise test cases / clone-custom-path (push) Successful in 1m33s
Exercise test cases / clone-origin-repo (push) Successful in 1m47s
Exercise test cases / clone-custom-ref (push) Successful in 1m52s
Exercise test cases / clone-recursive (push) Successful in 54s
Exercise test cases / clone-recursive-limited (push) Successful in 48s
Exercise test cases / clone-recursive-no-data (push) Successful in 47s
Exercise test cases / clone-get-deep-content (push) Successful in 37s
fix: INPUT_REF/REPOSITORY are in the environment, but empty
This prevents a non-default branch from being checked out properly.
2025-07-21 14:51:18 +02:00

160 lines
3.9 KiB
Python
Executable file

#!/usr/bin/env python3
from os import environ
from os.path import abspath
from shlex import split as sh_split
from subprocess import run as subprocess_run
import sys
from urllib.parse import (
urlparse,
urlunparse,
)
dryrun_mode = bool(environ.get('DATALAD_CLONE_ACTION_DRYRUN', ''))
def show_debug_info() -> None:
for k, v in environ.items():
if k.startswith('INPUT_') or k.startswith('GITHUB_'):
print(f'DBGENV: {k}={v}', file=sys.stderr)
def run(args, **kwargs) -> None:
if dryrun_mode:
print(f'DRYRUN: {" ".join(repr(a) for a in args)} {kwargs!r}')
else:
print(f'RUN: {args!r}', file=sys.stderr)
subprocess_run(args, **kwargs)
def setup_git_identity() -> None:
run(
['git', 'config', '--global', '--add',
'user.name', f"{environ['GITHUB_ACTOR']}"],
check=True,
)
run(
['git', 'config', '--global', '--add',
'user.email', f"{environ['GITHUB_ACTOR']}@{environ['HOSTNAME']}"],
check=True,
)
def get_clone_url() -> str:
serverurl = environ.get('INPUT_SERVER_URL')
if not serverurl:
serverurl = environ.get('GITHUB_SERVER_URL')
if not serverurl:
raise RuntimeError('No "serverurl"')
serverurl_p = urlparse(serverurl)
repo = environ.get('INPUT_REPOSITORY')
if not repo:
repo = environ['GITHUB_REPOSITORY']
assert repo
auth_token = environ.get("INPUT_TOKEN")
if auth_token is None \
and environ['GITHUB_SERVER_URL'] == serverurl:
auth_token = environ['GITHUB_TOKEN']
# auth-url
cloneurl = urlunparse(serverurl_p._replace(
netloc=f'{auth_token}@{serverurl_p.netloc}'
if auth_token else serverurl_p.netloc,
path=f'{serverurl_p.path}/{repo}.git'
if serverurl_p.path else f'{repo}.git'
))
return cloneurl
def get_dest_path() -> str:
path = environ.get('INPUT_PATH')
if path:
path = f"{environ.get('GITHUB_WORKSPACE', '.')}/{path}"
run(['mkdir', '-p', path])
else:
path = '.'
return path
def get_checkout_ref() -> str:
ref = environ.get('INPUT_REF')
if not ref and not environ.get('INPUT_REPOSITORY'):
ref = environ.get('GITHUB_SHA')
return ref
def clone() -> None:
clone_cmd = [
'datalad',
'-c', 'annex.private=true',
'clone',
]
clone_cmd.append(get_clone_url())
dest_path = get_dest_path()
# provide output with the final clone destination as an absolute path
with open(environ['GITHUB_OUTPUT'], 'a') as fp:
fp.write(f'path={abspath(dest_path)}\n')
clone_cmd.append(dest_path)
run(clone_cmd, check=True)
ref = get_checkout_ref()
if ref:
run(['git', '-C', dest_path, 'checkout', ref])
return dest_path
def get_content_spec() -> list:
content = environ['INPUT_GET_CONTENT']
return sh_split(content)
def get_content(repo_path: str) -> None:
if not environ.get('INPUT_GET_CONTENT') \
and not environ.get('INPUT_SUBMODULES'):
# no recursion, not content to get, nothing to do
return
get_cmd = [
'datalad',
'-c', 'annex.private=true',
'get',
]
recursive = environ.get('INPUT_RECURSIVE')
if recursive and recursive != 'false':
if recursive == 'true':
get_cmd.append('--recursive')
else:
get_cmd.extend((
'--recursive',
'--recursion-limit',
recursive,
))
if environ.get('INPUT_GET_FILE_CONTENT') == 'false':
get_cmd.append('--no-data')
get_cmd.extend(get_content_spec())
run(
get_cmd,
cwd=repo_path,
check=True,
)
def main() -> None:
if bool(environ.get('DATALAD_CLONE_ACTION_DEBUG', '')):
show_debug_info()
# datalad cannot exist without knowing about the user
setup_git_identity()
repo_path = clone()
get_content(repo_path)
if __name__ == "__main__":
main()