# along with this program. If not, see <https://www.gnu.org/licenses/>.
__init() {
- if ! include "log" "json"; then
+ if ! include "is" "log" "json"; then
return 1
fi
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
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"
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"
return 0
}
-gitlab_fork_sync() {
+gitlab_project_fork() {
local host="$1"
local token="$2"
local project="$3"
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
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
return 0
}
-gitlab_create_branch() {
+gitlab_project_create_branch() {
local host="$1"
local token="$2"
local project="$3"
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
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"
return 1
fi
- if ! echo "$resp" | jq -r ".[].name"; then
+ if ! jq -e -r ".[].name" <<< "$resp"; then
return 1
fi
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
return 0
}
-gitlab_project_get_merge_requests() {
+gitlab_project_get_mergerequests() {
local host="$1"
local token="$2"
local project="$3"
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"
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
return 0
}
-gitlab_project_mergerequest_comment() {
+gitlab_mergerequest_add_comment() {
local host="$1"
local token="$2"
local project="$3"
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
return 0
}
-gitlab_list_merge_requests() {
+gitlab_mergerequest_get_list() {
local host="$1"
local token="$2"
local scope="$3"
return 0
}
-gitlab_project_merge_merge_request() {
+gitlab_mergerequest_merge() {
local host="$1"
local token="$2"
local project="$3"
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"
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