ruslan-korneev / glitch

GitLab Interface in Telegram Chat
0 stars 0 forks source link

Time Tracking Report - Worker Class #11

Open ruslan-korneev opened 1 year ago

ruslan-korneev commented 1 year ago

class TimeTracker

methods to get list of spent time.

there should be possibility to filter by:

  • last week
  • one specific date
  • daterange (from_date - to_date)

Spent Time should be an instance of class SpentTime with attributes:

  • target: Target (Target is StrEnum based class with 2 values: Issue, MergeRequest)
  • spent_time: int (integer value in seconds)
  • posted_at: datetime | None (date and time of spent-time post)
  • gitlab_profile_id: int | None (id of author who posted spent time`

Solution

To specify users who spent time for some issue/merge-request, we could parse an {ProjectIssue/ProjectMergeRequest}.notes body, if body ends with "time spent" we could catch the time and translate from human-readable to seconds. Example, how to get ProjectIssue,ProjectMergeRequest:

from gitlab import Gitlab

gitlab = Gitlab(oauth=user.get_gitlab_access_token())
project = gitlab.projects.get(project_id)
project.issues         # returns list of `ProjectIssue`
project.mergerequests  # returns list of `ProjectMergeRequest`

IMPORTANT:

we need exactly an instance of ProjectIssue or ProjectMergeRequest, not an instance of Issue or MergeRequest this is important

To collect all spent time without information about user we could use just existing information from gitlab-api, issue and merge-request object has method time_stats, where we can get time in seconds.

ruslan-korneev commented 1 year ago

example of note i've got from one of issues

python query

project.issues.list()[0].notes.list()[3].asdict()

jsonified dictionary response

{
    "id": 10005,
    "type": null,
    "body": "added 6h of time spent",
    "attachment": null,
    "author": {
        "id": 48,
        "username": "sahakyan",
        "name": "Edita Sahakyan",
        "state": "active",
        "avatar_url": null,
        "web_url": "https://gitlab.example.com/sahakyan"
    },
    "created_at": "2023-07-25T10:15:12.137+03:00",
    "updated_at": "2023-07-25T10:15:12.140+03:00",
    "system": true,
    "noteable_id": 1459,
    "noteable_type": "Issue",
    "resolvable": false,
    "confidential": false,
    "noteable_iid": 5,
    "commands_changes": {}
}