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

Could not get GitLab project #8

Closed jostyee closed 6 years ago

jostyee commented 6 years ago

My Env:

GitLab 10.0.4
Jenkins 2.73.2

I setup violation comments gitlab plugin as below:

gitlab

It threw an exception when running its Post-build Action:

19:00:09 Will comment PR https://gitlab.company.com/ 253 2917
19:00:09 java.lang.RuntimeException: Could not get project 253
19:00:09 at se.bjurr.violations.comments.gitlab.lib.GitLabCommentsProvider.<init>(GitLabCommentsProvider.java:51)
19:00:09 at se.bjurr.violations.comments.gitlab.lib.ViolationCommentsToGitLabApi.toPullRequest(ViolationCommentsToGitLabApi.java:125)
19:00:09 at org.jenkinsci.plugins.jvctgl.perform.JvctglPerformer.doPerform(JvctglPerformer.java:137)
19:00:09 at org.jenkinsci.plugins.jvctgl.perform.JvctglPerformer$1.invoke(JvctglPerformer.java:220)
19:00:09 at org.jenkinsci.plugins.jvctgl.perform.JvctglPerformer$1.invoke(JvctglPerformer.java:208)
19:00:09 at hudson.FilePath.act(FilePath.java:998)
19:00:09 at hudson.FilePath.act(FilePath.java:976)
19:00:09 at org.jenkinsci.plugins.jvctgl.perform.JvctglPerformer.jvctsPerform(JvctglPerformer.java:207)
19:00:09 at org.jenkinsci.plugins.jvctgl.ViolationsToGitLabRecorder.perform(ViolationsToGitLabRecorder.java:66)
19:00:09 at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:81)
19:00:09 at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
19:00:09 at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:736)
19:00:09 at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:682)
19:00:09 at hudson.model.Build$BuildExecution.post2(Build.java:186)
19:00:09 at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:627)
19:00:09 at hudson.model.Run.execute(Run.java:1762)
19:00:09 at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
19:00:09 at hudson.model.ResourceController.execute(ResourceController.java:97)
19:00:09 at hudson.model.Executor.run(Executor.java:421)

In fact GitLab project 253 exists.

tomasbjerre commented 6 years ago

There is also ProjectIid perhaps that works if you try it.

jostyee commented 6 years ago

@tomasbjerre No there isn't any projectlid env var.

tomasbjerre commented 6 years ago

So you can do this?

curl -XGET "https://gitlab.com/api/v4/projects/253"
jostyee commented 6 years ago

@tomasbjerre sure.

curl -XGET "https://gitlab.company.com/api/v4/projects/253?private_token=personal_access_token"
{"id":253,"description":"","default_branch":"master","tag_list":[],"ssh_url_to_repo":"git@gitlab.company.com:group/demo-grpc.git","http_url_to_repo":"https://gitlab.company.com/group/demo-grpc.git","web_url":"https://gitlab.company.com/group/demo-grpc","name":"demo-grpc","name_with_namespace":"group / demo-grpc","path":"demo-grpc","path_with_namespace":"group/demo-grpc","star_count":0,"forks_count":0,"created_at":"2017-02-28T19:21:53.133+08:00","last_activity_at":"2017-11-01T18:07:11.090+08:00","_links":{"self":"http://gitlab.company.com/api/v4/projects/253","issues":"http://gitlab.company.com/api/v4/projects/253/issues","merge_requests":"http://gitlab.company.com/api/v4/projects/253/merge_requests","repo_branches":"http://gitlab.company.com/api/v4/projects/253/repository/branches","labels":"http://gitlab.company.com/api/v4/projects/253/labels","events":"http://gitlab.company.com/api/v4/projects/253/events","members":"http://gitlab.company.com/api/v4/projects/253/members"},"archived":false,"visibility":"internal","resolve_outdated_diff_discussions":null,"container_registry_enabled":true,"issues_enabled":true,"merge_requests_enabled":true,"wiki_enabled":false,"jobs_enabled":true,"snippets_enabled":false,"shared_runners_enabled":true,"lfs_enabled":true,"creator_id":8,"namespace":{"id":32,"name":"group","path":"group","kind":"group","full_path":"group","parent_id":null},"import_status":"none","avatar_url":null,"open_issues_count":2,"public_jobs":true,"ci_config_path":null,"shared_with_groups":[],"only_allow_merge_if_pipeline_succeeds":false,"request_access_enabled":false,"only_allow_merge_if_all_discussions_are_resolved":false,"printing_merge_request_link_enabled":true,"permissions":{"project_access":null,"group_access":{"access_level":30,"notification_level":3}}}

