Guest User

Untitled

a guest
Feb 28th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. >./jira -h
  2.  
  3. ----------jira options----------
  4. Required:
  5. -d [data/issueID]
  6. -u [username] -> [username] is your JIRA username
  7. -p [password] -> [password] is your JIRA password
  8.  
  9. Optional:
  10. -q -> quiet, i.e. no output to console
  11. -h -> help menu
  12.  
  13. >./jira -u jsimmons
  14.  
  15. ----------jira options----------
  16. Required:
  17. -d [data/issueID]
  18. -u [username] -> [username] is your JIRA username
  19. -p [password] -> [password] is your JIRA password
  20.  
  21. Optional:
  22. -q -> quiet, i.e. no output to console
  23. -h -> help menu
  24.  
  25. #!/bin/bash
  26.  
  27. #using getopts to parse options
  28. while getopts ":hqd:u:p:" opt; do
  29. case $opt in
  30. h)
  31. help="true"
  32. ;;
  33. q)
  34. quiet="true"
  35. ;;
  36. d)
  37. data=$OPTARG
  38. ;;
  39. u)
  40. username=$OPTARG
  41. ;;
  42. p)
  43. password=$OPTARG
  44. ;;
  45. ?)
  46. echo "Invalid option: -$OPTARG" >&2
  47. ;;
  48. :)
  49. echo "Option -$OPTARG requires an argument." >&2
  50. ;;
  51. esac
  52. done
  53.  
  54. #check if required options have been set
  55. if [[ -n $data && -n $username && -n $password ]]; then
  56.  
  57. wget -q
  58. --http-user=$username
  59. --http-passwd=$password
  60. --header="Content-Type: application/json" [URI]
  61.  
  62.  
  63. #placing issue info into variable
  64.  
  65. response=$(< $data)
  66.  
  67.  
  68. #using heredoc to run python script
  69. #python script uses regular expressions to find the value of the field
  70. #customfield_10701 ie the branch version
  71.  
  72. output=$(python - <<EOF
  73. import re
  74.  
  75. matchObj = re.search(
  76. r'(?<=customfield_10701":").*(?=","customfield_10702)',
  77. '$response',
  78. re.I
  79. )
  80. if(matchObj):
  81. print(matchObj.group())
  82.  
  83. EOF
  84. )
  85.  
  86.  
  87. #writes branch version in .txt file
  88.  
  89. echo $output>branchversion.txt
  90.  
  91.  
  92. #prints the branch version if the quiet option hasn't been set
  93. if [ -z $quiet ]; then
  94. echo "-------------------------------------------"
  95. echo ""
  96. echo "The branch version for issue $data is:"
  97. cat branchversion.txt
  98. echo ""
  99. fi
  100.  
  101. #removes file that wget creates containing all data members for the issue
  102.  
  103. rm $data
  104. elif [ -n $help ]; then
  105. #if help option has been set
  106. echo ""
  107. echo "----------jira options----------"
  108. echo "Required:"
  109. echo "-d [data/issueID]"
  110. echo "-u [username] -> [username] is your JIRA username"
  111. echo "-p [password] -> [password] is your JIRA password"
  112. echo ""
  113. echo "Optional:"
  114. echo "-q -> quiet, i.e. no output to console"
  115. echo "-h -> help menu"
  116. echo ""
  117. #http GET data members for issue
  118. else
  119. #if not all required options or help have been set
  120. echo "Error: Missing argument(s)"
  121. echo "Usage: ./jira [option(s)] -d [data] -u [username] -p [password]"
  122. echo ""
  123. echo "Try: ./jira -h for more options"
  124. fi
  125.  
  126. if [ ... ]; then #posix compliant condition tests
  127.  
  128. if [[ ... ]]; then #extended condition tests
  129.  
  130. > if [ -n $unsetVar ];then echo yes ; fi
  131. yes
  132. >
  133.  
  134. > if [ -n "$unsetVar" ];then echo yes ; fi
  135. >
  136.  
  137. > if [[ -n $unsetVar ]];then echo yes ; fi
  138. >
  139.  
  140. #!/bin/bash
  141.  
  142. URI='...whatever...'
  143.  
  144. usage() {
  145. # print optional error message and help message and exit.
  146.  
  147. [ -z "$*" ] || printf "%snn" "$*"
  148. # or, if you don't want help printed with every error message:
  149. # [ -z "$*" ] || printf "%snnUse -h option for help.n" "$*" && exit 1
  150.  
  151. cat >&2 <<__EOF__
  152. ----------jira options----------
  153. Required:
  154. -d [data/issueID]
  155. -u [username] -> [username] is your JIRA username
  156. -p [password] -> [password] is your JIRA password
  157.  
  158. Optional:
  159. -q -> quiet, i.e. no output to console
  160. -h -> help menu
  161. __EOF__
  162.  
  163. exit 1
  164. }
  165.  
  166. quiet=''
  167.  
  168. #using getopts to parse options
  169. while getopts ":hqd:u:p:" opt; do
  170. case $opt in
  171. h) usage ;;
  172. q) quiet='true' ;;
  173. d) data="$OPTARG" ;;
  174. u) username="$OPTARG" ;;
  175. p) password="$OPTARG" ;;
  176. ?) usage "Invalid option: -$OPTARG" ;;
  177. :) usage "Option -$OPTARG requires an argument." ;;
  178. esac
  179. done
  180.  
  181. # check for required options
  182.  
  183. [ -z "$data" ] && usage '-d is required'
  184. [ -z "$username" ] && usage '-u is required'
  185. [ -z "$password" ] && usage '-p is required'
  186.  
  187. # your embedded python overkill could easily be done with `sed`
  188. # or `awk`. or maybe `jq` as the wget output is json.
  189. # but without a sample of the input I can't provide a guaranteed
  190. # working example. The awk script below is a first attempt at
  191. # extracting the value of `customfield_10701`
  192.  
  193. wget -q --http-user="$username"
  194. --http-passwd="$password"
  195. --header='Content-Type: application/json'
  196. -O -
  197. "$URI" |
  198. awk -F': ' '/"customfield_10701"/ {
  199. gsub(/[",]/,"",$2);
  200. print $2
  201. }' > branchversion.txt
  202.  
  203. # alternatively, if you have `jq` installed, try piping wget's
  204. # output through something like `jq -r .customfield_10701` instead of awk.
  205.  
  206. #prints the branch version if the quiet option hasn't been set
  207. if [ -z "$quiet" ]; then
  208. echo "-------------------------------------------"
  209. echo ""
  210. echo "The branch version for issue $data is:"
  211. cat branchversion.txt
  212. echo ""
  213. fi
Add Comment
Please, Sign In to add comment