This repository was archived by the owner on Nov 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Use more unique id for task names #19
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d9cc664
merge background script and trektor.js;
wendelin-as 446a5f2
keep background script and trektor.js seperate;
wendelin-as c7e438b
switch from shortId to shortLink for task name generation
wendelin-as d4df38c
switch from shortUrl to card id + board id
wendelin-as 243bb07
remove debug log
wendelin-as 3a2cb77
use card id plus short link
wendelin-as 53fe1dd
Merge branch 'main' into feature/board_id
wendelin-as File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| if (typeof browser == "undefined") { | ||
| globalThis.browser = chrome | ||
| } | ||
|
|
||
| class BackgroundScript { | ||
|
|
||
| static trektor; | ||
|
|
||
| static init(trektor) { | ||
| BackgroundScript.trektor = trektor | ||
|
|
||
| browser.runtime.onMessage.addListener(async (msg) => { | ||
| switch (msg.action) { | ||
| case 'track': | ||
| await BackgroundScript.track(...msg.args); | ||
| return; | ||
| case 'addTask': | ||
| await BackgroundScript.addTask(...msg.args); | ||
| return; | ||
| default: | ||
| throw new Error(`unknown action: ${msg.action}`); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| static async track(cardId) { | ||
| const task = await BackgroundScript.addTask(cardId); | ||
| const card = await BackgroundScript.trektor.trelloGateway.getCard(cardId); | ||
| const cardName = BackgroundScript.stripStoryPointsAndTaskToken(card.name); | ||
| const response = await BackgroundScript.trektor.togglGateway.startTimeEntry(task.id, cardName); | ||
| return response.data; | ||
| } | ||
|
|
||
| static async addTask(cardId) { | ||
| const card = await BackgroundScript.trektor.trelloGateway.getCard(cardId); | ||
|
|
||
| const taskPrefixes = card.labels | ||
| .map((label) => label.name.match(/(?<=#)[a-z0-9]+$/)?.[0]) | ||
| .filter((prefix) => prefix !== undefined); | ||
|
|
||
| if (taskPrefixes.length === 0) { | ||
| throw new Error('Card has no valid project labels.'); | ||
| } | ||
| if (taskPrefixes.length > 1) { | ||
| throw new Error('Card has multiple project labels.'); | ||
| } | ||
| const taskPrefix = taskPrefixes[0]; | ||
|
|
||
| let taskName = card.name.match(/(?<=#)[A-Za-z0-9_-]+/)?.[0]; | ||
|
|
||
| if (taskName === undefined) { | ||
| taskName = `${taskPrefix}_${card.idShort}_${card.idBoard.substring(0, 3)}`; | ||
|
|
||
| await BackgroundScript.trektor.trelloGateway.updateCard(card.id, { | ||
| name: `${card.name} #${taskName}`, | ||
| }); | ||
| } | ||
|
|
||
| const workspaces = await BackgroundScript.trektor.togglGateway.getWorkspaces(); | ||
|
|
||
| if (workspaces.length === 0) { | ||
| throw new Error('Could not find any toggl workspaces.'); | ||
| } | ||
| if (workspaces.length > 1) { | ||
| throw new Error('Found multiple toggl workspaces. Not sure how to deal with that...'); | ||
| } | ||
| const allProjects = await BackgroundScript.trektor.togglGateway.getProjects(workspaces[0].id); | ||
| const projects = allProjects.filter((project) => project.name.endsWith(`(${taskPrefix})`)); | ||
|
|
||
| if (projects.length === 0) { | ||
| throw new Error('Could not find any matching toggl project.'); | ||
| } | ||
| if (projects.length > 1) { | ||
| throw new Error('Found multiple matching toggl projects. Not sure how to deal with that...'); | ||
| } | ||
| const tasks = await BackgroundScript.trektor.togglGateway.getTasks(projects[0].id); | ||
| const task = tasks.find((task) => task.name === taskName); | ||
| if (task !== undefined) return task; | ||
|
|
||
| const response = await BackgroundScript.trektor.togglGateway.createTask(projects[0].id, taskName) | ||
| return response.data; | ||
| } | ||
|
|
||
| static stripStoryPointsAndTaskToken(cardName) { | ||
| return cardName | ||
| .replace(/^(\s*\(\d+\))?\s*/, '') // story points, e.g. (3) | ||
| .replace(/\s*#[a-z0-9_]+\s*$/, ''); // task token, e.g. #orga_5417 | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| importScripts('background.js', 'trektor.js'); | ||
|
|
||
| BackgroundScript.init(getTrektor()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| BackgroundScript.init(getTrektor()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.