Search code examples
pythongitlabgitlab-api

Add project avatar in gitlab using API


During a migration from gitswarm to gitlab we lost all of the projects avatar. I'm trying to restore them via a python script, but while it is clear how to set the path of the avatar via the project settings API, I do not understand how to upload the avatar. I searched the GitLab API documentation, but I was not able to find anything. Can someone help me please?

PS: GitLab API: upload projects avatar is of no help, since nobody explained the upload command


Solution

  • Finally I was able to upload the avatar of a project via api and python request. There is no need to upload the file first and then set the url, one can simply use the "Edit project" api:

    import requests
    
    filename = 'avatar.png'
    baseUrl = 'https://gitlabrepositoryaddress.com'
    url = baseUrl + '/api/v4/projects/' + str(id)
    up = {'avatar':(filename, open(filename, 'rb'), 'multipart/form-data')}
    authHeader = {'PRIVATE-TOKEN': 'XXXXXXXXXX'}
    request = requests.put(url, files=up, headers=authHeader)
    

    Reference for the api is here: https://docs.gitlab.com/ee/api/projects.html#edit-project

    I hope this may help someone else