0

I am trying to pass the GitHub branch creating API URL as a string into a function to get the status code. But as I tried in many ways, it is not working as I think,

the original URL is :

curl -s -X POST -u [user]:[token] -d '{"ref": "refs/heads/feature/bar", "sha": "'$SHA'"}'  https://api.github.com/repos/$user/$repo/git/refs

what I am trying to do is, taking some part of this URL and passing into a function as a string as follows:

new_branch_creating_url="-X POST -u $username:$password -d '{"'ref'": "'refs/heads/'$new_branch_to_be_created''", "'sha'": ""$old_sha_value""}'  https://api.github.com/repos/$username/$repository_name/git/refs"

My intention is to get the status code of that URL... and for that my function is

#get status code
get_status_code(){
  test_url=$1
  status_code=$(curl -s -I $test_url | awk '/HTTP/{print $2}')
  #echo "status code :$status_code"
  if [ $status_code == 404 ]
  then 
    echo "wrong credentials passed..."
    exit 1
  else
    return $status_code
  fi  
}

and while debugging the code, I am getting like

++ curl -s -I -X POST -u myusername:tokenid -d ''\''{ref:' refs/heads/branch2, sha: '1b2hudksjahhisabkhsd6asdihds8dsajbsualhcn}'\''' https://api.github.com/repos/myusername/myrepo/git/refs
++ awk '/HTTP/{print $2}'

and also my doubt is why sometimes I am received a wrong status code from the function above which I used to get a status code?

while debugging my code:

status_code=
 + '[' == 404 ']'
git_branches.sh: line 154: [: ==: unary operator expected
 + return
 + new_branch_status_code=2
 + echo 'new branch status code ... 2'
new branch status code ... 2
 + '[' 2 == 200 ']'

actually, it is nothing there on status_code from the function, but I received status code =2,,,

not only this time, but I also received 153 instead of 409... why it is like that?

I know this is not a relevant to ask but I have no choice and also it would be very helpful if someone helps me in the early stage of my learning in the shell scripting... thank you...

Ganesh
  • 21
  • 2
  • 4
  • 2
    Storing lists of complex arguments in a text variable (or passing them as a single parameter) doesn't work; see ["Why does shell ignore quoting characters in arguments passed to it through variables?"](https://stackoverflow.com/questions/12136948) and [BashFAQ #50: "I'm trying to put a command in a variable, but the complex cases always fail!"](http://mywiki.wooledge.org/BashFAQ/050). Pass them as separate args to the function, then use `"$@"` to pass them on to `curl` (see [this](https://stackoverflow.com/questions/4824590) and [this](https://stackoverflow.com/questions/3811345)). – Gordon Davisson Jan 16 '22 at 11:43
  • Thank you, @Gordon Davisson I used the following by using one of the articles above ... `merge_branch_url="-u $username:$password -X POST https://api.github.com/repos/$user_name/$repository_name/merges " data_part='{"base":"'$source_branch'","head":"'$dest_branch'"}' complete_url=""${merge_branch_url}" -d "${data_part}"" ` and it worked... is this preferable and any other better approaches? thanks again – Ganesh Jan 18 '22 at 04:45

1 Answers1

2

Instead of curl -s -I, use :

curl -I -s -o /dev/null -w "%{http_code}"

which will give directly http_code.

You don't need awk

Philippe
  • 20,025
  • 2
  • 23
  • 32
  • Thank you @Philippe, but while doing this, it will write to a file in the specific path that mentioned, so, there may be chances of utilizing memory for this. correct? – Ganesh Jan 17 '22 at 11:57
  • `-o /dev/null` makes `curl` to write nothing execpt `http_code` – Philippe Jan 17 '22 at 12:01
  • Thanks for clarifying, but still not able to understand how to pass my URL into function as a string argument... I referred to some articles mentioned above but was still confused – Ganesh Jan 17 '22 at 13:52