How to create a merge request at the end of a successful pipeline in Gitlab?

后端 未结 3 1430
孤独总比滥情好
孤独总比滥情好 2021-02-06 02:15

I\'m very new to gitlab and gitlab CI and I have put a pipeline in place that is successfully completing. My master and development branches are protected so a merge request is

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-06 03:05

    Short Answer: Sure - anything's possible. GitLab has a great API (including creating an MR). But I think going that route is bad form. You should utilize GitLab as it's designed. You're starting your Merge Request too late. Start it before you begin any work and your Merge Request will remain open the entire duration of your branch.

    Long Answer: This is the ideal GitLab workflow:

    1. Someone creates an ISSUE against a repository. Maybe a feature request, maybe an actual problem, whatever - someone wants something changed, so it's an 'issue'
    2. Developer opens the issue and clicks CREATE MERGE REQUEST
      • This generates an Merge Request (MR), a matching branch, and ties it back to the issue
    3. Developer works the branch, pushing changes as they go
    4. Developer gets a passing pipeline and hits "Resolve WIP" on that merge request page when they're ready for the customer to preview the work and/or another developer to code review.
    5. From here, have that reviewer either click MERGE when done reviewing or even better, turn on APPROVALS in repository settings and set the people or groups of people you want reviews from.
    6. Next to the merge button, be sure to delete the source branch (for sanity), and merged code will automatically close the issue - and link all 3 elements together.

    This is fundamentally backward from the way GitHub works (which I came from) where you don't have to tell people what you're working on.

    • Pull Requests on GitHub are created when the work is finished and you want to merge into master.
    • Merge Requests on GitLab are created when the work is beginning and you want to tell the world you're about to embark on working on a feature. This allows for people to do a quick shutdown if it's not needed or prevents multiple devs from duplicating effort.

    EDIT: It sounds like you're interested in leveraging the API. There's a python package called 'python-gitlab' actually that works decently http://python-gitlab.readthedocs.io/en/stable/gl_objects/mrs.html

    import gitlab
    import os
    
    origin = "https://gitlab.example.com"
    # Private token is set as an env var
    gl = gitlab.Gitlab(origin, private_token, api_version='4')
    gl.auth()
    
    def create_merge_request(origin, private_token):
        mr = project.mergerequests.create({'source_branch': 'cool_feature',
                                   'target_branch': 'master',
                                   'title': 'merge cool feature',
                                   'labels': ['label1', 'label2']})
        mr.assignee_id = gl.users.get(2).id # Assign it to coworker
    
    def lookup_last_pipeline(origin, private_token):
        current_pipeline_id = os.environ['CI_PIPELINE_ID']
        pipelines = gl.projects.get(os.environ['CI_PROJECT_ID']).pipelines.list()
        for pipeline in pipelines:
            if pipeline.status == 'success' and pipeline.id == current_pipeline_id:
                create_merge_request()
    

    This is of course an example, you'll have to adapt it to your precise needs.

提交回复
热议问题