github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/contrib/nightly/github.sh (about)

     1  # Ported over beautiful code from https://raw.githubusercontent.com/neovim/bot-ci/master/ci/common/github-api.sh.
     2  
     3  # Exit if there's an error message.
     4  # ${1}: Additional information about API call.
     5  _check_gh_error() {
     6    local response="${1}"
     7    local error_message="$(echo "${response}" | jq -r '.message?')"
     8    if [[ -n "${error_message}" && "${error_message}" != 'null' ]]; then
     9      >&2 echo "Error ${2}: ${error_message}."
    10      return 1
    11    else
    12      echo "${response}"
    13    fi
    14  }
    15  
    16  # Send a request to the Github API.
    17  # ${1}: API endpoint.
    18  # ${2}: HTTP verb (default: GET).
    19  send_gh_api_request() {
    20    local endpoint="${1}"
    21    local verb="${2:-GET}"
    22  
    23    local response="$(curl -v -H "Accept: application/vnd.github.v3+json" \
    24      -H "User-Agent: travis" \
    25      -u "${GH_TOKEN}:x-oauth-basic" \
    26      -X ${verb} \
    27      https://api.github.com/${endpoint} \
    28      2>/dev/null)"
    29    _check_gh_error "${response}" "calling ${endpoint} (${verb})"
    30  }
    31  
    32  # Send a data request to the Github API.
    33  # ${1}: API endpoint.
    34  # ${2}: HTTP verb.
    35  # ${3}: JSON data.
    36  send_gh_api_data_request() {
    37    local endpoint="${1}"
    38    local verb="${2}"
    39    local data="${3}"
    40  
    41    local response="$(curl -H "Accept: application/vnd.github.v3+json" \
    42      -H "User-Agent: travis" \
    43      -u "${GH_TOKEN}:x-oauth-basic" \
    44      -X ${verb} \
    45      -d "${data}" \
    46      https://api.github.com/${endpoint} \
    47      2>/dev/null)"
    48    _check_gh_error "${response}" "calling ${endpoint} (${verb})"
    49  }
    50  
    51  # Upload an asset to a Github release.
    52  # ${1}: Local path to file to upload.
    53  # ${2}: Desired file name on Github.
    54  # ${3}: Repository to upload the asset to.
    55  # ${4}: Release ID to upload the asset to.
    56  upload_release_asset() {
    57    local file="${1}"
    58    local file_name="${2}"
    59    local repository="${3}"
    60    local release_id="${4}"
    61    local mime_type="$(file --mime-type -b "${file}")"
    62  
    63    local response="$(curl -H "Accept: application/vnd.github.v3+json" \
    64      -H "User-Agent: travis" \
    65      -H "Content-Type: ${mime_type}" \
    66      -u "${GH_TOKEN}:x-oauth-basic" \
    67      -T "${file}"\
    68      https://uploads.github.com/repos/${repository}/releases/${release_id}/assets?name=${file_name} \
    69      2>/dev/null)"
    70    _check_gh_error "${response}" 'uploading release assets'
    71  }