(
*,
request_circle_token: bool = False,
request_github_token: bool = True,
)
| 122 | |
| 123 | |
| 124 | def read_config( |
| 125 | *, |
| 126 | request_circle_token: bool = False, |
| 127 | request_github_token: bool = True, |
| 128 | ) -> Config: # noqa: C901 |
| 129 | config = configparser.ConfigParser() |
| 130 | |
| 131 | config_path = None |
| 132 | current_dir = Path(os.getcwd()) |
| 133 | |
| 134 | while current_dir != current_dir.parent: |
| 135 | tentative_config_path = "/".join([str(current_dir), ".ghstackrc"]) |
| 136 | if os.path.exists(tentative_config_path): |
| 137 | config_path = tentative_config_path |
| 138 | break |
| 139 | current_dir = current_dir.parent |
| 140 | |
| 141 | write_back = False |
| 142 | if config_path is None: |
| 143 | config_path = str( |
| 144 | get_path_from_env_var(GHSTACKRC_PATH_VAR) or DEFAULT_GHSTACKRC_PATH |
| 145 | ) |
| 146 | write_back = True |
| 147 | |
| 148 | logging.debug(f"config_path = {config_path}") |
| 149 | config.read([".ghstackrc", config_path]) |
| 150 | |
| 151 | if not config.has_section("ghstack"): |
| 152 | config.add_section("ghstack") |
| 153 | write_back = True |
| 154 | |
| 155 | if config.has_option("ghstack", "github_url"): |
| 156 | github_url = config.get("ghstack", "github_url") |
| 157 | else: |
| 158 | github_url = input("GitHub enterprise domain (leave blank for OSS GitHub): ") |
| 159 | if not github_url: |
| 160 | github_url = "github.com" |
| 161 | if not re.match(r"[\w\.-]+\.\w+$", github_url): |
| 162 | raise RuntimeError( |
| 163 | f"{github_url} is not a valid domain name (do not include http:// scheme)" |
| 164 | ) |
| 165 | config.set("ghstack", "github_url", github_url) |
| 166 | write_back = True |
| 167 | |
| 168 | # Environment variable overrides config file |
| 169 | # This envvar is legacy from ghexport days |
| 170 | github_oauth = os.getenv("OAUTH_TOKEN") |
| 171 | gh_cli_username = None # Track username from gh CLI |
| 172 | if github_oauth is not None: |
| 173 | logging.warning( |
| 174 | "Deprecated OAUTH_TOKEN environment variable used to populate github_oauth--" |
| 175 | "this is probably not what you intended; unset OAUTH_TOKEN from your " |
| 176 | "environment to use the setting in .ghstackrc instead." |
| 177 | ) |
| 178 | if github_oauth is None and config.has_option("ghstack", "github_oauth"): |
| 179 | github_oauth = config.get("ghstack", "github_oauth") |
| 180 | |
| 181 | # Try GitHub CLI if available and no token found yet |
nothing calls this directly
no test coverage detected