diff --git a/github_app_geo_project/module/audit/__init__.py b/github_app_geo_project/module/audit/__init__.py index 2b27a7cd516..4e613420c15 100644 --- a/github_app_geo_project/module/audit/__init__.py +++ b/github_app_geo_project/module/audit/__init__.py @@ -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: @@ -248,6 +247,7 @@ async def _process_snyk_dpkg( else: _LOGGER.debug("No changes to commit") + module_utils.close_pull_request_issues(context.github_project) full_repo = f"{context.github_project.owner}/{context.github_project.repository}" transversal_message = ", ".join(short_message) diff --git a/github_app_geo_project/module/utils.py b/github_app_geo_project/module/utils.py index 39b06c3485a..ac8cfe86d01 100644 --- a/github_app_geo_project/module/utils.py +++ b/github_app_geo_project/module/utils.py @@ -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 @@ -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( @@ -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: #%s", pull_request.number, issue.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.integration.get_app().slug + "[bot]" # type: ignore[arg-type] + ) + if issues.totalCount > 0: + for candidate in issues: + if title == candidate.title: + candidate.add_comment(f"Up") + found = True + if body != candidate.body: + candidate.edit(body=body) + if not found: + issue = 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, @@ -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"): @@ -664,7 +678,17 @@ 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): + title = f"Pull request {message} is open for 5 days" + issues = project.repo.get_issues( + state="open", creator=project.integration.get_app().slug + "[bot]" # type: ignore[arg-type] + ) + for issue in issues: + if title == issue.title: + issue.edit(state="closed") def git_clone(github_project: configuration.GithubProject, branch: str) -> bool: