是否有任何选项可以使用 curl/python 请求获取 GitLab 管道的持续时间?
Is there any option to get duration of GitLab's pipeline using curl/python requests?
我正在尝试获取 GitLab 管道的持续时间。我的 GitLab 版本是 12.10。我可以获得管道状态:
{
"id": 7475,
"sha": "someid",
"ref": "Someref",
"status": "success",
"created_at": "2021-04-28T12:07:17.807Z",
"updated_at": "2021-04-28T12:07:36.071Z",
"web_url": "https://git.somedomain.net/infra/some_project/pipelines/7475",
"before_sha": "0000000000000000000000000000000000000000",
"tag": false,
"yaml_errors": null,
"user": {
"id": 85,
"name": "some.name",
"username": "some.name",
"state": "active",
"avatar_url": "https://secure.gravatar.com/avatar/someid?s=80&d=identicon",
"web_url": "https://git.somedomain.net/some.name"
},
"started_at": null,
"finished_at": "2021-04-28T12:07:36.070Z",
"committed_at": null,
"duration": null,
"coverage": null,
"detailed_status": {
"icon": "status_success",
"text": "passed",
"label": "passed",
"group": "success",
"tooltip": "passed",
"has_details": false,
"details_path": "/infra/some_project/pipelines/7475",
"illustration": null,
"favicon": "/assets/ci_favicons/favicon_status_success-8451333011eee8ce9f2ab25dc487fe24a8758c694827a582f17f42b0a90446a2.png"
}
}
是否有获取管道持续时间的选项?或者,如何获得持续时间作为 "finished_at": "2021-04-28T12:07:36.070Z"
和 "created_at": "2021-04-28T12:07:17.807Z"
之间的差异 python?
状态字典中有几个值可以在您的代码中预先定义,例如:
false = False
null = None
那么你可以这样做:
status = { "id": 7475, "sha": "someid", "ref": "Someref", "status": "success", "created_at": "2021-04-28T12:07:17.807Z", "updated_at": "2021-04-28T12:07:36.071Z", "web_url": "https://git.somedomain.net/infra/some_project/pipelines/7475", "before_sha": "0000000000000000000000000000000000000000", "tag": false, "yaml_errors": null, "user": { "id": 85, "name": "some.name", "username": "some.name", "state": "active", "avatar_url": "https://secure.gravatar.com/avatar/someid?s=80&d=identicon", "web_url": "https://git.somedomain.net/some.name" }, "started_at": null, "finished_at": "2021-04-28T12:07:36.070Z", "committed_at": null, "duration": null, "coverage": null, "detailed_status": { "icon": "status_success", "text": "passed", "label": "passed", "group": "success", "tooltip": "passed", "has_details": false, "details_path": "/infra/some_project/pipelines/7475", "illustration": null, "favicon": "/assets/ci_favicons/favicon_status_success-8451333011eee8ce9f2ab25dc487fe24a8758c694827a582f17f42b0a90446a2.png" } }
然后使用datetime
模块来:
将字符串转换为日期时间对象(参考:https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes),以及
执行持续时间计算。
import datetime as dt
format_str = '%Y-%m-%dT%H:%M:%S.%fZ'
created_at = dt.datetime.strptime(status['created_at'],format_str)
finished_at = dt.datetime.strptime(status['finished_at'],format_str)
duration = finished_at - created_at
结果:
In [24]: duration
Out[24]: datetime.timedelta(seconds=18, microseconds=263000)
我正在尝试获取 GitLab 管道的持续时间。我的 GitLab 版本是 12.10。我可以获得管道状态:
{
"id": 7475,
"sha": "someid",
"ref": "Someref",
"status": "success",
"created_at": "2021-04-28T12:07:17.807Z",
"updated_at": "2021-04-28T12:07:36.071Z",
"web_url": "https://git.somedomain.net/infra/some_project/pipelines/7475",
"before_sha": "0000000000000000000000000000000000000000",
"tag": false,
"yaml_errors": null,
"user": {
"id": 85,
"name": "some.name",
"username": "some.name",
"state": "active",
"avatar_url": "https://secure.gravatar.com/avatar/someid?s=80&d=identicon",
"web_url": "https://git.somedomain.net/some.name"
},
"started_at": null,
"finished_at": "2021-04-28T12:07:36.070Z",
"committed_at": null,
"duration": null,
"coverage": null,
"detailed_status": {
"icon": "status_success",
"text": "passed",
"label": "passed",
"group": "success",
"tooltip": "passed",
"has_details": false,
"details_path": "/infra/some_project/pipelines/7475",
"illustration": null,
"favicon": "/assets/ci_favicons/favicon_status_success-8451333011eee8ce9f2ab25dc487fe24a8758c694827a582f17f42b0a90446a2.png"
}
}
是否有获取管道持续时间的选项?或者,如何获得持续时间作为 "finished_at": "2021-04-28T12:07:36.070Z"
和 "created_at": "2021-04-28T12:07:17.807Z"
之间的差异 python?
状态字典中有几个值可以在您的代码中预先定义,例如:
false = False
null = None
那么你可以这样做:
status = { "id": 7475, "sha": "someid", "ref": "Someref", "status": "success", "created_at": "2021-04-28T12:07:17.807Z", "updated_at": "2021-04-28T12:07:36.071Z", "web_url": "https://git.somedomain.net/infra/some_project/pipelines/7475", "before_sha": "0000000000000000000000000000000000000000", "tag": false, "yaml_errors": null, "user": { "id": 85, "name": "some.name", "username": "some.name", "state": "active", "avatar_url": "https://secure.gravatar.com/avatar/someid?s=80&d=identicon", "web_url": "https://git.somedomain.net/some.name" }, "started_at": null, "finished_at": "2021-04-28T12:07:36.070Z", "committed_at": null, "duration": null, "coverage": null, "detailed_status": { "icon": "status_success", "text": "passed", "label": "passed", "group": "success", "tooltip": "passed", "has_details": false, "details_path": "/infra/some_project/pipelines/7475", "illustration": null, "favicon": "/assets/ci_favicons/favicon_status_success-8451333011eee8ce9f2ab25dc487fe24a8758c694827a582f17f42b0a90446a2.png" } }
然后使用datetime
模块来:
将字符串转换为日期时间对象(参考:https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes),以及
执行持续时间计算。
import datetime as dt
format_str = '%Y-%m-%dT%H:%M:%S.%fZ'
created_at = dt.datetime.strptime(status['created_at'],format_str)
finished_at = dt.datetime.strptime(status['finished_at'],format_str)
duration = finished_at - created_at
结果:
In [24]: duration
Out[24]: datetime.timedelta(seconds=18, microseconds=263000)