How to get SonarQube Issues Report via API - sonarqube

Is there a way to get an issues report by querying the SonarQube web API?
With previous versions of SonarQube, I was able to generate an HTML report after each build but this feature looks like it's been deprecated in newer installments.
At the moment, I'm trying this bit of code
curl -u foo:bar https://sonar.example.com/api/issues/search?pageSize=100&componentKeys=my-app&metricKeys=violations,ncloc,line
But it errors with {"errors":[{"msg":"The 'component' parameter is missing"}]
Ideally, what I'm after is to just get the lines of code, the number of bugs, vulnerabilities, and Code smells in each run/scan.
Something like this
But through querying the API after each analysis.
Is this possible, please?

Had the same issue. The problem is that there is something wrong with the CURL command and you need to specify the full url as string using quotes.
Your case would be:
curl -u foo:bar "https://sonar.example.com/api/measures/search?pageSize=100&componentKeys=my-app&metricKeys=ncloc,violations,complexity"
This is an example with measures. Be sure to check the required parameters.

Related

Apollo on android: can't download schema or generate classes

I'm trying Apollo and graphql for the first time and I want to use this api and I'm following the official example here. However
running the schema download task
gradlew :app:downloadApolloSchema --endpoint='https://countries.trevorblades.com/' --schema='app/src/main/graphql/com/trevorblades/countries/schema.json'
results in
Execution failed for task ':app:downloadApolloSchema'.
> Expected URL scheme 'http' or 'https' but no colon was found
with both the repo I want to use and the example's.
If I try to manually download the schema using the button on the repo's page, place it in the correct directory and run
gradlew generateApolloSources
it results in
Failed to parse GraphQL schema introspection query from `[...]app\src\main\graphql\com\trevorblades\countries\schema.json`
again with both my repo and the one from the example.
I've the feeling that I'm missing something really trivial here, but I really can't figure it out.
any help will be appreciated.
Try the JS GraphQL IDEA plugin, which might make life a tad easier. And as it suggested here ...you might not pass a valid endpoint at all. Try --endpoint=https://countries.trevorblades.com/ or in " double-quotes . The error message is definitely concerning the --endpoint and not concerning the --schema. I have the suspicion that Windows might not like these ' single quotes
(obviously the same would also apply to --schema).
gradlew.bat :app:downloadApolloSchema --endpoint=https://countries.trevorblades.com/ --schema=app/src/main/graphql/com/trevorblades/countries/schema.json
or:
gradlew.bat :app:downloadApolloSchema --endpoint="https://countries.trevorblades.com/" --schema="app/src/main/graphql/com/trevorblades/countries/schema.json"

Get all quality gate information for all projects through sonarqube api (version 8.4.2)

I have list of projects in my sonarqube dashboard, how can I get quality information like code coverage, code smell, vulnerability, and other details of all the projects in a single api call (sonarqube web api) ?
Referred web-api documentaion (https://next.sonarqube.com/sonarqube/web_api/), but not able to get much help from it.
The following is a short excerpt from code that gets some metrics from a scan after it is completed:
"curl -s -X GET -u ${authString} \'${sonarProps['sonar.host.url']}/api/measures/component?" +
"metricKeys=coverage,duplicated_lines_density" +
"&component=${sonarQubeProjectName}&branch=${branchName}\'"
The syntax of the property references are unimportant for your question. Notice the list of "metricKeys" being supplied.

How to generate code using Postman Collection

I'm aware it is possible to generate code snippets from individual Postman requests, but I can't find similar functionality for a entire collection.
I also know that there are tools like newman for running an exported collection in Postman Collection 2.1 json format.
But specifically what I'm looking for is a tool that generates bash code from a collection or from a exported collection in Postman Collection 2.1 json format. This way my co-workers who don't use Postman can replicate the API requests.
Is this option available in Postman and I'm just missing it or are there any tools that do this?
I ended using the Postman SDK and postman-code-gen to create code snippets from Postman Collections that I exported from Postman.
Not really sure why this feature isn't provided already by Postman, but I made a simple CLI tool that does it:
https://github.com/arashout/postman-collection-gen
# cURL request generation
node main.js -c example_collection.json
# curl --location --request GET 'https://v7rr12wbr7.execute-api.us-west-2.amazonaws.com/prod/courses?c0=PHYS153&c1=APSC160&c2=CHEM154&version_key=1.2'
# curl --location --request GET 'https://v7rr12wbr7.execute-api.us-west-2.amazonaws.com/prod/courses?c0=PHYS153'
# How to generate other languages:
node main.js -c example_collection.json -l shell,httpie
node main.js -c example_collection.json -l Swift,URLSession
I'm not sure I understand your question completely.
But I can tell you how I export collection and then run tests.
Export Collection to JSON: choose tab 'Collections' at the left tab of the Postman. Select the 3-dot menu and choose 'Export'.
Export your test environment: Open 'Manage Environments'. Click download.
I don't know a tool that generates bash code, I write it manually.
Write .sh file like the following to run your tests.
"newman run your_test_collection.json -r cli -e your_test_environment.json --reporter-cli-no-assertions --global-var"
Usually, I create different .sh files for different sets of tests.

SonarQube REST APIs : Read Metrics for individual projects

My question:
I am using SonarQube version 7.1 and trying to extract the metrics and quality gate related to individual projects.
What we have tried
We were using Python SonarQube API to extract these data before our company upgraded to version 7.1. "api/resources" web service Deprecated since sonarqube5.4, so we cannot use it anymore.
I have also tried using getting data using CURL command via Web API using
curl -i -H "Content-Type: application/json" -H "x-api-key:token" -X GET 'http://MY_HOST/api/measures/component?metricKeys=key&component=project_key'
We are able to get a json payload for individual metrics, but involves tedious task of creating the URL every single time.
But I wanted to know if there is a better/smarter way to access these "measures", be it any language or implementation.
You could do this:
Call the API api/metrics/search first to get a (json) list of all the metrics and then iterate over that list and create a comma separated string of all the metric keys.
For example something like this: ncloc,complexity,violations .. as mentioned in the parameters example value in the API documentation here.
Then you could just add this comma separated list to the url as a parameter something like: http://MY_HOST/api/measures/component?metricKeys=ncloc,complexity,violations&component=project_key
and call it once to get the response for all metrics.
Also, I haven't tried this, but as per the latest documentation, the parameter component is optional. So if you omit that, ideally you should get a response with metrics of all the projects.

How can i get the metrics from SonarCloud using language filter?

I referred this page as described below and could get some values.
$ curl -v "https://sonarcloud.io/api/measures/component?metricKeys=violations&component=key" | jq
But how can I get these values with language filter?
Querying project-level metrics by language is not possible. As you can see on the description of the /api/measures/component web service, there's no language parameter.
If you are interested in issues in particular (I'm asking because I see that you are trying to query the violations metric), then you should be using the /api/issues/search web service for which you have a language parameter.
Thank you for a comment & answer! It was self resolved.
I tried sonar-scanner with language options like -Dsonar.language=c and after that, accessed the Web API. By doing so, I can get the value of C code.

Resources