-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
100 lines (87 loc) · 2.86 KB
/
tools.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
import json
import os
from dotenv import load_dotenv
load_dotenv()
DEFAULT_AREA_PATH = os.getenv("DEVOPS_DEFAULT_AREA_PATH")
DEFAULT_ITERATION_PATH = os.getenv("DEVOPS_DEFAULT_ITERATION_PATH")
DEFAULT_EMAIL = os.getenv("DEVOPS_DEFAULT_EMAIL")
DEFAULT_ORG = os.getenv("DEVOPS_ORG")
DEFAULT_PROJECT = os.getenv("DEVOPS_PROJECT")
DEFAULT_BASE_URL = os.getenv("DEVOPS_ORG") + "/" + os.getenv("DEVOPS_PROJECT")
def get_all_pbi() -> str:
os.system(
f"az devops configure --defaults organization={DEFAULT_ORG} project={DEFAULT_PROJECT}"
)
cmd = """
az boards query --wiql "
SELECT
[System.Id],
[System.Title],
[System.State],
[System.AreaPath],
[System.IterationPath],
[System.CreatedDate],
[System.Description],
[System.AssignedTo]
FROM workitems
WHERE
[System.WorkItemType] = 'Product Backlog Item'
AND [System.AssignedTo] = @Me
AND ([System.State] = 'New' OR [System.State] = 'Active')
ORDER BY [System.ChangedDate] DESC" --output json
"""
result = os.popen(cmd).read()
return result
def create_pbi(title: str, description: str) -> str:
pbi_parent_dict = os.getenv("DEVOPS_PBI_PARENT")
if pbi_parent_dict:
pbi_parent_dict = json.loads(pbi_parent_dict)
for k, v in pbi_parent_dict.items():
if k in title:
tag = k
parent = v
cmd = f"""
az boards work-item create --type "Product Backlog Item" \
--title "{title}" \
--assigned-to "{DEFAULT_EMAIL}" \
--description "{description}" \
--area "{DEFAULT_AREA_PATH}" \
--iteration "{DEFAULT_ITERATION_PATH}" \
--fields "System.Tags={tag}"
"""
result = os.popen(cmd).read()
json_result = json.loads(result)
id = json_result["id"]
set_parent(id, parent)
print(f"Created PBI: {title} {DEFAULT_BASE_URL}/_workitems/edit/{id}")
return id
def set_parent(current_id: int, parent_id: int) -> bool:
cmd = f"""
az boards work-item relation add --id {current_id} --relation-type 'Parent' --target-id {parent_id}
"""
os.system(cmd)
return True
def create_task(title: str, description: str, parent_id: int) -> str:
cmd = f"""
az boards query --wiql "SELECT [System.IterationPath]
FROM workitems
WHERE
[System.WorkItemType] = 'Task'
AND [System.AssignedTo] = @Me
ORDER BY [System.CreatedDate] DESC" --output json | jq -r '.[0].fields["System.IterationPath"]'
"""
iteration = os.popen(cmd).read().strip()
cmd = f"""
az boards work-item create --type "Task" \
--title "{title}" \
--assigned-to "{DEFAULT_EMAIL}" \
--description "{description}" \
--area "{DEFAULT_AREA_PATH}" \
--iteration "{iteration}" \
"""
result = os.popen(cmd).read()
json_result = json.loads(result)
id = json_result["id"]
set_parent(id, parent_id)
print(f"Created Task: {title} {DEFAULT_BASE_URL}/_workitems/edit/{id}")
return f"{DEFAULT_BASE_URL}/_workitems/edit/{id}"