I guess the plugin doesn't pass GitLab personal access token when querying this API?

tomasbjerre commented 6 years ago

The API token is passed to the Java Gitlab API: https://github.com/tomasbjerre/violation-comments-to-gitlab-lib/blob/master/src/main/java/se/bjurr/violations/comments/gitlab/lib/GitLabCommentsProvider.java#L41

Can you do that curl command from the server running Jenkins?

jostyee commented 6 years ago

I did execute the command from the host server of Jenkins. Is there any issue with the official Jenkins docker image? we're running it as a container.

tomasbjerre commented 6 years ago

If you update the plugin to 1.16 you will get more information in the exception. It will include the chained exception from the library.

jostyee commented 6 years ago

@tomasbjerre The API query was not set right, token param name should be private_token other than access_token

15:27:58 Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: https://gitlab.company.com/api/v4/projects/253?access_token=personal_access_token
15:27:58 at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
15:27:58 at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
15:27:58 at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
15:27:58 at org.gitlab.api.http.GitlabHTTPRequestor.parse(GitlabHTTPRequestor.java:394)
15:27:58 at org.gitlab.api.http.GitlabHTTPRequestor.to(GitlabHTTPRequestor.java:168)
15:27:58 ... 21 more

refer to https://docs.gitlab.com/ce/api/README.html#personal-access-tokens, my above comment showed a working log: https://github.com/jenkinsci/violation-comments-to-gitlab-plugin/issues/8#issuecomment-341235656

tomasbjerre commented 6 years ago

It looks like you did not check the "Private token" checkbox in the GUI.

jostyee commented 6 years ago

@tomasbjerre Not the Use API token input?

tomasbjerre commented 6 years ago

You need both.

tomasbjerre commented 6 years ago

Is this solved?

jostyee commented 6 years ago

I'm sorry to forget to reply here, I've switched to other solution so this is not an issue for me any longer.

mnunezdm commented 6 years ago

Hi, I'm experiencing this same issue...

I have a pipeline project, my project is being triggered via gitlab webhook plugin, gitlab.com, last version of your plugin and last jenkins version

I'm using the following call in my pipeline script and the parameters have this format

