• gitlab 一键 merge request(III)


    已经有两位同学写过类似的 wiki 了,值得一看:

    为啥我又来凑热闹呢?基于下面两个原由:

      1. 我的机子是 Win10,上面脚本的适用环境是 Mac/Linux
      1. gitlab 仓库换了地址,导致上面的脚本不可用了

    因为我的开发环境是:win10 + vscode + git,计划就在这个基础上优化提交 request 流程。所以,采用cygwin + shell来实现下面的功能。

    vscode 如何集成 cygwin,可以参考我的这篇文章:vscode 集成 cygwin

    提供能力

    Create merge request
    • 自动识别仓库/项目名称和ID
    • 当前本地分支作为 source branch
    • 手动输入远程分支名作为 target branch,默认 test 分支
    • 使用 last commit 作为 merge title
    Accept merge request
    • 提供可选的 Accept merge request 功能

    如何在项目中使用?

    1、在 win10 上安装 cygwin

    2、在 vscode 中集成 cygwin

    3、将下面脚本的 PRIVATE_TOKEN 更改为你自己的后保存为 mr.sh,我把它放在 cygwin 安装目录的 /home/廖大爷/sh 文件夹下。ps:你可以把它放在任何位置

    4、更改脚本的 GITLAB_URL 为你项目所在的 gitlab 地址

    5、打开 cygwin 终端,执行 vim .bashrc,键入alias merge='bash ~/sh/mr.sh',保存并退出,再执行source .bashrc

    6、使用 vscode 进入项目后,打开 vscode 终端,或使用 cygwin cd 到项目目录,执行merge等同于merge test,或执行merge 其他远程分支名

    mr.sh

    #!/bin/bash
    
    set -e
    
    TARGET_BRANCH=$1
    if [ -z $TARGET_BRANCH ]; then
      TARGET_BRANCH=test
    fi
    
    PRIVATE_TOKEN='xxxxxxxxxxxxxxxxxxxxxxxxxxx'
    GITLAB_URL='gitlab 仓库域名,比如 http://192.168.8.258'
    REMOTE_URL=$(git remote -v | grep push | awk '{print $2}')
    PROJECT_NAME=$(echo $REMOTE_URL | cut -d ':' -f2 | cut -d '.' -f1)
    
    API_URL=$GITLAB_URL/api/v3
    PROJECT_URL=$GITLAB_URL/$PROJECT_NAME
    assignee_name=xuyang
    assignee_id=57
    
    RED_COLOR='e[31m'
    BLUE_COLOR='e[34m'
    END_COLOR='e[0m'
    
    urlencode() {
      local length="${#1}"
      for (( i = 0; i < length; i++ )); do
        local c="${1:i:1}"
        case $c in
          [a-zA-Z0-9.~_-]) printf "$c" ;;
        *) printf "$c" | xxd -p -c1 | while read x;do printf "%%%s" "$x";done
      esac
    done
    }
    
    getUserId() {
      printf $(curl --header 
        "PRIVATE-TOKEN: ${PRIVATE_TOKEN}" 
        $API_URL/users?username=${assignee_name} | cut -d ',' -f3 | cut -d ':' -f2)
    }
    
    getProjectId() {
      printf $(curl --header 
        "Private-Token: ${PRIVATE_TOKEN}" 
        $PROJECT_URL 2>/dev/null | grep 'data-autocomplete-project-id' | cut -d '=' -f4 | cut -d ' ' -f1 | cut -d "'" -f2)
    }
    echo -e "
    ${BLUE_COLOR}Get merge request info ... ${END_COLOR}"
    
    projectId=$(getProjectId)
    projectId=${projectId:-902}
    sourceBranch=$(git branch | grep * | cut -d ' ' -f2)
    targetBranch=$TARGET_BRANCH
    title=$(git log -1 --pretty=%B)
    # echo merge info
    echo -e "
      projectId: ${RED_COLOR}$projectId${END_COLOR}
      sourceBranch: ${sourceBranch}
      targetBranch: ${targetBranch}
      title: ${title}
      remoteUrl: ${REMOTE_URL}"
    
    # create merge request
    createMR() {
      title=$(urlencode "$title")
      data="source_branch=$sourceBranch&target_branch=$targetBranch&assignee_id=$assignee_id&title=$title"
    
      echo $(curl --header "PRIVATE-TOKEN: ${PRIVATE_TOKEN}" 
        --data $data 
        "$API_URL/projects/$projectId/merge_requests" 2>/dev/null)
    }
    
    echo -e "
    ${BLUE_COLOR}Creating merge request... ${END_COLOR}"
    
    merge_request_res=$(createMR)
    merge_request_id=$(echo $merge_request_res | cut -d ':' -f2 | cut -d ',' -f1)
    
    # Accept merge request
    if [[ $merge_request_id == *[0-9] ]]; then
      echo -e "
      ${BLUE_COLOR}Create merge request success! The merge_request_id is $merge_request_id${END_COLOR}"
    
      read -p "Accept this merge request? (y/n) " isConfirm
    
      if [[ $isConfirm == "n" ]]; then
        echo -e "
        ${RED_COLOR}Cancel accept merge request${END_COLOR}"
        exit
      fi
      
      curl -X PUT --header "PRIVATE-TOKEN: ${PRIVATE_TOKEN}" 
        "$API_URL/projects/$projectId/merge_requests/$merge_request_id/merge" 
        2>/dev/null
      
      echo "
      Merged Success!"
      exit
    else 
    echo -e "
    ${RED_COLOR}Create merge request Fail:${END_COLOR}
      $merge_request_res"
    fi
    
    echo -e "
    ${BLUE_COLOR}Click on the link below for more details:${END_COLOR}
      $PROJECT_URL/merge_requests
    "
    

    gitlab api文档

  • 相关阅读:
    选择排序
    迭代器使用过程中为什么抛出ConcurrentModificationException
    自定义实现的ArrayList以及自定义实现的Iterator迭代器
    单链表
    collections方法记录
    JDK1.9中关于集合的新方法
    请使用时间相关的API,计算一个人从出生到现在一共活了多少天?
    java中对象的向上转型和向下转型
    异常相关知识整理
    可变参数相关知识
  • 原文地址:https://www.cnblogs.com/fayin/p/10243391.html
Copyright © 2020-2023  润新知