Skip to content

Commit

Permalink
v1.1.0 add validation of records
Browse files Browse the repository at this point in the history
  • Loading branch information
nixargh committed Jan 14, 2024
1 parent ea2f339 commit 0fdb7c5
Show file tree
Hide file tree
Showing 7 changed files with 402 additions and 237 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
# compiled binary
tired

# test data
test_data/*

# vim
*.sw?
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.1.0] - 2024-01-14
### Changed
- Split code onto files.

### Added
- Records validation.
- Records number.

## [1.0.2] - 2023-08-03
### Fixed
- Skip unfinished lines.
Expand Down
69 changes: 69 additions & 0 deletions file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

log "github.com/sirupsen/logrus"
)

var marker string = ">>> TIRED <<<"

func readTimesheet(path string) []string {
clog.WithFields(log.Fields{"path": path}).Info("Reading timesheet.")

absPath, _ := filepath.Abs(path)
content, err := ioutil.ReadFile(absPath)

if err != nil {
clog.WithFields(log.Fields{"error": err}).Fatal("Failed to read timesheet.")
}

timesheet := strings.Split(string(content), "\n")

return timesheet
}

func writeTimesheet(path string, content []string) {
clog.WithFields(log.Fields{"path": path}).Info("Updating timesheet file.")

absPath, _ := filepath.Abs(path)
bkpPath := fmt.Sprintf("%s.bak", absPath)

// Move old file as backup
os.Rename(absPath, bkpPath)

// create the file
f, err := os.Create(absPath)
if err != nil {
clog.WithFields(log.Fields{"error": err}).Fatal("Failed to create the file.")
}

defer f.Close()

// write a string
for i, line := range content {
// Remove marker
if line == marker {
continue
}

_, lErr := f.WriteString(line + "\n")

if lErr != nil {
clog.WithFields(log.Fields{
"number": i,
"error": lErr,
}).Fatal("Failed to write the line.")
}
}

// Re-add marker to the end of file
_, lErr := f.WriteString(marker)
if lErr != nil {
clog.WithFields(log.Fields{"error": lErr}).Fatal("Failed to write the marker.")
}
}
48 changes: 48 additions & 0 deletions jira.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"strings"

jira "github.com/andygrunwald/go-jira"
log "github.com/sirupsen/logrus"
)

func sendWorkRecords(jiraClient *jira.Client, records []WorkRecord, dry bool) int {
errCount := 0

clog.Info("Sending work records to Jira.")

for _, record := range records {
// Check the issue exists
issue, _, err := jiraClient.Issue.Get(record.Issue, nil)

if err != nil {
errShort := strings.Split(err.Error(), ":")[0]
clog.WithFields(log.Fields{"issue": record.Issue, "error": errShort}).Error("Can't get access to the issue.")
errCount++
continue
}
clog.WithFields(log.Fields{"issue": record.Issue, "worklog_total": issue.Fields.Worklog.Total, "summary": issue.Fields.Summary}).Debug("Issue found.")

// Send work log
jiraStartTime := jira.Time(record.ParsedStartTime)

workRec := jira.WorklogRecord{
Comment: record.Comment,
Started: &jiraStartTime,
TimeSpentSeconds: record.Duration,
}

if dry == false {
_, _, aErr := jiraClient.Issue.AddWorklogRecord(record.Issue, &workRec)
if err != nil {
clog.WithFields(log.Fields{"issue": record.Issue, "error": aErr}).Error("Failed to add Work Record to the issue.")
errCount++
continue
}
}
clog.WithFields(log.Fields{"dry": dry, "issue": record.Issue, "worklog_total": issue.Fields.Worklog.Total, "summary": issue.Fields.Summary}).Info("Work Record added.")
}

return errCount
}
Loading

0 comments on commit 0fdb7c5

Please sign in to comment.