Skip to content

Commit

Permalink
Merge pull request #17 from aubreypwd/feature/-
Browse files Browse the repository at this point in the history
Improve how Notes are added and improve how bullets are interpreted
  • Loading branch information
andrejilderda authored Dec 31, 2021
2 parents d250f0f + df4071f commit 81d5ca6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 59 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
- Uses the latest Harvest API (v2)

## Installation & setup
Install the latest version which you can find under [Releases](https://github.com/ajilderda/alfred-harvest-v2/releases).

Install the latest version which you can find under [Releases](https://github.com/andrejilderda/alfred-harvest-v2/releases).

1. Make sure you have [node.js](https://nodejs.org/en/) installed. To check run `node -v` from your terminal.
2. Go to https://id.getharvest.com/developers › 'Create New Personal Access Token' and give it a name, i.e. 'Alfred workflow'.
3. Copy the access token and remember your Account ID.
Expand All @@ -16,19 +18,22 @@ Install the latest version which you can find under [Releases](https://github.co
6. Type your 6-digit Account ID and press <kbd>enter</kbd>,
7. Type `hvn` and start your first timer!

*The first time you run the workflow you may be prompted to grant access to the API token (which is stored in your Keychain).
\*The first time you run the workflow you may be prompted to grant access to the API token (which is stored in your Keychain).

_Note: In order to install workflows you need the [Alfred Powerpack](https://www.alfredapp.com/powerpack/)._

## How to use

### Start a new timer

![Start a new timer](https://user-images.githubusercontent.com/487182/68616415-48858600-04c5-11ea-921c-38d3b8d0217b.gif)

- Type <kbd>hvn</kbd> to list the available projects,
- Select a project and press <kbd>enter</kbd>,
- Select the task and press <kbd>enter</kbd>

### Toggle timer

![Toggle timer](https://user-images.githubusercontent.com/487182/68616434-4f13fd80-04c5-11ea-8379-77b7ba7919e0.gif)

- Type `hvt` to list today's timers.
Expand All @@ -37,6 +42,7 @@ _Note: In order to install workflows you need the [Alfred Powerpack](https://www
**Tip:** Hold <kbd>alt</kbd> to delete the selected task.

### Adjust timer

![Adjust timer](https://user-images.githubusercontent.com/487182/68617779-3bb66180-04c8-11ea-8ea8-2b35ebe934ad.gif)

- Type `hva` to list today's timers,
Expand All @@ -46,6 +52,7 @@ _Note: In order to install workflows you need the [Alfred Powerpack](https://www
**Tip:** Add a `+`/`-` in front of your timer to add or subtract time, i.e. `-30m` will subtract 30 minutes from your timer.

### Add notes

![Add note](https://user-images.githubusercontent.com/487182/68617778-3bb66180-04c8-11ea-9951-ee7d23e10fdc.gif)

- Type `hvnt` to list today's timers,
Expand All @@ -54,6 +61,7 @@ _Note: In order to install workflows you need the [Alfred Powerpack](https://www
**Tip:** Your newly typed note will be appended by default. If you want to overwrite the note, hold down the <kbd>alt</kbd>-key when selecting a task from the list.

## Credits & thank you’s

This workflow was inspired by Neil Renicker’s [Harvest workflow](https://github.com/tinystride/alfred-harvest) which I've used for years (but stopped working for me).

All icons (except the Harvest logo) are from Daniel Bruce’s [Entypo](http://www.entypo.com/)-pack, and are distributed under the [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/)-license.
All icons (except the Harvest logo) are from Daniel Bruce’s [Entypo](http://www.entypo.com/)-pack, and are distributed under the [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/)-license.
12 changes: 6 additions & 6 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -979,8 +979,8 @@
<integer>0</integer>
<key>script</key>
<string># THESE VARIABLES MUST BE SET. SEE THE ONEUPDATER README FOR AN EXPLANATION OF EACH.
readonly remote_info_plist='https://raw.githubusercontent.com/ajilderda/alfred-harvest-v2/master/info.plist'
readonly workflow_url='ajilderda/alfred-harvest-v2'
readonly remote_info_plist='https://raw.githubusercontent.com/andrejilderda/alfred-harvest-v2/master/info.plist'
readonly workflow_url='andrejilderda/alfred-harvest-v2'
readonly download_type='github_release'
readonly frequency_check='4'
Expand Down Expand Up @@ -1223,7 +1223,7 @@ fi</string>
<key>spaces</key>
<string></string>
<key>url</key>
<string>https://github.com/ajilderda/alfred-harvest-v2#installation--setup</string>
<string>https://github.com/andrejilderda/alfred-harvest-v2#installation--setup</string>
<key>utf8</key>
<true/>
</dict>
Expand Down Expand Up @@ -1300,7 +1300,7 @@ fi</string>
</array>
<key>readme</key>
<string>For more information head over to:
https://github.com/ajilderda/alfred-harvest-v2</string>
https://github.com/andrejilderda/alfred-harvest-v2</string>
<key>uidata</key>
<dict>
<key>0113364C-D18F-4B1C-B082-9579BE03D03C</key>
Expand Down Expand Up @@ -1558,8 +1558,8 @@ https://github.com/ajilderda/alfred-harvest-v2</string>
<key>variablesdontexport</key>
<array/>
<key>version</key>
<string>1.1.4</string>
<string>1.1.5</string>
<key>webaddress</key>
<string>https://github.com/ajilderda/alfred-harvest-v2</string>
<string>https://github.com/andrejilderda/alfred-harvest-v2</string>
</dict>
</plist>
107 changes: 57 additions & 50 deletions src/update-task.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,69 @@
import { apiCall } from './utils/helpers';
import { notify } from './utils/notifications';
import { apiCall } from "./utils/helpers";
import { notify } from "./utils/notifications";

const vars = process.env;
const { action, goTo, taskId, taskNotes, requestMethod, taskHours } = vars;
const stopRestart = vars.stopRestart || '';
const stopRestart = vars.stopRestart || "";

let url = `https://api.harvestapp.com/v2/time_entries/${taskId}/${stopRestart}`;

if (action === 'note') {
const prevNote = taskNotes || '';
const newNote = process.argv[2].replace(//g, '\n– ')
const note = encodeURIComponent(`${prevNote}${newNote}`);
url = `https://api.harvestapp.com/v2/time_entries/${taskId}?notes=${note}`;
if (action === "note") {
const prevNote = taskNotes || "";
const newNote = process.argv[2];
const bulletRegex = /^(\*|•|-|–)\s?/g;
const match = newNote.match(bulletRegex);

// Convert any bullets (dash and en-dashes to '– ' and asterisks and bullets to '• ')
const bulletChar = !match
? ""
: match[0].trim() === "*" || match[0].trim() === "•"
? "• "
: "– ";

const newNoteFormatted = process.argv[2].replace(bulletRegex, bulletChar);

// Format previous notes from new notes.
const noteFormatted = !prevNote.length
? newNoteFormatted // Just use the new note without any concern for the previous (because there is none).
: bulletChar // Check if a bullet character was used
? `${prevNote}\n${newNoteFormatted}` // Bullets get a single line separator \n from the previous note (bullet or otherwise).
: `${prevNote}\n\n${newNoteFormatted}`; // While all other notes get two separators \n\n from the previous.

const note = encodeURIComponent(noteFormatted.trim());

url = `https://api.harvestapp.com/v2/time_entries/${taskId}?notes=${note}`;
}

if (action === 'adjust-timer') {
url = `https://api.harvestapp.com/v2/time_entries/${taskId}?hours=${taskHours}`;
if (action === "adjust-timer") {
url = `https://api.harvestapp.com/v2/time_entries/${taskId}?hours=${taskHours}`;
}

// bail when user is 'redirected' to other command
if (!goTo) {

await apiCall(url, requestMethod)
.then(response => {
if (action === 'note') {
notify(
'Harvest note updated!',
response.notes
);
}
else if (action === 'adjust-timer') {
notify(
'Harvest timer adjusted!',
`Hours: ${response.hours}`
);
}
else if (stopRestart === 'stop') {
notify(
'Harvest timer stopped!',
`${response.project.name}, ${response.task.name}`
);
}
else if (stopRestart === 'restart') {
notify(
'Harvest timer started!',
`${response.project.name}, ${response.task.name}`
);
}
else {
notify(
'Harvest timer updated!'
);
}
})
.catch(error => {
notify(
'Failed to update task.',
'Check your network connection and try again.'
);
});
}
await apiCall(url, requestMethod)
.then((response) => {
if (action === "note") {
notify("Harvest note updated!", response.notes);
} else if (action === "adjust-timer") {
notify("Harvest timer adjusted!", `Hours: ${response.hours}`);
} else if (stopRestart === "stop") {
notify(
"Harvest timer stopped!",
`${response.project.name}, ${response.task.name}`
);
} else if (stopRestart === "restart") {
notify(
"Harvest timer started!",
`${response.project.name}, ${response.task.name}`
);
} else {
notify("Harvest timer updated!");
}
})
.catch((error) => {
notify(
"Failed to update task.",
"Check your network connection and try again."
);
});
}

0 comments on commit 81d5ca6

Please sign in to comment.