From a66332e1f3e5de5437edc151f8cb508c8b2c0f09 Mon Sep 17 00:00:00 2001
From: kazix <tmakzmierczak@man.poznan.pl>
Date: Tue, 2 Aug 2022 10:29:42 +0200
Subject: [PATCH] added files

---
 .gitignore       |   5 ++
 main.py          | 152 +++++++++++++++++++++++++++++++++++++++++++++++
 requirements.txt | Bin 0 -> 322 bytes
 3 files changed, 157 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 main.py
 create mode 100644 requirements.txt

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c8ba6b7
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+/gitlab-projects.html
+/gitlab-projects.json
+/.idea
+/.idea/*
+.env
\ No newline at end of file
diff --git a/main.py b/main.py
new file mode 100644
index 0000000..531a010
--- /dev/null
+++ b/main.py
@@ -0,0 +1,152 @@
+# from re import I
+import gitlab
+import requests
+import json
+from json2html import *
+import argparse
+import os
+from dotenv import load_dotenv
+
+parser = argparse.ArgumentParser(description="Get Project, License and Owners from gitlab")
+
+parser.add_argument("--production", action='store_true', help="Use for Production")
+parser.add_argument("--developer", action='store_true', help="Use for Developer")
+args = parser.parse_args()
+
+load_dotenv()
+
+try:
+    if args.production:
+        token = os.getenv('PRODUCTION_TOKEN')
+        print('produkcyjny')
+        headers = {'PRIVATE-TOKEN': token}
+        gl = gitlab.Gitlab(url='https://gitlab.geant.org', private_token=token)
+        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'
+        projects = gl.projects.list(all=True)
+    elif args.developer:
+        token = os.getenv('DEVELOPER_TOKEN')
+        print('testowy')
+        headers = {'PRIVATE-TOKEN': token}
+        gl = gitlab.Gitlab(url='https://k8s-gitlab.software.geant.org', private_token=token)
+        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'
+        projects = gl.projects.list(all=True)
+    else:
+        print("No variable, exit")
+        exit()
+
+    # 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'
+    # projects = gl.projects.list()
+    # # (all=True)
+
+
+    # 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'
+    # projects = gl.projects.list(all=True)
+
+    j_project = {}
+    j_project['projects'] = {}
+    for project in projects:
+        # print('Project ID: ', project.attributes['id'])
+        # print('Project Name: ', project.attributes['name_with_namespace'])
+        # # print('branch', project.attributes['default_branch'])
+        # print('status', project.attributes['visibility'])
+        # print('aktywny', project.attributes['last_activity_at'])
+        # print('grupa', project.attributes['namespace']['name'])
+        x = project.attributes['id']
+        check_b = gl_proj + '/' + str(x)
+        check_b_response = requests.get(check_b, headers=headers)
+        check_b_data = check_b_response.text
+        check_b_answer = json.loads(check_b_data)
+        if "default_branch" in check_b_answer:
+            branch = project.attributes['default_branch']
+            # x = project.attributes['id']
+            c_link = gl_proj + '/' + str(x) + '/' + gl_license + branch
+            c_response = requests.get(c_link, headers=headers)
+            c_data = c_response.text
+            c_answer = json.loads(c_data)
+            if "message" in c_answer:
+                # print("License file: ", c_answer['message'])
+                lic = c_answer['message']
+            if "file_name" in c_answer:
+                # print("License file: ", c_answer['file_name'])
+                lic = c_answer['file_name']
+            # print('branch', project.attributes['default_branch'])
+        else:
+            lic = 'No created repository'
+        sep = 'T'
+        full_act = project.attributes['last_activity_at']
+        last_act = full_act.split(sep, 1)[0]
+        # print("ostatnia aktywnosc: ", last_act)
+        tmp = {
+            'project_id': project.attributes['id'],
+            'project_name': project.attributes['name_with_namespace'],
+            'project_group': project.attributes['namespace']['name'],
+            'project_status': project.attributes['visibility'],
+            'project_license': lic,
+            'project_last_activity': last_act
+        }
+        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*"-")
+except:
+    print('Cant connect to GitLab server, check credential')
+    exit()
+
+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)
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000000000000000000000000000000000000..2277aec4c490117eeb958eb1841708fd24b001d0
GIT binary patch
literal 322
zcmezWFPR~gp@^Y`A(J7EA(O$D!Ir^@!GOVt!H7YR!HhwV!H~g}ftP`cA(<hAA(5eo
zp_n0+p@cz~A&()Sp@<=uA(0`6A(NpBYzjm-NRJ+a0a#ZiLkdG4Ln7E*V+K72W3Xrz
zLoq`>Lmq<>Lk2?$LoP!OSl$pU57Sw|P{~ljkO5Yw%aG2H$xy<O!;r|31Xc?&3*<V8
zEkz8e422A(45<vo3?&T3U=<+u8!=dbeE@Plx*A=E5{7(+e1;r`B!*Om9EK9GE&~QV
c21~e}QidXi9I*eA7>vR4AU7I8!v$hD0NV61CjbBd

literal 0
HcmV?d00001

-- 
GitLab