Skip to content

Commit

Permalink
Use --https-url as the hostname in gh/glab API requests (#330)
Browse files Browse the repository at this point in the history
* use --https-url value as the hostname in gh/glab API requests

* update graphql gitlab query to return repo and repo owner path instead of display name (#14)

Co-authored-by: Christopher Harris <[email protected]>

* use https_url in all commands

* update group -> namespace in gitlab clone script (#15)

* update graphql gitlab query to return repo and repo owner path instead of display name

* change group -> namespace in gitlab clone script

---------

Co-authored-by: Christopher Harris <[email protected]>
  • Loading branch information
trxcllnt and cwharris authored Jun 25, 2024
1 parent 66c25e3 commit a702f8f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ clone_${NAME}() {
-b,--branch
-d,--directory
-u,--upstream
--ssh-url
--https-url
' "$@" <&0)";

eval "$(rapids-get-num-archs-jobs-and-load --archs 0 "$@")";
Expand Down
2 changes: 1 addition & 1 deletion features/src/utils/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "devcontainer-utils",
"id": "utils",
"version": "24.8.4",
"version": "24.8.5",
"description": "A feature to install RAPIDS devcontainer utility scripts",
"containerEnv": {
"BASH_ENV": "/etc/bash.bash_env"
Expand Down
35 changes: 19 additions & 16 deletions features/src/utils/opt/devcontainer/bin/github/repo/clone.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@

get_default_branch() {
local repo="${1}";
gh repo view "${repo}" --json defaultBranchRef --jq '.defaultBranchRef.name';
GH_HOST="${https_url}" gh repo view "${repo}" --json defaultBranchRef --jq '.defaultBranchRef.name';
}

get_repo_name() {
local repo="${1}";
gh repo view "${repo}" --json 'name' --jq '.name';
GH_HOST="${https_url}" gh repo view "${repo}" --json 'name' --jq '.name';
}

get_repo_owner() {
local repo="${1}";
gh repo view "${repo}" --json 'owner' --jq '.owner.login';
GH_HOST="${https_url}" gh repo view "${repo}" --json 'owner' --jq '.owner.login';
}

get_repo_git_url() {
local repo="${1}";
echo "$(gh repo view "${repo}" --json url --jq ".url").git";
echo "$(GH_HOST="${https_url}" gh repo view "${repo}" --json url --jq ".url").git";
}

get_repo_ssh_url() {
local repo="${1}";
gh repo view "${repo}" --json sshUrl --jq ".sshUrl";
GH_HOST="${https_url}" gh repo view "${repo}" --json sshUrl --jq ".sshUrl";
}

get_user_fork_name() {
Expand All @@ -67,12 +67,12 @@ get_user_fork_name() {
________EOF
)";
local nameWithOwner;
nameWithOwner="$(gh repo list --limit 9999 "${user}" --fork --json nameWithOwner --json parent --jq ". ${query}" 2>/dev/null || echo "err")";
nameWithOwner="$(GH_HOST="${https_url}" gh repo list --limit 9999 "${user}" --fork --json nameWithOwner --json parent --jq ". ${query}" 2>/dev/null || echo "err")";
if [ "${nameWithOwner}" = "err" ]; then
nameWithOwner="";
# Work around https://github.com/cli/cli/issues/7881 by explicitly enumerating each user fork and checking the parent info
for repo in $(gh repo list --limit 9999 "${user}" --fork --json name --jq 'map(.name)[]'); do
nameWithOwner="$(gh repo view "${repo}" --json nameWithOwner --json parent --jq "[.] ${query}" 2>/dev/null || echo "")";
for repo in $(GH_HOST="${https_url}" gh repo list --limit 9999 "${user}" --fork --json name --jq 'map(.name)[]'); do
nameWithOwner="$(GH_HOST="${https_url}" gh repo view "${repo}" --json nameWithOwner --json parent --jq "[.] ${query}" 2>/dev/null || echo "")";
if test -n "${nameWithOwner}"; then
break;
fi
Expand Down Expand Up @@ -106,6 +106,9 @@ clone_github_repo() {
local fork=;
local owner=;

ssh_url="${ssh_url:-${GITHUB_HOST:-github.com}}";
https_url="${https_url:-${GITHUB_HOST:-github.com}}";

if test -z "${no_fork:-}" && \
test -z "${clone_upstream:-}" && \
devcontainer-utils-shell-is-interactive; then
Expand Down Expand Up @@ -136,7 +139,7 @@ clone_github_repo() {
case "${CHOICE:-}" in
[Nn]* ) origin="${upstream}"; break;;
[Yy]* ) origin="${user}/${name}";
gh repo fork "${upstream}" --clone=false --default-branch-only;
GH_HOST="${https_url}" gh repo fork "${upstream}" --clone=false --default-branch-only;
break;;
* ) echo "Please answer 'y' or 'n'";;
esac
Expand All @@ -148,8 +151,8 @@ clone_github_repo() {

if test -z "${no_fork:-}" && \
test -z "${clone_upstream:-}" && \
gh auth status >/dev/null 2>&1; then
if [ "$(gh config get git_protocol)" = "ssh" ]; then
gh auth status --hostname "${https_url}" >/dev/null 2>&1; then
if [ "$(gh config get git_protocol --host "${https_url}")" = "ssh" ]; then
origin_="$(get_repo_ssh_url "${origin}")";
upstream_="$(get_repo_ssh_url "${upstream}")";
else
Expand All @@ -159,12 +162,12 @@ clone_github_repo() {
fi

if test -z "${origin_:-}" || test -z "${upstream_:-}"; then
if [ "$(gh config get git_protocol)" = "ssh" ]; then
origin_="${origin_:-"ssh://git@${ssh_url:-${GITHUB_HOST:-github.com}}/${origin}.git"}";
upstream_="${upstream_:-"ssh://git@${ssh_url:-${GITHUB_HOST:-github.com}}/${upstream}.git"}";
if [ "$(gh config get git_protocol --host "${https_url}")" = "ssh" ]; then
origin_="${origin_:-"ssh://git@${ssh_url}/${origin}.git"}";
upstream_="${upstream_:-"ssh://git@${ssh_url}/${upstream}.git"}";
else
origin_="${origin_:-"https://${https_url:-${GITHUB_HOST:-github.com}}/${origin}.git"}";
upstream_="${upstream_:-"https://${https_url:-${GITHUB_HOST:-github.com}}/${upstream}.git"}";
origin_="${origin_:-"https://${https_url}/${origin}.git"}";
upstream_="${upstream_:-"https://${https_url}/${upstream}.git"}";
fi
fi

Expand Down
41 changes: 22 additions & 19 deletions features/src/utils/opt/devcontainer/bin/gitlab/repo/clone.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

get_default_branch() {
local repo="${1}";
glab api graphql -f query="$(cat <<________EOF | tr -s '[:space:]'
glab api graphql --hostname "${https_url}" -f query="$(cat <<________EOF | tr -s '[:space:]'
query {
project(fullPath: "${repo}") {
repository {
Expand All @@ -42,35 +42,35 @@ ________EOF

get_repo_name() {
local repo="${1}";
glab api graphql -f query="$(cat <<________EOF | tr -s '[:space:]'
glab api graphql --hostname "${https_url}" -f query="$(cat <<________EOF | tr -s '[:space:]'
query {
project(fullPath: "${repo}") {
name
path
}
}
________EOF
)" \
| jq -r '.data.project.name';
| jq -r '.data.project.path';
}

get_repo_owner() {
local repo="${1}";
glab api graphql -f query="$(cat <<________EOF | tr -s '[:space:]'
glab api graphql --hostname "${https_url}" -f query="$(cat <<________EOF | tr -s '[:space:]'
query {
project(fullPath: "${repo}") {
group {
name
namespace {
path
}
}
}
________EOF
)" \
| jq -r '.data.project.group.name';
| jq -r '.data.project.namespace.path';
}

get_repo_git_url() {
local repo="${1}";
glab api graphql -f query="$(cat <<________EOF | tr -s '[:space:]'
glab api graphql --hostname "${https_url}" -f query="$(cat <<________EOF | tr -s '[:space:]'
query {
project(fullPath: "${repo}") {
httpUrlToRepo
Expand All @@ -84,7 +84,7 @@ ________EOF

get_repo_ssh_url() {
local repo="${1}";
glab api graphql -f query="$(cat <<________EOF | tr -s '[:space:]'
glab api graphql --hostname "${https_url}" -f query="$(cat <<________EOF | tr -s '[:space:]'
query {
project(fullPath: "${repo}") {
sshUrlToRepo
Expand All @@ -102,7 +102,7 @@ get_user_fork_name() {
if [ "${user}" = "${owner}" ]; then
echo "${owner}/${name}";
else
glab api "projects/$(jq -rn --arg x "${owner}/${name}" '$x|@uri')/forks?owned=true" | jq -r '. | map(.path_with_namespace)[]';
glab api --hostname "${https_url}" "projects/$(jq -rn --arg x "${owner}/${name}" '$x|@uri')/forks?owned=true" | jq -r '. | map(.path_with_namespace)[]';
fi
}

Expand Down Expand Up @@ -130,6 +130,9 @@ clone_gitlab_repo() {
local user=;
local owner=;

ssh_url="${ssh_url:-${GITLAB_HOST:-gitlab.com}}";
https_url="${https_url:-${GITLAB_HOST:-gitlab.com}}";

if test -z "${no_fork:-}" && \
test -z "${clone_upstream:-}" && \
devcontainer-utils-shell-is-interactive; then
Expand Down Expand Up @@ -160,7 +163,7 @@ clone_gitlab_repo() {
case "${CHOICE:-}" in
[Nn]* ) origin="${upstream}"; break;;
[Yy]* ) origin="${user}/${name}";
glab repo fork "${upstream}" --clone=false;
GL_HOST="${https_url}" glab repo fork "${upstream}" --clone=false;
break;;
* ) echo "Please answer 'y' or 'n'";;
esac
Expand All @@ -172,8 +175,8 @@ clone_gitlab_repo() {

if test -z "${no_fork:-}" && \
test -z "${clone_upstream:-}" && \
! glab auth status 2>&1 | grep -q "No token provided"; then
if [ "$(glab config get git_protocol)" = "ssh" ]; then
! glab auth status --hostname "${https_url}" 2>&1 | grep -q "No token provided"; then
if [ "$(glab config get git_protocol --host "${https_url}")" = "ssh" ]; then
origin_="$(get_repo_ssh_url "${origin}")";
upstream_="$(get_repo_ssh_url "${upstream}")";
else
Expand All @@ -183,12 +186,12 @@ clone_gitlab_repo() {
fi

if test -z "${origin_:-}" || test -z "${upstream_:-}"; then
if [ "$(glab config get git_protocol)" = "ssh" ]; then
origin_="${origin_:-"ssh://git@${ssh_url:-${GITLAB_HOST:-gitlab.com}}/${origin}.git"}";
upstream_="${upstream_:-"ssh://git@${ssh_url:-${GITLAB_HOST:-gitlab.com}}/${upstream}.git"}";
if [ "$(glab config get git_protocol --host "${https_url}")" = "ssh" ]; then
origin_="${origin_:-"ssh://git@${ssh_url}/${origin}.git"}";
upstream_="${upstream_:-"ssh://git@${ssh_url}/${upstream}.git"}";
else
origin_="${origin_:-"https://${https_url:-${GITLAB_HOST:-gitlab.com}}/${origin}.git"}";
upstream_="${upstream_:-"https://${https_url:-${GITLAB_HOST:-gitlab.com}}/${upstream}.git"}";
origin_="${origin_:-"https://${https_url}/${origin}.git"}";
upstream_="${upstream_:-"https://${https_url}/${upstream}.git"}";
fi
fi

Expand Down

0 comments on commit a702f8f

Please sign in to comment.