-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.py
119 lines (105 loc) · 3.71 KB
/
export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import argparse
import csv
import datetime
import json
import time
from datetime import datetime as datetime_format
import requests
def get_graphql_response(url, token, query):
data = requests.post(url, json=query, headers={"Authorization": "Bearer " + token})
return data.content.decode()
def get_issue_spend_times(gitlab_url, bearer_token, project_name):
ql_query = """
query getProject($path: ID!, $afterCursor: String, $pageSize: Int) {
project(fullPath: $path) {
issues(first: $pageSize, after: $afterCursor) {
edges {
node {
iid
title
timelogs {
edges {
node {
spentAt
user { name }
summary
timeSpent
}
}
totalSpentTime
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
"""
issues_found = []
after = ""
while True:
result2 = get_graphql_response(gitlab_url + "/api/graphql", bearer_token, {
"operationName": "getProject",
"query": ql_query,
"variables": {
"pageSize": 100,
"path": project_name,
"afterCursor": after
}
})
result = json.loads(result2)
issue_times = result["data"]["project"]["issues"]
for issueNode in issue_times["edges"]:
node = issueNode["node"]
for time_node in node["timelogs"]["edges"]:
new_issue = {
"project": project_name,
"iid": node["iid"],
"title": node["title"],
"date": time_node["node"]["spentAt"],
"user": time_node["node"]["user"]["name"],
"summary": time_node["node"]["summary"],
"spend": time_node["node"]["timeSpent"],
}
issues_found.append(new_issue)
if issue_times["pageInfo"]["hasNextPage"]:
after = issue_times["pageInfo"]["endCursor"]
else:
break
return issues_found
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--gitlab-instance', dest='gitlab', type=str, help='url of gitlab (https://yourgitlab.com)')
parser.add_argument('--project-names', dest='projects', type=str, help='project path (group/project)')
parser.add_argument('--access-token', dest='access_token', type=str,
help='user access token (xxxxx-XxXxXxXxXxXxXxXxXxXx)')
args = parser.parse_args()
projects = args.projects.split(",")
issues = []
for project in projects:
issues += get_issue_spend_times(args.gitlab, args.access_token, project)
filename = 'timesheet_' + time.strftime("%Y-%m-%d") + '.csv'
utcdiff = datetime_format.now() - datetime_format.utcnow()
# newline='' added because windows added 2 newlines for each newline?
with open(filename, 'w', newline='', encoding="utf8") as file:
w = csv.writer(file)
w.writerow(["Spend At", "Spend At Clock", "Project", "Issue Id", "Title", "Time Spend", "User", "Summary"])
f = "%Y-%m-%dT%H:%M:%SZ"
for issue in issues:
out = datetime_format.strptime(issue["date"], f) + utcdiff
w.writerow(
[
out.strftime("%d.%m.%Y"),
out.strftime("%H:%M:%S"),
issue["project"],
"#" + issue["iid"],
issue["title"],
str(datetime.timedelta(seconds=issue["spend"])),
issue["user"],
issue["summary"]
])
if __name__ == "__main__":
main()