From 12bd9e8b42fff17d3bbaa498840e3be89f71f539 Mon Sep 17 00:00:00 2001 From: sarbjitgrewal Date: Mon, 11 Sep 2023 16:08:23 +0530 Subject: [PATCH 01/10] add bitbucket pipeline --- Dockerfile.bitbucket_pipeline | 11 ++++++ bitbucket_pipeline/entrypoint.sh | 2 ++ docker-compose.yml | 36 +++++++++++++++++++ pr_agent/servers/bitbucket_pipeline_runner.py | 16 +++++++++ pr_agent/settings/configuration.toml | 18 +++++++++- pr_agent/tools/pr_description.py | 15 ++++---- 6 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 Dockerfile.bitbucket_pipeline create mode 100644 bitbucket_pipeline/entrypoint.sh create mode 100644 docker-compose.yml create mode 100644 pr_agent/servers/bitbucket_pipeline_runner.py diff --git a/Dockerfile.bitbucket_pipeline b/Dockerfile.bitbucket_pipeline new file mode 100644 index 000000000..b75600011 --- /dev/null +++ b/Dockerfile.bitbucket_pipeline @@ -0,0 +1,11 @@ +FROM python:3.10 as base + +WORKDIR /app +ADD pyproject.toml . +ADD requirements.txt . +RUN pip install . && rm pyproject.toml requirements.txt +ENV PYTHONPATH=/app +ADD pr_agent pr_agent +ADD bitbucket_pipeline/entrypoint.sh / +RUN chmod +x /entrypoint.sh +ENTRYPOINT ["/entrypoint.sh"] diff --git a/bitbucket_pipeline/entrypoint.sh b/bitbucket_pipeline/entrypoint.sh new file mode 100644 index 000000000..cbea4b876 --- /dev/null +++ b/bitbucket_pipeline/entrypoint.sh @@ -0,0 +1,2 @@ +#!/bin/bash +python /app/pr_agent/servers/bitbucket_pipeline_runner.py \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..65f2a1955 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,36 @@ +version: '3' +services: + myapp: + build: + context: . + dockerfile: Dockerfile.bitbucket_pipeline + ports: + - "8080:80" + environment: + - BITBUCKET_BRANCH=${BITBUCKET_BRANCH} + - BITBUCKET_BUILD_NUMBER=${BITBUCKET_BUILD_NUMBER} + - BITBUCKET_CLONE_DIR=${BITBUCKET_CLONE_DIR} + - BITBUCKET_COMMIT=${BITBUCKET_COMMIT} + - BITBUCKET_GIT_HTTP_ORIGIN=${BITBUCKET_GIT_HTTP_ORIGIN} + - BITBUCKET_GIT_SSH_ORIGIN=${BITBUCKET_GIT_SSH_ORIGIN} + - BITBUCKET_PIPELINE_UUID=${BITBUCKET_PIPELINE_UUID} + - BITBUCKET_PROJECT_KEY=${BITBUCKET_PROJECT_KEY} + - BITBUCKET_PROJECT_UUID=${BITBUCKET_PROJECT_UUID} + - BITBUCKET_PR_DESTINATION_BRANCH=${BITBUCKET_PR_DESTINATION_BRANCH} + - BITBUCKET_PR_DESTINATION_COMMIT=${BITBUCKET_PR_DESTINATION_COMMIT} + - BITBUCKET_PR_ID=${BITBUCKET_PR_ID} + - BITBUCKET_REPO_FULL_NAME=${BITBUCKET_REPO_FULL_NAME} + - BITBUCKET_REPO_IS_PRIVATE=${BITBUCKET_REPO_IS_PRIVATE} + - BITBUCKET_REPO_OWNER=${BITBUCKET_REPO_OWNER} + - BITBUCKET_REPO_OWNER_UUID=${BITBUCKET_REPO_OWNER_UUID} + - BITBUCKET_REPO_SLUG=${BITBUCKET_REPO_SLUG} + - BITBUCKET_REPO_UUID=${BITBUCKET_REPO_UUID} + - BITBUCKET_SSH_KEY_FILE=${BITBUCKET_SSH_KEY_FILE} + - BITBUCKET_STEP_RUN_NUMBER=${BITBUCKET_STEP_RUN_NUMBER} + - BITBUCKET_STEP_TRIGGERER_UUID=${BITBUCKET_STEP_TRIGGERER_UUID} + - BITBUCKET_STEP_UUID=${BITBUCKET_STEP_UUID} + - BITBUCKET_WORKSPACE=${BITBUCKET_WORKSPACE} + - CI=${CI} + - DOCKER_HOST=${DOCKER_HOST} + - PIPELINES_JWT_TOKEN=${PIPELINES_JWT_TOKEN} + - OPENAI_API_KEY=${OPENAI_API_KEY} \ No newline at end of file diff --git a/pr_agent/servers/bitbucket_pipeline_runner.py b/pr_agent/servers/bitbucket_pipeline_runner.py new file mode 100644 index 000000000..8a9677563 --- /dev/null +++ b/pr_agent/servers/bitbucket_pipeline_runner.py @@ -0,0 +1,16 @@ +import os +from pr_agent.agent.pr_agent import PRAgent +import os +from pr_agent.tools.pr_reviewer import PRReviewer +import asyncio + +async def run_action(): + pull_request_id = os.environ.get("BITBUCKET_PR_ID", '') + slug = os.environ.get("BITBUCKET_REPO_SLUG", '') + workspace = os.environ.get("BITBUCKET_WORKSPACE", '') + if pull_request_id and slug and workspace: + pr_url = f"https://bitbucket.org/{workspace}/{slug}/pull-requests/{pull_request_id}" + await PRReviewer(pr_url).run() + +if __name__ == "__main__": + asyncio.run(run_action()) \ No newline at end of file diff --git a/pr_agent/settings/configuration.toml b/pr_agent/settings/configuration.toml index 75dfcf979..d247f86b8 100644 --- a/pr_agent/settings/configuration.toml +++ b/pr_agent/settings/configuration.toml @@ -1,7 +1,7 @@ [config] model="gpt-4" fallback_models=["gpt-3.5-turbo-16k"] -git_provider="github" +git_provider="bitbucket" publish_output=true publish_output_progress=true verbosity_level=0 # 0,1,2 @@ -50,6 +50,7 @@ extra_instructions = "" [github] # The type of deployment to create. Valid values are 'app' or 'user'. deployment_type = "user" +user_token = "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" # A GitHub personal access token with 'repo' scope. ratelimit_retries = 5 [github_app] @@ -95,6 +96,21 @@ polling_interval_seconds = 30 # token to authenticate in the patch server # patch_server_token = "" + +[openai] +key = "" # Acquire through https://platform.openai.com +#org = "" # Optional, may be commented out. +# Uncomment the following for Azure OpenAI +#api_type = "azure" +#api_version = '2023-05-15' # Check Azure documentation for the current API version +#api_base = "" # The base URL for your Azure OpenAI resource. e.g. "https://.openai.azure.com" +#deployment_id = "" # The deployment name you chose when you deployed the engine +#fallback_deployments = [] # For each fallback model specified in configuration.toml in the [config] section, specify the appropriate deployment_id + +[bitbucket] +# Bitbucket personal bearer token +bearer_token = "V7w1WLUjFIoTQYijE7qgaUArjl75tHSJwCkiqNjCnHVKJCZ6J3r0UUjSP5OYrzTedjT4oXVX4BSWZoiEpdMBXFg8ACpt8wrgAm9zZqGgPiKYMi46E-QtVhHtIFh3PCE=" + [litellm] #use_client = false diff --git a/pr_agent/tools/pr_description.py b/pr_agent/tools/pr_description.py index f30b01654..cd2767856 100644 --- a/pr_agent/tools/pr_description.py +++ b/pr_agent/tools/pr_description.py @@ -75,12 +75,15 @@ async def run(self): if get_settings().pr_description.publish_description_as_comment: self.git_provider.publish_comment(pr_body) else: - self.git_provider.publish_description(pr_title, pr_body) - if self.git_provider.is_supported("get_labels"): - current_labels = self.git_provider.get_labels() - if current_labels is None: - current_labels = [] - self.git_provider.publish_labels(pr_types + current_labels) + if get_settings().config.git_provider == 'bitbucket': + self.git_provider.publish_description(pr_title, description) + else: + self.git_provider.publish_description(pr_title, pr_body) + if self.git_provider.is_supported("get_labels"): + current_labels = self.git_provider.get_labels() + if current_labels is None: + current_labels = [] + self.git_provider.publish_labels(pr_types + current_labels) self.git_provider.remove_initial_comment() return "" From 9bd5140ea4a1cb2ef6a92a16b69ed5c1653312a3 Mon Sep 17 00:00:00 2001 From: sarbjitgrewal Date: Mon, 11 Sep 2023 16:33:45 +0530 Subject: [PATCH 02/10] update access token --- pr_agent/settings/configuration.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pr_agent/settings/configuration.toml b/pr_agent/settings/configuration.toml index d247f86b8..f0659bb11 100644 --- a/pr_agent/settings/configuration.toml +++ b/pr_agent/settings/configuration.toml @@ -109,7 +109,7 @@ key = "" # Acquire through https://platform.openai.com [bitbucket] # Bitbucket personal bearer token -bearer_token = "V7w1WLUjFIoTQYijE7qgaUArjl75tHSJwCkiqNjCnHVKJCZ6J3r0UUjSP5OYrzTedjT4oXVX4BSWZoiEpdMBXFg8ACpt8wrgAm9zZqGgPiKYMi46E-QtVhHtIFh3PCE=" +bearer_token = "v7TX01wzW-hBYZRVl1DMAtX7pjCQoiZ9s9f32XaTbblrLGjBkDR8Zo9p3sIw7iQ40Re102Vos2e0UISExktOp25Pf4XSd4P-msTSkNTwp8fF66Sch5oofe44brsPW_vS" [litellm] #use_client = false From 9bee3055c27d80bfda6ae2fd79e5b108c8dafbf9 Mon Sep 17 00:00:00 2001 From: sarbjitgrewal Date: Tue, 12 Sep 2023 11:15:10 +0530 Subject: [PATCH 03/10] add bitbucket access token --- docker-compose.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 65f2a1955..04a50a22d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -33,4 +33,5 @@ services: - CI=${CI} - DOCKER_HOST=${DOCKER_HOST} - PIPELINES_JWT_TOKEN=${PIPELINES_JWT_TOKEN} - - OPENAI_API_KEY=${OPENAI_API_KEY} \ No newline at end of file + - OPENAI_API_KEY=${OPENAI_API_KEY} + - BITBUCKET_ACCESS_TOKEN=${BITBUCKET_ACCESS_TOKEN} \ No newline at end of file From caee7cbf50435246d240f549905686ba2a1791f5 Mon Sep 17 00:00:00 2001 From: sarbjitgrewal Date: Tue, 12 Sep 2023 11:20:38 +0530 Subject: [PATCH 04/10] add bitbucket access token --- pr_agent/settings/configuration.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pr_agent/settings/configuration.toml b/pr_agent/settings/configuration.toml index f0659bb11..920ea20f9 100644 --- a/pr_agent/settings/configuration.toml +++ b/pr_agent/settings/configuration.toml @@ -109,7 +109,7 @@ key = "" # Acquire through https://platform.openai.com [bitbucket] # Bitbucket personal bearer token -bearer_token = "v7TX01wzW-hBYZRVl1DMAtX7pjCQoiZ9s9f32XaTbblrLGjBkDR8Zo9p3sIw7iQ40Re102Vos2e0UISExktOp25Pf4XSd4P-msTSkNTwp8fF66Sch5oofe44brsPW_vS" +bearer_token = "" [litellm] #use_client = false From 67052aa7140c6008a16c297e376cb7c9856953a8 Mon Sep 17 00:00:00 2001 From: sarbjitgrewal Date: Tue, 12 Sep 2023 11:28:36 +0530 Subject: [PATCH 05/10] add bitbucket access token --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 04a50a22d..3b4b4d3b1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -34,4 +34,4 @@ services: - DOCKER_HOST=${DOCKER_HOST} - PIPELINES_JWT_TOKEN=${PIPELINES_JWT_TOKEN} - OPENAI_API_KEY=${OPENAI_API_KEY} - - BITBUCKET_ACCESS_TOKEN=${BITBUCKET_ACCESS_TOKEN} \ No newline at end of file + - BITBUCKET_BEARER_TOKEN=${BITBUCKET_BEARER_TOKEN} \ No newline at end of file From 978c56c128c3c8c28a802d5f94145336345bfdbe Mon Sep 17 00:00:00 2001 From: sarbjitgrewal Date: Fri, 15 Sep 2023 12:38:34 +0530 Subject: [PATCH 06/10] update pipeline --- pr_agent/servers/bitbucket_pipeline_runner.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pr_agent/servers/bitbucket_pipeline_runner.py b/pr_agent/servers/bitbucket_pipeline_runner.py index 8a9677563..4781396af 100644 --- a/pr_agent/servers/bitbucket_pipeline_runner.py +++ b/pr_agent/servers/bitbucket_pipeline_runner.py @@ -1,6 +1,6 @@ import os from pr_agent.agent.pr_agent import PRAgent -import os +from pr_agent.config_loader import get_settings from pr_agent.tools.pr_reviewer import PRReviewer import asyncio @@ -8,9 +8,19 @@ async def run_action(): pull_request_id = os.environ.get("BITBUCKET_PR_ID", '') slug = os.environ.get("BITBUCKET_REPO_SLUG", '') workspace = os.environ.get("BITBUCKET_WORKSPACE", '') + bearer_token = os.environ.get('BITBUCKET_BEARER_TOKEN', None) + + # Check if required environment variables are set + if not bearer_token: + print("BITBUCKET_BEARER_TOKEN not set") + + # Set the environment variables in the settings + get_settings().set("BITBUCKET.BEARER_TOKEN", bearer_token) + if pull_request_id and slug and workspace: pr_url = f"https://bitbucket.org/{workspace}/{slug}/pull-requests/{pull_request_id}" await PRReviewer(pr_url).run() if __name__ == "__main__": - asyncio.run(run_action()) \ No newline at end of file + asyncio.run(run_action()) + From e343ce8468ca8a41ab31278d423a1986f1742a29 Mon Sep 17 00:00:00 2001 From: sarbjitgrewal Date: Fri, 15 Sep 2023 16:05:55 +0530 Subject: [PATCH 07/10] bitbucket pipeline for adding reviews --- INSTALL.md | 58 +++++++++++++++++++ README.md | 1 + pr_agent/servers/bitbucket_pipeline_runner.py | 10 ++++ 3 files changed, 69 insertions(+) diff --git a/INSTALL.md b/INSTALL.md index 5f107b20c..31042b60e 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 a1a7dc383..44ab208e3 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 4781396af..12c2c6d3e 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}" From 43af4aa1827a2ef58fba6ac944b13405bcf2f2d1 Mon Sep 17 00:00:00 2001 From: sarbjitgrewal Date: Fri, 15 Sep 2023 16:08:13 +0530 Subject: [PATCH 08/10] remove token from config --- pr_agent/settings/configuration.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pr_agent/settings/configuration.toml b/pr_agent/settings/configuration.toml index 9a4aa3f97..3b5f83e0f 100644 --- a/pr_agent/settings/configuration.toml +++ b/pr_agent/settings/configuration.toml @@ -55,7 +55,7 @@ extra_instructions = "" [github] # The type of deployment to create. Valid values are 'app' or 'user'. deployment_type = "user" -user_token = "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" # A GitHub personal access token with 'repo' scope. +user_token = "" # A GitHub personal access token with 'repo' scope. ratelimit_retries = 5 [github_app] From 46ec3c0754d4d64876ecc2d02b1447769294fbac Mon Sep 17 00:00:00 2001 From: sarbjitgrewal Date: Thu, 21 Sep 2023 14:42:59 +0530 Subject: [PATCH 09/10] implement suggestions in bitbucket pipeline --- Dockerfile.bitbucket_pipeline | 7 +++ INSTALL.md | 18 +++---- docker-compose.yml | 37 ------------- pr_agent/servers/bitbucket_pipeline_runner.py | 52 +++++++++---------- pr_agent/settings/configuration.toml | 18 +------ 5 files changed, 42 insertions(+), 90 deletions(-) delete mode 100644 docker-compose.yml diff --git a/Dockerfile.bitbucket_pipeline b/Dockerfile.bitbucket_pipeline index b75600011..889ee1201 100644 --- a/Dockerfile.bitbucket_pipeline +++ b/Dockerfile.bitbucket_pipeline @@ -1,5 +1,12 @@ FROM python:3.10 as base +ENV OPENAI_API_KEY=${OPENAI_API_KEY} \ + BITBUCKET_BEARER_TOKEN=${BITBUCKET_BEARER_TOKEN} \ + BITBUCKET_PR_ID=${BITBUCKET_PR_ID} \ + BITBUCKET_REPO_SLUG=${BITBUCKET_REPO_SLUG} \ + BITBUCKET_WORKSPACE=${BITBUCKET_WORKSPACE} + + WORKDIR /app ADD pyproject.toml . ADD requirements.txt . diff --git a/INSTALL.md b/INSTALL.md index 4593dccf4..b3ecd177c 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -379,21 +379,21 @@ You can use our pre-build Bitbucket-Pipeline docker image to run as Bitbucket-Pi 1. Add the following file in your repository bitbucket_pipelines.yml - pipelines: + pipelines: pull-requests: - '**': + '**': - step: name: PR Agent Pipeline caches: - - pip - image: python:3.8 # Use an appropriate Python image with pip + - pip + image: python:3.8 services: - - docker + - docker script: - - git clone https://github.com/Codium-ai/pr-agent.git - - cd pr-agent - - pip install docker-compose - - docker-compose up --build + - git clone https://github.com/Codium-ai/pr-agent.git + - cd pr-agent + - docker build -t bitbucket_runner:latest -f Dockerfile.bitbucket_pipeline . + - docker run -e OPENAI_API_KEY=$OPENAI_API_KEY -e BITBUCKET_BEARER_TOKEN=$BITBUCKET_BEARER_TOKEN -e BITBUCKET_PR_ID=$BITBUCKET_PR_ID -e BITBUCKET_REPO_SLUG=$BITBUCKET_REPO_SLUG -e BITBUCKET_WORKSPACE=$BITBUCKET_WORKSPACE bitbucket_runner:latest 2. Add the following secret to your repository under Repository settings > Pipelines > Repository variables. OPENAI_API_KEY: diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 3b4b4d3b1..000000000 --- a/docker-compose.yml +++ /dev/null @@ -1,37 +0,0 @@ -version: '3' -services: - myapp: - build: - context: . - dockerfile: Dockerfile.bitbucket_pipeline - ports: - - "8080:80" - environment: - - BITBUCKET_BRANCH=${BITBUCKET_BRANCH} - - BITBUCKET_BUILD_NUMBER=${BITBUCKET_BUILD_NUMBER} - - BITBUCKET_CLONE_DIR=${BITBUCKET_CLONE_DIR} - - BITBUCKET_COMMIT=${BITBUCKET_COMMIT} - - BITBUCKET_GIT_HTTP_ORIGIN=${BITBUCKET_GIT_HTTP_ORIGIN} - - BITBUCKET_GIT_SSH_ORIGIN=${BITBUCKET_GIT_SSH_ORIGIN} - - BITBUCKET_PIPELINE_UUID=${BITBUCKET_PIPELINE_UUID} - - BITBUCKET_PROJECT_KEY=${BITBUCKET_PROJECT_KEY} - - BITBUCKET_PROJECT_UUID=${BITBUCKET_PROJECT_UUID} - - BITBUCKET_PR_DESTINATION_BRANCH=${BITBUCKET_PR_DESTINATION_BRANCH} - - BITBUCKET_PR_DESTINATION_COMMIT=${BITBUCKET_PR_DESTINATION_COMMIT} - - BITBUCKET_PR_ID=${BITBUCKET_PR_ID} - - BITBUCKET_REPO_FULL_NAME=${BITBUCKET_REPO_FULL_NAME} - - BITBUCKET_REPO_IS_PRIVATE=${BITBUCKET_REPO_IS_PRIVATE} - - BITBUCKET_REPO_OWNER=${BITBUCKET_REPO_OWNER} - - BITBUCKET_REPO_OWNER_UUID=${BITBUCKET_REPO_OWNER_UUID} - - BITBUCKET_REPO_SLUG=${BITBUCKET_REPO_SLUG} - - BITBUCKET_REPO_UUID=${BITBUCKET_REPO_UUID} - - BITBUCKET_SSH_KEY_FILE=${BITBUCKET_SSH_KEY_FILE} - - BITBUCKET_STEP_RUN_NUMBER=${BITBUCKET_STEP_RUN_NUMBER} - - BITBUCKET_STEP_TRIGGERER_UUID=${BITBUCKET_STEP_TRIGGERER_UUID} - - BITBUCKET_STEP_UUID=${BITBUCKET_STEP_UUID} - - BITBUCKET_WORKSPACE=${BITBUCKET_WORKSPACE} - - CI=${CI} - - DOCKER_HOST=${DOCKER_HOST} - - PIPELINES_JWT_TOKEN=${PIPELINES_JWT_TOKEN} - - OPENAI_API_KEY=${OPENAI_API_KEY} - - BITBUCKET_BEARER_TOKEN=${BITBUCKET_BEARER_TOKEN} \ No newline at end of file diff --git a/pr_agent/servers/bitbucket_pipeline_runner.py b/pr_agent/servers/bitbucket_pipeline_runner.py index 12c2c6d3e..3c04e9ce1 100644 --- a/pr_agent/servers/bitbucket_pipeline_runner.py +++ b/pr_agent/servers/bitbucket_pipeline_runner.py @@ -5,32 +5,30 @@ import asyncio async def run_action(): - pull_request_id = os.environ.get("BITBUCKET_PR_ID", '') - 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}" - await PRReviewer(pr_url).run() - + try: + pull_request_id = os.environ.get("BITBUCKET_PR_ID", '') + 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}" + await PRReviewer(pr_url).run() + except Exception as e: + print(f"An error occurred: {e}") if __name__ == "__main__": asyncio.run(run_action()) - diff --git a/pr_agent/settings/configuration.toml b/pr_agent/settings/configuration.toml index 3f86b330d..a272e1f93 100644 --- a/pr_agent/settings/configuration.toml +++ b/pr_agent/settings/configuration.toml @@ -1,7 +1,7 @@ [config] model="gpt-4" fallback_models=["gpt-3.5-turbo-16k"] -git_provider="bitbucket" +git_provider="github" publish_output=true publish_output_progress=true verbosity_level=0 # 0,1,2 @@ -56,7 +56,6 @@ extra_instructions = "" [github] # The type of deployment to create. Valid values are 'app' or 'user'. deployment_type = "user" -user_token = "" # A GitHub personal access token with 'repo' scope. ratelimit_retries = 5 [github_app] @@ -102,21 +101,6 @@ polling_interval_seconds = 30 # token to authenticate in the patch server # patch_server_token = "" - -[openai] -key = "" # Acquire through https://platform.openai.com -#org = "" # Optional, may be commented out. -# Uncomment the following for Azure OpenAI -#api_type = "azure" -#api_version = '2023-05-15' # Check Azure documentation for the current API version -#api_base = "" # The base URL for your Azure OpenAI resource. e.g. "https://.openai.azure.com" -#deployment_id = "" # The deployment name you chose when you deployed the engine -#fallback_deployments = [] # For each fallback model specified in configuration.toml in the [config] section, specify the appropriate deployment_id - -[bitbucket] -# Bitbucket personal bearer token -bearer_token = "" - [litellm] #use_client = false From 3e2e2d6c6e4512f307eb073284fec5b6fc84d60f Mon Sep 17 00:00:00 2001 From: sarbjitgrewal Date: Fri, 22 Sep 2023 12:23:41 +0530 Subject: [PATCH 10/10] update install.md --- INSTALL.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/INSTALL.md b/INSTALL.md index b3ecd177c..78331ee48 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -379,6 +379,7 @@ You can use our pre-build Bitbucket-Pipeline docker image to run as Bitbucket-Pi 1. Add the following file in your repository bitbucket_pipelines.yml +```yaml pipelines: pull-requests: '**': @@ -394,6 +395,7 @@ You can use our pre-build Bitbucket-Pipeline docker image to run as Bitbucket-Pi - cd pr-agent - docker build -t bitbucket_runner:latest -f Dockerfile.bitbucket_pipeline . - docker run -e OPENAI_API_KEY=$OPENAI_API_KEY -e BITBUCKET_BEARER_TOKEN=$BITBUCKET_BEARER_TOKEN -e BITBUCKET_PR_ID=$BITBUCKET_PR_ID -e BITBUCKET_REPO_SLUG=$BITBUCKET_REPO_SLUG -e BITBUCKET_WORKSPACE=$BITBUCKET_WORKSPACE bitbucket_runner:latest +``` 2. Add the following secret to your repository under Repository settings > Pipelines > Repository variables. OPENAI_API_KEY: