Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] intergrate gh tool #63

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,16 @@ Syntax:
$ oca-port <source> <target> <module_path> [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=<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 <path/to/OCA/cloned_repository>
$ oca-port origin/16.0 origin/18.0 <module_path> --verbose --dry-run

Expand Down
3 changes: 1 addition & 2 deletions oca_port/app.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions oca_port/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.

Expand Down Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions oca_port/utils/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import re

import os
import subprocess
import requests

from .git import PullRequest
Expand All @@ -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):
Expand Down Expand Up @@ -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
Loading