Skip to content

Commit

Permalink
Remove project query
Browse files Browse the repository at this point in the history
  • Loading branch information
berlam committed Oct 4, 2019
1 parent 40246af commit b3653d4
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 79 deletions.
7 changes: 1 addition & 6 deletions pkg/jira/cloud/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
)

const (
BasePath = "/rest/api/3/"
searchProjectUrl = "project/search"
BasePath = "/rest/api/3/"
)

type Api struct {
Expand All @@ -32,10 +31,6 @@ func (api Api) previousVersion() *v2.Api {
return api.v2
}

func (api Api) Projects(startAt int) ([]pkg.Project, error) {
return api.previousVersion().Projects(startAt)
}

func (api Api) Me() (model.Account, *time.Location, error) {
return api.previousVersion().Me()
}
Expand Down
8 changes: 0 additions & 8 deletions pkg/jira/jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,6 @@ func do(api model.Api, year int, month time.Month, projects []pkg.Project, accou
// The jql query uses afaik the time zone of the requesting user.
fromDate, toDate := pkg.GetTimeRange(year, month)

if projects == nil || len(projects) == 0 {
projects, err = api.Projects(0)
if err != nil {
log.Println("Could not get projects.", err)
return pkg.Timesheet{}
}
}

i := 0
accountIds := make([]model.Account, len(accounts))
for account := range accounts {
Expand Down
5 changes: 0 additions & 5 deletions pkg/jira/model/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@ type Account string
type IssueKey string

type Api interface {
ProjectAccessor
UserAccessor
IssueAccessor
WorklogAccessor
}

type ProjectAccessor interface {
Projects(startAt int) ([]pkg.Project, error)
}

type UserAccessor interface {
Me() (Account, *time.Location, error)
User(user *pkg.User, projects []pkg.Project) (Account, *time.Location, error)
Expand Down
4 changes: 2 additions & 2 deletions pkg/jira/model/jql.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
type Jql []string

func (query Jql) Projects(projects ...pkg.Project) Jql {
if projects == nil || len(projects) == 0 {
if len(projects) == 0 {
return query
}
result := make([]string, len(projects))
Expand All @@ -27,7 +27,7 @@ func (query Jql) Projects(projects ...pkg.Project) Jql {
}

func (query Jql) Users(users ...Account) Jql {
if users == nil || len(users) == 0 {
if len(users) == 0 {
return query
}
result := make([]string, len(users))
Expand Down
64 changes: 13 additions & 51 deletions pkg/jira/v2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import (
)

const (
BasePath = "/rest/api/2/"
myselfUrl = "myself"
searchProjectUrl = "project/search?startAt=%s"
searchUserUrl = "user/assignable/multiProjectSearch?projectKeys=%s&query=%s&maxResults=2"
searchIssueUrl = "search"
worklogUrl = "issue/%s/worklog?startAt=%s"
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"
)

type Api struct {
Expand All @@ -31,47 +31,6 @@ type Api struct {
Userinfo *url.Userinfo
}

func (api Api) Projects(startAt int) ([]pkg.Project, error) {
projectUrl, err := api.Server.Parse(fmt.Sprintf(searchProjectUrl, strconv.Itoa(startAt)))
response, err := pkg.CreateJsonRequest(api.Client, http.MethodGet, projectUrl, api.Userinfo, nil)
if err != nil {
return nil, err
}
defer func() {
err := response.Body.Close()
if err != nil {
log.Println("Response could not be closed.", err)
}
}()

reader, _ := charset.NewReader(response.Body, response.Header.Get("Content-Type"))
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
if response.StatusCode != 200 {
return nil, fmt.Errorf(response.Status)
}

var result = projectQueryResult{}
err = json.Unmarshal(data, &result)
if err != nil {
return nil, err
}
projectKeys := make([]pkg.Project, 0, result.Total)
for _, project := range result.Values {
projectKeys = append(projectKeys, pkg.Project(project.ProjectKey))
}
if (result.IsLast == nil && result.Total >= startAt+result.MaxResults) || (result.IsLast != nil && !*result.IsLast) {
nextProjectKeys, err := api.Projects(startAt + result.MaxResults)
if err != nil {
return nil, err
}
projectKeys = append(projectKeys, nextProjectKeys...)
}
return projectKeys, nil
}

func (api Api) Me() (model.Account, *time.Location, error) {
myselfUrl, err := api.Server.Parse(myselfUrl)
response, err := pkg.CreateJsonRequest(api.Client, http.MethodGet, myselfUrl, api.Userinfo, nil)
Expand Down Expand Up @@ -103,11 +62,14 @@ func (api Api) Me() (model.Account, *time.Location, error) {
}

func (api Api) User(user *pkg.User, projects []pkg.Project) (model.Account, *time.Location, error) {
projectQueryPart := make([]string, len(projects))
for i, project := range projects {
projectQueryPart[i] = string(project)
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)))
}
userUrl, err := api.Server.Parse(fmt.Sprintf(searchUserUrl, strings.Join(projectQueryPart, ","), url.QueryEscape(user.DisplayName)))
response, err := pkg.CreateJsonRequest(api.Client, http.MethodGet, userUrl, api.Userinfo, nil)
if err != nil {
return "", nil, err
Expand Down
7 changes: 0 additions & 7 deletions pkg/jira/v2/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ type PaginatedResult struct {
IsLast *bool `json:"isLast,omitempty"`
}

type projectQueryResult struct {
*PaginatedResult
Values []struct {
ProjectKey projectKey `json:"key"`
} `json:"values"`
}

type userQueryResult struct {
AccountId model.Account `json:"accountId"`
DisplayName string `json:"displayName"`
Expand Down

0 comments on commit b3653d4

Please sign in to comment.