gitlabTargetRepoHttpUrl=https://gitlab.com/user/repo.git
gitlabMergeRequestTargetProjectId=0000000
I have tryed with either
gitlabMergeRequestIid=000 or gitlabMergeRequestId=0000000
ViolationsToGitLab([
    gitLabUrl: env.gitlabTargetRepoHttpUrl, projectId: env.gitlabMergeRequestTargetProjectId,
    mergeRequestId: env.gitlabMergeRequestIid, commentOnlyChangedContent: true,
    createCommentWithAllSingleFileComments: true, minSeverity: 'INFO',
    apiToken: 'XXXXXXXXXXXXXXXXXXXX', apiTokenPrivate: true,
    authMethodHeader: true, ignoreCertificateErrors: true, keepOldComments: false,
    violationConfigs: [[ pattern: '.*/pmddelta.xml$', parser: 'PMD', reporter: 'PMD' ]]
])
Will comment PR https://gitlab.com/user/repo.git 0000000 000
[Code Quality Tests] java.lang.RuntimeException: Could not get project 0000000
[Code Quality Tests]    at se.bjurr.violations.comments.gitlab.lib.GitLabCommentsProvider.<init>(GitLabCommentsProvider.java:51)
[Code Quality Tests]    at se.bjurr.violations.comments.gitlab.lib.ViolationCommentsToGitLabApi.toPullRequest(ViolationCommentsToGitLabApi.java:125)
[Code Quality Tests]    at org.jenkinsci.plugins.jvctgl.perform.JvctglPerformer.doPerform(JvctglPerformer.java:142)
[Code Quality Tests]    at org.jenkinsci.plugins.jvctgl.perform.JvctglPerformer$1.invoke(JvctglPerformer.java:227)
[Code Quality Tests]    at org.jenkinsci.plugins.jvctgl.perform.JvctglPerformer$1.invoke(JvctglPerformer.java:215)
[Code Quality Tests]    at hudson.FilePath.act(FilePath.java:1009)
[Code Quality Tests]    at hudson.FilePath.act(FilePath.java:987)
[Code Quality Tests]    at org.jenkinsci.plugins.jvctgl.perform.JvctglPerformer.jvctsPerform(JvctglPerformer.java:214)
[Code Quality Tests]    at org.jenkinsci.plugins.jvctgl.ViolationsToGitLabRecorder.perform(ViolationsToGitLabRecorder.java:64)
[Code Quality Tests]    at org.jenkinsci.plugins.workflow.steps.CoreStep$Execution.run(CoreStep.java:80)
[Code Quality Tests]    at org.jenkinsci.plugins.workflow.steps.CoreStep$Execution.run(CoreStep.java:67)
[Code Quality Tests]    at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution$1$1.call(SynchronousNonBlockingStepExecution.java:49)
[Code Quality Tests]    at hudson.security.ACL.impersonate(ACL.java:290)
[Code Quality Tests]    at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution$1.run(SynchronousNonBlockingStepExecution.java:46)
[Code Quality Tests]    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
[Code Quality Tests]    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[Code Quality Tests]    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
[Code Quality Tests]    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
[Code Quality Tests]    at java.lang.Thread.run(Thread.java:748)
[Code Quality Tests] Caused by: java.io.FileNotFoundException: https://gitlab.com/user/repo.git/api/v4/projects/0000000
[Code Quality Tests]    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1890)
[Code Quality Tests]    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
[Code Quality Tests]    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:263)
[Code Quality Tests]    at org.gitlab.api.http.GitlabHTTPRequestor.parse(GitlabHTTPRequestor.java:394)
[Code Quality Tests]    at org.gitlab.api.http.GitlabHTTPRequestor.to(GitlabHTTPRequestor.java:168)
[Code Quality Tests]    at org.gitlab.api.http.GitlabHTTPRequestor.to(GitlabHTTPRequestor.java:139)
[Code Quality Tests]    at org.gitlab.api.GitlabAPI.getProject(GitlabAPI.java:610)
[Code Quality Tests]    at se.bjurr.violations.comments.gitlab.lib.GitLabCommentsProvider.<init>(GitLabCommentsProvider.java:49)
[Code Quality Tests]    ... 18 more
[Code Quality Tests] 
tomasbjerre commented 6 years ago

Change gitLabUrl: env.gitlabTargetRepoHttpUrl to gitLabUrl: 'https://gitlab.com/'.

tomasbjerre commented 6 years ago

I'm releasing a new version to clarify the gitlabMergeRequestI(i)d.

https://github.com/jenkinsci/violation-comments-to-gitlab-plugin/commit/5a9db9a63559fc6ee134c94b76ebf077eba75198

mnunezdm commented 6 years ago

Managed to get it working with env.gitlabMergeRequestIid, the only bad thing is that the GitLab plugin does not provide an env variable that contains the URL so we would have to cut gitlabTargetRepoHttpUrl

Thank you @tomasbjerre !

tomasbjerre commented 6 years ago

You could try my generic-webhook-trigger-plugin: https://github.com/jenkinsci/generic-webhook-trigger-plugin/blob/master/src/test/resources/org/jenkinsci/plugins/gwt/bdd/gitlab/gitlab-mergerequest.feature