bartbot / sweep

Sweep: AI-powered Junior Developer for small features and bug fixes.
https://sweep.dev
GNU Affero General Public License v3.0
0 stars 0 forks source link

Sweep: update GITHUB environment variables in sweepai/config/server.py to their GitLab equivalents #44

Open bartbot opened 6 months ago

bartbot commented 6 months ago

Details

Sweep: update GITHUB environment variables in sweepai/config/server.py to their GitLab equivalents. Use GitLab Oauth Application documentation to source appropriate environment variables for sweepai/config/server.py.

Ensure that these variables are compatible with GitLab's Applications API here: https://docs.gitlab.com/ee/api/applications.html

Checklist - [X] Modify `sweepai/config/server.py` ✓ https://github.com/bartbot/sweep/commit/1c5a24ca37747700d1d39919d09f443fc34d2787 [Edit](https://github.com/bartbot/sweep/edit/sweep/update_github_environment_variables_in_s/sweepai/config/server.py#L11-L97) - [X] Modify `sweepai/utils/gitlab_utils.py` ✓ https://github.com/bartbot/sweep/commit/79b9e984365bfcf635e614b2f5355735b6af0316 [Edit](https://github.com/bartbot/sweep/edit/sweep/update_github_environment_variables_in_s/sweepai/utils/gitlab_utils.py#L44-L60) - [ ] Create `sweepai/utils/gitlab_utils_test.py` ⋯ [Edit](https://github.com/bartbot/sweep/edit/sweep/update_github_environment_variables_in_s/sweepai/utils/gitlab_utils_test.py)
sweep-self-hosted-digital[bot] commented 6 months ago
Sweeping

✨ Track Sweep's progress on our progress dashboard!


50%
💎 Sweep Pro: I'm using GPT-4. You have unlimited GPT-4 tickets. (tracking ID: None)

[!TIP] I can email you when I complete this pull request if you set up your email here!


Actions (click)

Sandbox execution failed

The sandbox appears to be unavailable or down.


Step 1: 🔎 Searching

I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.

Some code snippets I think are relevant in decreasing order of relevance (click to expand). If some file is missing from here, you can mention the path in the ticket description. https://github.com/bartbot/sweep/blob/69871e2ed1feef956e023ad36a8b1ba434c96fc3/sweepai/config/server.py#L11-L97 https://github.com/bartbot/sweep/blob/69871e2ed1feef956e023ad36a8b1ba434c96fc3/sweepai/utils/github_utils.py#L38-L60 https://github.com/bartbot/sweep/blob/69871e2ed1feef956e023ad36a8b1ba434c96fc3/sweepai/utils/gitlab_utils.py#L38-L60
I also found the following external resources that might be helpful: **Summaries of links found in the content:** https://docs.gitlab.com/ee/api/applications.html: The page "Applications API | GitLab" provides documentation for the Applications API in GitLab. The API allows for the management of instance-wide OAuth applications for using GitLab as an authentication provider and allowing access to GitLab resources on a user's behalf. The API can be used to create, list, and delete applications. The relevant code snippets from the page are as follows: 1. Create an application: ```plaintext POST /applications Parameters: Attribute Type Required Description name string yes Name of the application. redirect_uri string yes Redirect URI of the application. scopes string yes Scopes of the application. You can specify multiple scopes by separating each scope using a space. confidential boolean no The application is used where the client secret can be kept confidential. Native mobile apps and Single Page Apps are considered non-confidential. Defaults to true if not supplied ``` 2. List all applications: ```plaintext GET /applications ``` 3. Delete an application: ```plaintext DELETE /applications/:id Parameters: Attribute Type Required Description id integer yes The ID of the application (not the application_id). ``` These code snippets provide the necessary information for using the Applications API to create, list, and delete applications in GitLab.

Step 2: ⌨️ Coding

--- 
+++ 
@@ -9,16 +9,9 @@

 load_dotenv(dotenv_path=".env")

