Skip to content

Instantly share code, notes, and snippets.

@tschwaerzl
Created February 2, 2018 10:06
Show Gist options
  • Save tschwaerzl/b1655f1a254d5a0c46124a35270415b6 to your computer and use it in GitHub Desktop.
Save tschwaerzl/b1655f1a254d5a0c46124a35270415b6 to your computer and use it in GitHub Desktop.
Delete all branch-directories on repository server which are non-existent in corresponding GitLab project
import os
import shutil
import requests
GITLAB_URL = 'https://foo.com/api/v4/projects/210/repository/branches'
GITLAB_TOKEN = '123456abcdefg' # GitLab user token
SERVER_PATH = '/apps/foobar/ios/development' # Where are the build branches
def remove_folder(path):
# check if folder exists
if os.path.exists(path):
# remove if exists
shutil.rmtree(path)
res = requests.get(GITLAB_URL, headers={'PRIVATE-TOKEN':GITLAB_TOKEN})
data = res.json()
directories = os.listdir(SERVER_PATH)
for branch in data:
try:
directories.remove(branch['name'])
except ValueError:
# Directory exists, but Branch no more.
# Leave in list so it gets deleted
pass
# exclude 'release' branch from list
try:
directories.remove('release')
except:
pass
# delete old directories
for folder in directories:
path = os.path.join(d, folder)
print("Deleting: %s" % path)
remove_folder(path)
print("cleaned")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment