Skip to content

Instantly share code, notes, and snippets.

@gmemstr
Last active November 26, 2019 13:06
Show Gist options
  • Save gmemstr/6f778791ced56ddd1c2ee3ef81555231 to your computer and use it in GitHub Desktop.
Save gmemstr/6f778791ced56ddd1c2ee3ef81555231 to your computer and use it in GitHub Desktop.
# Useful for parsing workflow API data
# ./main.py gh/gmemstr/circleci-koans
import requests
import sys
import json
import os
def GetWorkflowData(project, token):
url = "https://circleci.com/api/v2/insights/{project}/workflows/workflow?view=full&circle-token={token}".format(
project=project, token=token)
r = requests.get(url)
data = r.json()
return data
def ParseWorkflowData(data):
final_string = ""
template = """
ID: {id}
Status: {status}
Duration: {duration} seconds
Created: {created}
Stopped: {stopped}
Credits used: {credits}
"""
for item in data['items']:
proj_string = template.format(
id=item['id'], status=item['status'],
duration=item['duration'], created=item['created_at'],
stopped=item['stopped_at'], credits=item['credits_used'])
final_string += proj_string
if final_string == "":
final_string = "No workflows found"
return final_string
def IsValidProject(project):
decon = project.split("/")
if len(decon) != 3:
return False
if decon[0] != "gh" and decon[0] != "bb":
return False
return True
if __name__ == '__main__':
if sys.argv[1] is None or IsValidProject(sys.argv[1]) is False:
print("No project given, should be formatted as <vcs>/<org name>/<project name>")
print("e.g gh/gmemstr/circleci-koans")
exit()
data = GetWorkflowData(sys.argv[1], os.getenv("CIRCLECI_TOKEN"))
projects = ParseWorkflowData(data)
print(projects)
@gmemstr
Copy link
Author

gmemstr commented Nov 26, 2019

Requires requests to be installed via pip and an environment variable named CIRCLECI_TOKEN with your CircleCI token.

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