Codium-ai / pr-agent

🚀CodiumAI PR-Agent: An AI-Powered 🤖 Tool for Automated Pull Request Analysis, Feedback, Suggestions and More! 💻🔍
Apache License 2.0
5.68k stars 524 forks source link

Running on Azure DevOps Pipeline error #1200

Open rethink-danielmohr opened 1 week ago

rethink-danielmohr commented 1 week ago

I was following the directions to run from an azure pipeline in devops. https://pr-agent-docs.codium.ai/installation/azure/

It took me a bit to get the pr trigger to work but I think that I got it in the end. My issue is that does not seem to like the url to my PR. I pasted the generated url into the browser and it goes to the PR page in devops so i am not sure what the issue is. Do you have any suggestions?

The yaml is the exact same as the example, no changes. The logs below are the same except for my company name being replaced with xxxxxx.

Thanks,

2024-09-06T21:30:30.1632154Z ##[section]Starting: Run PR Agent 2024-09-06T21:30:30.1636570Z ============================================================================== 2024-09-06T21:30:30.1636688Z Task : Command line 2024-09-06T21:30:30.1636749Z Description : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows 2024-09-06T21:30:30.1636855Z Version : 2.244.3 2024-09-06T21:30:30.1636911Z Author : Microsoft Corporation 2024-09-06T21:30:30.1636994Z Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/command-line 2024-09-06T21:30:30.1637109Z ============================================================================== 2024-09-06T21:30:30.5754461Z Generating script. 2024-09-06T21:30:30.5762937Z ========================== Starting Command Output =========================== 2024-09-06T21:30:30.5776340Z [command]/usr/bin/bash --noprofile --norc /__w/_temp/45bab00e-475c-430b-bdd9-a346011f724f.sh 2024-09-06T21:30:30.5832333Z Running PR Agent action step 2024-09-06T21:30:30.5832617Z PR_URL=https://xxxxxxx.visualstudio.com/xxxxxxxxx/_git/XXXXXMobile/pullrequest/23232 2024-09-06T21:30:30.5848390Z Organization URL: https://xxxxxxx.visualstudio.com 2024-09-06T21:30:35.1532178Z Traceback (most recent call last): 2024-09-06T21:30:35.1532432Z File "/app/pr_agent/git_providers/init.py", line 57, in get_git_provider_with_context 2024-09-06T21:30:35.1532676Z git_provider = _GIT_PROVIDERSprovider_id 2024-09-06T21:30:35.1532819Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-09-06T21:30:35.1532991Z File "/app/pr_agent/git_providers/azuredevops_provider.py", line 56, in init 2024-09-06T21:30:35.1533155Z self.set_pr(pr_url) 2024-09-06T21:30:35.1533334Z File "/app/pr_agent/git_providers/azuredevops_provider.py", line 190, in set_pr 2024-09-06T21:30:35.1533545Z self.workspace_slug, self.repo_slug, self.pr_num = self._parse_pr_url(pr_url) 2024-09-06T21:30:35.1533704Z ^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-09-06T21:30:35.1533904Z File "/app/pr_agent/git_providers/azuredevops_provider.py", line 554, in _parse_pr_url 2024-09-06T21:30:35.1534070Z raise ValueError( 2024-09-06T21:30:35.1534239Z ValueError: The provided URL does not appear to be a Azure DevOps PR URL 2024-09-06T21:30:35.1534331Z 2024-09-06T21:30:35.1534473Z The above exception was the direct cause of the following exception: 2024-09-06T21:30:35.1534575Z 2024-09-06T21:30:35.1534691Z Traceback (most recent call last): 2024-09-06T21:30:35.1535105Z File "/usr/local/bin/pr-agent", line 8, in 2024-09-06T21:30:35.1535258Z sys.exit(run()) 2024-09-06T21:30:35.1535361Z ^^^^^ 2024-09-06T21:30:35.1535482Z File "/app/pr_agent/cli.py", line 88, in run 2024-09-06T21:30:35.1535640Z result = asyncio.run(inner()) 2024-09-06T21:30:35.1535755Z ^^^^^^^^^^^^^^^^^^^^ 2024-09-06T21:30:35.1535910Z File "/usr/local/lib/python3.12/asyncio/runners.py", line 194, in run 2024-09-06T21:30:35.1536070Z return runner.run(main) 2024-09-06T21:30:35.1536196Z ^^^^^^^^^^^^^^^^ 2024-09-06T21:30:35.1536340Z File "/usr/local/lib/python3.12/asyncio/runners.py", line 118, in run 2024-09-06T21:30:35.1536512Z return self._loop.run_until_complete(task) 2024-09-06T21:30:35.1536655Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-09-06T21:30:35.1536924Z File "/usr/local/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete 2024-09-06T21:30:35.1537111Z return future.result() 2024-09-06T21:30:35.1537220Z ^^^^^^^^^^^^^^^ 2024-09-06T21:30:35.1537343Z File "/app/pr_agent/cli.py", line 79, in inner 2024-09-06T21:30:35.1537557Z result = await asyncio.create_task(PRAgent().handle_request(args.pr_url, [command] + args.rest)) 2024-09-06T21:30:35.1537733Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-09-06T21:30:35.1537893Z File "/app/pr_agent/agent/pr_agent.py", line 57, in handle_request 2024-09-06T21:30:35.1538401Z apply_repo_settings(pr_url) 2024-09-06T21:30:35.1538690Z File "/app/pr_agent/git_providers/utils.py", line 14, in apply_repo_settings 2024-09-06T21:30:35.1538877Z git_provider = get_git_provider_with_context(pr_url) 2024-09-06T21:30:35.1539013Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-09-06T21:30:35.1539188Z File "/app/pr_agent/git_providers/init.py", line 62, in get_git_provider_with_context 2024-09-06T21:30:35.1539391Z raise ValueError(f"Failed to get git provider for {pr_url}") from e 2024-09-06T21:30:35.1539647Z ValueError: Failed to get git provider for https://xxxxxx.visualstudio.com/xxxxxxx/_git/XXXXXXMobile/pullrequest/23232 2024-09-06T21:30:39.7910050Z Traceback (most recent call last): 2024-09-06T21:30:39.7910752Z File "/app/pr_agent/git_providers/init.py", line 57, in get_git_provider_with_context 2024-09-06T21:30:39.7911346Z git_provider = _GIT_PROVIDERSprovider_id 2024-09-06T21:30:39.7911657Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-09-06T21:30:39.7912019Z File "/app/pr_agent/git_providers/azuredevops_provider.py", line 56, in init 2024-09-06T21:30:39.7912306Z self.set_pr(pr_url) 2024-09-06T21:30:39.7912781Z File "/app/pr_agent/git_providers/azuredevops_provider.py", line 190, in set_pr 2024-09-06T21:30:39.7913545Z self.workspace_slug, self.repo_slug, self.pr_num = self._parse_pr_url(pr_url) 2024-09-06T21:30:39.7914480Z ^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-09-06T21:30:39.7914648Z File "/app/pr_agent/git_providers/azuredevops_provider.py", line 554, in _parse_pr_url 2024-09-06T21:30:39.7914831Z raise ValueError( 2024-09-06T21:30:39.7914987Z ValueError: The provided URL does not appear to be a Azure DevOps PR URL 2024-09-06T21:30:39.7915079Z 2024-09-06T21:30:39.7915235Z The above exception was the direct cause of the following exception: 2024-09-06T21:30:39.7915323Z 2024-09-06T21:30:39.7915435Z Traceback (most recent call last): 2024-09-06T21:30:39.7915872Z File "/usr/local/bin/pr-agent", line 8, in 2024-09-06T21:30:39.7916017Z sys.exit(run()) 2024-09-06T21:30:39.7916119Z ^^^^^ 2024-09-06T21:30:39.7916258Z File "/app/pr_agent/cli.py", line 88, in run 2024-09-06T21:30:39.7916399Z result = asyncio.run(inner()) 2024-09-06T21:30:39.7916680Z ^^^^^^^^^^^^^^^^^^^^ 2024-09-06T21:30:39.7916821Z File "/usr/local/lib/python3.12/asyncio/runners.py", line 194, in run 2024-09-06T21:30:39.7916976Z return runner.run(main) 2024-09-06T21:30:39.7917100Z ^^^^^^^^^^^^^^^^ 2024-09-06T21:30:39.7917242Z File "/usr/local/lib/python3.12/asyncio/runners.py", line 118, in run 2024-09-06T21:30:39.7917428Z return self._loop.run_until_complete(task) 2024-09-06T21:30:39.7917554Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-09-06T21:30:39.7917711Z File "/usr/local/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete 2024-09-06T21:30:39.7923189Z return future.result() 2024-09-06T21:30:39.7923557Z ^^^^^^^^^^^^^^^ 2024-09-06T21:30:39.7924021Z File "/app/pr_agent/cli.py", line 79, in inner 2024-09-06T21:30:39.7924388Z result = await asyncio.create_task(PRAgent().handle_request(args.pr_url, [command] + args.rest)) 2024-09-06T21:30:39.7924710Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-09-06T21:30:39.7925130Z File "/app/pr_agent/agent/pr_agent.py", line 57, in handle_request 2024-09-06T21:30:39.7927571Z apply_repo_settings(pr_url) 2024-09-06T21:30:39.7927884Z File "/app/pr_agent/git_providers/utils.py", line 14, in apply_repo_settings 2024-09-06T21:30:39.7928350Z git_provider = get_git_provider_with_context(pr_url) 2024-09-06T21:30:39.7929310Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-09-06T21:30:39.7929717Z File "/app/pr_agent/git_providers/init.py", line 62, in get_git_provider_with_context 2024-09-06T21:30:39.7930059Z raise ValueError(f"Failed to get git provider for {pr_url}") from e 2024-09-06T21:30:39.7930844Z ValueError: Failed to get git provider for https://xxxxxxx.visualstudio.com/xxxxxx/_git/XXXXXMobile/pullrequest/23232 2024-09-06T21:30:44.3753730Z Traceback (most recent call last): 2024-09-06T21:30:44.3754552Z File "/app/pr_agent/git_providers/init.py", line 57, in get_git_provider_with_context 2024-09-06T21:30:44.3755148Z git_provider = _GIT_PROVIDERSprovider_id 2024-09-06T21:30:44.3765847Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-09-06T21:30:44.3766518Z File "/app/pr_agent/git_providers/azuredevops_provider.py", line 56, in init 2024-09-06T21:30:44.3766918Z self.set_pr(pr_url) 2024-09-06T21:30:44.3767360Z File "/app/pr_agent/git_providers/azuredevops_provider.py", line 190, in set_pr 2024-09-06T21:30:44.3768076Z self.workspace_slug, self.repo_slug, self.pr_num = self._parse_pr_url(pr_url) 2024-09-06T21:30:44.3768444Z ^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-09-06T21:30:44.3768860Z File "/app/pr_agent/git_providers/azuredevops_provider.py", line 554, in _parse_pr_url 2024-09-06T21:30:44.3769229Z raise ValueError( 2024-09-06T21:30:44.3769602Z ValueError: The provided URL does not appear to be a Azure DevOps PR URL 2024-09-06T21:30:44.3769886Z 2024-09-06T21:30:44.3770205Z The above exception was the direct cause of the following exception: 2024-09-06T21:30:44.3770488Z 2024-09-06T21:30:44.3770778Z Traceback (most recent call last): 2024-09-06T21:30:44.3771412Z File "/usr/local/bin/pr-agent", line 8, in 2024-09-06T21:30:44.3771720Z sys.exit(run()) 2024-09-06T21:30:44.3772084Z ^^^^^ 2024-09-06T21:30:44.3774866Z File "/app/pr_agent/cli.py", line 88, in run 2024-09-06T21:30:44.3775176Z result = asyncio.run(inner()) 2024-09-06T21:30:44.3775461Z ^^^^^^^^^^^^^^^^^^^^ 2024-09-06T21:30:44.3775798Z File "/usr/local/lib/python3.12/asyncio/runners.py", line 194, in run 2024-09-06T21:30:44.3777592Z return runner.run(main) 2024-09-06T21:30:44.3777922Z ^^^^^^^^^^^^^^^^ 2024-09-06T21:30:44.3778320Z File "/usr/local/lib/python3.12/asyncio/runners.py", line 118, in run 2024-09-06T21:30:44.3778785Z return self._loop.run_until_complete(task) 2024-09-06T21:30:44.3779124Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-09-06T21:30:44.3779524Z File "/usr/local/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete 2024-09-06T21:30:44.3779911Z return future.result() 2024-09-06T21:30:44.3780174Z ^^^^^^^^^^^^^^^ 2024-09-06T21:30:44.3780542Z File "/app/pr_agent/cli.py", line 79, in inner 2024-09-06T21:30:44.3780947Z result = await asyncio.create_task(PRAgent().handle_request(args.pr_url, [command] + args.rest)) 2024-09-06T21:30:44.3781349Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-09-06T21:30:44.3781676Z File "/app/pr_agent/agent/pr_agent.py", line 57, in handle_request 2024-09-06T21:30:44.3782234Z apply_repo_settings(pr_url) 2024-09-06T21:30:44.3783576Z File "/app/pr_agent/git_providers/utils.py", line 14, in apply_repo_settings 2024-09-06T21:30:44.3783813Z git_provider = get_git_provider_with_context(pr_url) 2024-09-06T21:30:44.3783969Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-09-06T21:30:44.3784170Z File "/app/pr_agent/git_providers/init.py", line 62, in get_git_provider_with_context 2024-09-06T21:30:44.3784404Z raise ValueError(f"Failed to get git provider for {pr_url}") from e 2024-09-06T21:30:44.3784683Z ValueError: Failed to get git provider for https://xxxxxxx.visualstudio.com/xxxxxxx/_git/XXXXXXMobile/pullrequest/23232 2024-09-06T21:30:44.7322094Z 2024-09-06T21:30:44.7371327Z ##[error]Bash exited with code '1'. 2024-09-06T21:30:44.7467460Z ##[section]Finishing: Run PR Agent

