Skip to content

Commit

Permalink
feat: skip if the publiccode.yml was updated
Browse files Browse the repository at this point in the history
Don't open or update the issue if the publiccode.yml was updated
after the log date, but instead skip the repo and postpone the check to
the next run (usually after 24 hours).

If the publiccode.yml has changed it means the maintainer did potentially
made our error obsolete, or might have fixed all the errors, making the
publiccode.yml valid, and at that point it's fair for them to close the
issue.

Without this check, we'd be annoyingly reopen an obsolete issue.
  • Loading branch information
bfabio committed Jul 24, 2024
1 parent c7fc75d commit 3d99afb
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
API_BASEURL = os.getenv("API_BASEURL", "https://api.developers.italia.it/v1")
GITHUB_USERNAME = os.getenv("GITHUB_USERNAME", "publiccode-validator-bot")

SoftwareLog = namedtuple("SoftwareLog", "log_url software formatted_error_output")
SoftwareLog = namedtuple(
"SoftwareLog", "log_url software formatted_error_output datetime"
)


def to_markdown(error: str, repo_url: str) -> str:
Expand Down Expand Up @@ -124,9 +126,19 @@ def software_logs(days):
f'{API_BASEURL}/logs/{log["id"]}',
software,
to_markdown(match.group("parser_output"), software["url"]),
datetime.fromisoformat(log["createdAt"]),
)


def publiccodeyml_last_change(repo: github.Repository) -> datetime:
commits = repo.get_commits(path="publiccode.yml")

if commits.totalCount == 0:
return datetime.fromtimestamp(0)

return commits[0].commit.author.date.replace(tzinfo=timezone.utc)


def issues(gh: github.Github, repo: str) -> list[github.Issue.Issue]:
issues = gh.search_issues(
"publiccode.yml in:title state:open is:issue",
Expand Down Expand Up @@ -215,15 +227,19 @@ def run(gh, since, dry_run, lang):
iss = issues(gh, repo_path)

issue = has_issue(iss)
repo = gh.get_repo(f"{repo_path}", lazy=True)
if not issue:
repo = gh.get_repo(f"{repo_path}", lazy=True)
print(f"➕ Creating issue for {url}...")
if not dry_run:
repo.create_issue(
title="Errors in publiccode.yml file", body=content
)

issues_created += 1
elif publiccodeyml_last_change(repo) >= log.datetime:
print(
f"⌛ publiccode.yml was changed in the repo after our log, doing nothing ({url})"
)
elif should_update_issue(sha1sum, issue):
print(f"🔄 Updating issue for {url}...")
if not dry_run:
Expand All @@ -238,7 +254,10 @@ def run(gh, since, dry_run, lang):
reset_timestamp = int(e.headers.get("x-ratelimit-reset", 0))
sleep_duration = max(0, reset_timestamp - int(time.time()) + 10)

print(f"Rate limit exceeded. Sleeping for {sleep_duration} seconds...", end="")
print(
f"Rate limit exceeded. Sleeping for {sleep_duration} seconds...",
end="",
)
sys.stdout.flush()
time.sleep(sleep_duration)
print("done")
Expand Down

0 comments on commit 3d99afb

Please sign in to comment.