| 245 | |
| 246 | |
| 247 | class GitHubAPI(object): |
| 248 | |
| 249 | def __init__(self, project_name, cmd): |
| 250 | self.github_api = ( |
| 251 | f"https://api.github.com/repos/{ORG_NAME}/{project_name}" |
| 252 | ) |
| 253 | |
| 254 | token = None |
| 255 | config = load_configuration() |
| 256 | if "github" in config.sections(): |
| 257 | token = config["github"]["api_token"] |
| 258 | if not token: |
| 259 | token = os.environ.get('GH_TOKEN') |
| 260 | if not token: |
| 261 | token = os.environ.get('ARROW_GITHUB_API_TOKEN') |
| 262 | if token: |
| 263 | print('ARROW_GITHUB_API_TOKEN environment variable is ' |
| 264 | 'deprecated. Use GH_TOKEN environment variable instead.') |
| 265 | if not token: |
| 266 | token = cmd.prompt('Env GH_TOKEN nor ' |
| 267 | 'ARROW_GITHUB_API_TOKEN not set, ' |
| 268 | 'please enter your GitHub API token ' |
| 269 | '(GitHub personal access token):') |
| 270 | headers = { |
| 271 | 'Accept': 'application/vnd.github.v3+json', |
| 272 | 'Authorization': f'token {token}', |
| 273 | } |
| 274 | self.headers = headers |
| 275 | |
| 276 | def get_milestones(self): |
| 277 | return get_json("%s/milestones" % (self.github_api, ), |
| 278 | headers=self.headers) |
| 279 | |
| 280 | def get_milestone_number(self, version): |
| 281 | return next(( |
| 282 | m["number"] for m in self.get_milestones() if m["title"] == version |
| 283 | ), None) |
| 284 | |
| 285 | def get_issue_data(self, number): |
| 286 | return get_json("%s/issues/%s" % (self.github_api, number), |
| 287 | headers=self.headers) |
| 288 | |
| 289 | def get_pr_data(self, number): |
| 290 | return get_json("%s/pulls/%s" % (self.github_api, number), |
| 291 | headers=self.headers) |
| 292 | |
| 293 | def get_pr_commits(self, number): |
| 294 | return get_json("%s/pulls/%s/commits" % (self.github_api, number), |
| 295 | headers=self.headers) |
| 296 | |
| 297 | def get_branches(self): |
| 298 | return get_json("%s/branches" % (self.github_api), |
| 299 | headers=self.headers) |
| 300 | |
| 301 | def close_issue(self, number, comment): |
| 302 | issue_url = f'{self.github_api}/issues/{number}' |
| 303 | comment_url = f'{self.github_api}/issues/{number}/comments' |
| 304 | |