Skip to content

Instantly share code, notes, and snippets.

@muxueqz
Last active April 5, 2019 06:07
Show Gist options
  • Save muxueqz/b5903966a402f9306e20c682e9ba3d4f to your computer and use it in GitHub Desktop.
Save muxueqz/b5903966a402f9306e20c682e9ba3d4f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import json, sys, urllib
import urllib.parse
import urllib.request
gitlab_endpoint = 'http://gitlab-site/api/v4/'
token = ""
headers = {'PRIVATE-TOKEN' : token}
# data = urllib.parse.urlencode(values)
# data = data.encode('ascii')
# req = urllib.request.Request(url, data, headers)
def get_resource(resource):
# resource like projects
total_pages = "X-Total-Pages"
url = '%s%s' % (gitlab_endpoint, resource)
req = urllib.request.Request(url, None, headers)
with urllib.request.urlopen(req) as response:
print(response.headers)
gitlab_response = response.read()
return json.loads(gitlab_response)
def get_projects():
total_pages = 3
current_page = 0
results = []
while total_pages > current_page:
resource = 'projects?per_page=100&page=%d' % (current_page + 1)
print(resource)
url = '%s%s' % (gitlab_endpoint, resource)
req = urllib.request.Request(url, None, headers)
with urllib.request.urlopen(req) as response:
total_pages = int(response.headers["X-Total-Pages"])
gitlab_response = response.read()
results.extend(json.loads(gitlab_response))
current_page += 1
return results
def get_groups():
json_data = get_resource('groups?per_page=100')
keys = "name id".split(' ')
projects = {}
for line in json_data:
projects[line.get('id')] = line.get('name')
return projects
if __name__:
# print(get_groups())
# json_data = get_resource('groups/76/projects')
json_data = get_projects()
with open('repos.txt', 'w') as _fd:
for i in json_data:
repo = i.get('ssh_url_to_repo')
_fd.write(repo + "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment