Skip to content

Project created via API gets broken after follow-up initial push

Summary

Project created via API gets broken when i do follow-up initial push, looks like project is not fully created when created via API because this can be workarounded by running housekeeping or visiting project detail right after creation (and maybe more)

Steps to reproduce

  1. Create new project via API
  2. Add writable deploy key to this project via API
  3. Push to this project via SSH (use URL returned by project creation)

! It is important that all these steps are done outside of Gitlab web interface, cos visiting project detail before push workarounds this BUG.

I also created simple python script to reproduce this bug + found two workarounds to it (Just follow instructions)

pip3 install python-gitlab
python3 gitlab_bug.py

gitlab_bug.py:

import gitlab
import os
import datetime
import subprocess
import requests
import logging

logging.basicConfig(level=logging.DEBUG)


class WorkaroundEnum(object):
    NONE = 0
    HOUSEKEEPING = 1
    WEB_OPEN = 2


PRIVATE_TOKEN = '' # Put your private token here if you want to run this script multiple times to save time
GITLAB_URL = 'https://gitlab.com'
WORKAROUND_ENABLED = WorkaroundEnum.NONE

PRIVATE_TOKEN = input('Private token [{}]: '.format(PRIVATE_TOKEN)) or PRIVATE_TOKEN
GITLAB_URL = input('Gitlab URL [{}]: '.format(GITLAB_URL)) or GITLAB_URL

print('Enable workaround ?')
print('0) None')
print('1) Housekeeping workaround')
print('2) Web open workaround')
WORKAROUND_ENABLED = int(input('Workaround enabled [{}]: '.format(WORKAROUND_ENABLED)) or WORKAROUND_ENABLED)
if WORKAROUND_ENABLED not in [WorkaroundEnum.NONE, WorkaroundEnum.HOUSEKEEPING, WorkaroundEnum.WEB_OPEN]:
    print('Unknown option selected')
    exit()

gl = gitlab.Gitlab(
    GITLAB_URL,
    private_token=PRIVATE_TOKEN,
    api_version='4'
)

gl.auth()

repository_name = 'gitlab-bug-{}'.format(datetime.datetime.now().strftime('%d_%m_%Y-%H_%M_%S'))
print('Creating project  {}'.format(repository_name))

project = gl.projects.create({
    'name': repository_name,
    'visibility': 'public'
})

# This to prevent this BUG
if WORKAROUND_ENABLED == WorkaroundEnum.HOUSEKEEPING:
    # Workarounds this bug by running project housekeeping
    print('Using HOUSEKEEPING WORKAROUND')
    gl.http_post('/projects/{project_id}/housekeeping'.format(project_id=project.id))
elif WORKAROUND_ENABLED == WorkaroundEnum.WEB_OPEN:
    # Workarounds this bug by opening project detail
    print('Using WEB_OPEN WORKAROUND')
    requests.get(project.web_url)

print('Adding deploy key')
key = project.keys.create({
    'title': 'Our test key',
    'key': open(os.path.join(os.path.expanduser('~'), '.ssh', 'id_rsa.pub')).read(),
    'can_push': True  # We need write access
})

test_repo_path = os.path.join('/tmp', repository_name)

subprocess.Popen(['git', 'clone', 'https://github.com/Salamek/blacklist.git', test_repo_path]).communicate()
subprocess.Popen(['git', 'remote', 'add', 'gitlab', project.ssh_url_to_repo], cwd=test_repo_path).communicate()
subprocess.Popen(['git', 'push', '-u', 'gitlab', '--all'], cwd=test_repo_path).communicate()
subprocess.Popen(['git', 'push', '-u', 'gitlab', '--tags'], cwd=test_repo_path).communicate()

if WORKAROUND_ENABLED == WorkaroundEnum.NONE:
    print('Now go to {} to see totally fu**ed gitlab repository detail'.format(
        project.web_url
    ))
else:
    print('Bug workaround used, {} should be OK'.format(project.web_url))

Example Project

https://gitlab.com/Salamek/gitlab-bug-03_03_2018-01_44_20

What is the current bug behavior?

Project detail crashes to 404

What is the expected correct behavior?

Project detail should be shown as usual

Relevant logs and/or screenshots

None, and screenshot of standard gitlab 404 wont help you

Output of checks

This bug happens on GitLab.com

Possible fixes

Sorry not a ruby guy