Add HTML output format to gitHubPRs.py

Also better JSON output structure
This commit is contained in:
Jan Høydahl 2020-02-15 02:06:02 +01:00
parent f549ee3535
commit c777db7c8a
1 changed files with 62 additions and 20 deletions

View File

@ -28,10 +28,18 @@ import json
import re import re
from github import Github from github import Github
from jira import JIRA from jira import JIRA
from datetime import datetime
from time import strftime
try:
from jinja2 import Environment, BaseLoader
can_do_html = True
except:
can_do_html = False
def read_config(): def read_config():
parser = argparse.ArgumentParser(description='Find open Pull Requests that need attention') parser = argparse.ArgumentParser(description='Find open Pull Requests that need attention')
parser.add_argument('--json', action='store_true', default=False, help='Output as json') parser.add_argument('--json', action='store_true', default=False, help='Output as json')
parser.add_argument('--html', action='store_true', default=False, help='Output as html')
parser.add_argument('--token', help='Github access token in case you query too often anonymously') parser.add_argument('--token', help='Github access token in case you query too often anonymously')
newconf = parser.parse_args() newconf = parser.parse_args()
return newconf return newconf
@ -39,9 +47,34 @@ def read_config():
def out(text): def out(text):
global conf global conf
if not conf.json: if not (conf.json or conf.html):
print(text) print(text)
def make_html(dict):
if not can_do_html:
print ("ERROR: Cannot generate HTML. Please install jinja2")
sys.exit(1)
global conf
template = Environment(loader=BaseLoader).from_string("""
<h1>Lucene/Solr Github PR report</h1>
<p>Number of open Pull Requests: {{ open_count }}</p>
<h2>PRs lacking JIRA reference in title ({{ no_jira_count }})</h2>
<ul>
{% for pr in no_jira %}
<li><a href="https://github.com/apache/lucene-solr/pull/{{ pr.number }}">#{{ pr.number }}: {{ pr.created }} {{ pr.title }}</a> ({{ pr.user }})</li>
{%- endfor %}
</ul>
<h2>Open PRs with a resolved JIRA ({{ closed_jira_count }})</h2>
<ul>
{% for pr in closed_jira %}
<li><a href="https://github.com/apache/lucene-solr/pull/{{ pr.pr_number }}">#{{ pr.pr_number }}</a>: <a href="https://issues.apache.org/jira/browse/{{ pr.issue_key }}">{{ pr.status }} {{ pr.resolution_date }} {{ pr.issue_key}}: {{ pr.issue_summary }}</a> ({{ pr.assignee }})</li>
{%- endfor %}
</ul>
""")
return template.render(dict)
def main(): def main():
global conf global conf
@ -61,14 +94,14 @@ def main():
result['open_count'] = open_prs.totalCount result['open_count'] = open_prs.totalCount
lack_jira = list(filter(lambda x: not re.match(r'.*\b(LUCENE|SOLR)-\d{3,6}\b', x.title), open_prs)) lack_jira = list(filter(lambda x: not re.match(r'.*\b(LUCENE|SOLR)-\d{3,6}\b', x.title), open_prs))
lack_jira_dict = {} result['no_jira_count'] = len(lack_jira)
lack_jira_list = []
for pr in lack_jira: for pr in lack_jira:
lack_jira_dict[pr.number] = pr.title lack_jira_list.append({'title': pr.title, 'number': pr.number, 'user': pr.user.login, 'created': pr.created_at.strftime("%Y-%m-%d")})
result['no_jira_count'] = len(lack_jira_dict) result['no_jira'] = lack_jira_list
result['no_jira'] = lack_jira_dict
out("\nPRs lacking JIRA reference in title") out("\nPRs lacking JIRA reference in title")
for pr in lack_jira: for pr in lack_jira_list:
out(" #%s: %s" % (pr.number, pr.title)) out(" #%s: %s %s (%s)" % (pr['number'], pr['created'], pr['title'], pr['user'] ))
out("\nOpen PRs with a resolved JIRA") out("\nOpen PRs with a resolved JIRA")
has_jira = list(filter(lambda x: re.match(r'.*\b(LUCENE|SOLR)-\d{3,6}\b', x.title), open_prs)) has_jira = list(filter(lambda x: re.match(r'.*\b(LUCENE|SOLR)-\d{3,6}\b', x.title), open_prs))
@ -81,28 +114,37 @@ def main():
issue_to_pr[jira_issue_str] = pr issue_to_pr[jira_issue_str] = pr
resolved_jiras = jira.search_issues(jql_str="key in (%s) AND status in ('Closed', 'Resolved')" % ", ".join(issue_ids)) resolved_jiras = jira.search_issues(jql_str="key in (%s) AND status in ('Closed', 'Resolved')" % ", ".join(issue_ids))
closed_jira_dict = {} closed_jiras = []
for issue in resolved_jiras: for issue in resolved_jiras:
pr_title = issue_to_pr[issue.key].title pr_title = issue_to_pr[issue.key].title
pr_number = issue_to_pr[issue.key].number pr_number = issue_to_pr[issue.key].number
closed_jira_dict[pr_number] = { 'key': issue.key, assignee = issue.fields.assignee.name if issue.fields.assignee else None
'status': issue.fields.status.name, closed_jiras.append({ 'issue_key': issue.key,
'resolution': issue.fields.resolution.name, 'status': issue.fields.status.name,
'resolution_date': issue.fields.resolutiondate, 'resolution': issue.fields.resolution.name,
'pr_title': pr_title} 'resolution_date': issue.fields.resolutiondate[:10],
out(" #%s: %s status=%s, resolution=%s, resolutiondate=%s (%s)" % (pr_number, 'pr_number': pr_number,
issue.key, 'pr_title': pr_title,
issue.fields.status.name, 'issue_summary': issue.fields.summary,
issue.fields.resolution.name, 'assignee': assignee})
issue.fields.resolutiondate,
pr_title) closed_jiras.sort(key=lambda r: r['pr_number'], reverse=True)
for issue in closed_jiras:
out(" #%s: %s %s %s: %s (%s)" % (issue['pr_number'],
issue['status'],
issue['resolution_date'],
issue['issue_key'],
issue['issue_summary'],
issue['assignee'])
) )
result['closed_jira_count'] = len(resolved_jiras) result['closed_jira_count'] = len(resolved_jiras)
result['closed_jira'] = closed_jira_dict result['closed_jira'] = closed_jiras
if conf.json: if conf.json:
print(json.dumps(result, indent=4)) print(json.dumps(result, indent=4))
if conf.html:
print(make_html(result))
if __name__ == '__main__': if __name__ == '__main__':
try: try: