diff --git a/.gitea/workflows/test-python.yml b/.gitea/workflows/test-python.yml index 10ac984..5f982e5 100644 --- a/.gitea/workflows/test-python.yml +++ b/.gitea/workflows/test-python.yml @@ -20,7 +20,7 @@ jobs: python-version: '3.14' - name: Check Authentication - run: python3 python/check_github_token.py "${{ secrets.GH_TOKEN }}" + run: python3 python/install_github_release.py --check-token -k "${{ secrets.GH_TOKEN }}" - name: Go ACME Setup run: python3 python/install_github_release.py go-acme/lego -t archive -k "${{ secrets.GH_TOKEN }}" -p /tmp/python-cli-test diff --git a/.github/workflows/test-python.yml b/.github/workflows/test-python.yml index ba521f9..e47099b 100644 --- a/.github/workflows/test-python.yml +++ b/.github/workflows/test-python.yml @@ -20,7 +20,7 @@ jobs: python-version: '3.14' - name: Check Authentication - run: python3 python/check_github_token.py "${{ github.token }}" + run: python3 python/install_github_release.py --check-token -k "${{ github.token }}" - name: Go ACME Setup run: python3 python/install_github_release.py go-acme/lego -t archive -k "${{ github.token }}" -p /tmp/python-cli-test diff --git a/python/check_github_token.py b/python/check_github_token.py deleted file mode 100644 index 8f1c0b1..0000000 --- a/python/check_github_token.py +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env python3 - -import argparse -import os -import sys - -from install_github_release import fetch_latest_release - - -def main(): - """Parse command-line arguments and verify a GitHub token, reporting errors via the exit code.""" - parser = argparse.ArgumentParser( - usage='%(prog)s [token]', - allow_abbrev=False, - ) - parser.add_argument('token', nargs='?', help='The GitHub token to verify') - args = parser.parse_args() - - token = args.token or os.environ.get('GITHUB_TOKEN') - if not token: - print('Error: No GitHub token provided as an argument or found in GITHUB_TOKEN environment variable.', file=sys.stderr) - sys.exit(1) - - print('Verifying GitHub token...') - try: - fetch_latest_release('actions/checkout', token) - print('\x1b[32mSuccess: The provided GitHub token is valid and has sufficient permissions to access public repositories.\x1b[0m') - except Exception as error: - print('\x1b[31mError: GitHub token verification failed.\x1b[0m', file=sys.stderr) - print(f'Reason: {error}', file=sys.stderr) - sys.exit(1) - - -if __name__ == '__main__': - main() diff --git a/python/install_github_release.py b/python/install_github_release.py index 2bb96a5..7cfa580 100755 --- a/python/install_github_release.py +++ b/python/install_github_release.py @@ -79,6 +79,13 @@ def fetch_latest_release(repository, token=None): raise RuntimeError(f'Failed to fetch latest release for {repository}: {error.reason}. {body}') from error +def check_github_token(token): + """Verify a GitHub token by fetching the latest release of actions/checkout.""" + print('Verifying GitHub token...') + fetch_latest_release('actions/checkout', token) + print('Success: The provided GitHub token is valid and has sufficient permissions to access public repositories.') + + def download_asset(url, dest_path, token=None): """Download a release asset from url and write it to dest_path.""" headers = {'User-Agent': 'install-github-release-cli'} @@ -333,7 +340,7 @@ _install_github_release_complete() { COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" - opts="--dry-run -l --list -a --app-name -f --file-name -b --binary-name -t --file-type -p --install-path -o --output-directory -j --releases-json --system --arch -k --token -d --debug --completion -h --help" + opts="--dry-run -l --list -a --app-name -f --file-name -b --binary-name -t --file-type -p --install-path -o --output-directory -j --releases-json --system --arch -k --token -d --debug --completion --check-token -h --help" case "$prev" in -t|--file-type) @@ -376,6 +383,7 @@ _install_github_release() { '(-d --debug)'{-d,--debug}'[Enable debug logging]' '--dry-run[Run in test mode]' '--completion[Print shell completion script]:shell:(bash zsh)' + '--check-token[Verify the GitHub token against actions/checkout and exit]' '(-h --help)'{-h,--help}'[Show help message]' '1:repository:' ) @@ -413,12 +421,25 @@ def main(): parser.add_argument('-k', '--token', metavar='token', help='GitHub token') parser.add_argument('-d', '--debug', action='store_true', help='Enable debug logging') parser.add_argument('--completion', choices=['bash', 'zsh'], help='Print a shell completion script and exit') + parser.add_argument('--check-token', action='store_true', help='Verify the GitHub token against actions/checkout and exit') args = parser.parse_args() if args.completion: print(BASH_COMPLETION if args.completion == 'bash' else ZSH_COMPLETION, end='') sys.exit(0) + if args.check_token: + token = args.token or os.environ.get('GITHUB_TOKEN') + if not token: + print('Error: No GitHub token provided as an argument or found in GITHUB_TOKEN environment variable.', file=sys.stderr) + sys.exit(1) + try: + check_github_token(token) + except Exception as error: + print(f'Error: {error}', file=sys.stderr) + sys.exit(1) + sys.exit(0) + list_repo = args.list if isinstance(args.list, str) else None repository = list_repo or args.repository if not repository: