diff --git a/README.md b/README.md index 1fc3d7d..082231d 100644 --- a/README.md +++ b/README.md @@ -29,9 +29,16 @@ Syntax: $ oca-port [options] $ oca-port --help -To check if an addon could be migrated or to get eligible commits to port: +GITHUB_TOKEN can be passed by exposing to environment: $ export GITHUB_TOKEN= + +Alternatively, you can pass the token directly using the `--github-token` option + +If neither method is used, the tool will attempt to obtain the token using the `gh` client (if it's installed). + +To check if an addon could be migrated or to get eligible commits to port: + $ cd $ oca-port origin/16.0 origin/18.0 --verbose --dry-run diff --git a/oca_port/app.py b/oca_port/app.py index 1eda9c9..098c7e9 100644 --- a/oca_port/app.py +++ b/oca_port/app.py @@ -1,6 +1,5 @@ # Copyright 2022 Camptocamp SA # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl) -import os import pathlib from dataclasses import dataclass import re @@ -109,7 +108,7 @@ def __post_init__(self): self._check_branch_exists(self.source.ref, raise_exc=True) self._check_branch_exists(self.target.ref, raise_exc=True) # GitHub API helper - self.github = GitHub(self.github_token or os.environ.get("GITHUB_TOKEN")) + self.github = GitHub(self.github_token) # Initialize storage & cache self.storage = utils.storage.InputStorage(self.to_branch, self.addon) self.cache = utils.cache.UserCacheFactory(self).build() diff --git a/oca_port/cli/main.py b/oca_port/cli/main.py index 5e6ce0a..74a5ffe 100644 --- a/oca_port/cli/main.py +++ b/oca_port/cli/main.py @@ -98,6 +98,12 @@ @click.option("--fetch", is_flag=True, help="Fetch remote branches from upstream.") @click.option("--no-cache", is_flag=True, help="Disable user's cache.") @click.option("--clear-cache", is_flag=True, help="Clear the user's cache.") +@click.option( + "--github-token", + is_flag=True, + help="""Token to use when requesting GitHub API (highly recommended + to not trigger the "API rate limit exceeded" error).""", +) def main( addon_path: str, source: str, @@ -114,6 +120,7 @@ def main( no_cache: bool, clear_cache: bool, dry_run: bool, + github_token: str, ): """Migrate ADDON from SOURCE to TARGET or list Pull Requests to port. @@ -148,6 +155,7 @@ def main( clear_cache=clear_cache, dry_run=dry_run, cli=True, + github_token=github_token, ) except ForkValueError as exc: error_msg = prepare_remote_error_msg(*exc.args) diff --git a/oca_port/utils/github.py b/oca_port/utils/github.py index 77b89c1..9b622b6 100644 --- a/oca_port/utils/github.py +++ b/oca_port/utils/github.py @@ -3,6 +3,8 @@ import re +import os +import subprocess import requests from .git import PullRequest @@ -12,6 +14,8 @@ class GitHub: def __init__(self, token=None): + if not token: + token = self._get_token() self.token = token def request(self, url: str, method: str = "get", params=None, json=None): @@ -77,3 +81,16 @@ def search_migration_pr( def _addon_in_text(self, addon: str, text: str): """Return `True` if `addon` is present in `text`.""" return any(addon == term for term in re.split(r"\W+", text)) + + @staticmethod + def _get_token(): + token = os.environ.get("GITHUB_TOKEN") + if not token: + try: + # get from gh + token = subprocess.check_output( + ["gh", "auth", "token"], text=True + ).strip() + except subprocess.SubprocessError: + pass + return token