]> git.corax.cc Git - toolbox-restapis/commitdiff
include/gitlab: Add functions to modify webhooks unstable
authorMatthias Kruk <m@m10k.eu>
Mon, 6 Jun 2022 00:21:19 +0000 (09:21 +0900)
committerMatthias Kruk <m@m10k.eu>
Mon, 6 Jun 2022 00:21:19 +0000 (09:21 +0900)
The gitlab module does not provide functions to interact with the
webhooks of projects, making it hard to write scripts that use this
functionality.

This commit adds functions to list, get, add, and remove webhooks
from a project.

include/gitlab.sh

index bb6365941ec86d54ea645336a15e9987f07e3f7c..d67e84bda0c338db2981586825d7d1d40754384e 100755 (executable)
@@ -97,6 +97,22 @@ _gitlab_post() {
        return 1
 }
 
+_gitlab_delete() {
+       local token="$1"
+       local url="$2"
+
+       local res
+
+       if ! res=$(curl --silent --location -X DELETE    \
+                       --header "Private-Token: $token" \
+                       "$url"); then
+               return 1
+       fi
+
+       echo "$res"
+       return 0
+}
+
 _gitlab_put() {
        local token="$1"
        local url="$2"
@@ -456,6 +472,105 @@ gitlab_project_list() {
        return 0
 }
 
+gitlab_project_list_hooks() {
+       local host="$1"
+       local token="$2"
+       local project="$3"
+
+       local project_id
+       local url
+       local resp
+
+       if ! project_id=$(gitlab_project_get_id "$host" "$token" "$project"); then
+               return 1
+       fi
+
+       url="$host/api/v4/projects/$project_id/hooks"
+       if ! resp=$(_gitlab_get "$token" "$url"); then
+               return 1
+       fi
+
+       echo "$resp"
+       return 0
+}
+
+gitlab_project_get_hook() {
+       local host="$1"
+       local token="$2"
+       local project="$3"
+       local hook="$4"
+
+       local project_id
+       local url
+       local resp
+
+       if ! project_id=$(gitlab_project_get_id "$host" "$token" "$project"); then
+               return 1
+       fi
+
+       url="$host/api/v4/projects/$project_id/$hook"
+       if ! resp=$(_gitlab_get "$token" "$url"); then
+               return 1
+       fi
+
+       echo "$resp"
+       return 0
+}
+
+gitlab_project_add_hook() {
+       local host="$1"
+       local token="$2"
+       local project="$3"
+       local hook_url="$4"
+       local branch="$5"
+
+       local project_id
+       local hook_data
+       local url
+       local resp
+
+       if ! project_id=$(gitlab_project_get_id "$host" "$token" "$project"); then
+               return 1
+       fi
+
+       url="$host/api/v4/projects/$project_id/hooks"
+       hook_data=$(json_object "url"                       "$hook_url" \
+                               "push_events_branch_filter" "$branch")
+
+       log_info "POSTing $hook_data to $url"
+       if ! resp=$(_gitlab_post "$token" "$url" "$hook_data"); then
+               log_highlight "Response from Gitlab" <<< "$resp" | log_error
+               return 1
+       fi
+
+       echo "$resp"
+       return 0
+}
+
+gitlab_project_remove_hook() {
+       local host="$1"
+       local token="$2"
+       local project="$3"
+       local hook="$4"
+
+       local project_id
+       local url
+       local resp
+
+       if ! project_id=$(gitlab_project_get_id "$host" "$token" "$project"); then
+               return 1
+       fi
+
+       url="$host/api/v4/projects/$project_id/hooks/$hook"
+
+       if ! resp=$(_gitlab_delete "$token" "$url"); then
+               return 1
+       fi
+
+       echo "$resp"
+       return 0
+}
+
 gitlab_mergerequest_get_votes() {
        local host="$1"
        local token="$2"