gioboa / jira-plugin

Jira plugin for VsCode
https://marketplace.visualstudio.com/items?itemName=gioboa.jira-plugin
MIT License
265 stars 41 forks source link

issue: Added work log time begins at submitted time #153

Closed danstreeter closed 4 years ago

danstreeter commented 4 years ago

Describe the issue When submitting a work log via either the 'Set Working Issue -> No Working Issue' or 'Stop Working On Issue' commands, the time logged in Jira STARTS at the submitted time, not 'Now - time worked' E.G.

To Reproduce Steps to reproduce the behavior:

  1. Start work on an issue with the 'Set Working Issue' command
  2. Wait for a little while (for time to accumulate to a level where it is usable/visible once in Jira.
  3. Either use the 'Stop Working On Issue' command, or select 'No working issue' from the 'Set Working Issue' command to end the timer
  4. ENSURE YOU FILL IN THE COMMENT TO SUBMIT THE TIME LOG on either option above.
  5. Check the time logged within Jira
  6. See that the worklog has an start time of the submission time, NOT the start time of the worklog

Expected behavior The time logged should start at the time of the worklog tracking start time, not the submission time. E.G. (copied from above)

Screenshots None provided

Desktop (please complete the following information): Running on v0.22.0 on OSX, VSCode version 1.49.2

Log _None provided - entry is successfully submitted.

Additional Info As an initial investigation, it looks like the call to store.state.jira.addWorkLog within /src/commands/issue-add-worklog.ts is calling without any sort of 'start' parameter, just the issueKey, timeSpentSeconds and comment. Digging down into this method I cannot see that it accepts any other parameters, so the library/interface may need updating to support?

Question - Does the API even support this? Other members are using the API to support timeStart events - so need to investigate.

gioboa commented 4 years ago

Hi, thanks for your feedback. I will check if there is the possibility to force starting time. πŸ‘

danstreeter commented 4 years ago

No problem, i'm going to look also - and see how my colleagues are doing it (not with this plugin, but via their own home written Jira worklog updaters), but may not be able to look at this until late this afternoon/evening when we are 'offline' work mode.

gioboa commented 4 years ago

Here the Jira worklog API docs. Property started will solve the issue: from above docs started string The datetime on which the worklog effort was started. Required when creating a worklog. Optional when updating a worklog. Format: date-time

Here we can add the new property. πŸ‘

danstreeter commented 4 years ago

Indeed - just found the same. Am testing a change now. =)

danstreeter commented 4 years ago

I can confirm (via Postman tests) that the API allows the following payload to create a worklog at the submitted 'started' time:

{
    "issueKey": "TP-203",
    "timeSpentSeconds": 120,
    "comment": "Testing API Worklog Entry at specific time",
    "started": "2020-09-30T10:30:00.031+0100"
}

What I will struggle with is getting that format from the language (as not a language I know.

If you can provide me a way to get that date format, I can add, test and submit a new PR to fix this issue,

danstreeter commented 4 years ago

Agh, however it will need to be calculated using the timeSpentSeconds...

So 'started' == (now_in_seconds - timeSpentSeconds)

danstreeter commented 4 years ago

If you want to change yourself to get to the same point as me here is a simple patch:

diff --git a/src/commands/issue-add-worklog.ts b/src/commands/issue-add-worklog.ts
index 8d2403c..0fe81ef 100644
--- a/src/commands/issue-add-worklog.ts
+++ b/src/commands/issue-add-worklog.ts
@@ -11,7 +11,8 @@ export default async function issueAddWorklog(issueKey: string, timeSpentSeconds
         const response = await store.state.jira.addWorkLog({
           issueKey,
           timeSpentSeconds: Math.ceil(timeSpentSeconds / 60) * 60,
-          comment
+          comment,
+          started: "2020-09-30T10:30:00.031+0100"
         });
         const action = await vscode.window.showInformationMessage(`Worklog added`, 'Open in browser');
         if (action === 'Open in browser') {
diff --git a/src/services/http.model.ts b/src/services/http.model.ts
index dae3060..5586ed8 100644
--- a/src/services/http.model.ts
+++ b/src/services/http.model.ts
@@ -113,6 +113,7 @@ export interface IAddWorkLog {
   issueKey: string;
   timeSpentSeconds: number;
   comment?: string;
+  started: string;
 }

 export interface IIssueType {
gioboa commented 4 years ago

You deserve the resolution πŸ₯‡ I will release the new version with this fix πŸ‘

danstreeter commented 4 years ago

Agh, i was just about there! ;) Lets see what we both come up with!!! =)

danstreeter commented 4 years ago

Haha, yes - My final change worked and submits successfully... I'd be interested in your fix - knowing the language, as I had to include a new function to turn the Date(),toISOString() format into one that Jira accepts with the actual timezone format (+0100) instead of Zulu.

danstreeter commented 4 years ago

My final diff (ready for commit, but I wont as you probably have a better fix) is as follows:

diff --git a/src/commands/issue-add-worklog.ts b/src/commands/issue-add-worklog.ts
index 8d2403c..09e9a2b 100644
--- a/src/commands/issue-add-worklog.ts
+++ b/src/commands/issue-add-worklog.ts
@@ -3,15 +3,27 @@ import { logger, store } from '../services';
 import { NO_WORKING_ISSUE } from '../shared/constants';
 import openIssue from './open-issue';

+function dateToLocalISO(date: Date) {
+    const off    = date.getTimezoneOffset()
+    const absoff = Math.abs(off)
+    return (new Date(date.getTime() - off*60*1000).toISOString().substr(0,23) +
+            (off > 0 ? '-' : '+') + 
+            (absoff / 60).toFixed(0).padStart(2,'0') + 
+            (absoff % 60).toString().padStart(2,'0'))
+}
+
 export default async function issueAddWorklog(issueKey: string, timeSpentSeconds: number, comment: string): Promise<void> {
   try {
     if (issueKey !== NO_WORKING_ISSUE.key) {
       if (store.canExecuteJiraAPI()) {
         // call Jira API
+        const actualTimeSpentSeconds = Math.ceil(timeSpentSeconds / 60) * 60
+        const startedTime = new Date(Date.now() - (actualTimeSpentSeconds * 1000))
         const response = await store.state.jira.addWorkLog({
           issueKey,
-          timeSpentSeconds: Math.ceil(timeSpentSeconds / 60) * 60,
-          comment
+          timeSpentSeconds: actualTimeSpentSeconds,
+          comment,
+          started: dateToLocalISO(startedTime)
         });
         const action = await vscode.window.showInformationMessage(`Worklog added`, 'Open in browser');
         if (action === 'Open in browser') {
diff --git a/src/services/http.model.ts b/src/services/http.model.ts
index dae3060..5586ed8 100644
--- a/src/services/http.model.ts
+++ b/src/services/http.model.ts
@@ -113,6 +113,7 @@ export interface IAddWorkLog {
   issueKey: string;
   timeSpentSeconds: number;
   comment?: string;
+  started: string;
 }

 export interface IIssueType {
gioboa commented 4 years ago

Yep, I thought the same approach. Unfortunately date time is a difficult stuff in Js.

danstreeter commented 4 years ago

Yeah, I thought that... but thought it may have just been me having no real clue on this level of JS.

"Many a stack overflow post was read in the fixing of this bug"

danstreeter commented 4 years ago

@gioboa - Hows the new release going? Did you need me to push the code I did above or were you going with what you had?

gioboa commented 4 years ago

I will include the code in the develop branch and I will release the new version. πŸ‘