Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
gitlab-getlists
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
WP9-T2-SWD-Tools
gitlab-getlists
Commits
739ea5d8
Commit
739ea5d8
authored
2 years ago
by
kazix
Browse files
Options
Downloads
Patches
Plain Diff
create csv file
parent
61778473
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.py
+62
-5
62 additions, 5 deletions
main.py
with
62 additions
and
5 deletions
main.py
+
62
−
5
View file @
739ea5d8
...
@@ -6,6 +6,10 @@ from json2html import *
...
@@ -6,6 +6,10 @@ from json2html import *
import
argparse
import
argparse
import
os
import
os
from
dotenv
import
load_dotenv
from
dotenv
import
load_dotenv
from
pandas.io.json
import
json_normalize
import
pandas
as
pd
from
flatten_json
import
flatten
import
csv
parser
=
argparse
.
ArgumentParser
(
description
=
"
Get Project, License and Owners from gitlab
"
)
parser
=
argparse
.
ArgumentParser
(
description
=
"
Get Project, License and Owners from gitlab
"
)
...
@@ -28,7 +32,7 @@ try:
...
@@ -28,7 +32,7 @@ try:
projects
=
gl
.
projects
.
list
(
all
=
True
)
projects
=
gl
.
projects
.
list
(
all
=
True
)
elif
args
.
developer
:
elif
args
.
developer
:
token
=
os
.
getenv
(
'
DEVELOPER_TOKEN
'
)
token
=
os
.
getenv
(
'
DEVELOPER_TOKEN
'
)
print
(
'
Production
Mode, connect to server
'
)
print
(
'
Developer
Mode, connect to server
'
)
headers
=
{
'
PRIVATE-TOKEN
'
:
token
}
headers
=
{
'
PRIVATE-TOKEN
'
:
token
}
gl
=
gitlab
.
Gitlab
(
url
=
'
https://k8s-gitlab.software.geant.org
'
,
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_user
=
'
https://k8s-gitlab.software.geant.org/api/v4/users
'
...
@@ -36,19 +40,23 @@ try:
...
@@ -36,19 +40,23 @@ try:
gl_license
=
'
repository/files/LICENSE?ref=
'
gl_license
=
'
repository/files/LICENSE?ref=
'
gl_branch
=
'
repository/branches
'
gl_branch
=
'
repository/branches
'
projects
=
gl
.
projects
.
list
(
all
=
True
)
projects
=
gl
.
projects
.
list
(
all
=
True
)
# projects = gl.projects.list(all=True)
else
:
else
:
print
(
"
--production or --developer, please, now exit
"
)
print
(
"
--production or --developer, please, now exit
"
)
exit
()
exit
()
j_project
=
{}
j_project
=
{}
j_project
[
'
projects
'
]
=
{}
j_project
[
'
projects
'
]
=
{}
c_project
=
[]
for
project
in
projects
:
for
project
in
projects
:
#
print('Project ID: ', project.attributes['id'])
print
(
'
Project ID:
'
,
project
.
attributes
[
'
id
'
])
print
(
'
Project Name:
'
,
project
.
attributes
[
'
name_with_namespace
'
])
print
(
'
Project Name:
'
,
project
.
attributes
[
'
name_with_namespace
'
])
# # print('branch', project.attributes['default_branch'])
# # print('branch', project.attributes['default_branch'])
print
(
'
Status:
'
,
project
.
attributes
[
'
visibility
'
])
print
(
'
Status:
'
,
project
.
attributes
[
'
visibility
'
])
# print('aktywny', project.attributes['last_activity_at'])
print
(
'
aktywny
'
,
project
.
attributes
[
'
last_activity_at
'
])
# print('grupa', project.attributes['namespace']['name'])
print
(
'
grupa
'
,
project
.
attributes
[
'
namespace
'
][
'
name
'
])
# pages = project.wikis.list()
# print(pages)
x
=
project
.
attributes
[
'
id
'
]
x
=
project
.
attributes
[
'
id
'
]
check_b
=
gl_proj
+
'
/
'
+
str
(
x
)
check_b
=
gl_proj
+
'
/
'
+
str
(
x
)
check_b_response
=
requests
.
get
(
check_b
,
headers
=
headers
)
check_b_response
=
requests
.
get
(
check_b
,
headers
=
headers
)
...
@@ -91,13 +99,16 @@ try:
...
@@ -91,13 +99,16 @@ try:
response
=
requests
.
get
(
link
,
headers
=
headers
)
response
=
requests
.
get
(
link
,
headers
=
headers
)
data
=
response
.
text
data
=
response
.
text
owners
=
json
.
loads
(
data
)
owners
=
json
.
loads
(
data
)
c_user_tmp
=
[]
for
owner
in
owners
:
for
owner
in
owners
:
# c_user_tmp = []
if
owner
[
'
access_level
'
]
==
50
:
if
owner
[
'
access_level
'
]
==
50
:
# print('User ID: ', owner['id'])
# print('User ID: ', owner['id'])
# print('User Name: ', owner['name'])
# print('User Name: ', owner['name'])
# print('Access level: ', owner['access_level'])
# print('Access level: ', owner['access_level'])
y
=
owner
[
'
id
'
]
y
=
owner
[
'
id
'
]
j_project
[
'
projects
'
][
x
][
'
owners
'
][
y
]
=
{}
j_project
[
'
projects
'
][
x
][
'
owners
'
][
y
]
=
{}
# c_user_tmp = []
user
=
gl
.
users
.
get
(
y
)
user
=
gl
.
users
.
get
(
y
)
emails
=
user
.
emails
.
list
()
emails
=
user
.
emails
.
list
()
if
user
.
email
==
user
.
commit_email
:
if
user
.
email
==
user
.
commit_email
:
...
@@ -106,6 +117,7 @@ try:
...
@@ -106,6 +117,7 @@ try:
'
owner_name
'
:
owner
[
'
name
'
],
'
owner_name
'
:
owner
[
'
name
'
],
'
owner_mail
'
:
user
.
email
,
'
owner_mail
'
:
user
.
email
,
}
}
# c_user_tmp.append(owner['name'] + '<' + user.email + '>' + ';')
else
:
else
:
user_tmp
=
{
user_tmp
=
{
'
owner_id
'
:
owner
[
'
id
'
],
'
owner_id
'
:
owner
[
'
id
'
],
...
@@ -113,10 +125,27 @@ try:
...
@@ -113,10 +125,27 @@ try:
'
owner_mail
'
:
user
.
email
,
'
owner_mail
'
:
user
.
email
,
'
owner_mail_commit
'
:
user
.
commit_email
'
owner_mail_commit
'
:
user
.
commit_email
}
}
# c_user_tmp.append(owner['name'] + '<' + user.email + '>' + ';')
# c_user_tmp.append(j_project['projects'][x]['owners'][y]['owner_name'] + '<' +
# j_project['projects'][x]['owners'][y]['owner_mail'] + '>' + ';')
j_project
[
'
projects
'
][
x
][
'
owners
'
][
y
]
=
user_tmp
j_project
[
'
projects
'
][
x
][
'
owners
'
][
y
]
=
user_tmp
print
(
j_project
[
'
projects
'
][
x
][
'
owners
'
][
y
][
'
owner_name
'
]
+
'
<
'
+
j_project
[
'
projects
'
][
x
][
'
owners
'
][
y
][
'
owner_mail
'
]
+
'
>
'
+
'
;
'
)
c_user_tmp
.
append
(
j_project
[
'
projects
'
][
x
][
'
owners
'
][
y
][
'
owner_name
'
]
+
'
<
'
+
j_project
[
'
projects
'
][
x
][
'
owners
'
][
y
][
'
owner_mail
'
]
+
'
>
'
+
'
;
'
)
print
(
c_user_tmp
)
# c_owners = j_project['projects'][x]['owners']
# c_user = []
# for c_owner in c_owners:
# c_user.append(c_owner['owner_name'] + ':' + c_owner['owner_mail'])
c_project
.
append
([
project
.
attributes
[
'
id
'
],
project
.
attributes
[
'
name_with_namespace
'
],
project
.
attributes
[
'
visibility
'
],
lic
,
last_act
,
c_user_tmp
])
print
(
8
*
"
-
"
)
print
(
8
*
"
-
"
)
except
:
except
:
print
(
'
Cant connect to GitLab server, check credential
'
)
print
(
'
Something wrong or
Cant connect to GitLab server, check credential
'
)
exit
()
exit
()
new_jproject
=
json
.
dumps
(
j_project
,
indent
=
4
,
ensure_ascii
=
False
)
new_jproject
=
json
.
dumps
(
j_project
,
indent
=
4
,
ensure_ascii
=
False
)
...
@@ -131,4 +160,32 @@ new_jproject_html = json2html.convert(json=new_jproject)
...
@@ -131,4 +160,32 @@ new_jproject_html = json2html.convert(json=new_jproject)
with
open
(
"
gitlab-projects.html
"
,
"
w
"
,
encoding
=
'
utf8
'
)
as
outfile
:
with
open
(
"
gitlab-projects.html
"
,
"
w
"
,
encoding
=
'
utf8
'
)
as
outfile
:
outfile
.
write
(
new_jproject_html
)
outfile
.
write
(
new_jproject_html
)
print
(
c_project
)
with
open
(
'
gitlab-projects.csv
'
,
'
w
'
,
encoding
=
'
UTF8
'
,
newline
=
''
)
as
f
:
writer
=
csv
.
writer
(
f
)
# writer = csv.DictWriter(j_project)
# write the header
# writer.writerow(header)
# write multiple rows
writer
.
writerows
(
c_project
)
# for project in projects:
# new_project_csv = []
# new_project_csv.append(new_jproject)
# with open('gitlab-projects.json', encoding='utf8') as f:
# j_data = json.load(f)
# new_json_file_csv = pd.read_json(j_data, orient='index')
# # new_json_file_csv = pd.json_normalize(j_data, max_level=3)
# # flatten(j_project)
# df = pd.DataFrame(new_json_file_csv)
# df.to_csv("gitlab-projects.csv", encoding='utf-8', index=False)
# # with open("gitlab-projects.csv", "w", encoding='utf8') as outfile:
# # outfile.write(new_jproject_html)
# print(new_csv)
print
(
"
This is the end...
"
)
print
(
"
This is the end...
"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment