gabrie-allaigre / sonar-gitlab-plugin

Add to each commit GitLab in a global commentary on the new anomalies added by this commit and add comment lines of modified files
GNU Lesser General Public License v3.0
713 stars 207 forks source link

java.lang.NullPointerException in GitLabApiV4Wrapper.java:211 #234

Open thib3113 opened 5 years ago

thib3113 commented 5 years ago

I got this error :

java.lang.NullPointerException
    at com.talanlabs.sonar.plugins.gitlab.GitLabApiV4Wrapper.createOrUpdateSonarQubeStatus(GitLabApiV4Wrapper.java:215)
    at com.talanlabs.sonar.plugins.gitlab.CommitFacade.createOrUpdateSonarQubeStatus(CommitFacade.java:128)
    at com.talanlabs.sonar.plugins.gitlab.CommitPublishPostJob.execute(CommitPublishPostJob.java:113)
    at org.sonar.scanner.postjob.PostJobWrapper.execute(PostJobWrapper.java:49)
    at org.sonar.scanner.postjob.PostJobsExecutor.execute(PostJobsExecutor.java:48)
    at org.sonar.scanner.postjob.PostJobsExecutor.execute(PostJobsExecutor.java:39)
    at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:327)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122)
    at org.sonar.scanner.bootstrap.GlobalContainer.doAfterStart(GlobalContainer.java:128)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122)
    at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:73)
    at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:67)
    at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
    at com.sun.proxy.$Proxy0.execute(Unknown Source)
    at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:185)
    at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:137)
    at org.sonarsource.scanner.cli.Main.execute(Main.java:111)
    at org.sonarsource.scanner.cli.Main.execute(Main.java:75)
    at org.sonarsource.scanner.cli.Main.main(Main.java:61)

This line : https://github.com/gabrie-allaigre/sonar-gitlab-plugin/blob/master/src/main/java/com/talanlabs/sonar/plugins/gitlab/CommitPublishPostJob.java#L112 is called even if GitLabApiV4Wrapper.init() failed . Without any error message .

I think it's the bug described here : #194 but more generally .

In my case, the error catch here ( in the file CommitPublicPostJob ), is java.lang.IllegalStateException: Unable to perform GitLab WS operation, I don't know why you are doing this in the catch, so I can't really help you more .

vgorbulenko-amc commented 5 years ago

Just check this variable sonar.gitlab.project_id It has to contain the namespace of your GitLab repo

thib3113 commented 5 years ago

@vgorbulenko-amc this variables is correct .

In fact, my bug is harder to find . In fact the ssl handshake failed, because the gitlab of my company use a 4096 bits certificate, ( and my install in a alpine docker container, seems to dislike it ) . So, it produce an error with the DH exchange ...

And finally, I need to update the plugin, to see the problem ...

I think, a log of the error can be a better idea, and will help more :) .

liangrong74 commented 5 years ago

Hello

I also met this problem, and I would like to share my workout.

This problem at my side was at first caused by the NULL pointer of var gitLabProject at createOrUpdateSonarQubeStatus. So I made a trace, then found that with sonar.gitlab.project_id parameter, the gitlab project id could be correctly passed in and the var gitLabProject was correctly initialized.

So there must be some else reset of this var later.

As I am not quite familiar with the whole picture, what I have done was to add following protection when gitLabProject is called:

In getCommitCommentsPerRevision:

