mibexsoftware / sonar-bitbucket-plugin

SonarQube plug-in to create pull request comments for found issues in Bitbucket Cloud
Other
123 stars 72 forks source link

Not finding open pull requests on a branch #37

Closed lopesdasilva closed 7 years ago

lopesdasilva commented 7 years ago

Hi, I'm trying to get sonar-bitbucket-plugin to analyse multiple branch pull request. But it always says: [INFO] [sonar4bitbucket] No open pull requests with source branch 'releases/4B' found. No analysis will be performed.

But as you can see on my bitbucket repo I have pull request on that specific branch (releases/4B) screen shot 2016-12-04 at 01 37 53

I've also checked on the received json and the pull request are coming with the correct branch I'm using: -Dsonar.branch=${branch}

Can you please help me? Thanks

mrueegg commented 7 years ago

Hi,

Which version of the plug-in are you using?

Could you please follow the troubleshooting guide and attach the JSON from the log output when you run the build which contains the list of open pull requests where the plug-in is looking for one with the source branch releases/4B?

lopesdasilva commented 7 years ago

Thanks for your reply, Here is my json file https://gist.github.com/anonymous/2cbe36ee8f20f98ea155b397e8843082

Ok I've found out the issue:

The problem is that the parameter: -Dsonar.bitbucket.branchName=${bamboo.repository.git.branch} is the pull request author branch and not the destination branch.

Is this correct? Shouldn't it be the destination?

mrueegg commented 7 years ago

The problem is that there is no pull request with a SOURCE branch releases/4B. There are two pull requests where this branch is the destination branch, but if you run the plug-in with

-Dsonar.branch=${branch}

it expects to find a source branch with that name (this is the branch being analyzed by Sonar and where comments are created for found issues). I think you will need to adapt your CI settings so that you configure the above property with the source branch.

lopesdasilva commented 7 years ago

The problem is that we are using a fork approach where the owner has the possibility to create his own branch with the name he wants. In jenkins we don't have that kind of information.. is it possible to put this attribute optional and run for all pull requests on that repository?

mrueegg commented 7 years ago

If that would help you we could certainly provide such an option. But then the plug-in would create issues on all open pull requests, even if the branch would be completely different to the one you have analyzed in your Jenkins build. What do you think?

lopesdasilva commented 7 years ago

Hi,

do you think we can try your approach? setting it to be optional and analyse all pull requests? Thanks

mrueegg commented 7 years ago

When you run Jenkins on a branch (even if it is for a fork), you should be able to get the branch name by using the $GIT_BRANCH build variable. Doesn't that work in your case?

lopesdasilva commented 7 years ago

Hi, it does not work, because we are using forks. REPO: USER A Branch: develop ---Pull Request --- > REPO: Company Branch: releases/4A REPO USER B Branch:THE_NAME_The_Choose ---Pull Request----> REPO: Company Branch: releases/4A

As you can see the origin branch aren't the same, so it does not work. Do you have another idea? Thanks

mrueegg commented 7 years ago

I don't think I got it. When you analyze these fork branches with SonarQube in Jenkins, why can't you use the $GIT_BRANCH variable? I can only see a problem when people are using the same branch name in a forked repository leading to the situation where we would have duplicate Sonar project keys. But in that case I would add a user prefix to the branch name property to differentiate these name clashes.

lopesdasilva commented 7 years ago

How can I get the pull request author branch name and destination branch? (Different forks) I'm using jenkins.

mrueegg commented 7 years ago

The branch you are currently building in Jenkins is available with the $GIT_BRANCH variable. I don't think it is possible to obtain the pull request author name, as this is only available in Stash.

Which plug-in do you use to trigger builds from Stash? Maybe there is a Webhook that exports this information.

nfalco79 commented 7 years ago

In Jenkins multibranch pipeline the $GIT_BRANCH is not available. In case of pull request the BRANCH_NAME is the number of PR (like PR-6), you do not have any information about original branch

bash-4.1$ git branch
* (no branch)

So what about add a new property like sonar.bitbucket.pullrequest=6 so you do not have to looking for all open pull request for the current branch

mrueegg commented 7 years ago

