From 3ec4c0290d48c49c556e7e95206b97541676574f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Haitz=20Legarreta=20Gorro=C3=B1o?= Date: Mon, 8 Nov 2021 20:35:41 -0500 Subject: [PATCH 1/3] ENH: Improve issues to pages script encapsulation Improve issues to pages script encapsulation. --- issues_to_pages.py | 73 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 17 deletions(-) diff --git a/issues_to_pages.py b/issues_to_pages.py index 14729b9..ce018b0 100644 --- a/issues_to_pages.py +++ b/issues_to_pages.py @@ -32,6 +32,34 @@ def request_issues(url): return response +def check_issue_data_request(response): + """Check the issue request response success. + + Parameters + ---------- + response : Response + Response data. + + Returns + ------- + success : bool + `True` if response was. + """ + + if response.status_code != 200: + print( + "Cannot continue: no issues found or request for issues unsuccessful.\nResponse:\n".format( + response + ) + ) + success = False + + else: + success = True + + return success + + def get_issue_data(response): """Get the issue data contained in the response. @@ -226,18 +254,38 @@ def gather_website_project_data(issues): return projects +def save_project_data(website_project_data, path): + """Save the website project data as separate Markdown files. + + Parameters + ---------- + website_project_data : dict + Project data. + path : str + Path were project data will be saved. + """ + + for project in website_project_data.items(): + + file_rootname = ( + website_project_fname_label + underscore + str(project[1]["issue_number"]) + ) + file_basename = file_rootname + extension_sep + md_extension + fname = os.path.join(path, file_basename) + + with open(fname, "w") as json_file: + json.dump(project[1], json_file, indent=2) + + def main(): # Request issues url = "https://api.github.com/repos/brainhackorg/global2021/issues?per_page=100" response = request_issues(url) - if response.status_code != 200: - print( - "Cannot continue: no issues found or request for issues unsuccessful.\nResponse:\n".format( - response - ) - ) + # Check whether the response was successful + success = check_issue_data_request(response) + if not success: sys.exit() # Get issue data @@ -246,18 +294,9 @@ def main(): # Get the appropriate data for the website from the issues website_project_data = gather_website_project_data(issues) - # Save the website project data as separate Markdown files + # Save the project data path = "content/project" - for project in website_project_data.items(): - - file_rootname = ( - website_project_fname_label + underscore + str(project[1]["issue_number"]) - ) - file_basename = file_rootname + extension_sep + md_extension - fname = os.path.join(path, file_basename) - - with open(fname, "w") as json_file: - json.dump(project[1], json_file, indent=2) + save_project_data(website_project_data, path) if __name__ == "__main__": From 705f7a2bb4226dcd97564890b0c1972432198776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Haitz=20Legarreta=20Gorro=C3=B1o?= Date: Mon, 8 Nov 2021 20:36:52 -0500 Subject: [PATCH 2/3] ENH: Allow the issues to pages script to accept parameters Allow the issues to pages script to accept parameters. Document the purpose of the script. --- .github/workflows/issue-to-page.yml | 4 +++- issues_to_pages.py | 34 +++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/.github/workflows/issue-to-page.yml b/.github/workflows/issue-to-page.yml index f35ee43..5f06dd9 100644 --- a/.github/workflows/issue-to-page.yml +++ b/.github/workflows/issue-to-page.yml @@ -19,7 +19,9 @@ jobs: - name: Run script to generate separate pages from issues run: | - python issues_to_pages.py + url="https://api.github.com/repos/brainhackorg/global2021/issues?per_page=100" + path="content/project" + python issues_to_pages.py $url $path - name: Create Pull Request uses: peter-evans/create-pull-request@v3 diff --git a/issues_to_pages.py b/issues_to_pages.py index ce018b0..e88c1c9 100644 --- a/issues_to_pages.py +++ b/issues_to_pages.py @@ -1,6 +1,16 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +""" +Convert relevant issue data to Markdown files so that they can be used to +populate the website project section. + +Usage: +> python issues_to_pages.py "https://api.github.com/repos/brainhackorg/global2021/issues?per_page=100" "content/project" + +""" + +import argparse import json import os import requests @@ -277,11 +287,28 @@ def save_project_data(website_project_data, path): json.dump(project[1], json_file, indent=2) +def _build_arg_parser(): + + parser = argparse.ArgumentParser( + formatter_class=argparse.RawTextHelpFormatter, + description=__doc__) + + parser.add_argument("url", help="URL where to fetch issues.") + parser.add_argument( + "path", + help="Path where to write relevant issue data to Markdown files." + ) + + return parser + + def main(): + parser = _build_arg_parser() + args = parser.parse_args() + # Request issues - url = "https://api.github.com/repos/brainhackorg/global2021/issues?per_page=100" - response = request_issues(url) + response = request_issues(args.url) # Check whether the response was successful success = check_issue_data_request(response) @@ -295,8 +322,7 @@ def main(): website_project_data = gather_website_project_data(issues) # Save the project data - path = "content/project" - save_project_data(website_project_data, path) + save_project_data(website_project_data, args.path) if __name__ == "__main__": From d46594d9d7c62809b07a674d5794ef81b4dfef8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Haitz=20Legarreta=20Gorro=C3=B1o?= Date: Mon, 8 Nov 2021 20:45:47 -0500 Subject: [PATCH 3/3] ENH: Move issues to pages script to dedicated folder Move issues to pages script to dedicated folder. --- .github/workflows/issue-to-page.yml | 2 +- issues_to_pages.py => scripts/issues_to_pages.py | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename issues_to_pages.py => scripts/issues_to_pages.py (100%) diff --git a/.github/workflows/issue-to-page.yml b/.github/workflows/issue-to-page.yml index 5f06dd9..e05020b 100644 --- a/.github/workflows/issue-to-page.yml +++ b/.github/workflows/issue-to-page.yml @@ -21,7 +21,7 @@ jobs: run: | url="https://api.github.com/repos/brainhackorg/global2021/issues?per_page=100" path="content/project" - python issues_to_pages.py $url $path + python scripts/issues_to_pages.py $url $path - name: Create Pull Request uses: peter-evans/create-pull-request@v3 diff --git a/issues_to_pages.py b/scripts/issues_to_pages.py similarity index 100% rename from issues_to_pages.py rename to scripts/issues_to_pages.py