Last active
November 18, 2021 13:55
-
-
Save phucngta/9a67f8c66f4466167695882fc7f144d8 to your computer and use it in GitHub Desktop.
Auto create merge request Report to Production
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import gitlab | |
| import os | |
| GITLAB_PERSONAL_TOKEN = os.environ['GITLAB_PERSONAL_TOKEN'] | |
| CI_PROJECT_NAME = os.environ['CI_PROJECT_NAME'] | |
| PROJECT_ID = os.environ['CI_PROJECT_ID'] | |
| CI_SERVER_HOST = os.environ['CI_SERVER_HOST'] | |
| SERVER_URL = f'https://{CI_SERVER_HOST}' | |
| gl = gitlab.Gitlab(SERVER_URL, private_token=GITLAB_PERSONAL_TOKEN) | |
| project_name_with_namespace = "thanh/besco_minhdang" | |
| if CI_PROJECT_NAME in ['vinawood']: | |
| target_branchs = ['staging'] | |
| elif CI_PROJECT_NAME in ['besco_minhdang']: | |
| target_branchs = ['production'] | |
| SOURCE_BRANCH = 'report' | |
| project = gl.projects.get(PROJECT_ID) | |
| def create_merge_request(): | |
| for tg_branch in target_branchs: | |
| mrs = project.mergerequests.list( | |
| query_parameters={ | |
| 'state': 'opened', 'source_branch': {SOURCE_BRANCH}, 'target_branch': tg_branch} | |
| ) | |
| if not mrs: | |
| print(f"Create new Merge Request branch {SOURCE_BRANCH} into {tg_branch}") | |
| mr = project.mergerequests.create( | |
| { | |
| 'source_branch': SOURCE_BRANCH, | |
| 'target_branch': tg_branch, | |
| 'title': "WIP: Sync Modify Report in Nextcloud to Production", | |
| 'labels': ['report-nextcloud'] | |
| } | |
| ) | |
| else: | |
| mr = mrs[0] | |
| if not mr.changes()['changes']: | |
| print(f"Close Empty Merge Request ({SOURCE_BRANCH} into {tg_branch})") | |
| mr.state_event = 'close' | |
| mr.save() | |
| if __name__ == "__main__": | |
| create_merge_request() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment