Using Reviewdog with GitLab Connector
This guide demonstrates how to use custom CLI tools like reviewdog to interact with GitLab for automated code review on merge requests.
TOC
OverviewPrerequisitesWhat You'll LearnWorkflow OverviewStep 1: Create a GitLab ConnectorStep 2: Use Reviewdog to Post Review CommentsStep 3: Verify the ResultsUnderstanding the ConfigurationReviewdog Environment VariablesConnector Built-in ConfigurationsUsing with Real Linting ToolsNext StepsOverview
Reviewdog is an automated code review tool that can post review comments to various platforms, including GitLab. When combined with the GitLab Connector, you can run automated code reviews in your CI/CD pipelines without manually managing GitLab credentials.
Prerequisites
- A GitLab Connector configured with a Private Access Token that has
apiscope - A GitLab repository with an existing merge request
- kubectl access to your Kubernetes cluster
- Basic understanding of reviewdog and linting tools
What You'll Learn
- How to use GitLab Connector's built-in configurations to access GitLab API
- How to configure reviewdog to post automated code review comments on merge requests
Workflow Overview
The typical workflow for using reviewdog with GitLab Connector:
- Clone the repository and checkout the merge request branch using GitLab CLI (glab)
- Run linting/analysis tools on the code
- Use reviewdog to post findings as comments on the merge request via GitLab API
Step 1: Create a GitLab Connector
Ensure you have a GitLab Connector with appropriate permissions:
Replacements:
glpat-xxxxxxxxxxxxxxxxxxxx: Replace with your actual GitLab Private Access Token (must haveapiscope)https://gitlab.com: Replace with your GitLab server address (usehttps://gitlab.comfor GitLab.com or your self-hosted GitLab URL)
Step 2: Use Reviewdog to Post Review Comments
Here's a complete example that clones a repository, checks out a merge request branch, and runs reviewdog:
Replacements:
<your-group>/<your-repo>: Replace with your GitLab repository path (e.g.,gitlab-org/gitlab)<merge-request-id>: Replace with the merge request ID (e.g.,123)<mr-source-branch-name>: Replace with the merge request source branch name (e.g.,feature-branch)<your-group>: Replace with your GitLab group/user name<your-repo>: Replace with your repository nameecho "example.go:1:1: This is a review comment from reviewdog": Replace with actual linting tool output (ensure the file exists in the merge request and the line number is valid).
Step 3: Verify the Results
After applying the Pod, verify that reviewdog successfully posted comments to the merge request.
Check Pod logs:
you can see the output of the repository cloned successfully and checked out the merge request branch successfully:
You can see the output of the reviewdog completed successfully:
Verify in GitLab UI:
Open the merge request in GitLab and check the Changes tab. The review comment should appear on the specified file and line number.
If comments don't appear, check:
- Pod logs for errors:
kubectl logs reviewdog-demo -c reviewdog -n gitlab-demo - The file name and line number in the comment exist in the merge request
- The merge request ID is correct
- The Private Access Token has
apiscope - The connector is in "Ready" status:
kubectl get connector gitlab-connector -n gitlab-connector-demo
Understanding the Configuration
Reviewdog Environment Variables
The following environment variables are required for reviewdog to interact with GitLab:
More about it, please refer to the Reviewdog Documentation
Connector Built-in Configurations
The GitLab Connector provides the following built-in configuration files:
context.token: Kubernetes API token for accessing the connector proxy serviceconnector.status.proxyAddress: The connector proxy service address
For more details, see: Connectors-CSI Built-in Configurations
Using with Real Linting Tools
The example above uses a fake comment for demonstration. In production, use actual linting tools:
Example with golangci-lint:
Example with eslint:
For more reviewdog integrations, see: Reviewdog Documentation
Next Steps
- Learn how to use GitLab CLI with the connector: Using GitLab CLI
- Learn how to use GitLab Connector in CI/CD pipelines: Using GitLab Connector in CI/CD Pipelines
- Read concepts of GitLab ConnectorClass: GitLab ConnectorClass Concepts