Skip to content

Commit

Permalink
Add user to id mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
berlam committed Oct 4, 2019
1 parent d39bfee commit e5e2683
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 22 deletions.
4 changes: 2 additions & 2 deletions pkg/jira/cloud/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func (api Api) Me() (model.Account, *time.Location, error) {
return api.previousVersion().Me()
}

func (api Api) User(user *pkg.User, projects []pkg.Project) (model.Account, *time.Location, error) {
return api.previousVersion().User(user, projects)
func (api Api) User(user *pkg.User) (model.Account, *time.Location, error) {
return api.previousVersion().User(user)
}

func (api Api) Issues(jql model.Jql, startAt int) ([]model.Issue, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/jira/jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func GetBulkTimesheet(client *http.Client, server *url.URL, userinfo *url.Userin
return pkg.Timesheet{}
}

accounts, err := accounts(api, projects, users)
accounts, err := accounts(api, users)
if err != nil {
log.Println("Could not get user.", err)
return pkg.Timesheet{}
Expand Down Expand Up @@ -158,15 +158,15 @@ func do(api model.Api, year int, month time.Month, projects []pkg.Project, accou
return timesheet
}

func accounts(api model.Api, projects []pkg.Project, users []*pkg.User) (map[model.Account]*pkg.User, error) {
func accounts(api model.Api, users []*pkg.User) (map[model.Account]*pkg.User, error) {
result := make(map[model.Account]*pkg.User, len(users))
c := make(chan error)
var wg sync.WaitGroup
wg.Add(len(users))
for _, user := range users {
go func(user *pkg.User) {
defer wg.Done()
account, location, err := api.User(user, projects)
account, location, err := api.User(user)
if err != nil {
c <- err
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/jira/model/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Api interface {

type UserAccessor interface {
Me() (Account, *time.Location, error)
User(user *pkg.User, projects []pkg.Project) (Account, *time.Location, error)
User(user *pkg.User) (Account, *time.Location, error)
}

type IssueAccessor interface {
Expand Down
37 changes: 22 additions & 15 deletions pkg/jira/v2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ import (
"net/http"
"net/url"
"strconv"
"strings"
"time"
)

const (
BasePath = "/rest/api/2/"
myselfUrl = "myself"
searchUserUrl = "user/search?query=%s&maxResults=2"
searchProjectUserUrl = "user/assignable/multiProjectSearch?projectKeys=%s&query=%s&maxResults=2"
searchIssueUrl = "search"
worklogUrl = "issue/%s/worklog?startAt=%s"
BasePath = "/rest/api/2/"
myselfUrl = "myself"
getUserUrl = "user?accountId=%s"
searchUserUrl = "user/search?query=%s&maxResults=2"
searchIssueUrl = "search"
worklogUrl = "issue/%s/worklog?startAt=%s"
)

type Api struct {
Expand Down Expand Up @@ -61,15 +60,14 @@ func (api Api) Me() (model.Account, *time.Location, error) {
return result.AccountId, result.Location(), nil
}

func (api Api) User(user *pkg.User, projects []pkg.Project) (model.Account, *time.Location, error) {
userUrl, err := api.Server.Parse(fmt.Sprintf(searchUserUrl, url.QueryEscape(user.DisplayName)))
if len(projects) > 0 {
projectQueryPart := make([]string, len(projects))
for i, project := range projects {
projectQueryPart[i] = string(project)
}
userUrl, err = api.Server.Parse(fmt.Sprintf(searchProjectUserUrl, strings.Join(projectQueryPart, ","), url.QueryEscape(user.DisplayName)))
func (api Api) User(user *pkg.User) (model.Account, *time.Location, error) {
searchUrl := searchUserUrl
searchPart := user.DisplayName
if user.Id != "" {
searchUrl = getUserUrl
searchPart = user.Id
}
userUrl, err := api.Server.Parse(fmt.Sprintf(searchUrl, url.QueryEscape(searchPart)))
response, err := pkg.CreateJsonRequest(api.Client, http.MethodGet, userUrl, api.Userinfo, nil)
if err != nil {
return "", nil, err
Expand All @@ -90,6 +88,15 @@ func (api Api) User(user *pkg.User, projects []pkg.Project) (model.Account, *tim
return "", nil, fmt.Errorf(response.Status)
}

if user.Id != "" {
var result userQueryResult
err = json.Unmarshal(data, &result)
if err != nil {
return "", nil, err
}
return result.AccountId, result.Location(), nil
}

var result = make([]userQueryResult, 0, 2)
err = json.Unmarshal(data, &result)
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion pkg/timesheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Effort struct {

type User struct {
DisplayName string
Id string
TimeZone *time.Location
}

Expand All @@ -34,8 +35,14 @@ func Projects(projects []string) []Project {
func Users(users []string) []*User {
result := make([]*User, len(users))
for i, user := range users {
parts := strings.SplitN(user, "=", 2)
id := ""
if len(parts) == 2 {
id = parts[1]
}
result[i] = &User{
DisplayName: user,
DisplayName: parts[0],
Id: id,
}
}
return result
Expand Down

0 comments on commit e5e2683

Please sign in to comment.