forked from keyvanm/sdelements-api-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
refresh_task_relevance.py
52 lines (40 loc) · 1.77 KB
/
refresh_task_relevance.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
import collections
import os
import requests
SERVER = os.environ['SDE_SERVER'] # e.g. https://cd.sdelements.com
API_TOKEN = os.environ['SDE_API_TOKEN'] # e.g. f258c47557a3f98f55d4fbd0icb9d8354c86fbb52
PROJECTS_URL = f'{SERVER}/api/v2/projects/?page_size=100'
REQUESTS_HEADER = {
'Authorization': "Token " + API_TOKEN, # Authorization header + generated API TOKEN
'Accept': "application/json"
}
print("Fetching projects from: {}".format(PROJECTS_URL))
print("Using API Token: {}...".format(API_TOKEN[:5]))
projects_url = PROJECTS_URL
while True:
response = requests.get(projects_url, headers=REQUESTS_HEADER, verify=False)
response.raise_for_status()
data = response.json()
# loop over the projects to accept tasks
for project_id, name in [(project['id'], project['name']) for project in data['results']]:
tasks_url = f'{SERVER}/api/v2/projects/{project_id}/task-updates/'
response = requests.get(tasks_url, headers=REQUESTS_HEADER, verify=False)
response.raise_for_status()
data = response.json()
if not data["results"]:
print(f'Project {name} ({project_id}): no updates')
else:
added_tasks = 0
removed_tasks = 0
for task in data["results"]:
if task["accepted"] and not task["relevant"]:
removed_tasks += 1
elif not task["accepted"] and task["relevant"]:
added_tasks += 1
print(f'Project {name} ({project_id}): there are {added_tasks} new tasks and {removed_tasks} removed tasks')
# Grab the URL of the next page of results to fetch; if this entry is
# blank we have reached the last page
if "next" in data:
projects_url = data["next"]
else:
break