numerous-refrigerator-93306
09/02/2025, 10:21 PMnumerous-refrigerator-93306
09/02/2025, 11:28 PM# ------------------------------------------------------------
# gitlab_project_id – return the GitLab project ID.
#
# Usage examples:
#
# # 1️⃣ Use environment vars (no argument)
# GITLAB_URL=https://gitlab.wolfspyre.io \
# GITLAB_GROUP=mirrored_repos \
# GITLAB_REPO=ansible-role-collectd \
# gitlab_project_id
#
# # 2️⃣ Pass the full project path as argument (host taken from repo)
# gitlab_project_id "mirrored_repos/ansible-role-collectd"
#
# # 3️⃣ Let it auto‑detect everything from .git/config
# gitlab_project_id
#
# Returns: numeric project ID (e.g. 79). Errors are printed to stderr.
# ------------------------------------------------------------
gitlab_project_id() {
# -----------------------------------------------------------------
# 1️⃣ Token handling – optional, taken from env vars if present.
# -----------------------------------------------------------------
local token="${GITLAB_TOKEN:-${PRIVATE_TOKEN}}"
# -----------------------------------------------------------------
# 2️⃣ Determine the base GitLab URL (host + scheme).
# -----------------------------------------------------------------
# * If GITLAB_URL is set, use it directly.
# * Otherwise, extract host from the remote URL of the repo.
local base_url
if [[ -n "$GITLAB_URL" ]]; then
# Strip any trailing slash – we'll add our own later.
base_url="${GITLAB_URL%/}"
else
# Get remote URL (prefer git config, fallback to .git/config)
local remote=$(git config --get remote.origin.url 2>/dev/null ||
awk '/url =/ {print $3}' .git/config | head -n1)
if [[ -z "$remote" ]]; then
>&2 echo "Error: No remote URL found and GITLAB_URL not set."
return 1
fi
# Strip credentials & protocol, keep only host part.
base_url=$(printf '%s\n' "$remote" |
sed -E 's|^[^@]*@||; s|^[a-z]+://||' |
awk -F/ '{print $1}')
base_url="https://${base_url}"
fi
# -----------------------------------------------------------------
# 3️⃣ Determine the project path (group/subgroup/.../project).
# -----------------------------------------------------------------
local raw_path
if [[ -n "$1" ]]; then # argument supplied
raw_path="$1"
elif [[ -n "$GITLAB_GROUP" && -n "$GITLAB_REPO" ]]; then
raw_path="${GITLAB_GROUP}/${GITLAB_REPO}"
else
# Derive from the remote URL (same logic as original one‑liner)
local remote=$(git config --get remote.origin.url 2>/dev/null ||
awk '/url =/ {print $3}' .git/config | head -n1)
# Strip credentials & protocol.
remote=$(printf '%s\n' "$remote" |
sed -E 's|^[^@]*@||; s|^[a-z]+://||')
# Split host / path, drop trailing ".git", keep everything after host.
raw_path=$(awk -F/ '
{
host=$1;
path=substr($0,index($0,$2));
sub(/\.git$/,"",path);
print path;
}' <<<"$remote")
fi
if [[ -z "$raw_path" ]]; then
>&2 echo "Error: Could not determine project path."
return 1
fi
# -----------------------------------------------------------------
# 4️⃣ Encode slashes (%2F) – GitLab API expects this.
# -----------------------------------------------------------------
local encoded_path
encoded_path=$(printf '%s' "$raw_path" | sed 's#/#%2F#g')
# -----------------------------------------------------------------
# 5️⃣ Build the full API URL.
# -----------------------------------------------------------------
local api_url="${base_url}/api/v4/projects/${encoded_path}"
# -----------------------------------------------------------------
# 6️⃣ Perform the request (with token if we have one).
# -----------------------------------------------------------------
local response
if [[ -n "$token" ]]; then
response=$(curl -sSf -H "PRIVATE-TOKEN:$token" "$api_url")
else
response=$(curl -sSf "$api_url")
fi
if [[ $? -ne 0 ]]; then
>&2 echo "Error: HTTP request failed (check URL / token)."
return 1
fi
# -----------------------------------------------------------------
# 7️⃣ Extract the numeric ID (using jq if available, else a simple grep).
# -----------------------------------------------------------------
local id
if command -v jq >/dev/null 2>&1; then
id=$(printf '%s\n' "$response" | jq -r '.id')
else
id=$(printf '%s\n' "$response" |
grep -o '"id":[0-9]*' | cut -d':' -f2)
fi
if [[ -z "$id" || "$id" == "null" ]]; then
>&2 echo "Error: Could not extract project ID from response."
return 1
fi
# -----------------------------------------------------------------
# 8️⃣ Output the ID.
# -----------------------------------------------------------------
printf '%s\n' "$id"
}numerous-refrigerator-93306
09/03/2025, 1:28 AMroot@sandbox:/openhands/code# cd /workspace/org_openhands_wpl_synthetic/
root@sandbox:/workspace/org_openhands_wpl_synthetic# divineGitlabProjectID
5171