diff --git a/src/ghstack/cli.py b/src/ghstack/cli.py index 7b79aed..b178aa1 100644 --- a/src/ghstack/cli.py +++ b/src/ghstack/cli.py @@ -141,6 +141,7 @@ def land(force: bool, pull_request: str) -> None: github_url=config.github_url, remote_name=config.remote_name, force=force, + repo_default_branch_opt=config.repo_default_branch, ) @@ -271,6 +272,11 @@ def submit( revs=revs, stack=stack, direct_opt=direct_opt, + repo_name_opt=config.repo_name, + repo_owner_opt=config.repo_owner, + repo_is_fork_opt=config.repo_is_fork, + repo_id_opt=config.repo_id, + repo_default_branch_opt=config.repo_default_branch, ) @@ -287,4 +293,5 @@ def unlink(commits: List[str]) -> None: sh=shell, github_url=config.github_url, remote_name=config.remote_name, + repo_default_branch_opt=config.repo_default_branch, ) diff --git a/src/ghstack/config.py b/src/ghstack/config.py index 72a91d0..e8ad17d 100644 --- a/src/ghstack/config.py +++ b/src/ghstack/config.py @@ -40,6 +40,16 @@ ("github_url", str), # Name of the upstream remote ("remote_name", str), + # Repository name + ("repo_name", Optional[str]), + # Repository owner / organisation + ("repo_owner", Optional[str]), + # Whether the repo is a fork + ("repo_is_fork", Optional[str]), + # Numeric GitHub repository ID + ("repo_id", Optional[str]), + # Default branch name (e.g. "main") + ("repo_default_branch", Optional[str]), ], ) @@ -208,6 +218,26 @@ def read_config( config.write(f) logging.info("NB: configuration saved to {}".format(config_path)) + repo_name = None + if config.has_option("repo", "name"): + repo_name = config.get("repo", "name") + + repo_owner = None + if config.has_option("repo", "owner"): + repo_owner = config.get("repo", "owner") + + repo_is_fork = None + if config.has_option("repo", "is_fork"): + repo_is_fork = config.getboolean("repo", "is_fork") + + repo_id = None + if config.has_option("repo", "id"): + repo_id = config.get("repo", "id") + + repo_default_branch = None + if config.has_option("repo", "default_branch"): + repo_default_branch = config.get("repo", "default_branch") + conf = Config( github_oauth=github_oauth, circle_token=circle_token, @@ -218,6 +248,11 @@ def read_config( default_project_dir=default_project_dir, github_url=github_url, remote_name=remote_name, + repo_name=repo_name, + repo_owner=repo_owner, + repo_is_fork=repo_is_fork, + repo_id=repo_id, + repo_default_branch=repo_default_branch, ) logging.debug(f"conf = {conf}") return conf diff --git a/src/ghstack/land.py b/src/ghstack/land.py index 4a96d60..5e411fe 100644 --- a/src/ghstack/land.py +++ b/src/ghstack/land.py @@ -2,7 +2,7 @@ import logging import re -from typing import List, Tuple +from typing import List, Optional, Tuple import ghstack.git import ghstack.github @@ -50,6 +50,7 @@ def main( github_url: str, *, force: bool = False, + repo_default_branch_opt: Optional[str] = None, ) -> None: # We land the entire stack pointed to by a URL. @@ -60,14 +61,16 @@ def main( params = ghstack.github_utils.parse_pull_request( pull_request, sh=sh, remote_name=remote_name ) - default_branch = ghstack.github_utils.get_github_repo_info( - github=github, - sh=sh, - repo_owner=params["owner"], - repo_name=params["name"], - github_url=github_url, - remote_name=remote_name, - )["default_branch"] + default_branch = repo_default_branch_opt + if default_branch is None: + default_branch = ghstack.github_utils.get_github_repo_info( + github=github, + sh=sh, + repo_owner=params["owner"], + repo_name=params["name"], + github_url=github_url, + remote_name=remote_name, + )["default_branch"] needs_force = False try: diff --git a/src/ghstack/submit.py b/src/ghstack/submit.py index f7910c5..797d00f 100644 --- a/src/ghstack/submit.py +++ b/src/ghstack/submit.py @@ -308,6 +308,11 @@ class Submitter: # Presents as repo_name kwarg in main repo_name_opt: Optional[str] = None + # allow configuration to optionally define repo details + repo_is_fork_opt: Optional[str] = None + repo_id_opt: Optional[str] = None + repo_default_branch_opt: Optional[str] = None + # Print only PR URL to stdout short: bool = False @@ -375,17 +380,32 @@ class Submitter: # Post initialization def __post_init__(self) -> None: - # Network call in the constructor, help me father, for I have sinned - repo = ghstack.github_utils.get_github_repo_info( - github=self.github, - sh=self.sh, - repo_owner=self.repo_owner_opt, - repo_name=self.repo_name_opt, - github_url=self.github_url, - remote_name=self.remote_name, - ) - object.__setattr__(self, "repo_owner", repo["name_with_owner"]["owner"]) - object.__setattr__(self, "repo_name", repo["name_with_owner"]["name"]) + repo = {} + if ( + self.repo_id_opt is not None + and self.repo_owner_opt is not None + and self.repo_name_opt is not None + and self.repo_is_fork_opt is not None + and self.repo_default_branch_opt is not None + ): + repo["is_fork"] = self.repo_is_fork_opt + repo["id"] = self.repo_id_opt + repo["default_branch"] = self.repo_default_branch_opt + repo["name_with_owner"] = {} + repo["name_with_owner"]["owner"] = self.repo_owner_opt + repo["name_with_owner"]["name"] = self.repo_name_opt + else: + # Network call in the constructor, help me father, for I have sinned + repo = ghstack.github_utils.get_github_repo_info( + github=self.github, + sh=self.sh, + repo_owner=self.repo_owner_opt, + repo_name=self.repo_name_opt, + github_url=self.github_url, + remote_name=self.remote_name, + ) + object.__setattr__(self, "repo_owner", repo["name_with_owner"]["owner"]) + object.__setattr__(self, "repo_name", repo["name_with_owner"]["name"]) if repo["is_fork"]: raise RuntimeError( diff --git a/src/ghstack/unlink.py b/src/ghstack/unlink.py index 611f2e6..ce790f3 100644 --- a/src/ghstack/unlink.py +++ b/src/ghstack/unlink.py @@ -25,6 +25,7 @@ def main( repo_name: Optional[str] = None, github_url: str, remote_name: str, + repo_default_branch_opt: Optional[str] = None, ) -> GitCommitHash: # If commits is empty, we unlink the entire stack # @@ -36,14 +37,16 @@ def main( # Use CWD sh = ghstack.shell.Shell() - default_branch = ghstack.github_utils.get_github_repo_info( - github=github, - sh=sh, - repo_owner=repo_owner, - repo_name=repo_name, - github_url=github_url, - remote_name=remote_name, - )["default_branch"] + default_branch = repo_default_branch_opt + if default_branch is None: + default_branch = ghstack.github_utils.get_github_repo_info( + github=github, + sh=sh, + repo_owner=repo_owner, + repo_name=repo_name, + github_url=github_url, + remote_name=remote_name, + )["default_branch"] # Parse the commits parsed_commits: Optional[Set[GitCommitHash]] = None