-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
402 additions
and
237 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -17,5 +17,8 @@ | |
# compiled binary | ||
tired | ||
|
||
# test data | ||
test_data/* | ||
|
||
# vim | ||
*.sw? |
This file contains 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 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,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.") | ||
} | ||
} |
This file contains 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,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 | ||
} |
Oops, something went wrong.