#!/bin/bash
# Set GitLab API URL and access token
GITLAB_API_URL="http://IP/api/v4"
ACCESS_TOKEN="Token秘钥"
# Define repository IDs
declare -A repo_ids=(
["git@IP:kingmq/client.git"]="123"
["git@IP:kingmq/server.git"]="456"
)
# Function: Create a merge request
create_merge_request() {
local repo=$1
local src_branch=$2
local dest_branch=$3
local title=$4
local description=$5
local repo_id=${repo_ids[$repo]}
echo "Creating a merge request from $src_branch to $dest_branch for repository $repo"
# Make API request to create a merge request
response=$(curl -s --request POST --header "PRIVATE-TOKEN: $ACCESS_TOKEN" --data "source_branch=$src_branch&target_branch=$dest_branch&title=$title&description=$description" "$GITLAB_API_URL/projects/$repo_id/merge_requests")
# Parse the response to get the merge request URL
merge_request_url=$(echo "$response" | python -c 'import sys, json; print(json.load(sys.stdin)["web_url"])')
echo "Merge request created: $merge_request_url"
}
create_tag() {
local repo=$1
local branch=$2
local tag_name=$3
local tag_message=$4
local repo_id=${repo_ids[$repo]}
echo "Creating tag $tag_name on branch $branch for repository $repo"
# 获取分支的提交 SHA
commit_sha=$(curl -s --header "PRIVATE-TOKEN: $ACCESS_TOKEN" "$GITLAB_API_URL/projects/$repo_id/repository/branches/$branch" | python -c 'import sys, json; print(json.load(sys.stdin)["commit"]["id"])')
# 创建标签
response=$(curl -s --request POST --header "PRIVATE-TOKEN: $ACCESS_TOKEN" --data "tag_name=$tag_name&ref=$commit_sha&message=$tag_message" "$GITLAB_API_URL/projects/$repo_id/repository/tags")
echo "Tag created: $response"
}
# Interactive repository selection
select_repo() {
PS3="Select a repository: "
select repo in "kingmq-client" "kingmq-common" "kingmq-kmf" "kingmq-server" "Exit"; do
case $repo in
"Exit")
exit 0
;;
*)
break
;;
esac
done
repo_url="git@IP:XXX/$repo.git"
}
main(){
echo -e "输入 \n\
1: Merge from release to main \n\
4: release分支打标签 \n\
其他:取消"
read -p "请输入 " args
case $args in
1)
title="合并release到main"
description="合并release到main"
select_repo
create_merge_request "$repo_url" "release" "main" "$title" "$description"
;;
2)
echo "请输入tag编号:"
read version
title="$version"
description="baseline-$version"
select_repo
create_tag "$repo_url" "release" "$title" "$description"
;;
*)
echo "Invalid option: $1"
echo "Usage: $0 {1|2}"
exit 1
;;
esac
}
#开始执行
main $*