Skip to content

Commit e947906

Browse files
committed
Add on-add hook
1 parent 234fbd1 commit e947906

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

on_modify.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,13 @@ def extract_tags_from(json_obj):
6363

6464

6565
def extract_annotation_from(json_obj):
66-
6766
if 'annotations' not in json_obj:
6867
return '\'\''
6968

7069
return json_obj['annotations'][0]['description']
7170

7271

7372
def main(old, new):
74-
7573
start_or_stop = ''
7674

7775
# Started task.
@@ -104,7 +102,14 @@ def main(old, new):
104102

105103

106104
if __name__ == "__main__":
107-
old = json.loads(input_stream.readline().decode("utf-8", errors="replace"))
108-
new = json.loads(input_stream.readline().decode("utf-8", errors="replace"))
105+
# Hook is called with two lines input ('old', 'new') in case of an on-modify event,
106+
# but only with one line ('new') in case of an on-add event.
107+
# We want to call the hook with an emtpy 'old' ('{}') in the latter case.
108+
# Prepending '{}' makes sure this happens if only one line is added...
109+
lines = ['{}'] + [line.decode("utf-8", errors="replace") for line in input_stream.readlines()]
110+
111+
new = json.loads(lines.pop())
112+
old = json.loads(lines.pop())
113+
109114
print(json.dumps(new))
110115
main(old, new)

test/docker/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ WORKDIR /task-on-modify-hook
5555
# Install on-modify-hook
5656
RUN mkdir -p ~/.task/hooks
5757
RUN cp on_modify.py ~/.task/hooks/on-modify.timewarrior
58+
RUN ln -s on-add.timewarrior on-modify.timewarrior
5859
RUN chmod +x ~/.task/hooks/on-modify.timewarrior
5960

6061
RUN echo "timew: $( timew --version )" ; \

0 commit comments

Comments
 (0)