Last active
July 9, 2022 04:41
-
-
Save bauergeorg/856de20e2a99f930b384291d36b189a5 to your computer and use it in GitHub Desktop.
Print a Markdown-Page for more details of your workflow_dispatched Workflow Runs
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
from github import Github | |
import tomark | |
import datetime | |
from dateutil import tz | |
class pipeline_mngt_adv(): | |
''' Class for pipeline management ''' | |
def __init__(self, token, org): | |
''' Init function | |
Parameter: | |
token: Your token | |
org: Your organization | |
''' | |
# error handling | |
if token == '' or token == None: | |
raise Exception("You forget to type-in your token. Visit https://github.com/settings/tokens (github.com: settings > developer settings > Personal access token). Generate a new token and retry.") | |
# set a organization | |
self.org = org | |
# set a token | |
self.g = Github(token) | |
def get_repo(self, repo: str): | |
''' Get one explizit repository of your organization | |
Returns None of repo is not a repo of your organization. | |
Example: | |
>>> mngt = pipeline_mngt_adv(token) | |
>>> mngt.get_repo('ee-sandbox-pkg2') | |
... | |
''' | |
try: | |
return self.g.get_organization(self.org).get_repo(repo) | |
except: | |
return None | |
def get_workflow_run_list(self, repo, branch_name=None, workflow_name=None) -> list: | |
''' Get workflow run list | |
Parameter: | |
repo: Repository with type 'Github.get_repo()' or as str. | |
branch_name (str): e.g. develop, if not selected take all branches | |
workflow_name (str): name of workflow (default = None) | |
Return: | |
list of dicts | |
''' | |
# input management | |
if isinstance(repo, str): | |
repo = self.get_repo(repo) | |
# get a list of all workflows in the repo | |
# both none | |
if branch_name == None and workflow_name == None: | |
runs = repo.get_workflow_runs() | |
else: | |
# only branch_name | |
if workflow_name == None: | |
runs = repo.get_workflow_runs(branch=branch_name) | |
# only workflow_name | |
elif branch_name == None: | |
# init workflows | |
workflows = repo.get_workflows() | |
for workflow in workflows: | |
if workflow.name == workflow_name: | |
runs = workflow.get_runs() | |
break | |
# workflow_name and branch_name | |
else: | |
# init workflows | |
workflows = repo.get_workflows() | |
for workflow in workflows: | |
if workflow.name == workflow_name: | |
runs = workflow.get_runs(branch=branch_name) | |
break | |
# check | |
if runs.totalCount == 0: | |
# both none | |
if branch_name == None and workflow_name == None: | |
print("There are no workflows in your selected repository: '{}'".format(repo.name)) | |
else: | |
# only branch_name | |
if workflow_name == None: | |
print("There are no workflow runs in your selected repository: '{}' on branch '{}'".format(repo.name, branch_name)) | |
# only workflow_name | |
elif branch_name == None: | |
print("There are no workflow runs in your selected repository: '{}' on workflow '{}'".format(repo.name, workflow_name)) | |
# workflow_name and branch_name | |
else: | |
print("There are no workflow runs in your selected repository: '{}' on branch '{}' and workflow '{}'".format(repo.name, branch_name, workflow_name)) | |
return None | |
# init data list | |
data = [] | |
# read info from run(s) | |
for run in runs: | |
info = {} | |
info['name'] = run.raw_data['name'] # worflow name | |
info['id'] = run.id | |
info['number'] = run.run_number | |
info['conclusion'] = run.conclusion | |
info['status'] = run.status | |
info['branch'] = run.head_branch | |
info['sha'] = run.head_sha | |
info['event'] = run.event | |
info['created_at'] = run.raw_data['created_at'] | |
info['commit_author'] = run.raw_data['head_commit']['author']['name'] | |
info['commit_message'] = run.raw_data['head_commit']['message'] | |
data.append(info) | |
return data | |
def export_workflow_info_to_markdown_file(self, repo, file_name:str='docs/workflow_overview.md', timezone=None): | |
''' export workflow info to markdown file | |
Parameter: | |
repo: Repository with type 'Github.get_repo()' or as str. | |
filename: name of markdown-file | |
timezone: None is auto-mode. Alternative is to set a str linke this 'America/New_York' | |
''' | |
# input management | |
if isinstance(repo, str): | |
repo = self.get_repo(repo) | |
# Auto-detect zones | |
if timezone == None: | |
from_zone = tz.tzutc() | |
to_zone = tz.tzlocal() | |
# Hardcode time zones | |
else: | |
from_zone = tz.gettz('UTC') | |
to_zone = tz.gettz(timezone) | |
workflows = self.get_workflow_run_list(repo) | |
export_list = [] | |
# convert data | |
for workflow in workflows: | |
info = {} | |
# name | |
info['name'] = '[' + workflow['name'] + '](https://github.com/' + self.org + '/' + repo.name + '/actions/runs/'+ str(workflow['id']) + ')' | |
# number | |
info['#'] = workflow['number'] | |
# status | |
if workflow['status'] != 'completed': | |
info['status'] = '![Image](img/idle.png)' | |
else: | |
if workflow['conclusion'] == 'success': | |
info['stat'] = '![Image](img/pass.png)' | |
elif workflow['conclusion'] == 'failure': | |
info['stat'] = '![Image](img/fail.png)' | |
elif workflow['conclusion'] == 'cancelled': | |
info['stat'] = '![Image](img/skipped.png)' | |
else: | |
raise Exception("Unknown conclusion '{}'!".format(workflow['conclusion'])) | |
# branch name | |
info['branch'] = '[' + workflow['branch'] + '](https://github.com/' + self.org + '/' + repo.name + '/tree/' + workflow['branch'] + ')' | |
# commit sha | |
info['sha'] = '[' + workflow['sha'][0:7] + '](https://github.com/' + self.org + '/' + repo.name + '/commit/' + workflow['sha'] + ')' | |
# author | |
info['author'] = workflow['commit_author'] | |
# create date | |
utc_create_date = datetime.datetime.strptime(workflow['created_at'],"%Y-%m-%dT%H:%M:%SZ") | |
# Tell the datetime object that it's in UTC time zone since (datetime objects are 'naive' by default) | |
utc_create_date = utc_create_date.replace(tzinfo=from_zone) | |
# Convert time zone | |
central_create_date = utc_create_date.astimezone(to_zone) | |
info['create date'] = central_create_date.strftime("%Y-%m-%d %H:%M:%S") | |
#workflow['id'] | |
#workflow['event'] | |
#workflow['commit_message'] | |
#workflow['conclusion'] | |
#workflow['status'] | |
export_list.append(info) | |
# headline | |
headline = '# ' + repo.name + ' \n\n' | |
mark_export = tomark.Tomark.table(export_list) | |
with open(file_name, 'w') as f: | |
f.write(headline) | |
f.write(mark_export) | |
# run stuff | |
mngt = pipeline_mngt_adv(token='<your-token>', org='<your-org>') | |
mngt.export_workflow_info_to_markdown_file(repo='<your-repo>') | |
print("finished") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This snippet of python code generates a markdown page. You can implement it into github-pages like me.
Watch the result: