Skip to content

Commit

Permalink
Add API endpoint and interfaces for Jira
Browse files Browse the repository at this point in the history
  • Loading branch information
berlam committed Oct 4, 2019
1 parent 1306d34 commit ac177f6
Show file tree
Hide file tree
Showing 9 changed files with 482 additions and 343 deletions.
12 changes: 6 additions & 6 deletions cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ var showBcsCmd = &cobra.Command{
return nil
},
Run: func(cmd *cobra.Command, args []string) {
if internal.Config.Projects == nil {
if internal.Config.Projects == nil || len(internal.Config.Projects) == 0 {
bcs.GetTimesheet(
pkg.NewHttpClient(),
internal.Config.Server(),
Expand All @@ -81,7 +81,7 @@ var showBcsCmd = &cobra.Command{
internal.Config.Userinfo(),
internal.Config.Year,
time.Month(internal.Config.Month),
internal.Config.Projects[0],
pkg.Project(internal.Config.Projects[0]),
internal.Config.Report,
).Print(os.Stdout, true, internal.Config.Summarize, internal.Config.PrintEmptyLine)
}
Expand All @@ -94,14 +94,14 @@ var showJiraCmd = &cobra.Command{
Long: "Show your worklog data from Atlassian Jira.",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if internal.Config.Users == nil {
if internal.Config.Users == nil || len(internal.Config.Users) == 0 {
jira.GetTimesheet(
pkg.NewHttpClient(),
internal.Config.Server(),
internal.Config.Userinfo(),
internal.Config.Year,
time.Month(internal.Config.Month),
internal.Config.Projects,
internal.Projects(internal.Config.Projects),
).Print(os.Stdout, false, internal.Config.Summarize, internal.Config.PrintEmptyLine)
} else {
jira.GetBulkTimesheet(
Expand All @@ -110,8 +110,8 @@ var showJiraCmd = &cobra.Command{
internal.Config.Userinfo(),
internal.Config.Year,
time.Month(internal.Config.Month),
internal.Config.Projects,
internal.Config.Users,
internal.Projects(internal.Config.Projects),
internal.Users(internal.Config.Users),
).Print(os.Stdout, true, internal.Config.Summarize, internal.Config.PrintEmptyLine)
}
},
Expand Down
21 changes: 21 additions & 0 deletions internal/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"eager/pkg"
"net/url"
)

Expand All @@ -21,6 +22,22 @@ const (
FlagForce = "force"
)

func Projects(projects []string) []pkg.Project {
result := make([]pkg.Project, len(projects))
for i, project := range projects {
result[i] = pkg.Project(project)
}
return result
}

func Users(users []string) []pkg.User {
result := make([]pkg.User, len(users))
for i, user := range users {
result[i] = pkg.User(user)
}
return result
}

type Configuration struct {
Insecure bool `mapstructure:"insecure"`
Host string `mapstructure:"host"`
Expand Down Expand Up @@ -57,3 +74,7 @@ func (c *Configuration) Userinfo() *url.Userinfo {
}
return nil
}

func (c *Configuration) UsersArray() {

}
10 changes: 5 additions & 5 deletions pkg/bcs/bcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func GetTimesheet(client *http.Client, server *url.URL, userinfo *url.Userinfo,
return timesheet
}

func GetBulkTimesheet(client *http.Client, server *url.URL, userinfo *url.Userinfo, year int, month time.Month, project, report string) pkg.Timesheet {
func GetBulkTimesheet(client *http.Client, server *url.URL, userinfo *url.Userinfo, year int, month time.Month, project pkg.Project, report string) pkg.Timesheet {
client.Jar, _ = cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List,
})
Expand All @@ -78,7 +78,7 @@ func GetBulkTimesheet(client *http.Client, server *url.URL, userinfo *url.Userin
}
}()

err = showProjectEffortList(client, server, url.QueryEscape(report), month, year, url.QueryEscape(project))
err = showProjectEffortList(client, server, url.QueryEscape(report), month, year, project)
if err != nil {
log.Println("Could not show effort list.", err)
return pkg.Timesheet{}
Expand All @@ -91,7 +91,7 @@ func GetBulkTimesheet(client *http.Client, server *url.URL, userinfo *url.Userin
}

timesheet := pkg.Timesheet{}
spec := pkg.NewCsvSpecification().Header(true).Employee(true).Project(true).Task(true).Description(true).Date(true).Duration(true).Skip()
spec := pkg.NewCsvSpecification().Header(true).User(true).Project(true).Task(true).Description(true).Date(true).Duration(true).Skip()
timesheet, err = timesheet.ReadCsv(data, &spec)
if err != nil {
log.Println("Could not read effort list.", err)
Expand Down Expand Up @@ -161,8 +161,8 @@ func showEffortList(client *http.Client, server *url.URL, report string, month t
return nil
}

func showProjectEffortList(client *http.Client, server *url.URL, report string, month time.Month, year int, project string) error {
showEffortUrl, _ := server.Parse(fmt.Sprintf(bcsShowProjectEffort, project, report, strconv.Itoa(int(month)), strconv.Itoa(year)))
func showProjectEffortList(client *http.Client, server *url.URL, report string, month time.Month, year int, project pkg.Project) error {
showEffortUrl, _ := server.Parse(fmt.Sprintf(bcsShowProjectEffort, url.QueryEscape(string(project)), report, strconv.Itoa(int(month)), strconv.Itoa(year)))
resp, err := client.Get(showEffortUrl.String())
if err != nil {
return err
Expand Down
26 changes: 13 additions & 13 deletions pkg/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
type CsvSpecification struct {
fields int
header bool
employee *CsvProperty
user *CsvProperty
project *CsvProperty
task *CsvProperty
description *CsvProperty
Expand All @@ -29,7 +29,7 @@ func NewCsvSpecification() CsvSpecification {
return CsvSpecification{
fields: 0,
header: false,
employee: newCsvProperty(),
user: newCsvProperty(),
project: newCsvProperty(),
task: newCsvProperty(),
description: newCsvProperty(),
Expand All @@ -55,9 +55,9 @@ func (spec CsvSpecification) Skip() CsvSpecification {
return spec
}

func (spec CsvSpecification) Employee(enable bool) CsvSpecification {
func (spec CsvSpecification) User(enable bool) CsvSpecification {
if enable {
spec.addField(spec.employee)
spec.addField(spec.user)
}
return spec
}
Expand Down Expand Up @@ -130,8 +130,8 @@ func (ts Timesheet) ReadCsv(data []byte, spec *CsvSpecification) (Timesheet, err
}

effort := Effort{}
if spec.employee.enabled {
effort.Employee = Employee(row[spec.employee.index])
if spec.user.enabled {
effort.User = User(row[spec.user.index])
}
if spec.project.enabled {
effort.Project = Project(row[spec.project.index])
Expand Down Expand Up @@ -164,8 +164,8 @@ func (ts Timesheet) WriteCsv(writer io.Writer, spec *CsvSpecification, printEmpt
var result = make([]string, spec.fields)

if spec.header {
if spec.employee.enabled {
result[spec.employee.index] = "Employee"
if spec.user.enabled {
result[spec.user.index] = "User"
}
if spec.project.enabled {
result[spec.project.index] = "Project"
Expand Down Expand Up @@ -194,9 +194,9 @@ func (ts Timesheet) WriteCsv(writer io.Writer, spec *CsvSpecification, printEmpt
for e := range result {
result[e] = ""
}
for i := int(effort.Date.Sub(date).Truncate(time.Hour*24).Hours() / 24); i > 0; i-- {
if spec.employee.enabled {
result[spec.employee.index] = string(effort.Employee)
for i := int(effort.Date.Sub(date).Truncate(time.Hour * 24).Hours() / 24); i > 0; i-- {
if spec.user.enabled {
result[spec.user.index] = string(effort.User)
}
if spec.date.enabled {
result[spec.date.index] = date.Format(IsoYearMonthDay)
Expand All @@ -215,8 +215,8 @@ func (ts Timesheet) WriteCsv(writer io.Writer, spec *CsvSpecification, printEmpt
date = date.Add(time.Hour * 24)
}

if spec.employee.enabled {
result[spec.employee.index] = string(effort.Employee)
if spec.user.enabled {
result[spec.user.index] = string(effort.User)
}
if spec.project.enabled {
result[spec.project.index] = string(effort.Project)
Expand Down
Loading

0 comments on commit ac177f6

Please sign in to comment.