More and more IT companies insist on quality assurance process and tools to achieve it. Certainly, one of them is mutual reviewing code by developers. Good quality application is much easier and cheaper to maintain.
But… The code review process arouses controversy among developers. Not everyone agrees to check the quality of its code. In my opinion it’s better to include code review process into natural git flow other than manual check every commit after merge to master branch. Another advantage is, that master branch is much more stable. Sometimes we have to stop ours Harleys in the name of better software.
In this tutorial I will show you how to enforce pull requests in GitLab, but first of all you need to know that developers are allowed to push changes to non protected branches. Masters are responsible for merging them with main branches and to maintain project consistency.
- Create a new GitLab project and perform initial commit: G04-gitlab-pull-requests
- Ensure master branch is protected
Push to master – branch protection test
Clone G04-gitlab-pull-requests repository as tutorial_developer user.
git config --global user.name "Tutorial Developer"
git config --global user.email "firstname.lastname@example.org"
git clone http://ubuntu-tutorial:9093/better-coding-tutorials/G04-gitlab-pull-requests.git
git add developer-test.txt
git commit -m "Add developer-test.txt file"
git push origin master
As you can see the push was blocked.
tutorial@ubuntu-tutorial:/tmp/G04-gitlab-pull-requests$ git push origin master
Username for 'http://ubuntu-tutorial:9093': tutorial_developer
Password for 'http://tutorial_developer@ubuntu-tutorial:9093':
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 280 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
remote: GitLab: You are not allowed to push code to protected branches on this project.
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'http://ubuntu-tutorial:9093/better-coding-tutorials/G04-gitlab-pull-requests.git'
Creating pull request
First of all you need to switch/create another branch: my-test-branch and push it to origin.
git checkout -b my-test-branch
git push origin my-test-branch
Now create pull request as tutorial_developer user (my-test-branch -> master) and accept it as tutorial_master user.
- More about user permissions you can find here.
- Very good guide about git branch layout and git workflow you can find here.
- Create your your own protected branch layout based on one of described here.
- Try to create custom user permission layout to achieve above branch layout.
- Test it in practice.