File size: 2,697 Bytes
b301207
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import gitlab
from dotenv import load_dotenv

def setup_test_project():
    load_dotenv()
    gl = gitlab.Gitlab("https://gitlab.com", private_token=os.getenv("GITLAB_TOKEN"))
    gl.auth()
    
    project_name = "context-brain-test"
    
    # Check if project exists
    projects = gl.projects.list(get_all=False)
    target_project = next((p for p in projects if p.name == project_name), None)
    
    if not target_project:
        print(f"[*] Creating new project: {project_name}")
        target_project = gl.projects.create({'name': project_name})
    else:
        print(f"[*] Using existing project: {project_name} (ID: {target_project.id})")

    # Ensure project has some content
    try:
        target_project.files.get(file_path='README.md', ref='main')
    except:
        print("[*] Initializing repository with README.md")
        target_project.files.create({
            'file_path': 'README.md',
            'branch': 'main',
            'content': '# Context Brain Test Project\nThis is a dummy project for testing the GitLab Context Brain.',
            'commit_message': 'Initial commit'
        })

    # Create a new branch and a file for the MR
    branch_name = "feature/test-mr"
    try:
        target_project.branches.create({'branch': branch_name, 'ref': 'main'})
    except:
        pass # Branch might already exist

    try:
        target_project.files.create({
            'file_path': 'vuln_example.py',
            'branch': branch_name,
            'content': 'def handler(event):\n    print("Processing event...")\n    # Potential SQL Injection point\n    query = f"SELECT * FROM users WHERE id = {event.get(\'id\')}"\n    return query\n',
            'commit_message': 'Add dummy code with potential issues'
        })
    except:
        target_project.files.update({
            'file_path': 'vuln_example.py',
            'branch': branch_name,
            'content': 'def handler(event):\n    print("Processing event...")\n    # Potential SQL Injection point\n    query = f"SELECT * FROM users WHERE id = {event.get(\'id\')}"\n    return query\n',
            'commit_message': 'Update dummy code'
        })

    # Create Merge Request
    mr = target_project.mergerequests.create({
        'source_branch': branch_name,
        'target_branch': 'main',
        'title': 'Feature: Add dummy processing logic',
        'description': 'This MR adds some basic processing logic and needs context brain analysis.'
    })
    
    print(f"✅ Success! Created MR IID: {mr.iid} in Project ID: {target_project.id}")
    print(f"🔗 MR URL: {mr.web_url}")
    return target_project.id, mr.iid

if __name__ == "__main__":
    setup_test_project()