Skip to content

Commit

Permalink
Fix AzureCommentReporter not adding comments to PR (#4138)
Browse files Browse the repository at this point in the history
* pr_repo_name

* repository id

* updated changelog

* BUILD_REPOSITORY_ID fallback

* SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI config get

* Update .cspell.json

---------

Co-authored-by: Nicolas Vuillamy <[email protected]>
  • Loading branch information
lukelloyd1985 and nvuillam authored Oct 16, 2024
1 parent b822d6d commit 6c00575
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"RQASWB",
"SCSSLINT",
"SOURCEBRANCHNAME",
"SOURCEREPOSITORYURI",
"St\u00e9phane",
"Suero",
"TOOMANYREQUESTS",
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Note: Can be used with `oxsecurity/megalinter@beta` in your GitHub Action mega-l
- Fixes

- Reporters
- Fix AzureCommentReporter not adding comments to PR on 2024-10-15

- Doc

Expand Down
35 changes: 30 additions & 5 deletions megalinter/reporters/AzureCommentReporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- SYSTEM_ACCESSTOKEN
- SYSTEM_COLLECTIONURI
- SYSTEM_PULLREQUEST_PULLREQUESTID
- SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI
- SYSTEM_TEAMPROJECT
- BUILD_BUILDID
- BUILD_REPOSITORY_ID
Expand Down Expand Up @@ -59,6 +60,9 @@ def produce_report(self):
SYSTEM_TEAMPROJECT = urllib.parse.quote(
config.get(self.master.request_id, "SYSTEM_TEAMPROJECT")
)
SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI = config.get(
self.master.request_id, "SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI", ""
)
BUILD_REPOSITORY_ID = config.get(
self.master.request_id, "BUILD_REPOSITORY_ID"
)
Expand Down Expand Up @@ -97,9 +101,30 @@ def produce_report(self):
)
git_client: GitClient = connection.clients.get_git_client()

# Get repository id
if SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI == "":
logging.info(
"[Azure Comment Reporter] Missing ADO variable System.PullRequest.SourceRepositoryURI\n"
+ "Falling back to ADO variable Build.Repository.ID\n"
+ "See https://learn.microsoft.com/en-us/azure/devops/pipelines/"
+ "build/variables?view=azure-devops&tabs=yaml"
)
repository_id = BUILD_REPOSITORY_ID
else:
logging.info(
"[Azure Comment Reporter] Using ADO variable System.PullRequest.SourceRepositoryURI\n"
+ "See https://learn.microsoft.com/en-us/azure/devops/pipelines/"
+ "build/variables?view=azure-devops&tabs=yaml"
)
repository_name = SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI.split("/")[-1]
repository = git_client.get_repository(
repository_name
)
repository_id = repository

# Look for existing MegaLinter thread
existing_threads = git_client.get_threads(
BUILD_REPOSITORY_ID, SYSTEM_PULLREQUEST_PULLREQUESTID
repository_id, SYSTEM_PULLREQUEST_PULLREQUESTID
)
existing_thread_id = None
existing_thread_comment = None
Expand All @@ -119,13 +144,13 @@ def produce_report(self):
# Remove previous MegaLinter thread if existing
if existing_thread_id is not None:
git_client.delete_comment(
BUILD_REPOSITORY_ID,
repository_id,
SYSTEM_PULLREQUEST_PULLREQUESTID,
existing_thread_id,
existing_thread_comment_id,
)
existing_thread_comment = git_client.get_pull_request_thread(
BUILD_REPOSITORY_ID,
repository_id,
SYSTEM_PULLREQUEST_PULLREQUESTID,
existing_thread_id,
)
Expand All @@ -135,14 +160,14 @@ def produce_report(self):
}
git_client.update_thread(
existing_thread_comment,
BUILD_REPOSITORY_ID,
repository_id,
SYSTEM_PULLREQUEST_PULLREQUESTID,
existing_thread_id,
)

# Post thread
new_thread_result = git_client.create_thread(
thread_data, BUILD_REPOSITORY_ID, SYSTEM_PULLREQUEST_PULLREQUESTID
thread_data, repository_id, SYSTEM_PULLREQUEST_PULLREQUESTID
)
if new_thread_result.id is not None and new_thread_result.id > 0:
logging.debug(f"Posted Azure Pipelines comment: {thread_data}")
Expand Down

0 comments on commit 6c00575

Please sign in to comment.