App-git-hub

 view release on metacpan or  search on metacpan

share/lib/git-hub  view on Meta::CPAN


# Build a command to make the HTTP call to the API server, make the call, and
# check the result.
api-call() {
  format-curl-command "$@"

  if "$verbose_output"; then
    local cc="${curl_command[@]}"
    if ! "$show_token"; then
      [ -n "$api_token" ] &&
        cc="${cc/token $api_token/token ********}"
      [ -n "$GIT_HUB_PASSWORD" ] &&
        cc="${cc/$login:$GIT_HUB_PASSWORD/$login:********}"
    fi
    echo "$cc"$'\n' >&2
  fi

  if "$dry_run"; then
    nay '*** NOTE: This is a dry run only. ***'
    msg_ok=0
    return 0
  elif "$repeat_command"; then
    # Use the cache
    [ -e "$command_header_file" ] ||
      error "-R flag not valid. Command not previously run."
    rc=0
  else
    local cache_dir="$GIT_HUB_CACHE/$command_sha1"
    mkdir -p "$cache_dir"
    # Actually run the curl command!
    (
      set +e
      # set -x  # for debugging the real command
      "${curl_command[@]}"
    )
    rc=$?
    cache-response-files
  fi

  "$show_headers" && cat "$command_header_file" >&2

  if [ -s "$command_output_file" ]; then
    JSON__cache="$(cat "$command_output_file" | JSON.load)"
    "$show_output" && cat "$command_output_file" >&2
    "$show_json" && JSON.cache >&2
  fi

  check-api-call-status $rc

  true
}

# Build curl command in a global array. This is the only way to preserve
# quoted whitespace.
format-curl-command() {
  local action="$1"
  local url="$2"
  local data="$3"

  [[ "$url" =~ [a-zA-Z0-9]/(/|$) ]] &&
    error "API url '$url' looks suspiciously wrong"

  "$use_auth" && require-auth 1

  local user_agent="git-hub-$GIT_HUB_VERSION"
  # Cheap trick to make github pretty-print the JSON output.
  "$show_output" && user_agent="curl-$user_agent"

  [[ "$url" =~ ^https?: ]] || url="$GIT_HUB_API_URI$url"

  curl_command=(
    curl
      --request "$action"
    "$url"
  )
  [ -n "$data" ] || [ "$action" = "PUT" ] && curl_command+=(-d "$data")
  if [ -n "$basic_auth" ]; then
    local login="$(get-login)"
    if [ -n "$GIT_HUB_PASSWORD" ]; then
      curl_command+=(-u "$login:$GIT_HUB_PASSWORD")
    else
      curl_command+=(-u "$login")
    fi
    if [ -n "$GIT_HUB_2FA_OTP" ]; then
      curl_command+=(--header "X-GitHub-OTP: $GIT_HUB_2FA_OTP")
    fi
  elif [ -n "$api_token" ]; then
    curl_command+=(--header "Authorization: token $api_token")
  fi

  local sha1seed="${curl_command[@]}"
  [ -n "$api_token" ] && [ -n "$GIT_HUB_TEST_MAKE" ] &&
    sha1seed="${sha1seed/$api_token/0123456789abcdef0123456789abcdef01234567}"
  # echo "$sha1seed" >> sha1seed
  "$show_output" && sha1seed+=' -O'
  command_sha1="$(echo "$sha1seed" | $sha1sum | cut -d ' ' -f1)"
  command_header_file="$GIT_HUB_CACHE/$command_sha1/head"
  command_output_file="$GIT_HUB_CACHE/$command_sha1/out"
  command_error_file="$GIT_HUB_CACHE/$command_sha1/err"

  if ! "$no_cache" &&
    [[ ! "$url" =~ hooks$ ]] &&
    [ -e "$command_header_file" ]
  then
    etag="$(grep -Em1 '^ETag:' "$command_header_file" | tr -d '\r')"
    if [ -n "$etag" ]; then
      curl_command+=(--header "${etag/ETag/If-None-Match}")
    fi
  fi
  curl_command+=(
    --user-agent "$user_agent"
    --dump-header "$GIT_HUB_CACHE/head"
    --output "$GIT_HUB_CACHE/out"
    --stderr "$GIT_HUB_CACHE/err"
    --silent
    --show-error
  )
}

