Gerrit 自动加2(code review)和提交(submit)
这个本来很简单,但是由于一些细节问题.和对gerrit的不熟悉.也怪自己没有仔细看文档. 对未知的恐惧.导致了浪费了比较多的时间来调试这个接口.
其实Gerrit官方是提供了rest api
来做这个事情的. 所以我的工作就是去看看这个API是怎么用.然后调试一下.先说结论:
代码自动+2
接口: 文档
API: 'POST /changes/{change-id}/revisions/{revision-id}/review'
调用示例:
POST /changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/revisions/674ac754f91e64a0efb8087e59a176484bd534d1/review HTTP/1.0
Content-Type: application/json; charset=UTF-8
{
"tag": "jenkins",
"message": "Some nits need to be fixed.",
"labels": {
"Code-Review": -1
},
"comments": {
"gerrit-server/src/main/java/com/google/gerrit/server/project/RefControl.java": [
{
"line": 23,
"message": "[nit] trailing whitespace"
},
{
"line": 49,
"message": "[nit] s/conrtol/control"
},
{
"range": {
"start_line": 50,
"start_character": 0,
"end_line": 55,
"end_character": 20
},
"message": "Incorrect indentation"
}
]
}
}
As response a ReviewResult entity is returned that describes the applied labels and any added reviewers (e.g. yourself, if you set a label but weren’t previously a reviewer on this CL).
Response
HTTP/1.1 200 OK
Content-Disposition: attachment
Content-Type: application/json; charset=UTF-8
)]}'
{
"labels": {
"Code-Review": -1
}
}
如果是简单用,在不知道代码详情的情况下,我只想简单的+2
, 那你可以这样写SHELL
# 注意: 这个Header 特别重要:
# -H "Content-Type: application/json; charset=UTF-8"
# 如果没有,会有你意想不到的问题.
USER=xxx # 你的Gerrit的用户名
HTTP_TOKEN=xxx # 这个不是登陆密码,这个是后台设置的http token
curl -X POST -u "${USER}:${HTTP_TOKEN}" \
-H "Content-Type: application/json; charset=UTF-8" \
-H "Accept: application/json" \
-d '{"message": "auto review by tools", "labels": {"Code-Review": "+2"}}' \
"https://gerrit.zhenguanyu.com/a/changes/${CHANGE_ID}/revisions/${REVISION_ID}/review"
代码自动提交
接口: 文档
API: 'POST /changes/{change-id}/revisions/{revision-id}/submit'
调用示例:
Marks a file of a revision as reviewed by the calling user.
Request
PUT /changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/revisions/674ac754f91e64a0efb8087e59a176484bd534d1/files/gerrit-server%2Fsrc%2Fmain%2Fjava%2Fcom%2Fgoogle%2Fgerrit%2Fserver%2Fproject%2FRefControl.java/reviewed HTTP/1.0
Response
HTTP/1.1 201 Created
If the file was already marked as reviewed by the calling user the response is “200 OK”.
你可以这样写你的SHELL
resp=`curl -X POST -u "${USER}:${HTTP_TOKEN}" \
-H "Accept: application/json" \
--data-raw '{"drafts":"PUBLISH_ALL_REVISIONS","labels":{"Code-Review":"+2"}}' \
"https://gerrit.zhenguanyu.com/a/changes/${CHANGE_ID}/revisions/${REVISION_ID}/submit"`
echo "data: ${resp}"
注: 因为这个是PUT接口,而没有参数要传递.直接是没有BODY,因此. 那个重要的headr:
-H "Content-Type: application/json; charset=UTF-8"
也不用有了. 就这样.
over:
参考:
- https://gerrit-review.googlesource.com/Documentation/rest-api.html#authentication
- https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#submit-revision
- https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#set-review
- https://gerrit-documentation.storage.googleapis.com/Documentation/2.8/rest-api-changes.html#set-review
- https://stackoverflow.com/questions/35361276/gerrit-authentication-required