Article Preface
Every Friday morning by 12:00 am you need to merge the development branches of each group on the project into the soft set repository branch.Need to merge on ten projects
Programmers are generallyI hate trouble., so write this script forBatch merge, unified merge
In addition, if the project is going through a go-live or bug fixing scenario, and you need to merge branches to uat, release, master, etc., you can also use this script.
Functional scripts such as these are typically written in Python.Calls are made through the REST API provided by GitLab.
GitLab provides very rich REST API operations, you can search the official site to see the
Scripting Ideas
The script is a part of the scripts provided by people on the Internet, and the project itself provides this idea
- Getting a Personal Token in GitLab
- Get the IDs of the items to be merged
- Get the source and target branches of the project to be merged.
- Verify that the GitLab corresponds to the correct Token
- Make a merge request, failing to do so deletes the corresponding merge request.
Most of the implementations found on the internet were unsuccessful, andIt might have worked before., but withGitLab's API 3->4 UpgradeMost of the scripts on the Internet fail.
Pre-Operation Preparation
First, the computer needs to beInstall the python3 runtime environment, and download the import package dependencies.The most important thing isGet Token
Log in to the GitLab settings page, and click Access Token.
I set it to Chinese, but it doesn't make much of a difference, and the token is still recognizable.
Enter the Token name, Token validity period, and the corresponding permission range, and then enter the corresponding Token.Just copy it to the corresponding script
Specific implementation code
import import gitlab # ========================================Configuration Start================================================= # ⚠️ Project name, not meaningful project_name = "xxxx" # ⚠️ represents multiple project IDs, usually one project is enough, multiple projects need to make sure source_branch, target_branches are consistent. project_id_list = ['xxxx'] # ⚠️ Generating gitlab objects gitlab_url = 'xxxx' # ⚠️ need to generate your own token token token = 'xxxx' # ⚠️ merge target_branches on source_branch branches source_branch = 'test' target_branches = ['master'] # =========================================End of configuration================================================ # Authenticate login gl = (gitlab_url, token) def main(): for project_id in project_id_list: project = (project_id) print(' >>> Project ID :: [%s], Project Name :: [%s] ' % (str(), project_name)) for target in target_branches: print(" >>> Merging project :: [%s] branch of [%s] to [%s] branch " % (project_name, source_branch, target)) # mr merge requested objects mr = None try: # Create merge request mr = ({'source_branch': source_branch, 'target_branch': target, 'title': "Merge branch '%s' into '%s'" % ( source_branch, target) }) # Requests for consolidation accepted url = '%s/api/v4/projects/%s/merge_requests/%s/merge' % (gitlab_url, , ) print(' >>> Merge url :: [%s], title :: [%s] ' % (url, )) # Individual/project access tokens, added to request header headers = {"PRIVATE-TOKEN": token} req = (url=url, headers=headers, method="PUT") resp = (req) if == 200: print(" >>> Merge to target branch [%s] Success, end... \r\n" % target) else: () except Exception as e: print(" >>> Merge error, there may be a conflict that has not been resolved or the [%s] branch has not been updated, exception message:: \r\n" % source_branch, str(e)) # Delete the merge request that was created () if __name__ == "__main__": main()
summarize
To this point this article on Python3 using the GitLab API to merge branches in bulk is introduced to this article, more related to Python merge branch content, please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!