Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/src/lib/api
diff options
context:
space:
mode:
authorLevente Polyak <anthraxx@archlinux.org>2023-01-30 21:15:50 +0100
committerLevente Polyak <anthraxx@archlinux.org>2023-05-20 00:08:12 +0200
commiteda3a4aea00803d14400beff819d3b8be81774ce (patch)
tree78cb26de73b6632d4b40222f9055f1f23d380f18 /src/lib/api
parentf669a71e847da88fefc1296dd2dd9ba82549a8c6 (diff)
gitlab: add project path function to map special chars
Automatic path conversion is limited to GitLab API v4 and will be removed in the future. It's expected that the caller does the path conversion on caller side and only passes a valid path to the API within its limitations. Hence convert project names to valid paths: 1. replace single '+' between word boundaries with '-' 2. replace any other '+' with literal 'plus' 3. replace any special chars other than '_', '-' and '.' with '-' 4. replace consecutive '_-' chars with a single '-' 5. replace 'tree' with 'unix-tree' due to GitLab reserved keyword Signed-off-by: Levente Polyak <anthraxx@archlinux.org>
Diffstat (limited to 'src/lib/api')
-rw-r--r--src/lib/api/gitlab.sh26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/lib/api/gitlab.sh b/src/lib/api/gitlab.sh
index 649e205..e5f4237 100644
--- a/src/lib/api/gitlab.sh
+++ b/src/lib/api/gitlab.sh
@@ -81,16 +81,40 @@ gitlab_api_get_user() {
return 0
}
+# Convert arbitrary project names to GitLab valid path names.
+#
+# GitLab has several limitations on project and group names and also maintains
+# a list of reserved keywords as documented on their docs.
+# https://docs.gitlab.com/ee/user/reserved_names.html
+#
+# 1. replace single '+' between word boundaries with '-'
+# 2. replace any other '+' with literal 'plus'
+# 3. replace any special chars other than '_', '-' and '.' with '-'
+# 4. replace consecutive '_-' chars with a single '-'
+# 5. replace 'tree' with 'unix-tree' due to GitLab reserved keyword
+gitlab_project_name_to_path() {
+ local name=$1
+ printf "%s" "${name}" \
+ | sed -E 's/([a-zA-Z0-9]+)\+([a-zA-Z]+)/\1-\2/g' \
+ | sed -E 's/\+/plus/g' \
+ | sed -E 's/[^a-zA-Z0-9_\-\.]/-/g' \
+ | sed -E 's/[_\-]{2,}/-/g' \
+ | sed -E 's/^tree$/unix-tree/g'
+}
+
gitlab_api_create_project() {
local pkgbase=$1
- local outfile data path
+ local outfile data path project_path
[[ -z ${WORKDIR:-} ]] && setup_workdir
outfile=$(mktemp --tmpdir="${WORKDIR}" pkgctl-gitlab-api.XXXXXXXXXX)
+ project_path=$(gitlab_project_name_to_path "${pkgbase}")
+
# create GitLab project
data='{
"name": "'"${pkgbase}"'",
+ "path": "'"${project_path}"'",
"namespace_id": "'"${GIT_PACKAGING_NAMESPACE_ID}"'",
"request_access_enabled": "false"
}'