forked from refiito/pipes-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
teamweek.go
137 lines (123 loc) · 3.01 KB
/
teamweek.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"code.google.com/p/goauth2/oauth"
"github.com/fdelbos/go-teamweek/teamweek"
)
type TeamweekService struct {
emptyService
workspaceID int
*TeamweekParams
token oauth.Token
}
type TeamweekParams struct {
AccountID int `json:"account_id"`
}
func (s *TeamweekService) Name() string {
return "teamweek"
}
func (s *TeamweekService) WorkspaceID() int {
return s.workspaceID
}
func (s *TeamweekService) keyFor(objectType string) string {
if s.TeamweekParams == nil {
return fmt.Sprintf("teamweek:account:%s", objectType)
}
return fmt.Sprintf("teamweek:account:%d:%s", s.AccountID, objectType)
}
func (s *TeamweekService) setParams(b []byte) error {
if err := json.Unmarshal(b, &s); err != nil {
return err
}
if s.TeamweekParams == nil || s.AccountID == 0 {
return errors.New("account_id must be present")
}
return nil
}
func (s *TeamweekService) setAuthData(b []byte) error {
if err := json.Unmarshal(b, &s.token); err != nil {
return err
}
return nil
}
func (s *TeamweekService) client() *teamweek.Client {
t := &oauth.Transport{Token: &s.token}
return teamweek.NewClient(t.Client())
}
// Map Teamweek accounts to local accounts
func (s *TeamweekService) Accounts() ([]*Account, error) {
foreignObject, err := s.client().GetUserProfile()
if err != nil {
return nil, err
}
var accounts []*Account
for _, object := range foreignObject.Workspaces {
account := Account{
ID: int(object.ID),
Name: object.Name,
}
accounts = append(accounts, &account)
}
return accounts, nil
}
// Map Teamweek people to local users
func (s *TeamweekService) Users() ([]*User, error) {
foreignObjects, err := s.client().ListWorkspaceMembers(int64(s.AccountID))
if err != nil {
return nil, err
}
var users []*User
for _, object := range foreignObjects {
if object.Dummy {
continue
}
user := User{
ForeignID: strconv.FormatInt(object.ID, 10),
Name: object.Name,
Email: object.Email,
}
users = append(users, &user)
}
return users, nil
}
// Map Teamweek projects to projects
func (s *TeamweekService) Projects() ([]*Project, error) {
foreignObjects, err := s.client().ListWorkspaceProjects(int64(s.AccountID))
if err != nil {
return nil, err
}
var projects []*Project
for _, object := range foreignObjects {
project := Project{
ForeignID: strconv.FormatInt(object.ID, 10),
Name: object.Name,
Active: true,
}
projects = append(projects, &project)
}
return projects, nil
}
// Map Teamweek tasks to tasks
func (s *TeamweekService) Tasks() ([]*Task, error) {
foreignObjects, err := s.client().ListWorkspaceTasks(int64(s.AccountID))
if err != nil {
return nil, err
}
var tasks []*Task
for _, object := range foreignObjects {
if object.Project == nil {
continue
}
task := Task{
ForeignID: strconv.FormatInt(object.ID, 10),
Name: object.Name,
Active: !object.Done,
foreignProjectID: int(object.ProjectID),
}
tasks = append(tasks, &task)
}
return tasks, nil
}