This repository has been archived by the owner on May 7, 2024. It is now read-only.
forked from honboey/clone-assembla-repos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_assembla_tickets.py
99 lines (76 loc) · 3.36 KB
/
get_assembla_tickets.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
import subprocess
import json
import os
from dotenv import load_dotenv
from clone_assembla_repos import make_json_file_of_users_spaces, make_list_of_space_ids
load_dotenv()
api_key = os.getenv("ASSEMBLA_KEY")
api_secret = os.getenv("ASSEMBLA_SECRET")
def main():
make_json_file_of_users_spaces(api_key, api_secret)
list_of_space_ids = make_list_of_space_ids("data/users_spaces.json")
get_spaces_tickets(list_of_space_ids)
get_ticket_comments("data/users_tickets.json")
def get_spaces_tickets(lst):
"""
Given a list of space ids, call the api and create a JSON of the tickets
"""
# Create empty JSON file
with open("data/users_tickets.json", "w") as tickets:
json.dump([], tickets)
for space_id in lst:
call_api_for_spaces_tickets = f"curl -H 'X-Api-Key: {api_key}' -H 'X-Api-Secret: {api_secret}' https://api.assembla.com/v1/spaces/{space_id}/tickets.json"
output_raw = subprocess.run(
call_api_for_spaces_tickets,
shell=True,
text=True,
capture_output=True,
).stdout
# Convert raw output to JSON if it is not an empty string
if output_raw != "":
output_json = json.loads(output_raw)
# Open and then add to the JSON file
with open("data/users_tickets.json", "r") as tickets:
existing_data = json.load(tickets)
existing_data.append(output_json)
with open("data/users_tickets.json", "w") as tickets:
json.dump(existing_data, tickets, indent=2)
with open("data/users_tickets.json", "r") as tickets:
tickets_content = json.load(tickets)
return tickets_content
def get_ticket_comments(str):
"""
Given a JSON file of tickets, create another JSON of each tickets corresponding comments.
"""
# Open the JSON file of tickets
with open(str, "r") as tickets:
tickets_content = json.load(tickets)
# Create empty JSON file
with open("data/users_ticket_comments.json", "w") as ticket_comments:
json.dump([], ticket_comments)
for space in tickets_content:
for ticket in space:
try:
call_api_for_ticket_comments = f"curl -H 'X-Api-Key: {api_key}' -H 'X-Api-Secret: {api_secret}' https://api.assembla.com/v1/spaces/{ticket['space_id']}/tickets/{ticket['number']}/ticket_comments.json"
output_raw = subprocess.run(
call_api_for_ticket_comments,
shell=True,
text=True,
capture_output=True,
).stdout
except TypeError:
pass
# Convert raw output to JSON if it is not an empty string
if output_raw != "" and output_raw != "":
output_json = json.loads(output_raw)
# Open and then add to the JSON file
with open("data/users_ticket_comments.json", "r") as ticket_comments:
existing_data = json.load(ticket_comments)
existing_data.append(output_json)
with open("data/users_ticket_comments.json", "w") as ticket_comments:
json.dump(existing_data, ticket_comments, indent=2)
with open("data/users_ticket_comments.json", "r") as ticket_comments:
ticket_comments_content = json.load(ticket_comments)
return ticket_comments_content
if __name__ == "__main__":
main()