3838# only if the new code is made subject to such option by the copyright
3939# holder.
4040
41+ #
42+ # Map style.
43+ # Returns the value for the supplied key
44+ #
45+ # Args: key array_of_key=val
46+ #
47+ get_value_from_key_val_array (){
48+ local _array=${2}
49+ local array=(${_array[*]} )
50+ local i=0
51+ while [ ${i} -lt ${# array[*]} ]
52+ do
53+ entry=(` key_val_as_list ${array[${i}]} ` )
54+ if [ " ${entry[0]} " = " ${1} " ] ; then
55+ echo " ${entry[1]} "
56+ return 0
57+ fi
58+ i=$(( i+ 1 ))
59+ done
60+ }
61+
62+ #
63+ # Converts a key=value string into a list
64+ #
65+ # Args: key=value
66+ #
67+ key_val_as_list (){
68+ local param_key=` echo ${1} | cut -d ' =' -f1`
69+ local param_value=` echo ${1} | cut -d ' =' -f2`
70+ echo " ${param_key} ${param_value} "
71+ }
72+
73+ #
74+ # Returns the build parameters of a given test job run
75+ # As space separated string of PARAM_NAME=PARAM_VALUE
76+ #
77+ # Args: BUILD_NUMBER
78+ #
79+ get_test_job_params (){
80+ local url=" ${HUDSON_URL} /${1} /api/xml?xpath=//parameter&wrapper=list"
81+ curl " ${url} " | sed \
82+ -e s@' <list><parameter>' @@g -e s@' </list>' @@g \
83+ -e s@' <parameter>' @@g -e s@' </parameter>' @@g \
84+ -e s@' <name>' @@g -e s@' </name>' @@g \
85+ -e s@' <value>' @' =' @g -e s@' </value>' @' ' @g -e s@' <value/>' @' ' @g
86+
87+
88+ if [ ${PIPESTATUS[0]} -ne 0 ] ; then
89+ # exit with curl status code
90+ return ${PIPESTATUS[0]}
91+ fi
92+ }
93+
94+ #
95+ # Returns true if the test job was triggered by the current PARENT_ID and given TEST_ID
96+ #
97+ # Args: params array to match the job
98+ #
99+ is_test_job_match (){
100+ local array=${2}
101+ local match_params=(${array[*]} )
102+ set +e
103+ local job_param=(` get_test_job_params ${1} ` )
104+ local error_code=${?}
105+ set -e
106+
107+ if [ ${error_code} -ne 0 ] ; then
108+ # no match
109+ echo false
110+ return 0
111+ fi
112+ # match provided params with job params
113+ local i=0
114+ while [ ${i} -lt ${# match_params[*]} ]
115+ do
116+ local match_entry=(` key_val_as_list ${match_params[${i}]} ` )
117+ job_value=` get_value_from_key_val_array ${match_entry[0]} " ${job_param[*]} " `
118+ if [ " ${job_value} " != " ${match_entry[1]} " ] ; then
119+ echo false
120+ return 0
121+ fi
122+ i=$(( i+ 1 ))
123+ done
124+ # match
125+ echo true
126+ }
127+
128+ #
129+ # Gets the last build number of the test job.
130+ # This will include the currently on-going runs.
131+ #
132+ get_test_job_last_build_number (){
133+ local url=" ${HUDSON_URL} /api/xml?xpath=//lastBuild/number/text()"
134+ curl " ${url} "
135+ local error_code=${?}
136+ if [ ${error_code} -ne 0 ] ; then
137+ exit 1
138+ fi
139+ }
140+
141+ #
142+ # Find a test job for TEST_ID
143+ #
144+ # Args: TEST_ID PREVIOUS_LAST_BUILD
145+ #
146+ find_test_job (){
147+ local previous_last_build=${1}
148+ local last_build=` get_test_job_last_build_number`
149+
150+ # nothing running and nothing new completed
151+ if [ " ${previous_last_build} " = " ${last_build} " ] ; then
152+ return 1
153+ fi
154+
155+ # look into the newly completed run
156+ local i=$(( previous_last_build+ 1 ))
157+ while [ ${i} -le ${last_build} ]
158+ do
159+ local params
160+ params[0]=" BRANCH=${branch} "
161+ params[1]=" TEST_IDS=${test_ids_triggerd} "
162+ params[2]=" PR_NUMBER"
163+ params[3]=" FORK_ORIGIN=${fork_origin} "
164+ if ` is_test_job_match ${i} " ${params[*]} " ` ; then
165+ # the triggered run is already completed
166+ echo ${i}
167+ return 0
168+ fi
169+ i=$(( i+ 1 ))
170+ done
171+
172+ # not found
173+ return 1
174+ }
175+
41176USAGE=" Usage: rq -b BRANCH -a ---> For runing all the test ids\n\
42177 or rq -b BRANCH -g TESTGROUPNAME ---> For runing a TESTGROUPNAME\n\
43178 or rq -b BRANCH -t TESTIDS ---> For runing a space separated set of TESTIDS"
@@ -88,7 +223,24 @@ if [[ -z $test_ids ]]; then
88223 echo -e $USAGE
89224 exit 1
90225fi
91-
226+ fork_origin= ` git config --get remote.origin.url `
92227test_ids_encoded=` echo ${test_ids[@]} | tr ' ' ' +' `
93- params=" BRANCH=${branch} &TEST_IDS=${test_ids_encoded} "
94- curl -X POST " ${HUDSON_URL} /buildWithParameters?${params} &delay=0sec" 2> /dev/null
228+ params=" BRANCH=${branch} &TEST_IDS=${test_ids_encoded} &FORK_ORIGIN=${fork_origin} "
229+ last_build=` get_test_job_last_build_number`
230+ curl -X POST " ${HUDSON_URL} /buildWithParameters?${params} &delay=0sec" 2> /dev/null
231+ test_ids_triggerd=` echo ${test_ids[@]} `
232+ export branch fork_origin test_ids_triggerd
233+ job_build_number=` find_test_job ${last_build} `
234+ printf " ###################################################################\n"
235+ printf " ###################################################################\n"
236+ if [[ ! -z ${job_build_number} ]]; then
237+ printf " RQ triggered successfully. Please find the RQ link below\n"
238+ printf ${HUDSON_URL} /${job_build_number}
239+ printf " \n"
240+ printf " ###################################################################\n"
241+ printf " ###################################################################\n"
242+ else
243+ printf " Issue in RQ client.Please check your git settings\n"
244+ printf " ###################################################################\n"
245+ printf " ###################################################################\n"
246+ fi
0 commit comments