Skip to content

Commit 210c44d

Browse files
authored
sync base branch with master (demisto#8536)
1 parent 599072b commit 210c44d

4 files changed

Lines changed: 194 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Sync contributor base branch
2+
on:
3+
schedule:
4+
# runs at minute 0 of the 0th and 12th hour every day.
5+
- cron: '0 0,12 * * *'
6+
7+
jobs:
8+
sync_contributor_base_branch:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout
12+
uses: actions/checkout@v2
13+
- name: Setup Python
14+
uses: actions/setup-python@v2
15+
with:
16+
python-version: '3.8'
17+
- name: Install Python Dependencies
18+
run: |
19+
python -m pip install --upgrade pip
20+
pip install pipenv
21+
- name: Sync Base Branch to Master
22+
env:
23+
CONTENTBOT_GH_ADMIN_TOKEN: ${{ secrets.CONTENTBOT_GH_ADMIN_TOKEN }}
24+
run: |
25+
echo "Updating contribution base branches (contrib/*)"
26+
cd Utils/contribution_sync
27+
pipenv install --dev
28+
pipenv run ./sync_contrib_base.py
29+
echo "Finished updating base branches"

Utils/contribution_sync/Pipfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[[source]]
2+
name = "pypi"
3+
url = "https://pypi.org/simple"
4+
verify_ssl = true
5+
6+
[dev-packages]
7+
8+
[packages]
9+
pygithub = "*"
10+
requests = "*"
11+
12+
[requires]
13+
python_version = "3.8"

Utils/contribution_sync/Pipfile.lock

Lines changed: 85 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

Comments
 (0)