jenkinsci / violation-comments-to-gitlab-plugin

Comments GitLab merge requests with static code analyzer findings.
https://plugins.jenkins.io/violation-comments-to-gitlab
MIT License
20 stars 4 forks source link

How to configure #35

Closed benmathews closed 1 year ago

benmathews commented 1 year ago

Describe your use-case which is not covered by existing documentation.

There are a couple of configuration items that I'm struggling with.

 java.lang.RuntimeException: API Token not set!
    at org.jenkinsci.plugins.jvctgl.perform.JvctglPerformer.jvctsPerform(JvctglPerformer.java:230)

The env variables that might be useful are

 BRANCH_NAME=MR-7
 BUILD_DISPLAY_NAME=#14
 BUILD_ID=14
 BUILD_NUMBER=14
 BUILD_TAG=jenkins-Platform-vivint%2Fhorizontals%2Fplatform%2Faccountsystem-MR-7-14
 BUILD_URL=https://***.***.***/job/Platform/job/vivint%252Fhorizontals%252Fplatform%252Faccountsystem/job/MR-7/14/
 CHANGE_AUTHOR=ben.mathews
 CHANGE_AUTHOR_DISPLAY_NAME=Benjamin Mathews
 CHANGE_BRANCH=mergeRequestCommenting
 CHANGE_ID=7
 CHANGE_TARGET=master
 CHANGE_TITLE=Merge request commenting
 CHANGE_URL=https://gitlab.com/**/-/merge_requests/7
 CI=true
 EXECUTOR_NUMBER=10
 GITLAB_OBJECT_KIND=none
 HUDSON_HOME=/var/lib/jenkins
 HUDSON_URL=https://***.***.***/
 JENKINS_HOME=/var/lib/jenkins
 JENKINS_URL=https://***.***.***/
 JOB_BASE_NAME=MR-7
 JOB_DISPLAY_URL=https://***.***.***/job/Platform/job/vivint%252Fhorizontals%252Fplatform%252Faccountsystem/job/MR-7/display/redirect
 JOB_NAME=Platform/vivint%2Fhorizontals%2Fplatform%2Faccountsystem/MR-7
 JOB_URL=https://***.***.***/job/Platform/job/vivint%252Fhorizontals%252Fplatform%252Faccountsystem/job/MR-7/
 WORKSPACE=/var/lib/jenkins/workspace/workspace/tals_platform_accountsystem_MR-7
 WORKSPACE_TMP=/var/lib/jenkins/workspace/workspace/tals_platform_accountsystem_MR-7@tmp

Reference any relevant documentation, other materials or issues/pull requests that can be used for inspiration.

No response

tomasbjerre commented 1 year ago

How you get the values depends on what plugin you used to trigger the build.

Gitlab.com cannot be hardcoded because some people host it themselves.

benmathews commented 1 year ago

Any hints on setting up the credentials. That is my blocker.

tomasbjerre commented 1 year ago

Here is how I set it up, last time I used it: https://github.com/tomasbjerre/jenkins-configuration-as-code-sandbox/blob/007e7a11e15b317dc4a250dc9344230a5580aae3/jenkins-docker/jenkins.yaml#L31

And I supply it to the plugin here: https://github.com/tomasbjerre/jenkins-configuration-as-code-sandbox/blob/57f4df6d8df47d3b93ffea76f792564003ec3c36/vars/buildRepo.groovy#L148

Also another hint is to use the pipeline syntax GUI in Jenkins: image Have that generate pipeline code that sets the credential correctly.

benmathews commented 1 year ago

With the above hints, I was able to get the plugin working.

The most significant issue was the credential. The plugin requires a StringCredentials. In the GUI, I created a "Gitlab Personal Access Token" which seemed reasonable. In looking at the XML representation, I saw that this wasn't resulting in the string type. I determined that the "Secret Text" credential type in the GUI is what I needed.

The projected needs to be either the id or path_with_namespace value returned from https://gitlab.com/api/v4/projects

spyhunter99 commented 1 year ago

@benmathews 's comment is super helpful! thanks. Might i suggest that the other access token mechanisms be supported at some point? nothing more frustrating that putting in credentials and have them disappear. where they saved somewhere or not? don't know lol

thaarbach commented 1 year ago

I'm struggling with configuring this plugin. It is absolutely not clear how to include this plugin in a pipeline (Jenkinsfile).

My Configuration

pipeline {
other stages

 stage('Quality Gate') {
            steps {
               updateGitlabCommitStatus name: 'Quality Gate', state: 'pending'

                timeout(time: 5, unit: 'MINUTES') {
                    waitForQualityGate(credentialsId: 'sonar-api-token', abortPipeline: false)
                }
               updateGitlabCommitStatus name: 'Quality Gate', state: 'success'
               withCredentials([string(credentialsId: 'GITLAB_API_TOKEN', variable: 'TOKEN')]) {
                   ViolationsToGitLabRecorder([
                       apiTokenCredentialsId: TOKEN,
                       apiTokenPrivate: true,
                       authMethodHeader: true,
                       commentOnlyChangedContent: true,
                       commentTemplate: violationReporterTemplate() ,
                       createSingleFileComments: true,
                       gitLabUrl: <url_to_our_gitlab',
                       ignoreCertificateErrors: true,
                       keepOldComments: false,
                       mergeRequestIid: MR_IID,
                       minSeverity: 'INFO',
                       projectId: MR_PROJECT_ID,
                       violationConfigs: [
                            [ pattern: '.*/test-results/test/.*\\.xml$', parser: 'JUNIT', reporter: 'JUnit' ],
                            [ pattern: '.*/jacoco/.*\\.exec$', parser: 'JACOCO', reporter: 'Jacoco' ]
                       ]
                   ])
               }
            }
        }
}

I also tried

    stage("Report") {
        ViolationsToGitLab([
            apiTokenCredentialsId: 'gitlab-token', 

described here https://github.com/tomasbjerre/jenkins-configuration-as-code-sandbox/blob/57f4df6d8df47d3b93ffea76f792564003ec3c36/vars/buildRepo.groovy#L148

and

 step([
 $class: 'ViolationsToGitLabRecorder',
 config: [

described here https://github.com/jenkinsci/violation-comments-to-gitlab-plugin

Errors: java.lang.NoSuchMethodError: No such DSL method 'ViolationsToGitLabRecorder' java.lang.NoSuchMethodError: No such DSL method 'ViolationsToGitLab' or ClassDefNotFound

The plugin is installed.

tomasbjerre commented 1 year ago

Did you use the editor? https://github.com/jenkinsci/violation-comments-to-gitlab-plugin/issues/35#issuecomment-1372399030

thaarbach commented 1 year ago

@tomasbjerre i don't have problems with the credentials. Can't find an entry in the pipeline editor for this plugin.

tomasbjerre commented 1 year ago

If you cannot find it, it might not be installed.

thaarbach commented 1 year ago

If you cannot find it, it might not be installed.

If i take a look into installed plugins it's shown as installed.

image

tomasbjerre commented 1 year ago

Here is where it should show up: image