Daksh C Jain commited on
Commit
6236a08
·
1 Parent(s): 2f69cda

Add test MR creation scripts

Browse files
Files changed (2) hide show
  1. create_gcp_test_mr.py +80 -0
  2. fix_ci.py +53 -0
create_gcp_test_mr.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gitlab
3
+ from dotenv import load_dotenv
4
+
5
+ load_dotenv()
6
+
7
+ TEST_PROJECT_ID = 80447540
8
+ BRANCH_NAME = "feature/gcp-test-3"
9
+
10
+ def main():
11
+ gl_token = os.getenv("GITLAB_TOKEN")
12
+ if not gl_token:
13
+ print("Missing GITLAB_TOKEN")
14
+ return
15
+
16
+ gl = gitlab.Gitlab('https://gitlab.com', private_token=gl_token)
17
+ project = gl.projects.get(TEST_PROJECT_ID)
18
+
19
+ try:
20
+ project.branches.create({'branch': BRANCH_NAME, 'ref': 'main'})
21
+ print(f"Created branch {BRANCH_NAME}")
22
+ except Exception as e:
23
+ print(f"Branch might already exist: {e}")
24
+
25
+ # Code to commit
26
+ gcp_code = """
27
+ from google.cloud import storage
28
+ from google.cloud import exceptions
29
+
30
+ def upload_user_data(bucket_name, data_file, destination_blob_name):
31
+ # Potential issue: using a generic wildcard or hardcoded outdated project ID
32
+ # This might match some active Error Reporting logs if the model is smart.
33
+ # Security risk: No error handling for unauthenticated requests
34
+ storage_client = storage.Client(project="legacy-gcp-project-id")
35
+ bucket = storage_client.bucket(bucket_name)
36
+
37
+ blob = bucket.blob(destination_blob_name)
38
+
39
+ # We are directly uploading without validating file type or size
40
+ blob.upload_from_filename(data_file)
41
+ return True
42
+ """
43
+
44
+ data = {
45
+ 'branch': BRANCH_NAME,
46
+ 'commit_message': 'Add GCP Cloud Storage logic',
47
+ 'actions': [
48
+ {
49
+ 'action': 'create',
50
+ 'file_path': 'gcp_storage_handler.py',
51
+ 'content': gcp_code
52
+ }
53
+ ]
54
+ }
55
+
56
+ try:
57
+ project.commits.create(data)
58
+ print("Committed gcp_storage_handler.py")
59
+ except Exception as e:
60
+ print(f"Commit issue (file might exist): {e}")
61
+ try:
62
+ data['actions'][0]['action'] = 'update'
63
+ project.commits.create(data)
64
+ print("Updated gcp_storage_handler.py")
65
+ except Exception as e2:
66
+ pass
67
+
68
+ try:
69
+ mr = project.mergerequests.create({
70
+ 'source_branch': BRANCH_NAME,
71
+ 'target_branch': 'main',
72
+ 'title': 'Feature: GCP Storage Integration',
73
+ 'description': 'This MR introduces a new Cloud Storage upload flow. Needs context brain analysis to identify potential GCP runtime risks or security flaws.'
74
+ })
75
+ print(f"✅ MR created successfully: {mr.web_url}")
76
+ except Exception as e:
77
+ print(f"MR creation failed: {e}")
78
+
79
+ if __name__ == "__main__":
80
+ main()
fix_ci.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gitlab
3
+ from dotenv import load_dotenv
4
+
5
+ load_dotenv()
6
+ TEST_PROJECT_ID = 80447540
7
+
8
+ gl_token = os.getenv("GITLAB_TOKEN")
9
+ gl = gitlab.Gitlab('https://gitlab.com', private_token=gl_token)
10
+ project = gl.projects.get(TEST_PROJECT_ID)
11
+
12
+ ci_config = """
13
+ stages:
14
+ - analyze
15
+
16
+ context_brain_job:
17
+ stage: analyze
18
+ image: python:3.10-slim
19
+ script:
20
+ - apt-get update && apt-get install -y git
21
+ - git clone https://oauth2:${GITLAB_TOKEN}@gitlab.com/gitlab-ai-hackathon/participants/35466397.git context_brain_repo
22
+ - cd context_brain_repo
23
+ - pip install -r requirements.txt
24
+ - python main.py
25
+ - mv gl-code-quality-report.json ../gl-code-quality-report.json
26
+ artifacts:
27
+ when: always
28
+ reports:
29
+ codequality: gl-code-quality-report.json
30
+ rules:
31
+ - if: $CI_MERGE_REQUEST_IID
32
+ """
33
+ try:
34
+ project.commits.create({
35
+ 'branch': 'main',
36
+ 'commit_message': 'Add pipeline to main',
37
+ 'actions': [{'action': 'create', 'file_path': '.gitlab-ci.yml', 'content': ci_config}]
38
+ })
39
+ print("Added to main")
40
+ except Exception as e:
41
+ print("Check 1:", e)
42
+ pass
43
+
44
+ try:
45
+ project.commits.create({
46
+ 'branch': 'feature/gcp-test-3',
47
+ 'commit_message': 'Add pipeline to branch',
48
+ 'actions': [{'action': 'create', 'file_path': '.gitlab-ci.yml', 'content': ci_config}]
49
+ })
50
+ print("Added to branch")
51
+ except Exception as e:
52
+ print("Check 2:", e)
53
+ pass