Map<String, List<GitLabCommitComments>> getCommitCommentsPerRevision(List<String> revisions) throws IOException {
                if (gitLabProject==null) {
                        gitLabProject = getGitLabProject();
                        if (gitLabProject==null) {
                                throw new IllegalStateException("get null projectid in comment "+config.projectId());
                        };
                };

        // code continues

In getPatchPositionsToLineMapping:

private Map<String, Map<String, Set<Line>>> getPatchPositionsToLineMapping(List<String> revisions) throws IOException {
              if (gitLabProject==null) {
                        gitLabProject = getGitLabProject();
                        if (gitLabProject==null) {
                                throw new IllegalStateException("get null projectid in patch "+config.projectId());
                        };
                };
    // code continues

In createOrUpdateSonarQubeStatus(:

 public void createOrUpdateSonarQubeStatus(String status, String statusDescription) {
                if (gitLabProject==null) {
                        gitLabProject = getGitLabProject();
                        if (gitLabProject==null) {
                                throw new IllegalStateException("get null projectid in status "+config.projectId());
                        };
    // code continues

there could be other places where such protection were needed, but in my case I just added in these three.

The I have “Unable to perform GitLab WS operation” but it is due to SSL certificate reason, as we are using a self-signed certificate at GitLab side. So I just added our certificate as a trusted one. Then all pass now.

Hope this can help you to find way out.

thib3113 commented 5 years ago

( you can use 3 ` for multiline code ) .

About the self signed ssl, I see an option to disable ssl checking, isn't enough for you ?

liangrong74 commented 5 years ago

Since I am running GitLab CI in docker, I just add the certificate as a trusted for docker image.

heidricha commented 5 years ago

Same error occured here as well.

We use gradle (factory docker image) for build, and sonar plugin for gradle. Exception vanished since I use own gradle image from factory image, but with own CA certificate added.

I have another issue now, but I guess it solved this one.

heidricha commented 5 years ago

Similar error here:

07:49:17.027 INFO: Executing post-job 'GitLab Commit Issue Publisher'
07:49:57.636 INFO: ------------------------------------------------------------------------
07:49:57.636 INFO: EXECUTION FAILURE
07:49:57.636 INFO: ------------------------------------------------------------------------
07:49:57.636 INFO: Total time: 51.462s
07:49:57.691 INFO: Final Memory: 12M/60M
07:49:57.691 INFO: ------------------------------------------------------------------------
07:49:57.692 ERROR: Error during SonarQube Scanner execution
java.lang.NullPointerException
    at com.talanlabs.sonar.plugins.gitlab.GitLabApiV4Wrapper.createOrUpdateSonarQubeStatus(GitLabApiV4Wrapper.java:211)
    at com.talanlabs.sonar.plugins.gitlab.CommitFacade.createOrUpdateSonarQubeStatus(CommitFacade.java:128)
    at com.talanlabs.sonar.plugins.gitlab.CommitPublishPostJob.execute(CommitPublishPostJob.java:112)
    at org.sonar.scanner.postjob.PostJobWrapper.execute(PostJobWrapper.java:46)
    at org.sonar.scanner.phases.PostJobsExecutor.execute(PostJobsExecutor.java:51)
    at org.sonar.scanner.phases.PostJobsExecutor.execute(PostJobsExecutor.java:42)
    at org.sonar.scanner.phases.AbstractPhaseExecutor.execute(AbstractPhaseExecutor.java:80)
    at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:164)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122)
    at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:319)
    at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:314)
    at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:288)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122)
    at org.sonar.scanner.task.ScanTask.execute(ScanTask.java:48)
    at org.sonar.scanner.task.TaskContainer.doAfterStart(TaskContainer.java:82)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122)
    at org.sonar.scanner.bootstrap.GlobalContainer.executeTask(GlobalContainer.java:131)
    at org.sonar.batch.bootstrapper.Batch.doExecuteTask(Batch.java:116)
    at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:71)
    at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
    at com.sun.proxy.$Proxy0.execute(Unknown Source)
    at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:185)
    at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:137)
    at org.sonarsource.scanner.cli.Main.execute(Main.java:111)
    at org.sonarsource.scanner.cli.Main.execute(Main.java:75)
    at org.sonarsource.scanner.cli.Main.main(Main.java:61)

I guess I know the problem, and I don't know if it can be solved here...

Looks like sonar scanner opens https connections using some old lib which doesn't support SNI for servers serving multiple https sites with multiple server certificates. It means that at the TLS negotiation phase java always gets the default site's certificate. This way installing the CA CERT for the container that runs the scanner doesn't do the trick in itself since IF gitlab and sonar are hosted on the same nginx reverse proxy using different hostnames and certificates (like for me) it always fails somewhere (https://sonar or https://gitlab). I wonder if gitlab API call is handled by soma sonar scanner call, or implemented in the plugin... the last case would be possible to handle it better than sonar does.

antonovicha commented 4 years ago

Got the same error. Was caused by fact that GitLab user that was configured for SonarQube did not had rights to access that specific git repo. After adding user to the repo with enough rights error gone.

JingSpring commented 4 years ago

Maybe your sonar token is wrong.

fengkuok commented 4 years ago

The same problem. I solved this by apply 443 port permissions.

mnguyen25 commented 4 years ago

Got the same problem, everything correctly configured. Any idea how to fix this? Thanks.

I'm using SonarQube 7.6, sonar-gitlab-plugin 4.1.0