Skip to content

Commit

Permalink
Add command to create time entries
Browse files Browse the repository at this point in the history
  • Loading branch information
egegunes committed Jan 7, 2020
1 parent 423ce41 commit a0e6829
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 8 deletions.
7 changes: 7 additions & 0 deletions redmine/activity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Activity:
def __init__(self, **kwargs):
self.id = kwargs.get("id")
self.name = kwargs.get("name")

def __str__(self):
return f"{self.id:<4} {self.name:<20}"
1 change: 0 additions & 1 deletion redmine/cli/alias.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import click

from redmine.cli.config import Config


Expand Down
37 changes: 36 additions & 1 deletion redmine/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import sys
from collections import OrderedDict

import click
from requests.exceptions import HTTPError

import click
from redmine.activity import Activity
from redmine.cli.alias import AliasedGroup
from redmine.cli.config import Config, pass_config
from redmine.cli.helpers import get_description, get_note
Expand Down Expand Up @@ -297,6 +298,22 @@ def priority(redmine):
click.echo(Priority(**priority))


@list.command()
@click.pass_obj
def activity(redmine):
""" List time tracking activities """

try:
activities = sorted(
redmine.get("enumerations/time_entry_activities"), key=lambda x: x["id"]
)
except HTTPError as e:
return click.echo(click.style(f"Fatal: {e}", fg="red"))

for activity in activities:
click.echo(Activity(**activity))


@list.command()
@click.pass_obj
def user(redmine):
Expand Down Expand Up @@ -406,3 +423,21 @@ def times(redmine, **kwargs):

for entry in entries:
click.echo(Time(**entry))


@cli.command()
@click.argument("issue_id")
@click.argument("hours")
@click.option(OPTIONS["on"]["long"], default=None)
@click.option(OPTIONS["activity"]["long"], OPTIONS["activity"]["short"], default=None)
@click.option(OPTIONS["comment"]["long"], OPTIONS["comment"]["short"], default=None)
@click.pass_obj
def spent(redmine, issue_id, hours, **kwargs):
""" Create new time entry """

try:
redmine.create_time_entry(issue_id, hours, **kwargs)
except HTTPError as e:
return click.echo(click.style(f"Fatal: {e}", fg="red"))

click.echo(click.style("Time logged", fg="green"), err=True)
2 changes: 2 additions & 0 deletions redmine/cli/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@
"from": {"long": "--from"},
"to": {"long": "--to"},
"on": {"long": "--on"},
"activity": {"long": "--activity", "short": "-A"},
"comment": {"long": "--comment", "short": "-C"},
}
2 changes: 1 addition & 1 deletion redmine/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_header(self):

created_on = datetime.strptime(self.created_on, "%Y-%m-%dT%H:%M:%SZ")
header += (
f"Reported by {self.author['name']} on"
f"Reported by {self.author['name']} on "
f"{created_on.date()} {created_on.time()}\n\n"
)

Expand Down
31 changes: 29 additions & 2 deletions redmine/redmine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import os
from urllib.parse import urljoin

import click
import requests

import click


class Redmine:
def __init__(
Expand Down Expand Up @@ -82,7 +83,7 @@ def get_users(self):
memberships.extend(response["memberships"])

users = {}
membership_types = ['user', 'group', 'group_anonymous']
membership_types = ["user", "group", "group_anonymous"]

for m in memberships:
for t in membership_types:
Expand Down Expand Up @@ -221,3 +222,29 @@ def create_issue(self, **kwargs):
resp.raise_for_status()

return resp.json()["issue"]

def create_time_entry(self, issue_id, hours, **kwargs):
fields = {
"time_entry": {
"issue_id": issue_id,
"hours": hours,
"comments": kwargs.get("comment"),
}
}

if kwargs.get("activity"):
fields["time_entry"].update({"activity_id": kwargs.get("activity")})

if kwargs.get("on"):
fields["time_entry"].update({"spent_on": kwargs.get("on")})

resp = requests.post(
f"{self.url}/time_entries.json",
json=fields,
headers=self.auth_header,
verify=self.ssl_verify,
)

resp.raise_for_status()

return resp.json()
9 changes: 8 additions & 1 deletion redmine/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ def __init__(self, *args, **kwargs):
self.spent_on = kwargs.get("spent_on")

def __str__(self):
return f"{self.project['name']:<21.20} {self.issue['id']:>6} {self.user['name']:<21.20} {self.activity['name']:<15.14} {self.spent_on:<11} {self.hours:>6} hours"
time = f"{self.project['name']:<21.20} "
time += f"{self.issue['id']:>6} "
time += f"{self.user['name']:<21.20} "
time += f"{self.activity['name']:<15.14} "
time += f"{self.spent_on:<11} "
time += f"{self.hours:>6} hours"

return time
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-

from setuptools import setup, find_packages

from setuptools import find_packages, setup

with open("README.md") as f:
readme = f.read()
Expand Down

0 comments on commit a0e6829

Please sign in to comment.