Skip to content
Snippets Groups Projects

Master

Closed Tomasz Kaźmierczak requested to merge master into main
1 file
+ 116
0
Compare changes
  • Side-by-side
  • Inline
main.py 0 → 100644
+ 116
0
from re import I
import gitlab
import requests
import json
from json2html import *
# pobieranie listy projektów
# testowe
# headers = {'PRIVATE-TOKEN': 'glpat-hQFB1vBzz9rQzq8wuhcG'}
# gl = gitlab.Gitlab(url='https://k8s-gitlab.software.geant.org', private_token='glpat-hQFB1vBzz9rQzq8wuhcG')
# gl_user = 'https://k8s-gitlab.software.geant.org/api/v4/users'
# gl_proj = 'https://k8s-gitlab.software.geant.org/api/v4/projects'
# gl_license = 'repository/files/LICENSE?ref='
# gl_branch = 'repository/branches'
# produkcyjne
headers = {'PRIVATE-TOKEN': 'glpat-P6orz6-t2f3n1BoVDrnr'}
gl = gitlab.Gitlab(url='https://gitlab.geant.org', private_token='glpat-P6orz6-t2f3n1BoVDrnr')
gl_user = 'https://gitlab.geant.org/api/v4/users'
gl_proj = 'https://gitlab.geant.org/api/v4/projects'
gl_license = 'repository/files/LICENSE?ref='
gl_branch = 'repository/branches'
def check_license_file(id_proj) -> str:
"""main / master - domyslna nazwa repozytorium sprawdzenie obydwu
zwraca informacje o pliku licencji"""
branch = 'master'
b_link = gl_proj + '/' + str(id_proj) + '/' + gl_branch + '/' + branch
# print(b_link)
b_response = requests.get(b_link, headers=headers)
b_data = b_response.text
b_answer = json.loads(b_data)
# print(b_answer)
if 'name' in b_answer:
branch = 'master'
if 'message' in b_answer:
branch = 'main'
print('branch: ', branch)
c_link = gl_proj + '/' + str(id_proj) + '/' + gl_license + branch
c_response = requests.get(c_link, headers=headers)
c_data = c_response.text
c_answer = json.loads(c_data)
# print(c_answer)
if "message" in c_answer:
print(c_answer['message'])
lic = c_answer['message']
if "file_name" in c_answer:
print(c_answer['file_name'])
lic = c_answer['file_name']
return lic
projects = gl.projects.list(all=True)
# (all=True)
# users = gl.users.list(all=True)
# (all=True)
j_project = {}
j_project['projects'] = {}
for project in projects:
# print('Project ID: ', project.attributes['id'])
# print('Project Name: ', project.attributes['name'])
x = project.attributes['id']
# print(check_license_file(x))
licen = check_license_file(x)
tmp = {
'project_id': project.attributes['id'],
'project_name': project.attributes['name'],
'project_license': licen
}
j_project['projects'][x] = {}
j_project['projects'][x] = tmp
j_project['projects'][x]['owners'] = {}
link = (project.attributes['_links']['members'] + '/all')
response = requests.get(link, headers=headers)
data = response.text
owners = json.loads(data)
for owner in owners:
if owner['access_level'] == 50:
print('User ID: ', owner['id'])
print('User Name: ', owner['name'])
print('Access level: ', owner['access_level'])
y = owner['id']
j_project['projects'][x]['owners'][y] = {}
user = gl.users.get(y)
emails = user.emails.list()
if user.email == user.commit_email:
user_tmp = {
'owner_id': owner['id'],
'owner_name': owner['name'],
'owner_mail': user.email,
}
else:
user_tmp = {
'owner_id': owner['id'],
'owner_name': owner['name'],
'owner_mail': user.email,
'owner_mail_commit': user.commit_email
}
j_project['projects'][x]['owners'][y] = user_tmp
print(8*"-")
new_jproject = json.dumps(j_project, indent=4, ensure_ascii=False)
print(new_jproject)
with open("gitlab-projects.json", "w", encoding='utf8') as outfile:
outfile.write(new_jproject)
new_jproject_html = json2html.convert(json=new_jproject)
with open("gitlab-projects.html", "w", encoding='utf8') as outfile:
outfile.write(new_jproject_html)
\ No newline at end of file
Loading