Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.7 kB
0
Indexable
Never
import requests

# Set the GitLab API endpoint and user access token
api_endpoint = 'https://gitlab.com/api/v4'
user_access_token = 'glpat-HiNdT3K2-KyCubUjs14c'

# Get user ID based on username
def get_user_id(username):
    user_endpoint = f'{api_endpoint}/users?username={username}'
    headers = {'Authorization': f'Bearer {user_access_token}'}
    response = requests.get(user_endpoint, headers=headers)
    if response.status_code == 200:
        user_data = response.json()
        return user_data[0]['id']
    else:
        return None

# Get list of forked projects for user based on user ID
def get_user_forked_projects(user_id):
    projects_endpoint = f'{api_endpoint}/users/{user_id}/projects?membership=true&simple=true'
    headers = {'Authorization': f'Bearer {user_access_token}'}
    response = requests.get(projects_endpoint, headers=headers)
    if response.status_code == 200:
        projects_data = response.json()
        forked_projects = [project for project in projects_data if 'forked_from_project' in project]
        return forked_projects
    else:
        return None

# Get list of forked projects for user based on username
def get_user_forked_projects_by_username(username):
    user_id = get_user_id(username)
    if user_id:
        return get_user_forked_projects(user_id)
    else:
        return None

# Usage example
username = 'shubham1507'
forked_projects = get_user_forked_projects_by_username(username)
if forked_projects:
    print(f'List of forked projects for user {username}:')
    for project in forked_projects:
        print(project['name'])
else:
    print(f'Error: Unable to get list of forked projects for user {username}')