@nfalco79 Didn't know that, thanks for the hint. What I don't see yet is the connection between the "number of PR" and the ID of a pull request (API /bitbucket/pullrequests/ID/*) in Bitbucket. How is this related? Is it always the same? If yes, I'm glad to add the sonar.bitbucket.pullrequest option.

nfalco79 commented 7 years ago

I mean PR ID not number. In Jenkins Multibranch pipeline it lookup for all branches and than if a branch has a PR than it create a new job named PR-#PR-ID

[Mon Feb 20 15:57:36 CET 2017] Starting branch indexing...
Connecting to https://bitbucket.org using ******
Repository type: Git
Looking up acme/myrepository for branches
...
Looking up acme/myrepository for pull requests
Checking PR from acme/myrepository and branch feature/pipeline
      ‘Jenkinsfile’ found
Met criteria
Scheduled build for branch: PR-6
Checking PR from acme/myrepository and branch feature/config-unittest
      ‘Jenkinsfile’ not found
Does not meet criteria
[Mon Feb 20 15:57:50 CET 2017] Finished branch indexing. Indexing took 13 sec
Finished: SUCCESS

In my case 6 is the ID of PR. The Jenkins job (foo branch name PR-6) you have not access the original source branch name that has generate the pull request. So I have to do a tips & trick to get the original branch name and fight against the Jenkins groovy sandbox permissions.

With the sonar.bitbucket.pullrequest we can simply get the ID stripping the PR- prefix and you have less search to do. I do not know about bamboo, but I think the usual branch name properties could be a good faulback

mrueegg commented 7 years ago

Okay I see, so it fetches the pull request ID's from Bitbucket. In that case it should work.

I will go ahead and provide this new sonar.bitbucket.pullrequest flag with the next version. Would you be available for beta-testing it before I will release it?

nfalco79 commented 7 years ago

Of course!

nfalco79 commented 7 years ago

any news?

imunic commented 7 years ago

I already implemented this and have been using it for a couple of months now without an issue, you can find it on this branch. You are gonna have to build it yourself though.

After you install it you can provide the pull request id when using the multibranch plugin as follows: -Dsonar.bitbucket.pullRequestId=${env.CHANGE_ID}

It's also backwards compatible.

Hope it helps.

mrueegg commented 7 years ago

@imunic That's great news! If you like, we can integrate it into the plug-in. Just send me a pull request please.

imunic commented 7 years ago

created! 😄

nfalco79 commented 7 years ago

I'm using that branch since today and it works. Comments are pushed in the right PR on bitbucket

mrueegg commented 7 years ago

Merged the pull request of @imunic. Thanks again to all here!

anshulpatel25 commented 7 years ago

Hi Guys,

When I try the above with Teams, it is giving me 400 error

INFO: Executing post-job Sonar Plug-in for Bitbucket Cloud
INFO: [sonar4bitbucket] Plug-in is active and will analyze pull request with #7...
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 4.390s
INFO: Final Memory: 47M/188M
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarQube Scanner execution
com.sun.jersey.api.client.UniformInterfaceException: POST https://bitbucket.org/api/2.0/repositories/anveshak/javabitbucket/commit/7783ed525e46/statuses/build returned a response status of 400 Bad Request

Any Idea?

mrueegg commented 7 years ago

Could you please generate a log with debug level by using the following parameters to trigger SonarQube

mvn sonar:sonar -X -Dsonar.verbose=true ...

and attach the request and response for this REST call to this issue?

anshulpatel25 commented 7 years ago

Hi @mrueegg,

Please find the detailed REST output

09:48:49.720 DEBUG: Load line hashes (done) | time=22ms | file=my:javaProject:src/main/java/com/anveshak/app/App.java
09:48:49.733 INFO: 5/5 components tracked
09:48:49.733 INFO: ANALYSIS SUCCESSFUL
Mar 28, 2017 9:48:49 AM com.sun.jersey.api.client.filter.LoggingFilter log
INFO: 1 * Client out-bound request
1 > POST https://bitbucket.org/site/oauth2/access_token
1 > Content-Type: application/x-www-form-urlencoded
1 > Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
grant_type=client_credentials

Mar 28, 2017 9:48:49 AM com.sun.jersey.api.client.filter.LoggingFilter log
INFO: 1 * Client in-bound response
1 < 200
1 < Server: nginx
1 < X-Content-Type-Options: nosniff
1 < Connection: keep-alive
1 < X-Static-Version: 2f72fe9470e8
1 < X-Version: 2f72fe9470e8
1 < Last-Modified: Tue, 28 Mar 2017 09:48:49 GMT
1 < Date: Tue, 28 Mar 2017 09:48:49 GMT
1 < X-Frame-Options: SAMEORIGIN
1 < Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
1 < X-Render-Time: 0.0229737758636
1 < Cache-Control: no-cache, no-store, must-revalidate, max-age=0
1 < ETag: "d98ad35e308def4e45dd8135a49b9d8e"
1 < X-Served-By: app-124
1 < Vary: Accept-Language, Cookie
1 < Expires: Tue, 28 Mar 2017 09:48:49 GMT
1 < Content-Length: 226
1 < Content-Language: en
1 < X-Request-Count: 166
1 < Content-Type: application/json
1 <
{"access_token": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "scopes": "pullrequest:write account", "expires_in": 3600, "refresh_token": "ucYTxnUnZRvsHBdQvL", "token_type": "bearer"}

09:48:50.037 DEBUG: Post-jobs : Sonar Plug-in for Bitbucket Cloud (wrapped)
09:48:50.037 INFO: Executing post-job Sonar Plug-in for Bitbucket Cloud (wrapped)
Mar 28, 2017 9:48:50 AM com.sun.jersey.api.client.filter.LoggingFilter log
INFO: 2 * Client out-bound request
2 > GET https://bitbucket.org/api/2.0/repositories/anveshak/javabitbucket/pullrequests/7
2 > Accept: application/json
2 > Content-Type: application/json
2 > Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Mar 28, 2017 9:48:50 AM com.sun.jersey.api.client.filter.LoggingFilter log
INFO: 2 * Client in-bound response
2 < 200
2 < X-Accepted-OAuth-Scopes: pullrequest
2 < Server: nginx
2 < X-Content-Type-Options: nosniff
2 < Connection: keep-alive
2 < X-Static-Version: 2f72fe9470e8
2 < X-Version: 2f72fe9470e8
2 < Date: Tue, 28 Mar 2017 09:48:50 GMT
2 < X-Frame-Options: SAMEORIGIN
2 < Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
2 < X-Cache-Info: not cacheable; request contained Authorization header
2 < X-Render-Time: 0.0539841651917
2 < ETag: "9d21e25ece05ac42a83365e494f5acb8"
2 < X-Served-By: app-142
2 < Vary: Authorization, Accept-Encoding
2 < Content-Length: 3022
2 < X-OAuth-Scopes: pullrequest:write, account
2 < X-Request-Count: 50
2 < Content-Type: application/json; charset=utf-8
2 <
{"merge_commit": null, "description": "This is for FAL-7", "links": {"decline": {"href": "https://bitbucket.org/!api/2.0/repositories/anveshak/javabitbucket/pullrequests/7/decline"}, "commits": {"href": "https://bitbucket.org/!api/2.0/repositories/anveshak/javabitbucket/pullrequests/7/commits"}, "self": {"href": "https://bitbucket.org/!api/2.0/repositories/anveshak/javabitbucket/pullrequests/7"}, "comments": {"href": "https://bitbucket.org/!api/2.0/repositories/anveshak/javabitbucket/pullrequests/7/comments"}, "merge": {"href": "https://bitbucket.org/!api/2.0/repositories/anveshak/javabitbucket/pullrequests/7/merge"}, "html": {"href": "https://bitbucket.org/anveshak/javabitbucket/pull-requests/7"}, "activity": {"href": "https://bitbucket.org/!api/2.0/repositories/anveshak/javabitbucket/pullrequests/7/activity"}, "diff": {"href": "https://bitbucket.org/!api/2.0/repositories/anveshak/javabitbucket/pullrequests/7/diff"}, "approve": {"href": "https://bitbucket.org/!api/2.0/repositories/anveshak/javabitbucket/pullrequests/7/approve"}, "statuses": {"href": "https://bitbucket.org/!api/2.0/repositories/anveshak/javabitbucket/pullrequests/7/statuses"}}, "title": "Adding FAL-7", "close_source_branch": false, "reviewers": [], "destination": {"commit": {"hash": "94cb1f636abc", "links": {"self": {"href": "https://bitbucket.org/!api/2.0/repositories/anveshak/javabitbucket/commit/94cb1f636abc"}}}, "repository": {"links": {"self": {"href": "https://bitbucket.org/!api/2.0/repositories/anveshak/javabitbucket"}, "html": {"href": "https://bitbucket.org/anveshak/javabitbucket"}, "avatar": {"href": "https://bitbucket.org/anveshak/javabitbucket/avatar/32/"}}, "type": "repository", "name": "javabitbucket", "full_name": "anveshak/javabitbucket", "uuid": "{4551a740-5181-43ec-a8f5-f1255e059d12}"}, "branch": {"name": "develop"}}, "state": "OPEN", "closed_by": null, "source": {"commit": {"hash": "7783ed525e46", "links": {"self": {"href": "https://bitbucket.org/!api/2.0/repositories/anveshak/javabitbucket/commit/7783ed525e46"}}}, "repository": {"links": {"self": {"href": "https://bitbucket.org/!api/2.0/repositories/anveshak/javabitbucket"}, "html": {"href": "https://bitbucket.org/anveshak/javabitbucket"}, "avatar": {"href": "https://bitbucket.org/anveshak/javabitbucket/avatar/32/"}}, "type": "repository", "name": "javabitbucket", "full_name": "anveshak/javabitbucket", "uuid": "{4551a740-5181-43ec-a8f5-f1255e059d12}"}, "branch": {"name": "FAL-7"}}, "comment_count": 0, "author": {"username": "anshulpatel25", "display_name": "Anshul Patel", "type": "user", "uuid": "{0479b2a8-98ad-4e09-85bb-434961a00b6e}", "links": {"self": {"href": "https://bitbucket.org/!api/2.0/users/anshulpatel25"}, "html": {"href": "https://bitbucket.org/anshulpatel25/"}, "avatar": {"href": "https://bitbucket.org/account/anshulpatel25/avatar/32/"}}}, "created_on": "2017-03-28T07:20:05.072398+00:00", "participants": [], "reason": "", "updated_on": "2017-03-28T08:37:29.103272+00:00", "type": "pullrequest", "id": 7, "task_count": 0}

09:48:50.122 INFO: [sonar4bitbucket] Plug-in is active and will analyze pull request with #7...
Mar 28, 2017 9:48:50 AM com.sun.jersey.api.client.filter.LoggingFilter log
INFO: 3 * Client out-bound request
3 > POST https://bitbucket.org/api/2.0/repositories/anveshak/javabitbucket/commit/7783ed525e46/statuses/build
3 > Content-Type: application/json
3 > Accept: application/json
3 > Authorization: Bearer TUN8xMk-r_qkShp09ltFkPgRMybzgMdPF-AzLScjbgsfUEwPeasWyca9HDIJlnUsMlCyiIMGI-1YmYznBEw=
{"name":"Sonar analysis","state":"INPROGRESS","url":"null/dashboard?id=my:javaProject:FAL-7","description":"Sonar analysis in progress...","key":"SONAR-ANALYSIS-PR-7"}

Mar 28, 2017 9:48:50 AM com.sun.jersey.api.client.filter.LoggingFilter log
INFO: 3 * Client in-bound response
3 < 400
3 < X-Accepted-OAuth-Scopes: repository
3 < Server: nginx
3 < Connection: close
3 < X-Static-Version: 2f72fe9470e8
3 < X-Version: 2f72fe9470e8
3 < Date: Tue, 28 Mar 2017 09:48:50 GMT
3 < X-Frame-Options: SAMEORIGIN
3 < Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
3 < X-Cache-Info: not cacheable; request wasn't a GET or HEAD
3 < X-Render-Time: 0.0924861431122
3 < ETag: "8ff70ed2c64a7f0ed0789ce68e438dee"
3 < X-Served-By: app-139
3 < Vary: Authorization
3 < Content-Length: 97
3 < X-OAuth-Scopes: pullrequest:write, account
3 < X-Request-Count: 161
3 < Content-Type: application/json; charset=utf-8
3 <
{"type": "error", "error": {"fields": {"url": ["Enter a valid URL."]}, "message": "Bad request"}}

Thanks and Regards, Anshul P.

anshulpatel25 commented 7 years ago

Hey @mrueegg,

One weird thing I found is that it is taking SERVER_BASE_URL as null, "state":"INPROGRESS","url":"null/dashboard? . But I am passing -Dsonar.host.url=, Any idea?

Thanks!

anshulpatel25 commented 7 years ago

Hi @mrueegg

Strangely it is working if I pass -Dsonar.bitbucket.buildStatusEnabled=false :).

mrueegg commented 7 years ago

@anshulpatel25 Thanks for your feedback. By disabling the build status feature, the logic that results in the build failure is not run anymore. I have just pushed a fix that should solve the base URL null pointer issue. Could you please build the plug-in and try again?

mrueegg commented 7 years ago

@anshulpatel25 Fixed with release 1.2.0

anshulpatel25 commented 7 years ago

Sorry for late reply.

Thanks @mrueegg

PeterOsudio commented 7 years ago

I fixed the PR build state for the forks scenario in https://github.com/mibexsoftware/sonar-bitbucket-plugin/issues/45