From 4caf4e1512ae9348a7902e8afead91995c8f53ae Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Wed, 14 Jul 2021 21:36:22 +0900 Subject: [PATCH] include/gitlab: Clean up gitlab module API Function names in the gitlab module do not follow a consistent naming convention, making them rather irritating to use. This commit makes sure function names in the gitlab module are consistent in their naming. Further, this commit makes sure the log module is used for printing error messages, where necessary. --- include/gitlab.sh | 347 +++++++++++++++++++++++----------------------- 1 file changed, 174 insertions(+), 173 deletions(-) diff --git a/include/gitlab.sh b/include/gitlab.sh index 13f09cb..a808bbe 100755 --- a/include/gitlab.sh +++ b/include/gitlab.sh @@ -17,7 +17,7 @@ # along with this program. If not, see . __init() { - if ! include "log" "json"; then + if ! include "is" "log" "json"; then return 1 fi @@ -69,37 +69,35 @@ _gitlab_put() { return 0 } -gitlab_import_status() { +_gitlab_list_projects_page() { local host="$1" local token="$2" - local project="$3" + local perpage="$3" + local page="$4" - local url - local res + local url + local results - id=$(_gitlab_urlencode "$project") - url="$host/api/v4/projects/$id" + url="$host/api/v4/projects?simple=true&per_page=$perpage&page=$page" - if ! res=$(_gitlab_get "$token" "$url"); then - return 1 - fi + if ! results=$(_gitlab_get "$token" "$url"); then + return 1 + fi - echo "$res" | jq -r ".import_status" - return 0 + if ! jq -e -r ".[] | \"\(.id) \(.path_with_namespace)\"" <<< "$results"; then + return 1 + fi + + return 0 } -gitlab_download_file() { +gitlab_user_list() { local host="$1" local token="$2" - local project="$3" - local branch="$4" - local file="$5" local url - project=$(_gitlab_urlencode "$project") - file=$(_gitlab_urlencode "$file") - url="$host/api/v4/projects/$project/repository/files/$file/raw?ref=$branch" + url="$host/api/v4/users?per_page=512" if ! _gitlab_get "$token" "$url"; then return 1 @@ -108,36 +106,48 @@ gitlab_download_file() { return 0 } -gitlab_get_users() { +gitlab_user_list_short() { local host="$1" local token="$2" - local url + local resp - url="$host/api/v4/users?per_page=512" + if ! resp=$(gitlab_user_list "$host" "$token"); then + return 1 + fi - if ! _gitlab_get "$token" "$url"; then + if ! jq -e -r ".[] | \"\(.id) \(.username) \(.name)\"" <<< "$resp"; then return 1 fi return 0 } -gitlab_user_list() { +gitlab_user_get_id() { local host="$1" local token="$2" + local user="$3" local resp + local uid + local username + local fullname - if ! resp=$(gitlab_get_users "$host" "$token"); then + if ! resp=$(gitlab_user_list_short "$host" "$token"); then return 1 fi - echo "$resp" | jq -r ".[] | \"\(.id) \(.username) \(.name)\"" - return 0 + while read -r uid username fullname; do + if [[ "$username" == "$user" ]]; then + echo "$uid" + return 0 + fi + done <<< "$resp" + + return 1 } -gitlab_get_current_user() { +gitlab_user_whoami() { local host="$1" local token="$2" @@ -152,31 +162,49 @@ gitlab_get_current_user() { return 0 } -gitlab_get_user_id() { +gitlab_project_get_import_status() { local host="$1" local token="$2" - local user="$3" + local project="$3" - local resp - local uid - local username - local fullname + local url + local res - if ! resp=$(gitlab_user_list "$host" "$token"); then + id=$(_gitlab_urlencode "$project") + url="$host/api/v4/projects/$id" + + if ! res=$(_gitlab_get "$token" "$url"); then return 1 fi - while read -r uid username fullname; do - if [[ "$username" == "$user" ]]; then - echo "$uid" - return 0 - fi - done <<< "$resp" + if ! jq -r -e ".import_status" <<< "$res"; then + return 1 + fi - return 1 + return 0 } -gitlab_fork() { +gitlab_project_download_file() { + local host="$1" + local token="$2" + local project="$3" + local branch="$4" + local file="$5" + + local url + + project=$(_gitlab_urlencode "$project") + file=$(_gitlab_urlencode "$file") + url="$host/api/v4/projects/$project/repository/files/$file/raw?ref=$branch" + + if ! _gitlab_get "$token" "$url"; then + return 1 + fi + + return 0 +} + +gitlab_project_fork_async() { local host="$1" local token="$2" local project="$3" @@ -200,7 +228,7 @@ gitlab_fork() { return 0 } -gitlab_fork_sync() { +gitlab_project_fork() { local host="$1" local token="$2" local project="$3" @@ -209,13 +237,13 @@ gitlab_fork_sync() { local resp local fork_id - if ! resp=$(gitlab_fork "$host" "$token" "$project" "$namespace"); then - echo "Could not fork project" 1>&2 + if ! resp=$(gitlab_project_fork_async "$host" "$token" "$project" "$namespace"); then + log_error "Could not fork $project to $namespace" return 1 fi - if ! fork_id=$(echo "$resp" | jq ".id"); then - echo "Could not get id of fork" 1>&2 + if ! fork_id=$(jq -e -r ".id" <<< "$resp"); then + log_error "Invalid response from gitlab_project_fork_async()" return 1 fi @@ -225,15 +253,15 @@ gitlab_fork_sync() { while true; do local import_status - if ! import_status=$(gitlab_import_status "$host" \ - "$token" \ - "$fork_id"); then - echo "Could not get import status of fork" 1>&2 + if ! import_status=$(gitlab_project_get_import_status "$host" \ + "$token" \ + "$fork_id"); then + log_error "Could not get import status of $fork_id" return 1 fi if [[ "$import_status" == "none" ]] || - [[ "$import_status" == "finished" ]]; then + [[ "$import_status" == "finished" ]]; then break fi @@ -243,7 +271,7 @@ gitlab_fork_sync() { return 0 } -gitlab_create_branch() { +gitlab_project_create_branch() { local host="$1" local token="$2" local project="$3" @@ -254,7 +282,10 @@ gitlab_create_branch() { local url id=$(_gitlab_urlencode "$project") - data=$(json_make "id" "$id" "branch" "$branch" "ref" "$ref") + data=$(json_object "id" "$id" \ + "ref" "$ref" \ + "branch" "$branch") + url="$host/api/v4/projects/$id/repository/branches" if ! _gitlab_post "$token" "$url" "$data"; then @@ -264,7 +295,29 @@ gitlab_create_branch() { return 0 } -gitlab_project_get_branches() { +gitlab_project_get_id() { + local host="$1" + local token="$2" + local project="$3" + + local url + local resp + + project=$(_gitlab_urlencode "$project") + url="$host/api/v4/projects/$project" + + if ! resp=$(_gitlab_get "$token" "$url"); then + return 1 + fi + + if ! jq -e -r ".id" <<< "$resp"; then + return 1 + fi + + return 0 +} + +gitlab_project_get_branch_names() { local host="$1" local token="$2" local project="$3" @@ -279,7 +332,7 @@ gitlab_project_get_branches() { return 1 fi - if ! echo "$resp" | jq -r ".[].name"; then + if ! jq -e -r ".[].name" <<< "$resp"; then return 1 fi @@ -295,7 +348,7 @@ gitlab_project_get_members() { local url local resp - if ! project_id=$(gitlab_get_project_id "$host" \ + if ! project_id=$(gitlab_project_get_id "$host" \ "$token" \ "$project"); then return 1 @@ -310,7 +363,7 @@ gitlab_project_get_members() { return 0 } -gitlab_project_get_merge_requests() { +gitlab_project_get_mergerequests() { local host="$1" local token="$2" local project="$3" @@ -329,7 +382,41 @@ gitlab_project_get_merge_requests() { return 0 } -gitlab_project_mergerequest_get_votes() { +gitlab_project_list() { + local host="$1" + local token="$2" + + local page + local perpage + + page=1 + perpage=50 + + while true; do + local projects + local num + + if ! projects=$(_gitlab_list_projects_page "$host" \ + "$token" \ + "$perpage" \ + "$page"); then + return 1 + fi + + num=$(echo "$projects" | wc -l) + echo "$projects" + + if ((num < perpage)); then + break + fi + + ((page++)) + done + + return 0 +} + +gitlab_mergerequest_get_votes() { local host="$1" local token="$2" local project="$3" @@ -339,7 +426,7 @@ gitlab_project_mergerequest_get_votes() { local url local resp - if ! project_id=$(gitlab_get_project_id "$host" "$token" "$project"); then + if ! project_id=$(gitlab_project_get_id "$host" "$token" "$project"); then return 1 fi @@ -352,7 +439,7 @@ gitlab_project_mergerequest_get_votes() { return 0 } -gitlab_project_mergerequest_comment() { +gitlab_mergerequest_add_comment() { local host="$1" local token="$2" local project="$3" @@ -363,7 +450,7 @@ gitlab_project_mergerequest_comment() { local url local data - if ! project_id=$(gitlab_get_project_id "$host" "$token" "$project"); then + if ! project_id=$(gitlab_project_get_id "$host" "$token" "$project"); then return 1 fi @@ -377,7 +464,7 @@ gitlab_project_mergerequest_comment() { return 0 } -gitlab_list_merge_requests() { +gitlab_mergerequest_get_list() { local host="$1" local token="$2" local scope="$3" @@ -399,7 +486,7 @@ gitlab_list_merge_requests() { return 0 } -gitlab_project_merge_merge_request() { +gitlab_mergerequest_merge() { local host="$1" local token="$2" local project="$3" @@ -407,112 +494,25 @@ gitlab_project_merge_merge_request() { local project_id local url - local resp - if [[ "$project" =~ ^[0-9]+$ ]]; then + if is_digits "$project"; then project_id="$project" - elif ! project_id=$(gitlab_get_project_id "$host" "$token" "$project"); then - return 1 - fi - - url="$host/api/v4/projects/$project_id/merge_requests/$mergerequest/merge" - if ! resp=$(_gitlab_put "$token" "$url"); then + elif ! project_id=$(gitlab_project_get_id "$host" "$token" "$project"); then + log_error "Could not get project id of $project" return 1 fi - echo "$resp" - return 0 -} - -gitlab_get_project_id() { - local host="$1" - local token="$2" - local project="$3" - - local url - local resp - - project=$(_gitlab_urlencode "$project") - url="$host/api/v4/projects/$project" - - if ! resp=$(_gitlab_get "$token" "$url"); then - return 1 - fi - - echo "$resp" | jq ".id" - return 0 -} - -gitlab_list_projects_page() { - local host="$1" - local token="$2" - local perpage="$3" - local page="$4" - - local url - local results - - url="$host/api/v4/projects?simple=true&per_page=$perpage&page=$page" + url="$host/api/v4/projects/$project_id/merge_requests/$mergerequest/merge" - if ! results=$(_gitlab_get "$token" "$url"); then + if ! _gitlab_put "$token" "$url"; then return 1 fi - echo "$results" | jq -r ".[] | \"\(.id) \(.path_with_namespace)\"" - return 0 } -gitlab_list_projects() { - local host="$1" - local token="$2" - - local page - local perpage - - page=1 - perpage=50 - - while true; do - local projects - local num - - if ! projects=$(gitlab_list_projects_page "$host" \ - "$token" \ - "$perpage" \ - "$page"); then - return 1 - fi - - num=$(echo "$projects" | wc -l) - echo "$projects" - - if ((num < perpage)); then - break - fi - - ((page++)) - done - - return 0 -} - -# -# gitlab_merge_request - Create a new merge request -# -# SYNOPSIS -# gitlab_merge_request "$host" "$token" "$source" "$destination" -# "$title" "$assignee" "$description" -# -# DESCRIPTION -# The gitlab_merge_request function creates a new merge request from the -# repository:branch identified by $source to the repository:branch identified -# by $destination. The title, assignee, and description of the merge request -# will be set according to the $title, $assignee, and $description arguments, -# respectively. -# -gitlab_merge_request() { +gitlab_mergerequest_new() { local host="$1" local token="$2" local source="$3" @@ -535,40 +535,41 @@ gitlab_merge_request() { source_branch="${source##*:}" destination_branch="${destination##*:}" - if ! assignee_id=$(gitlab_get_user_id "$host" \ + if ! assignee_id=$(gitlab_user_get_id "$host" \ "$token" \ "$assignee"); then - echo "Invalid user: $assignee" 1>&2 + log_error "Invalid user: $assignee" return 1 fi if [ -z "$source_branch" ]; then - echo "Invalid source branch" 1>&2 + log_error "Invalid source branch" return 1 fi if [ -z "$destination_branch" ]; then - echo "Invalid destination branch" 1>&2 + log_error "Invalid destination branch" return 1 fi - if ! source_id=$(gitlab_get_project_id "$host" "$token" "$source_name"); then - echo "Could not get project id for $source_name" 1>&2 + if ! source_id=$(gitlab_project_get_id "$host" "$token" "$source_name"); then + log_error "Could not get project id for $source_name" return 1 fi - if ! destination_id=$(gitlab_get_project_id "$host" "$token" "$destination_name"); then - echo "Could not get project id for $destination_name" 1>&2 + if ! destination_id=$(gitlab_project_get_id "$host" "$token" "$destination_name"); then + log_error "Could not get project id for $destination_name" return 1 fi - data=$(json_make "id" "$source_id" \ - "target_project_id" "$destination_id" \ - "source_branch" "$source_branch" \ - "target_branch" "$destination_branch" \ - "title" "$title" \ - "assignee_id" "$assignee_id" \ - "description" "$description") + data=$(json_object "id" "$source_id" \ + "title" "$title" \ + "target_project_id" "$destination_id" \ + "source_branch" "$source_branch" \ + "target_branch" "$destination_branch" \ + "assignee_id" "$assignee_id" \ + "description" "$description") + url="$host/api/v4/projects/$source_id/merge_requests" if ! _gitlab_post "$token" "$url" "$data"; then -- 2.47.3