diff --git a/INSTALL.md b/INSTALL.md index 5f107b20..31042b60 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -16,6 +16,7 @@ There are several ways to use PR-Agent: - [Method 6: Deploy as a Lambda Function](INSTALL.md#method-6---deploy-as-a-lambda-function) - [Method 7: AWS CodeCommit](INSTALL.md#method-7---aws-codecommit-setup) - [Method 8: Run a GitLab webhook server](INSTALL.md#method-8---run-a-gitlab-webhook-server) +- [Method 9: Run as a Bitbucket Pipeline](INSTALL.md#method-9-run-as-a-bitbucket-pipeline) --- ### Method 1: Use Docker image (no installation required) @@ -370,4 +371,61 @@ In the "Trigger" section, check the ‘comments’ and ‘merge request events 6. Test your installation by opening a merge request or commenting or a merge request using one of CodiumAI's commands. + +### Method 9: Run as a Bitbucket Pipeline + + +You can use our pre-build Bitbucket-Pipeline docker image to run as Bitbucket-Pipeline. + +1. Add the following file in your repository bitbucket_pipelines.yml + + pipelines: + pull-requests: + '**': + - step: + name: PR Agent Pipeline + caches: + - pip + image: python:3.8 # Use an appropriate Python image with pip + services: + - docker + script: + - git clone https://github.com/Codium-ai/pr-agent.git + - cd pr-agent + - pip install docker-compose + - docker-compose up --build + +2. Add the following secret to your repository under Repository settings > Pipelines > Repository variables. +OPENAI_API_KEY: +BITBUCKET_BEARER_TOKEN: + +3. To get BITBUCKET_BEARER_TOKEN follow these steps + So here is my step by step tutorial + i) Insert your workspace name instead of {workspace_name} and go to the following link in order to create an OAuth consumer. + + https://bitbucket.org/{workspace_name}/workspace/settings/api + + set callback URL to http://localhost:8976 (doesn't need to be a real server there) + select permissions: repository -> read + + ii) use consumer's Key as a {client_id} and open the following URL in the browser + + https://bitbucket.org/site/oauth2/authorize?client_id={client_id}&response_type=code + + iii) + after you press "Grant access" in the browser it will redirect you to + + http://localhost:8976?code= + + iv) use the code from the previous step and consumer's Key as a {client_id}, and consumer's Secret as {client_secret} + + curl -X POST -u "{client_id}:{client_secret}" \ + https://bitbucket.org/site/oauth2/access_token \ + -d grant_type=authorization_code \ + -d code={code} \ + + +After completing this steps, you just to place this access token in the repository varibles. + + ======= diff --git a/README.md b/README.md index a1a7dc38..44ab208e 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,7 @@ There are several ways to use PR-Agent: - [Method 6: Deploy as a Lambda Function](INSTALL.md#method-6---deploy-as-a-lambda-function) - [Method 7: AWS CodeCommit](INSTALL.md#method-7---aws-codecommit-setup) - [Method 8: Run a GitLab webhook server](INSTALL.md#method-8---run-a-gitlab-webhook-server) +- [Method 9: Run as a Bitbucket Pipeline](INSTALL.md#method-9-run-as-a-bitbucket-pipeline) ## How it works diff --git a/pr_agent/servers/bitbucket_pipeline_runner.py b/pr_agent/servers/bitbucket_pipeline_runner.py index 4781396a..12c2c6d3 100644 --- a/pr_agent/servers/bitbucket_pipeline_runner.py +++ b/pr_agent/servers/bitbucket_pipeline_runner.py @@ -9,13 +9,23 @@ async def run_action(): slug = os.environ.get("BITBUCKET_REPO_SLUG", '') workspace = os.environ.get("BITBUCKET_WORKSPACE", '') bearer_token = os.environ.get('BITBUCKET_BEARER_TOKEN', None) + OPENAI_KEY = os.environ.get('OPENAI_API_KEY') or os.environ.get('OPENAI.KEY') + OPENAI_ORG = os.environ.get('OPENAI_ORG') or os.environ.get('OPENAI.ORG') # Check if required environment variables are set if not bearer_token: print("BITBUCKET_BEARER_TOKEN not set") + return + + if not OPENAI_KEY: + print("OPENAI_KEY not set") + return # Set the environment variables in the settings get_settings().set("BITBUCKET.BEARER_TOKEN", bearer_token) + get_settings().set("OPENAI.KEY", OPENAI_KEY) + if OPENAI_ORG: + get_settings().set("OPENAI.ORG", OPENAI_ORG) if pull_request_id and slug and workspace: pr_url = f"https://bitbucket.org/{workspace}/{slug}/pull-requests/{pull_request_id}"