Last active
November 26, 2019 13:06
-
-
Save gmemstr/6f778791ced56ddd1c2ee3ef81555231 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires
requests
to be installed via pip and an environment variable namedCIRCLECI_TOKEN
with your CircleCI token.