mrT23 commented 1 week ago

work from CLI, and debug. make sure the result of self.workspace_slug, self.repo_slug, self.pr_num = self._parse_pr_url(pr_url) makes sense

rethink-danielmohr commented 1 week ago

Thank you. We will try that when we get into the office on Monday.

From: Tal @.> Sent: Saturday, September 7, 2024 12:44 AM To: Codium-ai/pr-agent @.> Cc: Daniel Mohr @.>; Author @.> Subject: Re: [Codium-ai/pr-agent] Running on Azure DevOps Pipeline error (Issue #1200)

[CAUTION]: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.

work from CLI, and debug. make sure the result of self.workspace_slug, self.repo_slug, self.pr_num = self._parse_pr_url(pr_url) makes sense

- Reply to this email directly, view it on GitHubhttps://github.com/Codium-ai/pr-agent/issues/1200#issuecomment-2335039874, or unsubscribehttps://github.com/notifications/unsubscribe-auth/A2LAPAH62ZW2LB4VQVV6O7DZVKAAVAVCNFSM6AAAAABNZMFQHSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMZVGAZTSOBXGQ. You are receiving this because you authored the thread.Message ID: @.**@.>>

MikesHorcrux commented 1 week ago

I am still having this issue and have tried a few things. Here is where I think the issue is....

Description:

The PR Agent fails to recognize valid Azure DevOps PR URLs in the visualstudio.com format, raising a ValueError that the URL is not a valid Azure DevOps PR URL. Azure DevOps supports both dev.azure.com and visualstudio.com formats, but the PR Agent seems to only handle the dev.azure.com format.

Error Message:

Traceback (most recent call last):
  File "/app/pr_agent/git_providers/__init__.py", line 57, in get_git_provider_with_context
    git_provider = _GIT_PROVIDERS[provider_id](pr_url)
  File "/app/pr_agent/git_providers/azuredevops_provider.py", line 56, in __init__
    self.set_pr(pr_url)
  File "/app/pr_agent/git_providers/azuredevops_provider.py", line 190, in set_pr
    self.workspace_slug, self.repo_slug, self.pr_num = self._parse_pr_url(pr_url)
  File "/app/pr_agent/git_providers/azuredevops_provider.py", line 548, in _parse_pr_url
    raise ValueError("The provided URL does not appear to be a Azure DevOps PR URL")
ValueError: The provided URL does not appear to be a Azure DevOps PR URL

Steps to Reproduce:

  1. Set up the PR Agent to run on an Azure DevOps repository.
  2. Use a PR URL in the following format:
    https://{organization}.visualstudio.com/{project}/_git/{repository}/pullrequest/{id}
  3. Run the PR Agent to process the PR, which results in the error shown above.

Expected Behavior:

The PR Agent should be able to handle both Azure DevOps URL formats:

Actual Behavior:

The PR Agent raises a ValueError stating that the provided URL does not appear to be a valid Azure DevOps PR URL when using the visualstudio.com format.

Potential Cause:

The issue appears to be in the _parse_pr_url method in azuredevops_provider.py. The current logic may not account for URLs using the visualstudio.com format, resulting in the failure to recognize these URLs as valid.

Possible Fix:

Im not totaly sure but... Modify the _parse_pr_url function to support both visualstudio.com and dev.azure.com URL formats. For example, using a regex to match both formats:

import re

def _parse_pr_url(self, pr_url):
    # Support both dev.azure.com and visualstudio.com formats
    match = re.match(r'https://(?:[^.]+\.visualstudio\.com|dev\.azure\.com)/([^/]+)/_git/([^/]+)/pullrequest/(\d+)', pr_url)
    if not match:
        raise ValueError("The provided URL does not appear to be a valid Azure DevOps PR URL")
    return match.groups()  # Organization, Project, Repo, PR ID

This will allow the PR Agent to parse both URL formats correctly.

Workaround (If Applicable):

One potential workaround is to manually rewrite the visualstudio.com URL into the dev.azure.com format before passing it to the PR Agent. However, this isn't ideal and would require modifying pipeline scripts.

Environment:

All in all I really want to implement this tool so would love some help.

mrT23 commented 1 week ago

@MikesHorcrux Try first, from CLI, this PR: https://github.com/Codium-ai/pr-agent/pull/1216

make sure to properly change your organization:

[azure_devops]
org = "https://dev.azure.com/YOUR_ORGANIZATION/"

to

[azure_devops]
org = "https://{organization}.visualstudio.com/"

if it works, it can be merged to production

MikesHorcrux commented 1 week ago

@mrT23 Ill test this after its merged in when do you think this will be approved

mrT23 commented 1 week ago

@MikesHorcrux No. i wont merge something if i am not sure it works. and i dont have access to azure with the org = "https://{organization}.visualstudio.com/" format

checkout the branch, run it via CLI, and give feedback if it works, or not. that is the path for merging

rethink-danielmohr commented 1 week ago

@mrT23 and @MikesHorcrux, your fix seems to work, in the sense that it gets past that first error. Thank you. It seems to parse the visualstudio.com format.

I also found that based on your guy's conversation changing the URL in you azure pipeline example seem to have a similar effect:

_# Construct PR_URL PR_URL="https://dev.azure.com/${SYSTEM_TEAMPROJECT}/${SYSTEM_TEAMPROJECT}/_git/${BUILD_REPOSITORY_NAME}/pullrequest/${SYSTEM_PULLREQUEST_PULLREQUESTID}" echo "PR_URL=$PRURL"

It could be unique to my company but we needed the extra ${SYSTEM_TEAMPROJECT} for the link to work in the dev.azure.com format. I do not know if that applies to all DevOps, though. We can try the other format on the pipeline once you push the PR.

But it seems that in either case we run into a similar error:

For the PR that you asked Mike to try, we got this:

_2024-09-11 12:54:50.562 | ERROR | pr_agent.git_providers.azuredevops_provider:get_diff_files:353 - Failed to retrieve original file content of /TestRepo/TestRepo/badcode.swift at version {'additional_properties': {}, 'version': '0b487f9de5cc6cf2bc79654fb8b418db03f56ca4', 'version_options': None, 'versiontype': 'commit'}. Error: TF401174: The item 'TestRepo/TestRepo/badcode.swift' could not be found in the repository 'PRAGENTTEST' at the version specified by '<Commit: 0b487f9de5cc6cf2bc79654fb8b418db03f56ca4 >' (resolved to commit '0b487f9de5cc6cf2bc79654fb8b418db03f56ca4')

And for the pipeline we got this error:

image

Both are about round the additional_properties which is only in one part of the code.

The only debugging we did so far was for these values right before the error, and they looked correct. This was also only on the local cli build and not the azure pipeline, just to be clear.

            print(self.repo_slug)
            print(self.pr_num)
            print(self.workspace_slug)
            print(iteration_id)

I will try to look at this a bit later to see if I can find a smoking gun, but I am really not a Python dev (it does not come up for me as much as I would like), so I am learning on the fly. Any ideas what causes the additional_properties errors?

mrT23 commented 1 week ago

additional_properties

https://github.com/Codium-ai/pr-agent/blob/dd8d78e7d8eaf0b2bd8aabe88c17f9dae6a38844/pr_agent/git_providers/azuredevops_provider.py#L257

As the API result seem different for this kind of repo, in order to fully solve the issue, you will need to edit the PR (or start a new one). Catch a breakpoint there, and see if there is an easy fix, or if more through adjustments are need.