Skip to content

Instantly share code, notes, and snippets.

@shawngustaw
Last active June 17, 2016 17:08
Show Gist options
  • Save shawngustaw/60cf101169b1d0efa21870e53f903505 to your computer and use it in GitHub Desktop.
Save shawngustaw/60cf101169b1d0efa21870e53f903505 to your computer and use it in GitHub Desktop.
Output parent -> children mapping of project releases in SDElements
import requests
import argparse
import urlparse
parser = argparse.ArgumentParser()
parser.add_argument("--url")
parser.add_argument("--token")
args = parser.parse_args()
url = args.url
base_url = urlparse.urljoin(url, '/api/v2')
api_v2_token = args.token
headers = {
'Authorization': "Token {}".format(api_v2_token)
}
# Request all projects (you can see) from the API
r = requests.get(base_url + '/projects/', headers=headers).json()
results = r['results']
# collect all the projs without parents
roots = [proj for proj in results if not proj['parent']]
children = [proj for proj in results if proj['parent']]
# build dict of id -> children
parents = {}
for root in roots:
parents[root['id']] = []
# collect all the children
while children:
to_remove = []
for parent, child_list in parents.iteritems():
for child in children:
if parent == child['parent'] or child['parent'] in child_list:
child_list.append(child['id'])
to_remove.append(child['id'])
children = [child for child in children if child['id'] not in to_remove]
def get_name(id):
name = [proj['name'] for proj in results if id == proj['id']][0]
return name
def get_url(id):
url = [proj['url'] for proj in results if id == proj['id']][0]
return url
print "Parent \t\tChildren"
for parent, children in parents.iteritems():
print "* {0}".format(get_name(parent))
print "- {0}".format(get_url(parent))
for child in children:
print "\t* {0}".format(get_name(child))
print "\t- {0}".format(get_url(child))
print "\n"
@shawngustaw
Copy link
Author

usage: python script.py --token=token --url=http://www.sdelements.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment