Code Review with OneDev
Tags: programming, development
Published: 2022-06-28 04:52:16

[OneDev](https://onedev.io) understands importance of code review, and provides many facilities to make this process smooth and efficient. This article introduces how to perform code review in OneDev with these facilities.

# Review Rule Definition

Review rule specifies who should review a certain change, and how many approvals should get to accept the change into a branch. Review rule is defined in branch protection settings like below:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mu3t34bbx2v6t82wum6t.png)

In this example we define rules for changes to be accepted into main branch:
1. The change must pass the CI build
1. Changes touching models must get at least one approval from core team

With this rule specified, one will not be able to push changes touching models to main branch directly even with repository write permission. Instead a pull request must be submitted to go through the review process. 

# Reviewer Auto-Suggestion

When reviewers is specified as a group, OneDev will suggest the most appropriate members to review the change, based on commit history of touched files in the change. If for some reason, suggested reviewer can not review the change, you may simply remove and OneDev will pick up other appropriate reviewers. And you can add other reviewers as necessary even if review rule does not require it.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e16dia8fvjc7j4kdtlnv.png)

# Pull Request Build to Verify Changes

In above screenshot, the pull request also needs to be verified by CI build. We need to disable branch protection rule temporarily to edit build spec of main branch directly to add a pull request trigger for CI job like below:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3pjzy07b6opz8yifvbjr.png)

After committing change, do not forget to re-enable the branch protection setting. Check the pull request again, a CI job will be running to verify merged commit of pull request head and target branch head.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wekt8uqlpq5qadsqm3ri.png)

# Select Changes to Review

Code review can be performed from file changes tab. File changes can be shown commit by commit, for multiple commits, or since last review (last time approve or request for changes).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a27dyk0yzqo3ekggj8fu.png)

Further, files can be filtered to only show interested changes. For instance, below screenshot shows all changed java files under model package:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmw3mok6odulds4da3mw.png)

# Jump to Symbol Definition

While reviewing the change, one can jump to symbol definitions, or show all occurrences of the symbol in current commit:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tnio7cv090y6cuzjn2bq.gif)

# Test Coverage and Code Problems

OneDev can analyze test coverage and code analysis report generated by CI job to help code review, for instance:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nrbk0x6h4atdd2rl2s0j.png)

For an example set up of diff annotation, please check https://robinshen.medium.com/annotate-source-with-jest-eslint-information-in-onedev-c622641caa45

# Code Comment to Discuss Changes

To raise concerns or suggest changes for a code snippet, just select with mouse to add a code comment:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gyub6neurg0slbn6nbdo.gif)

After reviewing the changes, reviewer can give opinion by approving or requesting for changes. The pull request can only be merged after getting approvals from all reviewers.

Pull request submitter can check added code comments in code comments tab. For each suggested change, submitter can commit the change individually, or add to batch to commit together:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ft6vl2y4qjva6lxvvhfb.gif)

# Free-Style Review on Commit and File

Besides pull request based review, you can open arbitrary file or commit to add comment to initiate free-style review. User changing selected code will automatically be mentioned and notified to participate in the discussion. Further, source file will be annotated with history comments to help code understanding even if file is modified or renamed.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ppaydi1daqjl2nx8x0lo.gif)

In this mode, you can still suggest changes. When apply suggested changes, user has to tell which branch to apply the change.

That’s all. Hope these facilities helps with your next code review. Thanks for watching!