Skip to content

Instantly share code, notes, and snippets.

@mohanpedala
Created May 29, 2024 21:17
Show Gist options
  • Save mohanpedala/481a3986ff039cf5949b712a39167a75 to your computer and use it in GitHub Desktop.
Save mohanpedala/481a3986ff039cf5949b712a39167a75 to your computer and use it in GitHub Desktop.
API to enable automatic analysis in sonarcloud
  • Endpoint to onboard a repo with automatic analysis

    curl -X POST "https://sonarcloud.io/api/alm_integration/provision_projects" \
         -H "Authorization: Bearer <TOKEN>" \
         -H "Content-Type: application/x-www-form-urlencoded" \
         --data-urlencode "installationKeys={ORG_NAME}/{REPO_NAME}|{REPO_ID}" \
         --data-urlencode "organization={ORG_NAME}"
  • How to find github repo_id in chrome

    • Click on More Tools
    • Select < > Developer Tools (Shortcut key in mac: option + command + i)
    • Find (CMD + F) --> look for octolytics-dimension-repository_id
  • Python script to fetch the repo_id

 import os
 import requests


 # Get the GitHub token from environment variable
 GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
 ORG_NAME = os.getenv('ORG_NAME')
 REPO_NAME = os.getenv('REPO_NAME')

 def get_repo_id(owner, repo_name):
     url = f"https://api.github.com/repos/{owner}/{repo_name}"
     headers = {
         "Authorization": f"Bearer {GITHUB_TOKEN}",
         "Accept": "application/vnd.github.v3+json"
     }
     response = requests.get(url, headers=headers)

     if response.status_code == 200:
         repo_data = response.json()
         return repo_data['id']
     else:
         response.raise_for_status()

 if __name__ == "__main__":
     owner = {ORG_NAME}
     repo_name = {REPO_NAME}

     try:
         repo_id = get_repo_id(owner, repo_name)
         print(f"The ID of the repository '{repo_name}' is: {repo_id}")
     except requests.exceptions.RequestException as e:
         print(f"Error fetching repository ID: {e}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment