Skip to content

Commit

Permalink
fix: Respect jira timezone (#697)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitpokhrel authored Jan 9, 2024
1 parent cb134c3 commit 5eab299
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 22 deletions.
2 changes: 2 additions & 0 deletions internal/cmd/epic/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func singleEpicView(flags query.FlagParser, key, project, projectType, server st
return []string{}
}(),
TableStyle: cmdutil.GetTUIStyleConfig(),
Timezone: viper.GetString("timezone"),
},
}

Expand Down Expand Up @@ -214,6 +215,7 @@ func epicExplorerView(cmd *cobra.Command, flags query.FlagParser, project, proje
Display: view.DisplayFormat{
FixedColumns: fixedColumns,
TableStyle: cmdutil.GetTUIStyleConfig(),
Timezone: viper.GetString("timezone"),
},
}

Expand Down
1 change: 1 addition & 0 deletions internal/cmd/issue/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func loadList(cmd *cobra.Command) {
return []string{}
}(),
TableStyle: cmdutil.GetTUIStyleConfig(),
Timezone: viper.GetString("timezone"),
},
}

Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/sprint/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func singleSprintView(sprintQuery *query.Sprint, flags query.FlagParser, boardID
return []string{}
}(),
TableStyle: cmdutil.GetTUIStyleConfig(),
Timezone: viper.GetString("timezone"),
},
}

Expand Down Expand Up @@ -243,6 +244,7 @@ func sprintExplorerView(sprintQuery *query.Sprint, flags query.FlagParser, board
return []string{}
}(),
TableStyle: cmdutil.GetTUIStyleConfig(),
Timezone: viper.GetString("timezone"),
},
}

Expand Down
9 changes: 7 additions & 2 deletions internal/config/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type JiraCLIConfigGenerator struct {
mtls struct {
caCert, clientCert, clientKey string
}
timezone string
}
jiraClient *jira.Client
projectSuggestions []string
Expand Down Expand Up @@ -420,14 +421,17 @@ func (c *JiraCLIConfigGenerator) verifyLoginDetails(server, login string) error
ClientKey: c.value.mtls.clientKey,
},
})
if ret, err := c.jiraClient.Me(); err != nil {
ret, err := c.jiraClient.Me()
if err != nil {
return err
} else if c.value.authType == jira.AuthTypeBearer {
}
if c.value.authType == jira.AuthTypeBearer {
login = ret.Login
}

c.value.server = server
c.value.login = login
c.value.timezone = ret.Timezone

return nil
}
Expand Down Expand Up @@ -747,6 +751,7 @@ func (c *JiraCLIConfigGenerator) write(path string) (string, error) {
config.Set("issue.types", c.value.issueTypes)
config.Set("issue.fields.custom", c.value.customFields)
config.Set("auth_type", c.value.authType)
config.Set("timezone", c.value.timezone)

// MTLS
config.Set("mtls.ca_cert", c.value.mtls.caCert)
Expand Down
4 changes: 2 additions & 2 deletions internal/view/epic.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ func (el *EpicList) tabularize(issues []*jira.Issue) tui.TableData {
issue.Fields.Reporter.Name,
issue.Fields.Priority.Name,
issue.Fields.Resolution.Name,
formatDateTime(issue.Fields.Created, jira.RFC3339),
formatDateTime(issue.Fields.Updated, jira.RFC3339),
formatDateTime(issue.Fields.Created, jira.RFC3339, el.Display.Timezone),
formatDateTime(issue.Fields.Updated, jira.RFC3339, el.Display.Timezone),
})
}

Expand Down
11 changes: 9 additions & 2 deletions internal/view/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,19 @@ func MDRenderer() (*glamour.TermRenderer, error) {
)
}

