From fa502358319ae320123270748b367827cda559a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Wed, 1 May 2024 18:23:19 +0200 Subject: [PATCH] Improve gh login Use `gh auth token` as a third source of GitHub token --- README.md | 3 +++ tools/github_login.py | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/README.md b/README.md index acb3dee6..6ba67fc2 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,9 @@ Set and store the token to be used for Github auth using: Alternatively, the token can be set on the GITHUB_TOKEN environment variable. +As a third alternative, if you have the [gh](https://cli.github.com/) client installed +and have authenticated with `gh auth login`, maintainer tools will attempt to obtain the +token from it using `gh auth token`. ### Sync team users from community.odoo.com to GitHub teams diff --git a/tools/github_login.py b/tools/github_login.py index e08f096c..e253f620 100644 --- a/tools/github_login.py +++ b/tools/github_login.py @@ -4,6 +4,7 @@ import argparse import os +import subprocess from getpass import getpass import github3 from .config import read_config, write_config @@ -19,6 +20,14 @@ def login(): else: config = read_config() token = config.get("GitHub", "token") + if not token: + try: + # attempt to get token from gh + token = subprocess.check_output( + ["gh", "auth", "token"], text=True + ).strip() + except subprocess.SubprocessError: + pass if not token: raise GitHubLoginError( "No token has been generated for this script. "