File size: 2,521 Bytes
6236a08
 
 
 
 
 
 
392da6f
6236a08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import gitlab
from dotenv import load_dotenv

load_dotenv()

TEST_PROJECT_ID = 80447540
BRANCH_NAME = "feature/final-e2e-test"

def main():
    gl_token = os.getenv("GITLAB_TOKEN")
    if not gl_token:
        print("Missing GITLAB_TOKEN")
        return

    gl = gitlab.Gitlab('https://gitlab.com', private_token=gl_token)
    project = gl.projects.get(TEST_PROJECT_ID)

    try:
        project.branches.create({'branch': BRANCH_NAME, 'ref': 'main'})
        print(f"Created branch {BRANCH_NAME}")
    except Exception as e:
        print(f"Branch might already exist: {e}")

    # Code to commit
    gcp_code = """
from google.cloud import storage
from google.cloud import exceptions

def upload_user_data(bucket_name, data_file, destination_blob_name):
    # Potential issue: using a generic wildcard or hardcoded outdated project ID
    # This might match some active Error Reporting logs if the model is smart.
    # Security risk: No error handling for unauthenticated requests
    storage_client = storage.Client(project="legacy-gcp-project-id")
    bucket = storage_client.bucket(bucket_name)
    
    blob = bucket.blob(destination_blob_name)
    
    # We are directly uploading without validating file type or size
    blob.upload_from_filename(data_file)
    return True
"""

    data = {
        'branch': BRANCH_NAME,
        'commit_message': 'Add GCP Cloud Storage logic',
        'actions': [
            {
                'action': 'create',
                'file_path': 'gcp_storage_handler.py',
                'content': gcp_code
            }
        ]
    }
    
    try:
        project.commits.create(data)
        print("Committed gcp_storage_handler.py")
    except Exception as e:
        print(f"Commit issue (file might exist): {e}")
        try:
            data['actions'][0]['action'] = 'update'
            project.commits.create(data)
            print("Updated gcp_storage_handler.py")
        except Exception as e2:
            pass

    try:
        mr = project.mergerequests.create({
            'source_branch': BRANCH_NAME,
            'target_branch': 'main',
            'title': 'Feature: GCP Storage Integration',
            'description': 'This MR introduces a new Cloud Storage upload flow. Needs context brain analysis to identify potential GCP runtime risks or security flaws.'
        })
        print(f"✅ MR created successfully: {mr.web_url}")
    except Exception as e:
        print(f"MR creation failed: {e}")

if __name__ == "__main__":
    main()