func formatDateTime(dt, format string) string {
func formatDateTime(dt, format, tz string) string {
t, err := time.Parse(format, dt)
if err != nil {
return dt
}
return t.Format("2006-01-02 15:04:05")
if tz == "" {
return t.Format("2006-01-02 15:04:05")
}
loc, err := time.LoadLocation(tz)
if err != nil {
return dt
}
return t.In(loc).Format("2006-01-02 15:04:05")
}

func prepareTitle(text string) string {
Expand Down
17 changes: 12 additions & 5 deletions internal/view/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,38 @@ func TestFormatDateTime(t *testing.T) {
{
name: "it returns input date for invalid date input",
format: func() string {
return formatDateTime("2020-12-03 10:00:00", jira.RFC3339)
return formatDateTime("2020-12-03 10:00:00", jira.RFC3339, "UTC")
},
expected: "2020-12-03 10:00:00",
},
{
name: "it returns input date for invalid input format",
format: func() string {
return formatDateTime("2020-12-03 10:00:00", "invalid")
return formatDateTime("2020-12-03 10:00:00", "invalid", "UTC")
},
expected: "2020-12-03 10:00:00",
},
{
name: "it format input date from jira date format",
format: func() string {
return formatDateTime("2020-12-03T14:05:20.974+0100", jira.RFC3339)
return formatDateTime("2020-12-03T14:05:20.974+0100", jira.RFC3339, "UTC")
},
expected: "2020-12-03 14:05:20",
expected: "2020-12-03 13:05:20",
},
{
name: "it format input date from RFC3339 date format",
format: func() string {
return formatDateTime("2020-12-13T16:12:00.000Z", time.RFC3339)
return formatDateTime("2020-12-13T16:12:00.000Z", time.RFC3339, "UTC")
},
expected: "2020-12-13 16:12:00",
},
{
name: "it format input date using proper timezone",
format: func() string {
return formatDateTime("2020-12-13T16:12:00.000Z", time.RFC3339, "Asia/Kathmandu")
},
expected: "2020-12-13 21:57:00",
},
}

for _, tc := range cases {
Expand Down
7 changes: 4 additions & 3 deletions internal/view/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type DisplayFormat struct {
Columns []string
FixedColumns uint
TableStyle tui.TableStyle
Timezone string
}

// IssueList is a list view for issues.
Expand Down Expand Up @@ -188,7 +189,7 @@ func (l *IssueList) data() tui.TableData {
return data
}

func (*IssueList) assignColumns(columns []string, issue *jira.Issue) []string {
func (l *IssueList) assignColumns(columns []string, issue *jira.Issue) []string {
var bucket []string

for _, column := range columns {
Expand All @@ -210,9 +211,9 @@ func (*IssueList) assignColumns(columns []string, issue *jira.Issue) []string {
case fieldResolution:
bucket = append(bucket, issue.Fields.Resolution.Name)
case fieldCreated:
bucket = append(bucket, formatDateTime(issue.Fields.Created, jira.RFC3339))
bucket = append(bucket, formatDateTime(issue.Fields.Created, jira.RFC3339, l.Display.Timezone))
case fieldUpdated:
bucket = append(bucket, formatDateTime(issue.Fields.Updated, jira.RFC3339))
bucket = append(bucket, formatDateTime(issue.Fields.Updated, jira.RFC3339, l.Display.Timezone))
case fieldLabels:
bucket = append(bucket, strings.Join(issue.Fields.Labels, ","))
}
Expand Down
10 changes: 5 additions & 5 deletions internal/view/sprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ func (sl *SprintList) tabularize(issues []*jira.Issue) tui.TableData {
issue.Fields.Reporter.Name,
issue.Fields.Priority.Name,
issue.Fields.Resolution.Name,
formatDateTime(issue.Fields.Created, jira.RFC3339),
formatDateTime(issue.Fields.Updated, jira.RFC3339),
formatDateTime(issue.Fields.Created, jira.RFC3339, sl.Display.Timezone),
formatDateTime(issue.Fields.Updated, jira.RFC3339, sl.Display.Timezone),
strings.Join(issue.Fields.Labels, ","),
})
}
Expand Down Expand Up @@ -213,11 +213,11 @@ func (sl *SprintList) assignColumns(columns []string, sprint *jira.Sprint) []str
case fieldName:
bucket = append(bucket, sprint.Name)
case fieldStartDate:
bucket = append(bucket, formatDateTime(sprint.StartDate, time.RFC3339))
bucket = append(bucket, formatDateTime(sprint.StartDate, time.RFC3339, sl.Display.Timezone))
case fieldEndDate:
bucket = append(bucket, formatDateTime(sprint.EndDate, time.RFC3339))
bucket = append(bucket, formatDateTime(sprint.EndDate, time.RFC3339, sl.Display.Timezone))
case fieldCompleteDate:
bucket = append(bucket, formatDateTime(sprint.CompleteDate, time.RFC3339))
bucket = append(bucket, formatDateTime(sprint.CompleteDate, time.RFC3339, sl.Display.Timezone))
case fieldState:
bucket = append(bucket, sprint.Status)
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/jira/me.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (

// Me struct holds response from /myself endpoint.
type Me struct {
Login string `json:"name"`
Name string `json:"displayName"`
Email string `json:"emailAddress"`
Login string `json:"name"`
Name string `json:"displayName"`
Email string `json:"emailAddress"`
Timezone string `json:"timeZone"`
}

// Me fetches response from /myself endpoint.
Expand Down

0 comments on commit 5eab299

Please sign in to comment.