cache-response-files() {
  grep -E '^Status: 304' "$GIT_HUB_CACHE/head" &> /dev/null || {

share/lib/git-hub  view on Meta::CPAN


get-var-bool() {
  get-var "$1"
  local var="${1//-/_}"
  [ -z "${!var}" ] && printf -v "$var" false
  [ "${!var}" != false ] && printf -v "$var" true
  true
}

#------------------------------------------------------------------------------
# Detailed error messages:
#------------------------------------------------------------------------------
need-api-token() {
  cat <<...

Can't determine your Personal API Access Token, which is required by this
command. Usually you put your token into the ~/.git-hub/config file, like this:

  git hub config api-token <your-personal-api-access-token>

You can also specify the token using the --token= commandline option, or via
the GIT_HUB_API_TOKEN environment variable.

To list your API tokens:

  git hub tokens

To create a new api token:

  git hub token-new "my git-hub token"
  # You should probably add at least the 'repo' and 'user' scopes:
  git hub scope-add <token-id> repo user

You can also just run setup command, and it will guide you:

  git hub setup

Would you like to run it now?

...
  prompt-to-run-setup
}

config-not-setup() {
  cat <<...

Greetings!!!

You seem to be a brand new 'git hub' user, since your config is not set up.

The 'git-hub' should soon be your new best friend. Configuration is really
easy. Just run the following command and answer a few simple questions:

  git hub setup

This will guide you through the setup process. Press <ENTER> to run it now.

NOTE: You can also do the setup process by hand using the 'config', 'token'
      and 'scope' commands. See the 'git help hub' documentation for more
      information on these commands. Or just manually create a config file
      in ~/.git-hub/config that looks like this:

  [github]
          login = your-github-login-id
          api-token = c956f87abab2da54882cc1f1ade42efcc2b15dc7
          json-lib = json-perl.bash
          use-auth = 1

Would you like to start the automated 'setup' process right now? (Do it!)

...
  prompt-to-run-setup
}

prompt-to-run-setup() {
  prompt
  source-ext git-hub-setup
  command:setup || exit 1
  exit 0
}

#------------------------------------------------------------------------------
# Reusable helper functions:
#------------------------------------------------------------------------------
editor-title-body() {
  rm -f "$GIT_HUB_MSG_FILE"

  echo "$1" > "$GIT_HUB_MSG_FILE"

  $GIT_HUB_EDITOR "$GIT_HUB_MSG_FILE"
  local line
  body=''
  title="$(head -n1 "$GIT_HUB_MSG_FILE")"
  if [[ ! "$title" =~ [^[:space:]] ]]; then
    abort "no title provided"
  fi
  line="$(head -n2 "$GIT_HUB_MSG_FILE" | tail -n1)"
  if [[ ! "$line" =~ ^\s*$ ]] && [[ ! "$line" =~ ^\# ]]; then
    error "malformed message in '$GIT_HUB_MSG_FILE'"
  fi
  local count=0
  local ifs="$IFS"
  IFS=''
  while read -r line; do
    [[ $((count++)) -lt 2 ]] && continue
    [[ "$line" =~ ^\# ]] && break
    body+="$line"$'\n'
  done < "$GIT_HUB_MSG_FILE"
  IFS="$ifs"
}

editor-comment() {
  rm -f "$GIT_HUB_MSG_FILE"

  echo "$1" > "$GIT_HUB_MSG_FILE"

  $GIT_HUB_EDITOR "$GIT_HUB_MSG_FILE"
  local line
  body=''
  local ifs="$IFS"
  IFS=''



( run in 1.039 second using v1.01-cache-2.11-cpan-39bf76dae61 )