Gerrit SSH查询-从结果中提取提交ID

pgccezyw  于 2022-11-01  发布在  其他
关注(0)|答案(1)|浏览(203)

我正在尝试提取并保存一个给定gerrit提交的提交ID。下面的命令提供了关于该提交的所有信息:
ssh -p <port-num> <host> gerrit query --current-patch-set <change-id>
此命令将从groovy文件运行。
它会传回类似下列的结果:

change <change-id>
  project: <project-name>
  branch: master
  id: <change-id>
  number: 12678771
  subject: NO-JIRA
  owner:
    name: Kevin Niland
    email: <email>
    username: <username>
  url: <gerrit-url>
  commitMessage: NO-JIRA

                 Change-Id: <change-id>
  createdOn: 2022-06-16 16:58:21 CEST
  lastUpdated: 2022-06-17 10:07:40 CEST
  open: true
  status: NEW
  currentPatchSet:
    number: 14
    revision: <commit-id> <--- What I want to extract
    parents:
 [dsfgdsf]
    ref: refs/changes/12/1234567/12
    uploader:
      name: Kevin Niland
      email: <email>
      username: <username>
    createdOn: 2022-06-17 10:07:39 CEST
    author:
      name: Kevin Niland
      email: <email>
      username: <username>
    isDraft: false
    kind: REWORK
    sizeInsertions: 41
    sizeDeletions: -28

type: stats
rowCount: 1
runTimeMilliseconds: 5
moreChanges: false

也可以使用--format=JSON将结果格式化为JSON格式,如下所示:

{"project":"project","branch":"master","id":"change-id","number":12678771,"subject":"NO-JIRA","owner":{"name":"Kevin Niland","email":"email","username":"username"},"url":"gerrit-url","commitMessage":"NO-JIRA","createdOn":1655391501,"lastUpdated":1655453260,"open":true,"status":"NEW","currentPatchSet":{"number":14,"revision":"COMMIT_ID","parents":["fgeretdsgfdghdfg"],"ref":"refs/changes/12/12345566/12","uploader":{"name":"Kevin Niland","email":"email","username":"username"},"createdOn":1655453259,"author":{"name":"Kevin Niland","email":"email","username":"username"},"isDraft":false,"kind":"REWORK","sizeInsertions":41,"sizeDeletions":-28}}
{"type":"stats","rowCount":1,"runTimeMilliseconds":9,"moreChanges":false}

我知道JsonSlurper库,但是,如果可能的话,我想避免使用它和任何库。有没有手动的方法来实现这一点?快速浏览一下在线显示,许多解决方案使用一些库来提取信息。有没有方法从第一个命令中提取信息,这不是groovy格式的?

cgfeq70w

cgfeq70w1#

根据上面的文本,您可以使用一个简单的正则表达式来提取您想要的内容:

String txt = '''
change <change-id>
  project: <project-name>
  id: <change-id>
  lastUpdated: 2022-06-17 10:07:40 CEST
  currentPatchSet:
    number: 14
    revision: <commit-id> <--- What I want to extract
    parents:
 '''

String revision = ( txt =~ /revision: (.+)/ ).findAll().first().last()

assert revision == '<commit-id> <--- What I want to extract'

如果您需要更复杂的搜索,则应该使用JsonSlurper,以避免重复劳动。

相关问题