Code review tutorial based on Gitlab

Source: Internet
Author: User
Tags comments diff git workflow datagrip
First, preface 1, the main content of this article
    • GitLab Code Review Mechanism description
    • Git Workflow and git Code Review Workflow
    • GitLab Code Review Configuration Instructions
    • GitLab Code Review Process Demo
    • GitLab for IDE plugin introduction (JetBrains, etc.)
2. GitLab Code Review Mechanism

Gitlab can be supported in two ways when branching is merged:

    1. Locally merge the source branch code into the target branch (target branch) and push to the destination branch (target branch)
    2. The source branch is push to the far end, and then the merge Request is initiated on the Gitlab specified target branch (target branch), and the merge operation is performed on the target branch with the push permission. Complete the merge.

That is, using Gitlab for code review is to initiate the merge Request at the branch merge, and then the code review is completed to merge the codes into the target branch.

3. Environment information for this tutorial
Tools/Environment version
GitLab Gitlab.com, GitLab Community Edition available
Ide JetBrains (IntelliJ idea, Pycharm, Phpstorm, Webstorm, Rubymide, Appcode, Clion, Goland, Datagrip, Rider, Android studio, etc.)

Although code review does not have to be integrated with the IDE, it is also obliged to thank JetBrains for developing Ides that cover virtually all major programming languages.

JetBrains Tools currently covers the following mainstream languages: C + +, C #, DSL, F #, Go, Groovy, Java, JavaScript, TypeScript, Kotlin, Objective-c, PHP, Python, Ruby, Scala, SQL, Swift, VB. NET (in order of precedence)

Second, GitLab Code Review configuration 1. Code Review Workflow
    • General git Workflow Description

    1. After requirement confirmation, create develop branch from master
    2. Developers create their own feature branch from the Develop branch for development
    3. The master branch is changed and needs to be merged from the master branch to the develop branch, which can be considered for periodic merging
    4. Feature branch merging to the corresponding develop branch requires merging from the develop branch to the feature branch
    5. After the feature branch is merged into the corresponding develop branch, it is published to the test environment for testing
    6. The develop branch is merged into the release branch and published to the pre-release environment for testing after the test environment test passes
    7. Release Branch merges into the Master branch and publishes to the production environment for validation after the pre-release environment is validated

Branch Name Conventions:

Branch Type name Format Description
Master Master And there is only one
Release release-* *Can be a shuttle release date can also be a demand name abbreviation, or you can use only one release branch as needed
Develop develop-* *Usually is the shuttle release date or the demand name abbreviation
Feature feature-{username}-*
    • Code Review Link Selection

Taking the Git workflow as an example, the developer develops the feature branch and merges it into the develop branch after the development is completed.

The best way to do code review is to merge the feature branch into the develop part.

2, GitLab Repository configuration

Gitlab Warehouse related configuration take gitlab.com as an example, this content is also applicable to the Gitlab CE version of the privatized deployment if it is not specifically noted

    • Gitlab New Warehouse & Create Branch

Warehouse Address: Gitlab.com/ken-io/test

New branch:
Release (from Master)
Develop-test (from Master),
Feature-ken-test (from Develop-test)

    • Protected Branches Configuration

To ensure that the develop branch, release branch, and master branch must be changed in a merge way, we need to restrict push and merge permissions

Menu: Settings->repository Settings and then expand protected branches options
Gitlab.com/ken-io/test/settings/repository

Here, we restrict the branching, and all developers have no push permission on the develop branch, the release branch, and the Master branch, and can only merge to the corresponding branch in merge mode. And only users of the maintainers (Masters) group have merge permissions.

third, GitLab Code Review Example
    • Change Feature Branch

Modify feature-ken-test the branch README.md file online to provide a basis for the merge request

You can update a line of content here and then Commit changes .

    • Create a merge Request

Menu: Merge requests, then click:New Merge Request

Source Branch Selection:feature-ken-test
Target Branch Select:develop-test
And then:Compare branches and continue

Action Item/Fill in Item Description:

Action Item/fill-in ken.io description
Title title, no special requirements to leave the default
Description Description, need to describe the requirements of the change clearly, the best Attachment Code review Essentials
Assignee assigned to the person to whom the assigned person will receive an email notification, not necessarily related to the merge permission, and still the project's maintainers (Masters) role has merge permissions
Milestone Milestone, if not optional
label , if not optional
Approvers user approver/approver must be a member of the group in which the project is located, and if the approver is selected, the merger must be approved by the approver
Approvers Group approver groups to facilitate simultaneous selection of multiple approvers
Approvals required minimum number of approvals, if you have selected 3 approvers, approvals required is set to 1, then only 1 approver approval is required
Source Branch Origin Branch, followed by the previous step selection is consistent, here is mainly used to confirm
Target Branch destination Branch, followed by the previous step selection, which is mainly used to confirm

