Skip to content

Instantly share code, notes, and snippets.

@rssnyder
Last active September 25, 2023 20:12
Show Gist options
  • Save rssnyder/dddf7e4605b59153233087cc51ae732b to your computer and use it in GitHub Desktop.
Save rssnyder/dddf7e4605b59153233087cc51ae732b to your computer and use it in GitHub Desktop.
get a list of harness projects

get all harness projects using python

setup

set the following in your environment

HARNESS_ACCOUNT_ID: your harness account id

HARNESS_PLATFORM_API_KEY: a harness token with orgs and projects read

run it

python3 main.py

notes

the code currently fetches only 1 org and project per api call, for testing and to prove the pagenation.

if you are using on a real harness account, you should change the limit var to something larger, like 20-30.

from os import getenv
from requests import get
headers = {
"x-api-key": getenv("HARNESS_PLATFORM_API_KEY"),
"Harness-Account": getenv("HARNESS_ACCOUNT_ID"),
}
def get_orgs(page: int = 0, limit: int = 1):
url = f"https://app.harness.io/v1/orgs"
resp = get(url, headers=headers, params={"page": page, "limit": limit})
results = []
if resp.status_code == 200:
data = resp.json()
results += data
if len(data) == limit:
results += get_orgs(page + 1, limit)
else:
print(resp.text)
return results
def get_projects(org: str = "default", page: int = 0, limit: int = 1):
url = f"https://app.harness.io/v1/orgs/{org}/projects"
resp = get(url, headers=headers, params={"page": page, "limit": limit})
results = []
if resp.status_code == 200:
data = resp.json()
results += data
if len(data) == limit:
results += get_projects(org, page + 1, limit)
else:
print(resp.text)
return results
if __name__ == "__main__":
limit = 1
projects = []
for org in get_orgs(limit=limit):
projects += get_projects(org=org.get("org", {}).get("identifier"), limit=limit)
print(len(projects))
print(projects)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment