Commit 2c5feabf authored by jbleher's avatar jbleher
Browse files

Initial Commit

parents
Loading
Loading
Loading
Loading

README.md

0 → 100644
+17 −0
Original line number Diff line number Diff line
Check which projects are affected by the cleanup and prevent deletion by commiting to them. 


1. run script gitlab_check_projects.py

- checks which projects are affected by deletions
- creates an Excel file (might contain projects that do not exist anymore in the UI)


2. modify excel

- modify column "keep_project" to define which projects to keep (use WAHR or FALSCH)


3. run script commit_to_keep.py

- creates commits for project that are marked with "keep_project"

Readme.txt

0 → 100644
+19 −0
Original line number Diff line number Diff line
Check which projects are affected by the cleanup and prevent deletion by commiting to them. 



1. run script gitlab_check_projects.py

- checks which projects are affected by deletions
- creates an Excel file (might contain projects that do not exist anymore in the UI)


2. modify excel

- modify column "keep_project" to define which projects to keep (use WAHR or FALSCH)


3. run script commit_to_keep.py

- creates commits for project that are marked with "keep_project"
 No newline at end of file

commit_to_keep.py

0 → 100644
+52 −0
Original line number Diff line number Diff line
import pandas as pd
import requests
import os

# Constants
GITLAB_API_URL = "https://aidaho-edu.uni-hohenheim.de/gitlab/api/v4"
PERSONAL_ACCESS_TOKEN = ""
SUBFOLDER = "data"
# Headers for authentication
headers = {
    "Private-Token": PERSONAL_ACCESS_TOKEN,
    "Content-Type": "application/json"
}

def create_commit(project_id,branch="main"):
    url = f"{GITLAB_API_URL}/projects/{project_id}/repository/commits"
    data = {
        "branch": branch,
        "commit_message": "Automated commit",
        "actions": [
            {
                "action": "create",
                "file_path": "prevent_project_deletion.txt",
                "content": "This file was created to prevent the automated deletion of the project during GitLab clean up at the end of the semester. This file can be deleted. "
            }
        ]
    }
    response = requests.post(url, headers=headers, json=data)
    response.raise_for_status()
    return response.json()

def main():
    # Read the Excel file
    script_directory = os.path.dirname(os.path.abspath("__file__"))
    excel_path = os.path.join(script_directory,SUBFOLDER,'gitlab_projects_latest_commits.xlsx')
    df = pd.read_excel(excel_path)

    # Filter projects where "Keep Project" is True
    projects_to_keep = df[df['keep_project'] == True]

    # Iterate through filtered projects and create a commit
    for index, row in projects_to_keep.iterrows():
        project_id = row['Project ID'] 
        branch = row['Default Branch'] 
        try:
            commit_response = create_commit(project_id=project_id, branch=branch)
            print(f"Commit created for project {project_id}: {commit_response}")
        except Exception as e:
            print(f"Failed to create commit for project {project_id}: {e}")

if __name__ == "__main__":
    main()

data/.gitignore

0 → 100644
+1 −0
Original line number Diff line number Diff line
*.xlsx
+92 −0
Original line number Diff line number Diff line
import requests
import pandas as pd
from datetime import datetime
from dateutil.parser import parse # to convert times
import os

# Configuration
GITLAB_API_URL = "https://aidaho-edu.uni-hohenheim.de/gitlab/api/v4"
# List of owners to check for
PRIVATE_TOKEN = "<MY PRIVATE TOKEN>"
OWNER_LIST = ['aidaho','<MY USERNAME>']
CUTOFF_DATE = datetime.strptime("2024-01-01", "%Y-%m-%d")
SUBFOLDER = "data"
# Headers for authentication
headers = {
    "Private-Token": PRIVATE_TOKEN
}

def get_projects():
    projects = []
    page = 1
    while True:
        url = f"{GITLAB_API_URL}/projects?membership=true&per_page=100&page={page}"
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        project_batch = response.json()
        if not project_batch:
            break
        projects.extend(project_batch)
        page += 1
    return projects

def get_latest_commit(project,owner_list):
    project_id = project['id']
    try:
        project_owner = project['owner']['username'] 
    except KeyError:
        return None
    if project_owner not in owner_list:
        return None
    url = f"{GITLAB_API_URL}/projects/{project_id}/repository/commits?per_page=1"
    response = requests.get(url, headers=headers)
    response.raise_for_status()
    commits = response.json()
    if commits:
        return commits[0]
    return None

def get_default_branch(project,owner_list):
    try:
        project_owner = project['owner']['username'] 
        project_branch = project['default_branch']
    except KeyError:
        return None
    if project_owner not in owner_list:
        return None
    return(project_branch)

def main():
    projects = get_projects()
    data = []

    for project in projects:
        project_id = project['id']
        print(project_id)
        project_name = project['name']
        branch = get_default_branch(project,OWNER_LIST)
        latest_commit = get_latest_commit(project,OWNER_LIST)

        if latest_commit:
            commit_date = parse(latest_commit['created_at']).replace(tzinfo=None)
            is_before_cutoff = commit_date < CUTOFF_DATE
            data.append({
                'Project ID' : project_id,
                'Project Name': project_name,
                'Latest Commit': latest_commit['id'],
                'Default Branch': branch,
                'Commit Date': latest_commit['created_at'],
                'Affected by Delete': is_before_cutoff,
                'keep_project' :False 
            })

    df = pd.DataFrame(data)
    script_directory = os.path.dirname(os.path.abspath(__file__))
    subfolder = script_directory +"/"+ SUBFOLDER
    if not os.path.exists(subfolder):
        os.makedirs(subfolder)
    excel_path = os.path.join(subfolder,'gitlab_projects_latest_commits.xlsx')
    df.to_excel(excel_path, index=False)

if __name__ == "__main__":
    main()