-
Notifications
You must be signed in to change notification settings - Fork 0
/
tr_api
executable file
·158 lines (135 loc) · 4.07 KB
/
tr_api
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
#!/bin/bash
source "${TESTRAIL_API_SOURCE}/cli/output.sh"
_request() {
local url="${TESTRAIL_API_URL}/index.php?/api/v2/${1:?Endpoint is required}"
local curl_argv=("${@:2}")
local credential="$TESTRAIL_API_USER:$TESTRAIL_API_KEY"
local response
local api_error
response=$(curl -s \
-H 'Content-Type: application/json' \
-u "$credential" \
"$url" \
"${curl_argv[@]}") \
|| ERROR "Curl return code is $? for command:\n" \
|| return $?
api_error=$(jq -r 'if type =="object" and has("error") then .error else empty end' <<< "$response") \
&& test ! "$api_error" \
|| ERROR 'TestRail API returns error:\n' "$api_error" \
|| return $?
jq -c '.' <<< "$response" \
|| ERROR "Response is not a json:" "$response" \
|| return $?
}
get_case() {
local id=${1:?Case ID is required}
_request "get_case/${id}" \
|| ERROR "Couldn't get case with ID $id"
return $?
}
update_case() {
local id=${1:?Case ID is required}
local case_json=${2:?Test case is required}
_request "update_case/${id}" -X POST -d "$case_json" \
|| ERROR "Couldn't update case with ID $id:\n" "$case_json" \
|| return $?
}
get_test() {
local test_id=${1:?Test ID is required}
_request "get_test/${test_id}" \
|| ERROR "Couldn't get test with ID $test_id" \
|| return $?
}
get_tests() {
local run_id=${1:?Run ID is required}
_request "get_tests/${run_id}" \
|| ERROR "Couldn't get tests for run ID $run_id" \
|| return $?
}
get_cases_from_section() {
local project=${1:?Project ID is required}
local suite=${2:?Suite ID is required}
local section_id=${3:?Section ID is required}
_request "get_cases/${project}&suite_id=${suite}§ion_id=${section_id}" \
|| ERROR "Couldn't get cases for section ID $section_id" \
|| return $?
}
get_results() {
local test=${1:?Test ID is required}
_request "get_results/${test}" \
|| ERROR "Couldn't get result for test $test" \
|| return $?
}
get_results_for_run() {
local run_id=${1:?Run ID is required}
_request "get_results_for_run/${run_id}" \
|| ERROR "Couldn't get result for run $run_id" \
|| return $?
}
get_results_for_case() {
local run=${1:?Run ID is required}
local case=${2:?Case ID is required}
_request "get_results_for_case/${run}/${case}" \
|| ERROR "Couldn't get results for run $run and case $case" \
|| return $?
}
get_suite() {
local suite_id=${1:?Suite ID is required}
_request "get_suite/${suite_id}" \
|| ERROR "Couldn't get suite $suite_id" \
|| return $?
}
get_section() {
local section_id=${1:?Section ID is required}
_request "get_section/${section_id}" \
|| ERROR "Couldn't get section $section_id" \
|| return $?
}
get_sections() {
local project=${1:?Project ID is required}
local suite=${2:?Suite ID is required}
_request "get_sections/${project}&suite_id=${suite}" \
|| ERROR "Couldn't get sections for $project project and $suite suite" \
|| return $?
}
update_section() {
local id=${1:?Section ID is required}
local body=${2:?Section body is required}
_request "update_section/${id}" -X POST -d "$body" \
|| ERROR "Couldn't update section with ID $id" \
|| return $?
}
delete_section() {
local id=${1:?Section ID is required}
_request "delete_section/${id}" -X POST \
|| ERROR "Couldn't update section with ID $id" \
|| return $?
}
get_run() {
local run_id=${1:?Run ID is required}
_request "get_run/${run_id}" \
|| ERROR "Couldn't get run run_id" \
|| return $?
}
get_plan() {
local plan_id=${1:?Plan ID is required}
_request "get_plan/${plan_id}" \
|| ERROR "Couldn't get plan $plan_id" \
|| return $?
}
add_plan_entry() {
local plan_id=${1:?Plan ID is required}
local body=${2:?Test case is required}
_request "add_plan_entry/${plan_id}" -X POST -d "$body" \
|| ERROR "Couldn't add to plan $plan_id" \
|| return $?
}
update_plan_entry() {
local plan_id=${1:?Plan ID is required}
local entry_id=${2:?Entry ID is required}
local body=${3:?Test case is required}
_request "update_plan_entry/${plan_id}/${entry_id}" -X POST -d "$body" \
|| ERROR "Couldn't update entry to $entry_id plan $plan_id" \
|| return $?
}
eval "$*"