1

I'm trying to get duration of GitLab's pipeline. My GitLab version is 12.10. I can get status of pipeline :


{
    "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"
    }
}

Is there any option to get pipeline duration? or, how to get duration as difference between "finished_at": "2021-04-28T12:07:36.070Z" and "created_at": "2021-04-28T12:07:17.807Z" in python?

1 Answer 1

1

There are a couple of values in the status dict which can be pre-defined in your code, e.g.:

false = False
null = None

Then you can do:

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" } }

Then use the datetime module to:

  1. convert strings to datetime objects (reference: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes), and

  2. perform your duration calculation.

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

Result:

In [24]: duration
Out[24]: datetime.timedelta(seconds=18, microseconds=263000)
Sign up to request clarification or add additional context in comments.

1 Comment

@ŠtefanSedmák: you're welcome :) Best wishes with the rest of your project!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.