Need help with API commands

SonarQube version: 8.8.0.42792

I’ve been trying to figure out a way to get the description on SonarQube for a portfolio/project for the URL https://{host}/project/admin/extension/governance/console?id={project}&qualifier=VW. I need the description for a cleanup effort where we document what the account maps to what resources, and my plan was to put a JSON in the description I can parse and use. I’ve done this on other tools, but I can’t seem to figure this one out for the life of me.

So far, I have the script setup to pull a list of the portfolios and I pass that name to the URL as the project, but I get stuck on a loading screen when I try to CURL it. I’ve tried passing it my token as part of the header, but it always shows up as the loading screen. I tried using different python libraries to wait for the page to load, but it never goes past it.

I’m not sure if there is some command I am missing, or some logic anyone might be able to recommend that help. I appreciate any help/input anyone has!

Hi Josh, welcome to the SonarSource Community!

Seeing a loading indicator when you were hoping for an API response gives me a few hypotheses of what might be going on:

  • Something’s wrong with your request and it’s getting redirected to a main/login page
  • A firewall or proxy that your curl command doesn’t know about is interfering with the request

Maybe even both. What’s the literal API call that you’re trying to make at the time you get the loading response?

Hi Jeff!

Here is my code, I’ve tried swapping in api into the URL in a couple different places to see if that works, but I’m still just hitting the loading screen. I use both requests and SonarQubeClient since I was hoping to figure out which one lets me get the info I need, and then just use that after some refactoring.

Where would I look at to see if it is a bad request or hitting a firewall or proxy? For future reference for when I’m asked to automate more of the features

BTW - Sorry the code is a little messy, I’ve been hitting my head against the wall trying to figure out what I am just not seeing

import sys, requests, json
#Requires python-sonarqube-api package installed
from sonarqube import SonarQubeClient

URL_API = "https://{host}"
HEADER_CONTENT = "Content-Type:application/json"
HEADER_AUTHORIZATION = "access_token {token}"
HOST = ""
TOKEN = ""

if __name__ == '__main__':
  HOST = sys.argv[1]
  TOKEN = sys.argv[2]

  sonar = SonarQubeClient(sonarqube_url=URL_API.format(host=HOST), token=TOKEN)
  components = list(sonar.components.search_components(qualifiers="VW")) #APP, VW, SVW, TRK (Project)
  for component in components:
    print(component)
    HEADER = {'Authorization': HEADER_AUTHORIZATION.format(token=TOKEN), 'Content-Type': HEADER_CONTENT}
    try:
      URL = "{host}/project/admin/extension/governance/console?id={project}&qualifier=VW".format(host=URL_API.format(host=HOST), project=component['project'])
      response = requests.get(URL, headers=HEADER, timeout=10)
      response.raise_for_status()
      print(response.text)
    except requests.exceptions.HTTPError as err:
      print("HTTPError:")
      print(err)
      print(err.response.text)
    except requests.exceptions.RequestException as e:
      print("RequestException: {error}".format(error=e))
    print("\n")

Hi Jeff,

I finally figured out the issue and got it to work, I changed the URL, HEADER, and response lines to this

HEADER = {'Content-Type': HEADER_CONTENT}
    try:
      URL = "{host}/api/views/show?key={project}&qualifier=VW".format(host=URL_API.format(host=HOST), project=component['project'])
      response = requests.get(URL, headers=HEADER, auth=(TOKEN, ""))
1 Like

Glad you figured it out; I was about to come back on this thread and point out that your URL looked more like one of our internal UI URL references than a proper API call.

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