Query a GitHub API endpoint. Parameters: endpoint (str): The URL of the endpoint to query. Returns: A JSON object from the result of the query.
(endpoint: str)
| 141 | |
| 142 | |
| 143 | def get_gh_json(endpoint: str) -> dict[str, Any]: |
| 144 | """ |
| 145 | Query a GitHub API endpoint. |
| 146 | |
| 147 | Parameters: |
| 148 | endpoint (str): The URL of the endpoint to query. |
| 149 | |
| 150 | Returns: |
| 151 | A JSON object from the result of the query. |
| 152 | """ |
| 153 | curl_cmd = ['curl', '-LSs'] |
| 154 | if 'GITHUB_TOKEN' in os.environ: |
| 155 | # https://docs.github.com/en/rest/overview/authenticating-to-the-rest-api |
| 156 | curl_cmd += [ |
| 157 | '-H', |
| 158 | 'Accept: application/vnd.github+json', |
| 159 | '-H', |
| 160 | f"Authorization: Bearer {os.environ['GITHUB_TOKEN']}", |
| 161 | ] |
| 162 | curl_cmd.append(endpoint) |
| 163 | |
| 164 | try: |
| 165 | curl_out = subprocess.run(curl_cmd, capture_output=True, check=True, text=True).stdout |
| 166 | except subprocess.CalledProcessError as err: |
| 167 | msg = f"Failed to query GitHub API at {endpoint}: {err.stderr}" |
| 168 | raise RuntimeError(msg) from err |
| 169 | |
| 170 | return json.loads(curl_out) |
| 171 | |
| 172 | |
| 173 | def green(string: str) -> None: |