|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import urllib3 |
| 6 | +from github import Github, enable_console_debug_logging |
| 7 | +from github.Repository import Repository |
| 8 | +from typing import List |
| 9 | + |
| 10 | + |
| 11 | +urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 12 | + |
| 13 | + |
| 14 | +def get_master_commit_sha(repo: Repository) -> str: # noqa: E999 |
| 15 | + '''Return the sha commit of the master branch |
| 16 | +
|
| 17 | + Args: |
| 18 | + repo (Repository): The repository whose master branch will be queried |
| 19 | +
|
| 20 | + Returns: |
| 21 | + (str): The commit sha of the master branch's HEAD |
| 22 | + ''' |
| 23 | + branch_data = repo.get_branch('master') |
| 24 | + commit_sha = branch_data.commit.sha |
| 25 | + return commit_sha |
| 26 | + |
| 27 | + |
| 28 | +def get_branch_names_with_contrib(repo: Repository) -> List[str]: # noqa: E999 |
| 29 | + '''Return the list of branches that have the prefix of "contrib/" and that are base branches of open PRs |
| 30 | +
|
| 31 | + Args: |
| 32 | + repo (Repository): The repository whose branches will be searched and listed |
| 33 | +
|
| 34 | + Returns: |
| 35 | + (List[str]): List of branch names that have the "contrib/" prefix and are base branches of open PRs |
| 36 | + ''' |
| 37 | + branch_names = [] |
| 38 | + for branch in repo.get_branches(): |
| 39 | + if branch.name.startswith('contrib/'): |
| 40 | + prs_with_branch_as_base = repo.get_pulls(state='OPEN', base=branch.name) |
| 41 | + if prs_with_branch_as_base.totalCount >= 1: |
| 42 | + branch_names.append(branch.name) |
| 43 | + return branch_names |
| 44 | + |
| 45 | + |
| 46 | +def main(): |
| 47 | + debug_mode = len(sys.argv) >= 2 and 'debug' in sys.argv[1].casefold |
| 48 | + if debug_mode: |
| 49 | + enable_console_debug_logging() |
| 50 | + gh = Github(os.getenv('CONTENTBOT_GH_ADMIN_TOKEN'), verify=False) |
| 51 | + organization = 'demisto' |
| 52 | + repo = 'content' |
| 53 | + content_repo = gh.get_repo(f'{organization}/{repo}') |
| 54 | + |
| 55 | + master_sha = get_master_commit_sha(content_repo) |
| 56 | + contrib_base_branches = get_branch_names_with_contrib(content_repo) |
| 57 | + for branch_name in contrib_base_branches: |
| 58 | + git_ref = content_repo.get_git_ref(f'heads/{branch_name}') |
| 59 | + print(f'Updating branch "{branch_name}" to sha "{master_sha}"') |
| 60 | + git_ref.edit(master_sha, force=True) |
| 61 | + |
| 62 | + if debug_mode: |
| 63 | + print(f'{contrib_base_branches=}') |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + main() |
0 commit comments