Skip to content

Commit

Permalink
Audit: Don't create too many issues, close them when fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Aug 6, 2024
1 parent 1f53339 commit 135864e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
4 changes: 2 additions & 2 deletions github_app_geo_project/module/audit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,12 @@ async def _process_snyk_dpkg(
_LOGGER.error(message)

else:
repo = context.github_project.repo
new_success, pull_request = await module_utils.create_commit_pull_request(
branch,
new_branch,
f"Audit {key}",
"" if body is None else body.to_markdown(),
repo,
context.github_project,
)
success &= new_success
if not new_success:
Expand All @@ -248,6 +247,7 @@ async def _process_snyk_dpkg(

else:
_LOGGER.debug("No changes to commit")
module_utils.close_pull_request_issues(f"Audit {key}", context.github_project)

full_repo = f"{context.github_project.owner}/{context.github_project.repository}"
transversal_message = ", ".join(short_message)
Expand Down
47 changes: 38 additions & 9 deletions github_app_geo_project/module/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ async def create_commit(message: str, pre_commit_check: bool = True) -> bool:


def create_pull_request(
branch: str, new_branch: str, message: str, body: str, repo: github.Repository.Repository
branch: str, new_branch: str, message: str, body: str, project: configuration.GithubProject
) -> tuple[bool, github.PullRequest.PullRequest | None]:
"""Create a pull request."""
proc = subprocess.run( # nosec # pylint: disable=subprocess-run-check
Expand All @@ -614,7 +614,7 @@ def create_pull_request(
_LOGGER.warning(proc_message)
return False, None

pulls = repo.get_pulls(state="open", head=f"{repo.full_name.split('/')[0]}:{new_branch}")
pulls = project.repo.get_pulls(state="open", head=f"{project.repo.full_name.split('/')[0]}:{new_branch}")
if pulls.totalCount > 0:
pull_request = pulls[0]
_LOGGER.debug(
Expand All @@ -627,14 +627,28 @@ def create_pull_request(
if pull_request.created_at < datetime.datetime.now(tz=datetime.timezone.utc) - datetime.timedelta(
days=5
):
issue = repo.create_issue(
title=f"Pull request {message} is open for 5 days",
body=f"See: #{pull_request.number}",
_LOGGER.warning("Pull request #%s is open for 5 days", pull_request.number)
title = f"Pull request {message} is open for 5 days"
body = f"See: #{pull_request.number}"
found = False
issues = project.repo.get_issues(
state="open", creator=project.application.integration.get_app().slug + "[bot]"
)
_LOGGER.warning("Pull request #%s is open for 5 days: #%s", pull_request.number, issue.number)
if issues.totalCount > 0:
for candidate in issues:
if title == candidate.title:
candidate.create_comment("The pull request is still open.")
found = True
if body != candidate.body:
candidate.edit(body=body)
if not found:
project.repo.create_issue(
title=title,
body=body,
)
return False, pull_request
else:
pull_request = repo.create_pull(
pull_request = project.repo.create_pull(
title=message,
body=body,
head=new_branch,
Expand All @@ -646,7 +660,7 @@ def create_pull_request(


async def create_commit_pull_request(
branch: str, new_branch: str, message: str, body: str, repo: github.Repository.Repository
branch: str, new_branch: str, message: str, body: str, project: configuration.GithubProject
) -> tuple[bool, github.PullRequest.PullRequest | None]:
"""Do a commit, then create a pull request."""
if os.path.exists(".pre-commit-config.yaml"):
Expand All @@ -664,7 +678,22 @@ async def create_commit_pull_request(
_LOGGER.debug("pre-commit not installed")
if not await create_commit(message):
return False, None
return create_pull_request(branch, new_branch, message, body, repo)
return create_pull_request(branch, new_branch, message, body, project)


def close_pull_request_issues(message: str, project: configuration.GithubProject) -> None:
"""
Close the pull request issues.
If the pull request is open for 5 days, create an issue.
"""
title = f"Pull request {message} is open for 5 days"
issues = project.repo.get_issues(
state="open", creator=project.application.integration.get_app().slug + "[bot]"
)
for issue in issues:
if title == issue.title:
issue.edit(state="closed")


def git_clone(github_project: configuration.GithubProject, branch: str) -> bool:
Expand Down

0 comments on commit 135864e

Please sign in to comment.