diff --git a/format_checker/common_checks.py b/format_checker/common_checks.py index a656a3e10..f96f3e887 100644 --- a/format_checker/common_checks.py +++ b/format_checker/common_checks.py @@ -3,12 +3,14 @@ import re import csv import subprocess +import requests from utils import ( get_committed_lines, get_uncommitted_lines, log_info, log_std_error, log_esp_error, + log_archived_error, ) @@ -23,6 +25,17 @@ } +def check_repo_archived(checked_projects, filename, row, i, log): + project_url = row["Project URL"] + try: + resp = requests.get(project_url) + # Determine if it is an archived project + if "This repository has been archived by the owner. It is now read-only." in resp.text: + log_archived_error(filename, log, i, row, "Project URL") + except requests.exceptions.RequestException as e: + log_esp_error(filename, log, "The check for repo-archived failed due to ERROR:" + str(e)) + + def check_header(header, valid_dict, filename, log): """Validates that the header is correct.""" @@ -108,6 +121,7 @@ def run_checks(file, data_dict, log, commit_range, checks): if "1" in uncommitted_lines or "1" in committed_lines: check_header(list(header.values()), data_dict, file, log) if uncommitted_lines != [] or committed_lines != []: + checked_projects = set() for i, row in enumerate(info): i += 2 line = str(i) @@ -123,6 +137,9 @@ def run_checks(file, data_dict, log, commit_range, checks): if check_rule.__name__ == check_row_length.__name__: check_rule(len(header), *params) continue + if check_rule.__name__ == check_repo_archived.__name__: + check_rule(checked_projects, *params) + continue check_rule(*params) else: log_info(file, log, "There are no changes to be checked") diff --git a/format_checker/pr_checker.py b/format_checker/pr_checker.py index 4f3be9339..ddcb2ddb2 100644 --- a/format_checker/pr_checker.py +++ b/format_checker/pr_checker.py @@ -1,11 +1,13 @@ """Implements rule checks for the pr-data.csv file.""" import re -from utils import log_std_error, log_warning +from utils import log_std_error, log_esp_error, log_warning +import requests from common_checks import ( check_common_rules, check_row_length, check_sort, + check_repo_archived, run_checks, ) @@ -117,10 +119,22 @@ def check_status_consistency(filename, row, i, log): # If it contains a PR link, it should be a valid one else: check_pr_link(filename, row, i, log) + + if row["Status"] == "RepoArchived": + try: + response = requests.get(row["Project URL"]) + # Determine if it is not archived + if "This repository has been archived by the owner. It is now read-only." not in response.text: + log_esp_error(filename, log, "Status should not be \"RepoArchived\" when the repo is not archived.\n" + + "row " + str(i) + ", URL: " + row["Project URL"]) + except requests.exceptions.RequestException as e: + # handle(e) + pass if row["Status"] == "" and row["PR Link"] != "": check_pr_link(filename, row, i, log) - log_std_error(filename, log, i, row, "Status should not be empty when a PR link is provided.") + log_esp_error(filename, log, "Status should not be empty when a PR link is provided.\n" + + "row " + str(i) + ", URL: " + row["Project URL"]) def check_notes(filename, row, i, log): """Checks validity of Notes.""" @@ -149,6 +163,7 @@ def run_checks_pr(log, commit_range): check_category, check_status, check_status_consistency, + check_repo_archived, ] run_checks(filename, pr_data, log, commit_range, checks) check_sort(filename, log) diff --git a/format_checker/requirements.txt b/format_checker/requirements.txt index 556ccd5aa..f0610713e 100644 --- a/format_checker/requirements.txt +++ b/format_checker/requirements.txt @@ -1 +1,2 @@ errorhandler==2.0.1 +requests==2.18.4 diff --git a/format_checker/utils.py b/format_checker/utils.py index 46db55fa8..44110ca0e 100644 --- a/format_checker/utils.py +++ b/format_checker/utils.py @@ -116,7 +116,26 @@ def log_esp_error(filename, log, message): log_esp_error.tracker += 1 log.error("ERROR: On file " + filename + ": " + message) + +def log_archived_error(filename, log, line, row, key): + """Logs a archived-related error.""" + # log_archived_error.tracker += 1 # AttributeError: 'function' object has no attribute 'tracker' + log_esp_error.tracker += 1 + log.error( + "ERROR: On file " + + filename + + ", row " + + line + + ":\n" + + "Archived " + + key + + ': "' + + row[key] + + '"' + ) + + def log_warning(filename, log, line, message): """Logs a warning."""