newalan

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

Gerrit Commit批量获取

        stage('3. 获取变更列表') {
            steps {
                sh """
                    cd ${WORKSPACE}
                    ssh -p ${GERRIT_PORT} ${GERRIT_USER}@${GERRIT_HOST} gerrit query --current-patch-set status:open owner:${TARGET_OWNER} | awk '
/^change I/ {
    if( num != "" && branch == "${BRANCH}" && project ~ /^${PROJECT_HEAD}/ ) {
        split(ref,a,"/");
        print num, a[5], ref, project
    }
    num=project=branch=ref=""
}
/^  number: / { num=\$2 }
/^  project: / { project=\$2 }
/^  branch: / { branch=\$2 }
/^    ref: / { ref=\$2 }
END {
    if( num != "" && branch == "${BRANCH}" && project ~ /^${PROJECT_HEAD}/ ) {
        split(ref,a,"/");
        print num, a[5], ref, project
    }
}
' > aptiv_changes.txt
                    cat aptiv_changes.txt
                """
            }
        }

(1)解析 

ssh -p ${GERRIT_PORT} ${GERRIT_USER}@${GERRIT_HOST} gerrit query --current-patch-set status:open owner:${TARGET_OWNER}
  • 通过 SSH 连接 Gerrit
  • 使用 gerrit query 命令查询变更
  • --current-patch-set:获取最新版本号
  • status:open:只查 ** 待合入(未合并)** 的变更
  • owner:${TARGET_OWNER}:只查这个用户提交的变更
输出内容为:
$ cat log
change I69bcb4a5f2395a3fa4c8b8df9850deac98e3d0f6
  project: xxxxx-icc-platform-8295/android/vendor/xxxxx/frameworks/tbox
  branch: xxxxx-icc-platform-8295-sup-dev
  topic: xxxxx_done
  id: I69bcb4a5f2395a3fa4c8b8df9850deac98e3d0f6
  number: 405
  subject: [IXE-xxx][qnx][animation]: test commit 2026年 05月 09日 星期六 21:37:35 CST
  owner:
    name: ssss
    email: ssss@ssss.com.cn
    username: ssss
  url: http://ip:20260/c/xxxxx-icc-platform-8295/android/vendor/xxxxx/frameworks/tbox/+/405
  hashtags:
  createdOn: 2026-05-09 13:37:36 UTC
  lastUpdated: 2026-05-09 14:59:13 UTC
  open: true
  status: NEW
  currentPatchSet:
    number: 1
    revision: bde14aa3c022aa5b941338f5d4cd0f2c97ea8e8b
    parents:
 [3b0c5f894bb919119633117f5cdf422426935e19]
    ref: refs/changes/05/405/1
    uploader:
      name: ssss
      email: ssss@ssss.com.cn
      username: ssss
    createdOn: 2026-05-09 13:37:36 UTC
    author:
      name: ssss
      email: ssss@ssss.com.cn
      username: ssss
    kind: REWORK
    sizeInsertions: 14
    sizeDeletions: 1

change I8498bba1359795ff3eccab26cf420b7e6ad9fd56
.....

type: stats
rowCount: 6
runTimeMilliseconds: 23
moreChanges: false

 (2)解析 AWK语句

/^change I/ 匹配一个新变更开始

/^change I/ {
    # 如果上一个变更的数据有效
    if( num != "" && branch == "${BRANCH}" && project ~ /^${PROJECT_HEAD}/ ) {
        split(ref,a,"/");  # 把 ref 按 / 切成数组
        print num, a[5], ref, project;  # 输出干净数据
    }
    # 清空变量,准备存下一个变更
    num=project=branch=ref=""
}
  • 每次遇到新的变更(change I)
  • 先把上一个变更检查是否符合条件:
    • 有编号
    • 是目标分支
    • 是目标项目开头
     
  • 符合就输出一行干净数据
  • 然后清空变量,准备存下一条

  

#把 Gerrit 输出的每一行,对应存到变量里
/^  number: / { num=$2 }    # 抓取变更号 405
/^  project: / { project=$2 } # 抓取项目名
/^  branch: / { branch=$2 }  # 抓取分支
/^    ref: / { ref=$2 }    # 抓取 ref 拉取地址

  END 语句处理最后一个变更。因为最后一个变更后面没有新的 change I,所以要在 END 里输出。

(3) split 拆分 ref 提取版本号

#原始内容
ref: refs/changes/05/405/1

split(ref,a,"/") 后
得到

a[1] = refs
a[2] = changes
a[3] = 05
a[4] = 405
a[5] = 1  <-- patchSet 版本号

  

(4)结果

aptiv_changes.txt 的值为
405 1 refs/changes/05/405/1 proj-xxx
404 1 refs/changes/04/404/1 proj-xxx

  后面可以用while read c p r proj 读取。



posted on 2026-05-10 21:06  newalan  阅读(3)  评论(0)    收藏  举报