Unable to fetch branch specific source from /api/sources

SonarQube 8.9.1 (build 44547) - Developer Edition

Fetching the source via url “…/api/sources/key=PROJ:src/test_file&branch=b1” results in fetching from the default branch instead (usually master), which results in an error if the file does not exist on that branch.

Note that the URL “…/api/issues” works correctly with the branch parameter.

Looks similar to these issues on previous versions:
https://jira.sonarsource.com/browse/SONAR-9875
https://jira.sonarsource.com/browse/SONAR-10219

Thanks,
Chris

Hello @obe1line,

Can you clarify which API you’re using? api/source/key=PROJ:src/test_file&branch=b1 does not exist. There must be a typo somewhere…

api/sources/raw can return the source code of a file on a branch BUT this is an internal API not formally supported publicly by SonarSource

Olivier

Yes you are correct, typo in copying partial URLs. Sorry about that.

It should be the SCM endpoint i.e.
/api/sources/scm?key=PROJ:src/test_file&branch=b1

Chris

Hello @obe1line,

Yes, you’re correct. You cannot select branch from this API. Actually this API is not actively evolved since it is no longer used by SonarQube and there are alternatives API that do the job fine:

  • If you only want the raw source file you should use api/sources/raw
  • If you want the SCM data you should use the api/sources/lines. This API is internal though.

Both the above API support the branch parameter.

Now I am not quite sure why you want to fetch source code from SonarQube. This is 2nd hand information. You should get it from the SCM itself.

Olivier

It is the SCM data I needed for a report which I generate using the Python sonarqube library.

Originally the report was generated on another server without access to the GIT repository (post build/scan). That is no longer the case so I can fetch directly as you mention.

Thanks for the help.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.