The approvers option is temporarily not available for the latest stable version of Gitlab (11.1.4) and is expected to be supported later.
Fill in the description here, select Assignee, then submit the merge request.

    • Merge Request operation

When the Merge Request is created, it will go to the page, and the person assigned to it (Assignee) will receive an email alert if more than one person is required to do Code Review, as long as the link to the page is sent to other project members. Project members can view changes and comments, except that only members of the maintainers (Masters) role have the merge permission, as previously configured.

In the Changes tab, we can see all the changes. The comment button appears when you move the cursor to the line number, and we can click the Comment button to initiate the comment, which is visible to the project members and can be discussed in the discussion area. The final discussion initiator has the right to mark the discussion as resolved resolved

When all the issues have been resolved (if the approver is selected and approved), the maintainers (Masters) member clicks Merge to complete the merge.

After the merge is complete, you can select actions such as Remove Source branch.

The develop branch is merged into the release branch, and the Elease branch is merged into Master without having to go through the code review, directly merge. This is omitted here.

iv. IDE Merge Request Plugin Usage Introduction

As described earlier, creating a merge request from the Gitlab Web page and initiating code Review, but as a developer, or in conjunction with the IDE, is more convenient, Gitlab provides the relevant API, as long as we create the token of the response, Can be used by the IDE plug-in to access Gitlab to use the IDE instead of working on a Web page.

1. GitLab Access Token

Menu: User settings->
Access Tokens into Access token add page

Items Description
Name Name, according to your preferences can be
Expires at Expiry time, the farthest can choose to 10 years later, according to their own needs to fill in can
Scopes Range, the API selection here is sufficient.

Once created, trouble saving tokens for the time being. Because once the page is refreshed or re-opened, token is not visible.

2, JetBrains IDE Gitlab plug-in use

JetBrains offers many Ide:intellij idea, Pycharm, Phpstorm, Webstorm, Rubymide, Appcode, Clion, Goland, Datagrip, Rider, Android Studio and so on, if no accident, are applicable to the Gitlab plug-in.

Install the following two plugins:
Gitlab projects:plugins.jetbrains.com/plugin/7975-gitlab-projects
Gitlab integration:plugins.jetbrains.com/plugin/7319-gitlab-integration

    • Installing the Gitlab Plugin

Settings->plugins into Plugins Management page

Click Browse repositories and Search Gitlab

Install Gitlab projects and Gitlab integration, and then restart the IDE in effect

    • Configure Gitlab

Search in the Settings interfaceGitLab Settings

Fill in the Gitlab Server Url, Access Token, and click Add New One Finish to add
If the Gitlab is a privatized deployment, replace it with the opposite domain name or ip+port

    • Create Merge Request

Clone Project Feature-ken-test Branch to local, and push to Origin after change.
Then choose from the menu: Vcs->git->git lab-> Create Merge Request

This is the equivalent of creating an operation on the Gitlab Web page, with fewer options and no support for approvers related options.
Select the target branch, the assigned person, fill in the title, description and then click OK.
After the merge request is created, the plugin will prompt in the bottom right corner and click on the link to jump to the merge request page

If a conflict is prompted, first merge the target branch code into the current branch

    • Merge Request Manage

Project members Select from the menu: Vcs->git->git lab-> List Merge Request

The pending merge request can be seen here, and the Code Review merge request operator panel can be paged out with a click

Button Description
Diff View all change files and differences
Comments View, add comments
Assign to me Refer the following person to yourself
Merge Execute merge
    • Merge Request Diff

Diff Interface Description:

On the left is the commit record for this merge, and on the right is the merged file. Double-click the file to view the variance details

    • Merge Request Comments

The comments interface allows you to view the specified merge Reuqest comments or add comments and double-click to view the full comment.
However, it is not supported to initiate discussions on lines of code, to mark discussions as resolved, and so on.

The Gitlab plugin is more suitable for create Merge Request, or code Review for simpler commits. If you need to discuss features like this, it's recommended to do it on the Gitlab page

3, other IDE Gitlab plug-in use
    • Visual Studio

Visual Studio Gitlab Plug-in: Marketplace.visualstudio.com/items?itemname=mysticboy.gitlabextensionforvisualstudio

    • Visual Studio Code

Visual Studio Code Gitlab plugin: MARKETPLACE.VISUALSTUDIO.COM/ITEMS?ITEMNAME=JASONN-PORCH.GITLAB-MR

    • Atom

Atom GitLab Plugin: Atom.io/packages/gitlab

Gitlab Installation Deployment Tutorial: Ken.io/note/centos7-gitlab-install-tutorial

Above, if in doubt, please contact me: ken.io/home/about

    • This article was first published: 2018-08-20
    • This text link: ken.io/note/gitlab-code-review-tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.