Skip to main content

Golang Support

OneDev is able to analyze Golang code and CI/CD reports to make important information readily available to aid code navigation, comprehensation and review.

For Golang projects, it is able to:

  1. Analyze code for symbol navigation and search
  2. Display/search outline while view source code
  3. Suggest CI/CD job templates
  4. Show unit test, coverage and lint report, as well as statistics over time
  5. Annotate source code with coverage and lint information

Let’s see how to get all of these with a real-world Golang project grpc-go.

Prepare the Environment

Firstable, run OneDev with below command:

docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock -v ./onedev:/opt/onedev -p 6610:6610 -p 6611:6611 1dev/server

Follow on screen instructions to set up the server, then select import from url like below:

import-url

Specify import url as https://github.com/grpc/grpc-go on next screen, and click import. Two projects will be created after a while like below:

projects

Here project grpc corresponds to organization grpc, and its child project grpc/grpc-go contains imported repository. The hierarchical structure makes project management and setting inheritance a lot easier.

Code Analysis and Indexing

Now open project grpc/grpc-go to get into the repository page:

indexing

For just imported repository, a indexing in progress message will be shown to indicate that the source code is currently being analyzed and indexed. After that we can press T to search symbols. For instance, type security to list all related definitions:

quick search

If we select the first entry, the file defining Security struct will be shown:

symbol navigation

Symbols can be highlighted and navigated in source view page. It also shows the code outline, which can be searched by pressing O.

CI/CD Support and Report Analysis

Now let’s add CI/CD jobs for this project. To do it, go back to repository root and click the link adding .onedev-buildspec.yml like below:

add buildspec

OneDev shows a GUI for CI/CD job setup, and the result will be saved into file .onedev-buildspec.yml in root of the repository. You may add job manually at this point, but OneDev will analyze your repository files and provide appropriate templates for you to start with:

job suggestion

For golang projects, the template is created by inspecting file go.mod. Let’s use provided template and head on to job detail page:

job detail

You may edit steps and other settings as necessary here. For this project, we just save and commit without any change. The job will run automatically as it defines a trigger to fire upon commit:

job triggered

Switch to running build of the job. It will be successful after a while, with unit test and coverage report being published:

job successful

unit test report

coverage report

Now let’s edit the build spec to enable lint step which is disabled by default:

enable lint step

Wait a while for the build to finish. This time no go modules will be downloaded as they were cached after successful run of previous build. Build will also be failed due to lint rule violations of high severity:

lint failed

We can click the line numbers to drill down violations in source view:

source annotation

Besides violations, source view is also marked with code coverage info. When review code via pull request, these information will also be available in diff view if applicable.

Thanks for reading!