Skip to content

Commit

Permalink
Merge pull request #233 from sehlen-bsi/feature/approvers_in_auto_update
Browse files Browse the repository at this point in the history
Render list of Approvers into Patches from Auto-Update
  • Loading branch information
reneme authored Jul 22, 2024
2 parents 5929bb7 + 206d57a commit 3743277
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
8 changes: 8 additions & 0 deletions tools/auditinfo/auditinfo/auditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ def __init__(self, name: str, github_handle: str):
self.name = name
self.github_handle = github_handle[1:] if github_handle.startswith('@') else github_handle

def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
return self.github_handle == other.github_handle

def __hash__(self):
return self.github_handle.__hash__()

def authorative_auditors() -> list[Auditor]:
auditors_file = auditors_file_path()
strm = open(auditors_file, 'r')
Expand Down
9 changes: 8 additions & 1 deletion tools/auditupdate/auditupdate/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def update_uncategorized_patches(audit: genaudit.Audit, repo: genaudit.GitRepo,
]))

# render all found unreferenced patches
rendered_unrefed_patches = ''.join([f"\n{patch.render_patch(repo, True)}\n" for patch in unrefed_patches])
rendered_unrefed_patches = ''.join([f"\n{patch.render_patch(repo, yaml=True, approvers=True)}\n" for patch in unrefed_patches])
unrefed_topic.write(rendered_unrefed_patches)

return True
Expand Down Expand Up @@ -83,6 +83,9 @@ def main():
parser.add_argument('-n', '--output-topic-title',
help='Title of the topic to append unreferenced patches to',
default='Uncategorized Patches')
parser.add_argument('-d', '--dry-run',
help='Do not commit or push changes to the repository',
default=False, action='store_true')
parser.add_argument('audit_config_dir',
help='the audit directory to be updated')

Expand Down Expand Up @@ -130,6 +133,10 @@ def main():
logging.critical(f"Upstream repo was updated (from {old_target[0:7]} to {new_target[0:7]}) but didn't find new patches. Something is wrong here!")
return 1

if args.dry_run:
logging.info(f"DRY-RUN: Not committing or pushing changes to the repository.")
return 0

# Commit and push the new (uncategorized) patch references.
run_git(["add", audit.get_topic_file_path(args.output_topic)])
run_git(["commit", "--no-gpg-sign", "-m", f"Automatic update to match patches until {new_target[0:7]}"])
Expand Down
28 changes: 23 additions & 5 deletions tools/genaudit/genaudit/refs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from enum import IntEnum
from functools import total_ordering

import auditinfo

from github.PullRequest import PullRequest as GithubPullRequest
from github.Commit import Commit as GithubCommit

Expand Down Expand Up @@ -58,9 +60,9 @@ def __init__(self, commit_ref: str, classification: Classification, auditer: str
self.auditer = auditer
self.comment = comment

def render_patch(self, repo, yaml: bool = False):
def render_patch(self, repo, yaml: bool = False, approvers: bool = False):
if isinstance(self, PullRequest):
return self.render(repo.pull_request_info(self), yaml)
return self.render(repo.pull_request_info(self), yaml, approvers)
elif isinstance(self, Commit):
return self.render(repo.commit_info(self), yaml)
else:
Expand All @@ -86,10 +88,25 @@ def __lt__(self, other):
def merge_commit(self):
return Commit(self.ref) if self.ref else None

def render(self, pr_info: GithubPullRequest, yaml: bool = False) -> str:
def render(self, pr_info: GithubPullRequest, yaml: bool = False, render_approvers: bool = False) -> str:
if render_approvers:
all_approvers = list(set([auditinfo.Auditor(review.user.name, review.user.login) for review in pr_info.get_reviews() if review.state == "APPROVED"]))
authorative_auditors = [auditor for auditor in all_approvers if auditor in auditinfo.authorative_auditors()]
other_approvers = [approver for approver in all_approvers if approver not in auditinfo.authorative_auditors()]

if yaml:
out = [
f"# {pr_info.title} (@{pr_info.user.login})",
f"# {pr_info.title}",
f"# Author: @{pr_info.user.login}"
]
if render_approvers:
approvers = [f'@{auditor.github_handle}' for auditor in authorative_auditors]
approvers += [f'(@{approver.github_handle})' for approver in other_approvers]
if approvers:
out += [
f"# Approvals: {', '.join(approvers)}",
]
out += [
f"- pr: {pr_info.number} # {pr_info.html_url}",
f" merge_commit: {self.ref}",
f" classification: {self.classification.to_string()}",
Expand Down Expand Up @@ -127,7 +144,8 @@ def render(self, co_info: GithubCommit, yaml: bool = False) -> str:
author = co_info.commit.author.name
if yaml:
out = [
f"# {msg} ({author})",
f"# {msg}",
f"# Author: {author}",
f"- commit: {co_info.sha} # {co_info.html_url}",
f" classification: {self.classification.to_string()}",
]
Expand Down

0 comments on commit 3743277

Please sign in to comment.