Skip to content
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

build/

config.yaml

# Dependency directories (remove the comment below to include it)
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,11 @@ toggl-sync sync -period 1d --service
```

Where `--service` allows program to work as a daemon, it will repeatedly sync time entries every 1 day (`1d`).

## Development

### Build proto

```shell
protoc -I proto/ proto/*.proto --go_out=plugins=grpc:.
```
46 changes: 46 additions & 0 deletions cmd/plugins/jira/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module auth

go 1.19

replace godep.io/timemate => ./../../..

require (
github.com/andygrunwald/go-jira v1.16.0
github.com/hashicorp/go-plugin v1.4.9
godep.io/timemate v0.33.0
)

require (
github.com/fatih/color v1.9.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/go-hclog v0.14.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mitchellh/go-testing-interface v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.4.2 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.9.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/trivago/tgo v1.0.7 // indirect
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420 // indirect
golang.org/x/sys v0.0.0-20220330033206-e17cdc41300f // indirect
golang.org/x/text v0.3.6 // indirect
google.golang.org/genproto v0.0.0-20210828152312-66f60bf46e71 // indirect
google.golang.org/grpc v1.40.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
687 changes: 687 additions & 0 deletions cmd/plugins/jira/go.sum

Large diffs are not rendered by default.

102 changes: 102 additions & 0 deletions cmd/plugins/jira/impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package main

import (
"errors"
"log"

"godep.io/timemate/pkg/config"
"godep.io/timemate/pkg/task_tracker"
"godep.io/timemate/pkg/time_tracker"

"github.com/andygrunwald/go-jira"
)

type JiraTracker struct {
api *jira.Client
login string
}

func NewJiraTracker(config config.PluginConfig) (*JiraTracker, error) {
login := config.Config["login"]
token := config.Config["token"]
url := config.Config["url"]
tp := jira.BasicAuthTransport{
Username: login,
Password: token,
}
client, err := jira.NewClient(tp.Client(), url)
if err != nil {
return nil, err
}
return &JiraTracker{
api: client,
login: login,
}, nil
}

func findWorklog(worklogs []jira.WorklogRecord, entry time_tracker.ITimeEntry) *jira.WorklogRecord {
if worklogs == nil || len(worklogs) == 0 {
return nil
}
for _, w := range worklogs {
if w.Properties == nil || len(w.Properties) == 0 {
continue
}
for _, p := range w.Properties {
if v, ok := (p.Value).(map[string]interface{}); ok && p.Key == entry.GetSource() && v[entry.GetSource()] == entry.GetId() {
return &w
}
}
}
return nil
}

func (ji *JiraTracker) GetTasks(ids []string) ([]task_tracker.ITask, error) {
return nil, errors.New("not implemented")
}

func (ji *JiraTracker) UpdateTasks(tasks []task_tracker.ITask) (err error) {
log.Printf("UpdateTasks: %v", tasks)
for _, t := range tasks {
log.Printf("Processing toggl task %s\n", t.GetId())
worklog, _, err := ji.api.Issue.GetWorklogs(t.GetId(), jira.WithQueryOptions(&jira.AddWorklogQueryOptions{
Expand: "properties",
}))
if err != nil {
log.Printf("Error occured: %s\n", err)
continue
}
for _, e := range t.GetEntries() {
w := findWorklog(worklog.Worklogs, e)
st := jira.Time(e.GetStart())
diff := e.GetStop().Sub(e.GetStart())
// jira allows to save the minimum of 1m
secondsToSave := int(diff.Seconds()) - (int(diff.Seconds()) % 60)
// do not perform update if we have the same values for time/description
if w != nil && w.Comment == e.GetDescription() && secondsToSave == w.TimeSpentSeconds {
log.Printf("Time entry \"%s\" of %s for task %s is unchanged. Skipping update...\n", e.GetDescription(), diff, t.GetId())
continue
}
record := &jira.WorklogRecord{
Comment: e.GetDescription(),
Started: &st,
TimeSpentSeconds: secondsToSave,
Properties: []jira.EntityProperty{
{
Key: e.GetSource(),
Value: map[string]interface{}{
e.GetSource(): e.GetId(),
},
},
},
}
if w != nil {
_, _, err = ji.api.Issue.UpdateWorklogRecord(t.GetId(), w.ID, record)
} else {
_, _, err = ji.api.Issue.AddWorklogRecord(t.GetId(), record)
}
log.Printf("Synchronized time entry \"%s\" of %s for task %s\n", e.GetDescription(), diff, t.GetId())
}
}
return err
}
44 changes: 44 additions & 0 deletions cmd/plugins/jira/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"encoding/gob"
"time"

plugConfig "godep.io/timemate/pkg/config"
pkgPlugin "godep.io/timemate/pkg/plugin"
"godep.io/timemate/pkg/task_tracker"

"github.com/hashicorp/go-plugin"
)

func init() {
gob.Register(time.Time{})
gob.Register(task_tracker.Task{})
gob.Register(task_tracker.Project{})
}

const pluginName = "jira"

func main() {
config, err := plugConfig.ReadConfig()
if err != nil {
panic(err)
}
pluginConfig := config.FindPlugin(pluginName)
if pluginConfig == nil {
panic("No plugin configuration found")
}
impl, err := NewJiraTracker(*pluginConfig)
if err != nil {
panic(err)
}
m, err := pkgPlugin.GetPluginMap(config, pluginName, true, impl)
if err != nil {
panic("No plugin configuration found")
}
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: pkgPlugin.Handshake,
Plugins: m,
GRPCServer: plugin.DefaultGRPCServer,
})
}
42 changes: 42 additions & 0 deletions cmd/plugins/toggl/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module auth

go 1.19

replace godep.io/timemate => ./../../..

require (
github.com/andreaskoch/togglapi v0.4.2
github.com/hashicorp/go-plugin v1.4.9
godep.io/timemate v0.33.0
)

require (
github.com/fatih/color v1.9.0 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/go-hclog v0.14.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mitchellh/go-testing-interface v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.4.2 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.9.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420 // indirect
golang.org/x/sys v0.0.0-20220330033206-e17cdc41300f // indirect
golang.org/x/text v0.3.6 // indirect
google.golang.org/genproto v0.0.0-20210828152312-66f60bf46e71 // indirect
google.golang.org/grpc v1.40.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading