Spaces:
Sleeping
Sleeping
| import os | |
| import gitlab | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| # Setup config | |
| TEST_PROJECT_ID = 80447540 | |
| TEST_BRANCH = "feature/test-mr" | |
| def set_variable(project, key, value): | |
| try: | |
| project.variables.create({'key': key, 'value': value}) | |
| print(f"β Set variable {key}") | |
| except gitlab.exceptions.GitlabCreateError as e: | |
| if e.response_code == 400: | |
| # Variable already exists, update it | |
| var = project.variables.get(key) | |
| var.value = value | |
| var.save() | |
| print(f"β Updated variable {key}") | |
| else: | |
| print(f"β Failed to set {key}: {e}") | |
| def main(): | |
| gl_token = os.getenv("GITLAB_TOKEN") | |
| google_key = os.getenv("GOOGLE_API_KEY") | |
| if not gl_token or not google_key: | |
| print("Missing tokens in .env") | |
| return | |
| print("Authenticating with GitLab...") | |
| gl = gitlab.Gitlab('https://gitlab.com', private_token=gl_token) | |
| project = gl.projects.get(TEST_PROJECT_ID) | |
| print(f"Connected to {project.name}...") | |
| print("Configuring CI/CD Variables...") | |
| set_variable(project, "GITLAB_TOKEN", gl_token) | |
| set_variable(project, "GOOGLE_API_KEY", google_key) | |
| if os.getenv("GCP_PROJECT_ID"): | |
| set_variable(project, "GCP_PROJECT_ID", os.getenv("GCP_PROJECT_ID")) | |
| # Create the .gitlab-ci.yml pipeline configuration | |
| ci_config = f""" | |
| stages: | |
| - analyze | |
| context_brain_job: | |
| stage: analyze | |
| image: python:3.10-slim | |
| script: | |
| - apt-get update && apt-get install -y git | |
| # Clone the Context Brain code from the main hackathon repo | |
| - git clone https://oauth2:${{GITLAB_TOKEN}}@gitlab.com/gitlab-ai-hackathon/participants/35466397.git context_brain_repo | |
| - cd context_brain_repo | |
| - pip install -r requirements.txt | |
| - python src/main.py | |
| # Move the generated report back to the root of the test repo for GitLab to find it | |
| - mv gl-code-quality-report.json ../gl-code-quality-report.json | |
| artifacts: | |
| reports: | |
| codequality: gl-code-quality-report.json | |
| rules: | |
| - if: $CI_MERGE_REQUEST_IID | |
| """ | |
| print(f"Committing .gitlab-ci.yml to branch {TEST_BRANCH}...") | |
| try: | |
| data = { | |
| 'branch': TEST_BRANCH, | |
| 'commit_message': 'Add pipeline configuration to test Context Brain', | |
| 'actions': [ | |
| { | |
| 'action': 'create', | |
| 'file_path': '.gitlab-ci.yml', | |
| 'content': ci_config | |
| } | |
| ] | |
| } | |
| project.commits.create(data) | |
| print("β Successfully committed pipeline configuration! The pipeline should be running now.") | |
| except gitlab.exceptions.GitlabCreateError as e: | |
| if "A file with this name already exists" in str(e): | |
| print("Updating existing .gitlab-ci.yml...") | |
| data['actions'][0]['action'] = 'update' | |
| project.commits.create(data) | |
| print("β Successfully updated pipeline configuration! The pipeline should be running now.") | |
| else: | |
| print(f"β Failed to commit: {e}") | |
| if __name__ == "__main__": | |
| main() | |