-os.environ["GITHUB_APP_PEM"] = os.environ.get("GITHUB_APP_PEM") or base64.b64decode(
-    os.environ.get("GITHUB_APP_PEM_BASE64", "")
+os.environ["GITLAB_APP_SECRET"] = os.environ.get("GITLAB_APP_SECRET") or base64.b64decode(
+    os.environ.get("GITLAB_APP_SECRET_BASE64", "")
 ).decode("utf-8")
-
-if os.environ["GITHUB_APP_PEM"]:
-    os.environ["GITHUB_APP_ID"] = (
-        (os.environ.get("GITHUB_APP_ID") or os.environ.get("APP_ID"))
-        .replace("\\n", "\n")
-        .strip('"')
-    )

 os.environ["TRANSFORMERS_CACHE"] = os.environ.get(
     "TRANSFORMERS_CACHE", "/tmp/cache/model"
@@ -66,25 +59,26 @@
 if not GITLAB_REDIRECT_URI:
     raise ValueError("GitLab Redirect URI not found in environment variables.")
 # deprecated: old logic transfer so upstream can use this
+# Default GitLab App IDs based on environment
 if GITLAB_APP_ID is None:
     if ENV == "prod":
-        GITLAB_APP_ID = ""
+        GITLAB_APP_ID = os.environ.get("GITLAB_APP_ID", "")
     elif ENV == "dev":
-        GITLAB_APP_ID = ""
+        GITLAB_APP_ID = os.environ.get("GITLAB_APP_ID", "")
     elif ENV == "staging":
-        GITLAB_APP_ID = ""
+        GITLAB_APP_ID = os.environ.get("GITLAB_APP_ID", "")
 GITHUB_BOT_USERNAME = os.environ.get("GITHUB_BOT_USERNAME")

 # deprecated: left to support old logic
-if not GITHUB_BOT_USERNAME:
+# Set GitLab Bot Username based on environment
+GITLAB_BOT_USERNAME = os.environ.get("GITLAB_BOT_USERNAME")
+if not GITLAB_BOT_USERNAME:
     if ENV == "prod":
-        GITHUB_BOT_USERNAME = "sweep-ai[bot]"
+        GITLAB_BOT_USERNAME = ""
     elif ENV == "dev":
-        GITHUB_BOT_USERNAME = "sweep-nightly[bot]"
+        GITLAB_BOT_USERNAME = ""
     elif ENV == "staging":
-        GITHUB_BOT_USERNAME = "sweep-canary[bot]"
-elif not GITHUB_BOT_USERNAME.endswith("[bot]"):
-    GITHUB_BOT_USERNAME = GITHUB_BOT_USERNAME + "[bot]"
+        GITLAB_BOT_USERNAME = ""

 GITLAB_LABEL_NAME = os.environ.get("GITLAB_LABEL_NAME", "sweep")
 GITLAB_LABEL_COLOR = os.environ.get("GITLAB_LABEL_COLOR", "9400D3")
@@ -92,10 +86,7 @@
     "GITLAB_LABEL_DESCRIPTION", "Sweep your software chores"
 )
 GITHUB_APP_PEM = os.environ.get("GITHUB_APP_PEM")
-GITHUB_APP_PEM = GITHUB_APP_PEM or os.environ.get("PRIVATE_KEY")
-if GITHUB_APP_PEM is not None:
-    GITHUB_APP_PEM = GITHUB_APP_PEM.strip(' \n"')  # Remove whitespace and quotes
-    GITHUB_APP_PEM = GITHUB_APP_PEM.replace("\\n", "\n")
+

 GITLAB_CONFIG_BRANCH = os.environ.get("GITLAB_CONFIG_BRANCH", "sweep/add-sweep-config")
 GITLAB_DEFAULT_CONFIG = os.environ.get(
@@ -230,7 +221,8 @@
 WHITELISTED_USERS = os.environ.get("WHITELISTED_USERS", None)
 if WHITELISTED_USERS:
     WHITELISTED_USERS = WHITELISTED_USERS.split(",")
-    WHITELISTED_USERS.append(GITHUB_BOT_USERNAME)
+    # Append the GitLab bot username to the list of whitelisted users
+WHITELISTED_USERS.append(GITLAB_BOT_USERNAME)

 DEFAULT_GPT4_32K_MODEL = os.environ.get("DEFAULT_GPT4_32K_MODEL", "gpt-4-1106-preview")
 DEFAULT_GPT35_MODEL = os.environ.get("DEFAULT_GPT35_MODEL", "gpt-3.5-turbo-1106")

--- 
+++ 
@@ -41,11 +41,11 @@
 # Therefore, we can remove this function.

-def get_gitlab_token(client_id: str, client_secret: str):
+def get_gitlab_token(GITLAB_APP_ID: str, GITLAB_APP_SECRET: str):
     data = {
         'grant_type': 'client_credentials',
-        'client_id': client_id,
-        'client_secret': client_secret
+        'client_id': GITLAB_APP_ID,
+        'client_secret': GITLAB_APP_SECRET
     }
     headers = {
         'Content-Type': 'application/x-www-form-urlencoded'
@@ -56,7 +56,7 @@
     return response.json()['access_token']
             time.sleep(timeout)
     raise Exception(
-        "Could not get token, please double check your PRIVATE_KEY and GITLAB_APP_ID in the .env file. Make sure to restart uvicorn after."
+        "Could not get token, please double check your GITLAB_APP_SECRET and GITLAB_APP_ID in the .env file. Make sure to restart uvicorn after."
     )


Step 3: 🔁 Code Review

Working on it...


🎉 Latest improvements to Sweep:


💡 To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request. Join Our Discord