-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestrail_lifecycle.py
More file actions
executable file
·175 lines (138 loc) · 4.17 KB
/
testrail_lifecycle.py
File metadata and controls
executable file
·175 lines (138 loc) · 4.17 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python3
# coding: utf-8
"""
Testrail lifecycle policy
"""
from argparse import (
ArgumentParser,
RawDescriptionHelpFormatter
)
import time
from testrail_utils import (
close_plan,
get_plans_created_before,
delete_plan
)
# Globals
EXCLUDE_PATTERNS = ['promoted', 'rc', 'pw', 'postmerge', 'post-merge']
def arg_parse():
"""
Parse script arguments
"""
parser = ArgumentParser(
formatter_class=RawDescriptionHelpFormatter,
usage='%(prog)s [options]'
)
parser.add_argument(
'-x', '--exclude',
nargs='+',
help='list of exclude patterns',
required=False,
default=EXCLUDE_PATTERNS
)
parser.add_argument(
'-r', '--retention_time',
help="retention time",
type=int,
required=False,
default=2592000
)
parser.add_argument(
'-a', '--action',
help="available actions: 'garbage' or 'close'",
required=False,
)
parser.add_argument(
'-u', '--duration',
help='Garbage loop duration (seconds)',
type=int,
required=False,
default=300)
args = parser.parse_args()
return parser, args
def trash(duration: int, retention: int, exclude_patterns: list) -> tuple:
"""
Delete plans according to creation date and name
:return: list of successfully deleted plans
"""
start = time.time()
timestamp = start - retention
plans = get_plans_created_before(timestamp)
delete_plans = []
ignore_plans = []
c_duration = 0
offset = 0
while plans and (c_duration < duration):
c_duration = int(time.time()) - start
for plan in plans:
plan_id = plan.get('id')
url = plan.get('url')
# Ignored plans according to exclude patterns
if any(c in plan.get('name') for c in exclude_patterns):
ignore_plans.append(plan.get('name'))
print("keep {0}".format(plan.get('name')))
else:
# Delete plans
print("deleting {0}".format(url))
ret = delete_plan(plan_id)
if ret.status_code == 200:
print("deleted {0}".format(url))
delete_plans.append(plan.get('name'))
# Set the plans offset
offset += len(ignore_plans)
if len(plans) == len(ignore_plans):
break
plans = get_plans_created_before(timestamp, offset)
return delete_plans, ignore_plans
def close(retention: int, duration: int) -> list:
"""
Close open plans
:param retention: retention time before closing
:type retention: int
:return: list of deleted plans
:rtype: list
"""
start = time.time()
timestamp = time.time() - retention
plans = get_plans_created_before(timestamp)
open_plans = [plan for plan in plans if not plan.get('is_completed')]
closed = []
c_duration = 0
offset = 0
while plans and (c_duration < duration):
c_duration = int(time.time()) - start
for plan in open_plans:
print("Closing: {0} ...".format(plan.get("url")))
ret = close_plan(plan.get("id"))
if ret.status_code == 204:
closed.append(plan.get("url"))
# Set the plans offset
offset += len(plans)
plans = get_plans_created_before(timestamp, offset)
open_plans = [plan for plan in plans if not plan.get('is_completed')]
return plans
def main() -> None:
"""
Entry point
"""
# Parse arguments
parser, args = arg_parse()
print(args)
action = args.action
excl = args.exclude
retention = args.retention_time
duration = args.duration
# Launch action
if action == 'garbage':
delete, kept = trash(
duration=duration, retention=retention, exclude_patterns=excl
)
print("deleted plans:\n{0}".format(delete))
print("kept plans:\n{0}".format(kept))
elif action == 'close':
closed = close(retention=retention, duration=duration)
print("closed plans:\n{0}".format(closed))
else:
parser.print_help()
if __name__ == '__main__':
main()