Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow updating active worklog entry #28

Merged
merged 6 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Allow for quickly switching actively tracked issue
- Add support for fallback comments
- Allow updating active worklog entry
- Add support for JIRA Cloud installation
- Allow shifting timestamps for worklog entries using h/j/k/l/J/K
- Show time spent on unsynced worklog entries
- Improved error handling
- Upgrade to go 1.23.4
- Dependency upgrades

## [v1.1.0] - Jul 2, 2024

### Added

- Allow tweaking time when saving worklog
- Add first time help, "tracking started since" indicator
- Show indicator for currently tracked item
- Show unsynced count
- Add more colors for issue type
- Dependency upgrades

[unreleased]: https://github.com/dhth/punchout/compare/v1.1.0...HEAD
[v1.1.0]: https://github.com/dhth/punchout/compare/v1.0.0...v1.1.0
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ Issue List View
S Quick switch recording; will save a worklog entry without
a comment for the currently active issue, and start
recording time for another issue
<ctrl+s> Add manual worklog entry
<ctrl+s> Update active worklog entry (when tracking active), or add
manual worklog entry (when not tracking)
<ctrl+t> Go to currently tracked item
<ctrl+x> Discard currently active recording
<ctrl+b> Open issue in browser
Expand Down
42 changes: 40 additions & 2 deletions internal/persistence/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ func StopCurrentlyActiveEntry(db *sql.DB, issueKey string, endTs time.Time) erro
stmt, err := db.Prepare(`
UPDATE issue_log
SET active = 0,
end_ts = ?,
comment = ''
end_ts = ?
WHERE issue_key = ?
AND active = 1;
`)
Expand Down Expand Up @@ -296,3 +295,42 @@ func QuickSwitchActiveIssue(db *sql.DB, currentIssue, selectedIssue string, curr

return InsertNewEntry(db, selectedIssue, currentTime)
}

func UpdateActiveWLBeginTs(db *sql.DB, beginTs time.Time) error {
stmt, err := db.Prepare(`
UPDATE issue_log
SET begin_ts=?
WHERE active is true;
`)
if err != nil {
return err
}
defer stmt.Close()

_, err = stmt.Exec(beginTs.UTC(), true)
if err != nil {
return err
}

return nil
}

func UpdateActiveWL(db *sql.DB, beginTs time.Time, comment string) error {
stmt, err := db.Prepare(`
UPDATE issue_log
SET begin_ts=?,
comment=?
WHERE active is true;
`)
if err != nil {
return err
}
defer stmt.Close()

_, err = stmt.Exec(beginTs.UTC(), comment, true)
if err != nil {
return err
}

return nil
}
30 changes: 17 additions & 13 deletions internal/ui/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ func quickSwitchActiveIssue(db *sql.DB, selectedIssue string, currentTime time.T
}
}

func updateActiveWL(db *sql.DB, beginTS time.Time, comment *string) tea.Cmd {
return func() tea.Msg {
var err error
if comment == nil {
err = pers.UpdateActiveWLBeginTs(db, beginTS)
} else {
err = pers.UpdateActiveWL(db, beginTS, *comment)
}

return activeWLUpdatedMsg{beginTS, comment, err}
}
}

func insertManualEntry(db *sql.DB, issueKey string, beginTS time.Time, endTS time.Time, comment string) tea.Cmd {
return func() tea.Msg {
stmt, err := db.Prepare(`
Expand Down Expand Up @@ -126,23 +139,24 @@ WHERE ID = ?;
func fetchActiveStatus(db *sql.DB, interval time.Duration) tea.Cmd {
return tea.Tick(interval, func(time.Time) tea.Msg {
row := db.QueryRow(`
SELECT issue_key, begin_ts
SELECT issue_key, begin_ts, comment
from issue_log
WHERE active=1
ORDER BY begin_ts DESC
LIMIT 1
`)
var activeIssue string
var beginTs time.Time
err := row.Scan(&activeIssue, &beginTs)
var comment *string
err := row.Scan(&activeIssue, &beginTs, &comment)
if err == sql.ErrNoRows {
return fetchActiveMsg{activeIssue: activeIssue}
}
if err != nil {
return fetchActiveMsg{err: err}
}

return fetchActiveMsg{activeIssue: activeIssue, beginTs: beginTs}
return fetchActiveMsg{activeIssue: activeIssue, beginTs: beginTs, comment: comment}
})
}

Expand Down Expand Up @@ -240,23 +254,13 @@ func syncWorklogWithJIRA(cl *jira.Client, entry common.WorklogEntry, fallbackCom
return wlAddedOnJIRA{index, entry, fallbackCmtUsed, errWorklogsEndTSIsEmpty}
}

// if entry.Comment == nil && fallbackComment == nil {
// return wlAddedOnJIRA{index, entry, fallbackCmtUsed, errWorklogsCommentIsEmpty}
// }

var comment string
if entry.NeedsComment() && fallbackComment != nil {
comment = *fallbackComment
fallbackCmtUsed = true
} else {
comment = *entry.Comment
}
// if !entry.NeedsComment() {
// comment = *entry.Comment
// } else {
// comment = *fallbackComment
// fallbackCmtUsed = true
// }

err := addWLtoJira(cl, entry.IssueKey, entry.BeginTS, *entry.EndTS, comment, timeDeltaMins)
return wlAddedOnJIRA{index, entry, fallbackCmtUsed, err}
Expand Down
3 changes: 2 additions & 1 deletion internal/ui/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ var helpText = fmt.Sprintf(`
S Quick switch recording; will save a worklog entry without
a comment for the currently active issue, and start
recording time for another issue
<ctrl+s> Add manual worklog entry
<ctrl+s> Update active worklog entry (when tracking active), or add
manual worklog entry (when not tracking)
<ctrl+t> Go to currently tracked item
<ctrl+x> Discard currently active recording
<ctrl+b> Open issue in browser
Expand Down
2 changes: 2 additions & 0 deletions internal/ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
issueListView stateView = iota
worklogView
syncedWorklogView
editActiveWLView
askForCommentView
manualWorklogEntryView
helpView
Expand Down Expand Up @@ -84,6 +85,7 @@ type Model struct {
syncedWorklogList list.Model
activeIssueBeginTS time.Time
activeIssueEndTS time.Time
activeIssueComment *string
trackingInputs []textinput.Model
trackingFocussedField trackingFocussedField
helpVP viewport.Model
Expand Down
7 changes: 7 additions & 0 deletions internal/ui/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ type activeIssueSwitchedMsg struct {
err error
}

type activeWLUpdatedMsg struct {
beginTS time.Time
comment *string
err error
}

type manualEntryInserted struct {
issueKey string
err error
Expand All @@ -39,6 +45,7 @@ type manualEntryUpdated struct {
type fetchActiveMsg struct {
activeIssue string
beginTs time.Time
comment *string
err error
}

Expand Down
9 changes: 9 additions & 0 deletions internal/ui/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ var (
Bold(true).
Background(lipgloss.Color(toolNameColor))

baseHeadingStyle = lipgloss.NewStyle().
Bold(true).
PaddingLeft(1).
PaddingRight(1).
Foreground(lipgloss.Color(c.DefaultBackgroundColor))

workLogEntryHeadingStyle = baseHeadingStyle.
Background(lipgloss.Color(worklogListColor))

formContextStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color(formContextColor))

Expand Down
Loading
Loading