Skip to content

Commit

Permalink
refactor(items): rename every occurance of item to entry
Browse files Browse the repository at this point in the history
  • Loading branch information
gabor-boros committed Oct 8, 2021
1 parent 000a6b7 commit 38f37ab
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 45 deletions.
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ func runRootCmd(_ *cobra.Command, _ []string) {
return incompleteEntries[i].Task.Name < incompleteEntries[j].Task.Name
})

printEntries("Incomplete items", incompleteEntries)
printEntries("Complete items", completeEntries)
printEntries("Incomplete entries", incompleteEntries)
printEntries("Complete entries", completeEntries)

if strings.ToLower(prompt("Continue? [y/n]")) != "y" {
fmt.Println("User interruption. Aborting.")
Expand Down
6 changes: 3 additions & 3 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func printEntries(header string, entries []worklog.Entry) {
fmt.Printf("%s:\n", header)

if len(entries) == 0 {
fmt.Printf("No items found.\n\n")
fmt.Printf("No entries found.\n\n")
return
}

Expand Down Expand Up @@ -64,9 +64,9 @@ func prompt(message string) string {
return strings.TrimSpace(input)
}

func isSliceContains(item string, slice []string) bool {
func isSliceContains(entry string, slice []string) bool {
for _, s := range slice {
if s == item {
if s == entry {
return true
}
}
Expand Down
12 changes: 6 additions & 6 deletions internal/pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ type FetchOpts struct {
End time.Time
}

// Fetcher specifies the functions used to fetch worklog items.
// Fetcher specifies the functions used to fetch worklog entries.
type Fetcher interface {
// FetchEntries from a given source and return the list of worklog items
// If the fetching resulted in an error, the list of worklog items will be
// FetchEntries from a given source and return the list of worklog entries
// If the fetching resulted in an error, the list of worklog entries will be
// nil and an error will return.
FetchEntries(ctx context.Context, opts *FetchOpts) ([]worklog.Entry, error)
}
Expand All @@ -97,16 +97,16 @@ type UploadOpts struct {
User string
}

// Uploader specifies the functions used to upload worklog items.
// Uploader specifies the functions used to upload worklog entries.
type Uploader interface {
// UploadEntries to a given target.
// If the upload resulted in an error, the upload will stop and an error
// will return.
UploadEntries(ctx context.Context, items []worklog.Entry, opts *UploadOpts) error
UploadEntries(ctx context.Context, entries []worklog.Entry, opts *UploadOpts) error
}

// FetchUploader is the combination of Fetcher and Uploader.
// The FetchUploader can to fetch items from and upload to a given resource.
// The FetchUploader can to fetch entries from and upload to a given resource.
type FetchUploader interface {
Fetcher
Uploader
Expand Down
26 changes: 13 additions & 13 deletions internal/pkg/client/clockify/clockify.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ func (c *clockifyClient) splitEntry(entry FetchEntry, bd time.Duration, ubd time
}
}

var items []worklog.Entry
var entries []worklog.Entry
totalTasks := len(tasks)

for taskID, taskName := range tasks {
splitBillableDuration := time.Duration(math.Round(float64(bd.Nanoseconds()) / float64(totalTasks)))
splitUnbillableDuration := time.Duration(math.Round(float64(ubd.Nanoseconds()) / float64(totalTasks)))

items = append(items, worklog.Entry{
entries = append(entries, worklog.Entry{
Client: worklog.IDNameField{
ID: entry.Project.ClientID,
Name: entry.Project.ClientName,
Expand All @@ -143,15 +143,15 @@ func (c *clockifyClient) splitEntry(entry FetchEntry, bd time.Duration, ubd time
})
}

return &items, nil
return &entries, nil
}

func (c *clockifyClient) FetchEntries(ctx context.Context, opts *client.FetchOpts) ([]worklog.Entry, error) {
var items []worklog.Entry
var entries []worklog.Entry
currentPage := 1
pageSize := 100

// Naive pagination as the API does not return the number of total items
// Naive pagination as the API does not return the number of total entries
for currentPage*pageSize < MaxPageLength {
searchParams := &WorklogSearchParams{
Start: opts.Start.Format(DateFormat),
Expand All @@ -172,17 +172,17 @@ func (c *clockifyClient) FetchEntries(ctx context.Context, opts *client.FetchOpt
return nil, err
}

var entries []FetchEntry
if err = json.NewDecoder(resp.Body).Decode(&entries); err != nil {
var fetchedEntries []FetchEntry
if err = json.NewDecoder(resp.Body).Decode(&fetchedEntries); err != nil {
return nil, err
}

// The API returned no entries, meaning no entries left
if len(entries) == 0 {
if len(fetchedEntries) == 0 {
break
}

for _, entry := range entries {
for _, entry := range fetchedEntries {
billableDuration := entry.TimeInterval.End.Sub(entry.TimeInterval.Start)
unbillableDuration := time.Duration(0)

Expand All @@ -192,14 +192,14 @@ func (c *clockifyClient) FetchEntries(ctx context.Context, opts *client.FetchOpt
}

if c.opts.TasksAsTags && len(entry.Tags) > 0 {
pageItems, err := c.splitEntry(entry, billableDuration, unbillableDuration)
pageEntries, err := c.splitEntry(entry, billableDuration, unbillableDuration)
if err != nil {
return nil, err
}

items = append(items, *pageItems...)
entries = append(entries, *pageEntries...)
} else {
items = append(items, worklog.Entry{
entries = append(entries, worklog.Entry{
Client: worklog.IDNameField{
ID: entry.Project.ClientID,
Name: entry.Project.ClientName,
Expand All @@ -224,7 +224,7 @@ func (c *clockifyClient) FetchEntries(ctx context.Context, opts *client.FetchOpt
currentPage++
}

return items, nil
return entries, nil
}

// NewClient returns a new Clockify client.
Expand Down
38 changes: 19 additions & 19 deletions internal/pkg/client/tempo/tempo.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ func (c *tempoClient) FetchEntries(ctx context.Context, opts *client.FetchOpts)
return nil, fmt.Errorf("%v: %v", client.ErrFetchEntries, err)
}

var entries []FetchEntry
if err = json.NewDecoder(resp.Body).Decode(&entries); err != nil {
var fetchedEntries []FetchEntry
if err = json.NewDecoder(resp.Body).Decode(&fetchedEntries); err != nil {
return nil, fmt.Errorf("%v: %v", client.ErrFetchEntries, err)
}

var items []worklog.Entry
for _, entry := range entries {
items = append(items, worklog.Entry{
var entries []worklog.Entry
for _, entry := range fetchedEntries {
entries = append(entries, worklog.Entry{
Client: worklog.IDNameField{
ID: entry.Issue.AccountKey,
Name: entry.Issue.AccountKey,
Expand All @@ -111,16 +111,16 @@ func (c *tempoClient) FetchEntries(ctx context.Context, opts *client.FetchOpts)
})
}

return items, nil
return entries, nil
}

func (c *tempoClient) uploadEntry(ctx context.Context, item worklog.Entry, opts *client.UploadOpts, errChan chan error) {
billableDuration := item.BillableDuration
unbillableDuration := item.UnbillableDuration
func (c *tempoClient) uploadEntry(ctx context.Context, entry worklog.Entry, opts *client.UploadOpts, errChan chan error) {
billableDuration := entry.BillableDuration
unbillableDuration := entry.UnbillableDuration
totalTimeSpent := billableDuration + unbillableDuration

if opts.TreatDurationAsBilled {
billableDuration = item.UnbillableDuration + item.BillableDuration
billableDuration = entry.UnbillableDuration + entry.BillableDuration
unbillableDuration = 0
}

Expand All @@ -130,32 +130,32 @@ func (c *tempoClient) uploadEntry(ctx context.Context, item worklog.Entry, opts
totalTimeSpent = billableDuration + unbillableDuration
}

entry := &UploadEntry{
Comment: item.Summary,
uploadEntry := &UploadEntry{
Comment: entry.Summary,
IncludeNonWorkingDays: true,
OriginTaskID: item.Task.Name,
Started: item.Start.Local().Format("2006-01-02"),
OriginTaskID: entry.Task.Name,
Started: entry.Start.Local().Format("2006-01-02"),
BillableSeconds: int(billableDuration.Seconds()),
TimeSpentSeconds: int(totalTimeSpent.Seconds()),
Worker: opts.User,
}

if _, err := client.SendRequest(ctx, http.MethodPost, PathWorklogCreate, entry, &c.opts.HTTPClientOptions); err != nil {
if _, err := client.SendRequest(ctx, http.MethodPost, PathWorklogCreate, uploadEntry, &c.opts.HTTPClientOptions); err != nil {
errChan <- err
return
}

errChan <- nil
}

func (c *tempoClient) UploadEntries(ctx context.Context, items []worklog.Entry, opts *client.UploadOpts) error {
func (c *tempoClient) UploadEntries(ctx context.Context, entries []worklog.Entry, opts *client.UploadOpts) error {
errChan := make(chan error)

for _, item := range items {
go c.uploadEntry(ctx, item, opts, errChan)
for _, entry := range entries {
go c.uploadEntry(ctx, entry, opts, errChan)
}

for i := 0; i < len(items); i++ {
for i := 0; i < len(entries); i++ {
if err := <-errChan; err != nil {
return fmt.Errorf("%v: %v", client.ErrUploadEntries, err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/worklog/worklog.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func groupEntries(entries []Entry) []Entry {
}

groupedEntries := make([]Entry, 0, len(entryGroup))
for _, item := range entryGroup {
groupedEntries = append(groupedEntries, item)
for _, entry := range entryGroup {
groupedEntries = append(groupedEntries, entry)
}

return groupedEntries
Expand Down

0 comments on commit 38f37ab

Please sign in to comment.