Skip to content

Commit eb86e6d

Browse files
mindlesscloudabeizn
authored andcommitted
fix: the jql time zone issue (#4932)
1 parent 75ad0b0 commit eb86e6d

2 files changed

Lines changed: 116 additions & 11 deletions

File tree

backend/plugins/jira/tasks/issue_collector.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"io"
2424
"net/http"
2525
"net/url"
26+
"time"
2627

2728
"github.com/apache/incubator-devlake/core/errors"
2829
"github.com/apache/incubator-devlake/core/plugin"
@@ -66,18 +67,8 @@ func CollectIssues(taskCtx plugin.SubTaskContext) errors.Error {
6667
// build jql
6768
// IMPORTANT: we have to keep paginated data in a consistence order to avoid data-missing, if we sort issues by
6869
// `updated`, issue will be jumping between pages if it got updated during the collection process
69-
jql := "created is not null ORDER BY created ASC"
70-
71-
// timer filter
72-
if data.TimeAfter != nil {
73-
jql = fmt.Sprintf("updated >= '%v' AND %v", data.TimeAfter.Format("2006/01/02 15:04"), jql)
74-
}
75-
76-
// diff sync
7770
incremental := collectorWithState.IsIncremental()
78-
if incremental {
79-
jql = fmt.Sprintf("updated >= '%v' AND %v", collectorWithState.LatestState.LatestSuccessStart.Format("2006/01/02 15:04"), jql)
80-
}
71+
jql := buildJQL(data.TimeAfter, collectorWithState.LatestState.LatestSuccessStart, incremental)
8172

8273
err = collectorWithState.InitCollector(api.ApiCollectorArgs{
8374
ApiClient: data.ApiClient,
@@ -144,3 +135,24 @@ func CollectIssues(taskCtx plugin.SubTaskContext) errors.Error {
144135

145136
return collectorWithState.Execute()
146137
}
138+
139+
// buildJQL build jql based on timeAfter and incremental mode
140+
func buildJQL(timeAfter, latestSuccessStart *time.Time, isIncremental bool) string {
141+
jql := "ORDER BY created ASC"
142+
var moment time.Time
143+
if timeAfter != nil {
144+
moment = *timeAfter
145+
}
146+
// if isIncremental is true, we should not collect data before latestSuccessStart
147+
if isIncremental {
148+
// subtract 24 hours to avoid missing data due to time zone difference
149+
latest := latestSuccessStart.Add(-24 * time.Hour)
150+
if latest.After(moment) {
151+
moment = latest
152+
}
153+
}
154+
if !moment.IsZero() {
155+
jql = fmt.Sprintf("updated >= '%s' %s", moment.In(time.UTC).Format("2006/01/02 15:04"), jql)
156+
}
157+
return jql
158+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one or more
3+
contributor license agreements. See the NOTICE file distributed with
4+
this work for additional information regarding copyright ownership.
5+
The ASF licenses this file to You under the Apache License, Version 2.0
6+
(the "License"); you may not use this file except in compliance with
7+
the License. You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
package tasks
19+
20+
import (
21+
"testing"
22+
"time"
23+
)
24+
25+
func Test_buildJQL(t *testing.T) {
26+
base := time.Date(2021, 2, 3, 4, 5, 6, 7, time.UTC)
27+
timeAfter := base
28+
add48 := base.Add(48 * time.Hour)
29+
minus48 := base.Add(-48 * time.Hour)
30+
type args struct {
31+
timeAfter *time.Time
32+
latestSuccessStart *time.Time
33+
isIncremental bool
34+
}
35+
tests := []struct {
36+
name string
37+
args args
38+
want string
39+
}{
40+
{
41+
name: "test incremental",
42+
args: args{
43+
timeAfter: nil,
44+
latestSuccessStart: nil,
45+
isIncremental: false,
46+
},
47+
want: "ORDER BY created ASC"},
48+
{
49+
name: "test incremental",
50+
args: args{
51+
timeAfter: nil,
52+
latestSuccessStart: &add48,
53+
isIncremental: true,
54+
},
55+
want: "updated >= '2021/02/04 04:05' ORDER BY created ASC",
56+
},
57+
{
58+
name: "test incremental",
59+
args: args{
60+
timeAfter: &base,
61+
latestSuccessStart: nil,
62+
isIncremental: false,
63+
},
64+
want: "updated >= '2021/02/03 04:05' ORDER BY created ASC",
65+
},
66+
{
67+
name: "test incremental",
68+
args: args{
69+
timeAfter: &timeAfter,
70+
latestSuccessStart: &add48,
71+
isIncremental: true,
72+
},
73+
want: "updated >= '2021/02/04 04:05' ORDER BY created ASC",
74+
},
75+
{
76+
name: "test incremental",
77+
args: args{
78+
timeAfter: &timeAfter,
79+
latestSuccessStart: &minus48,
80+
isIncremental: true,
81+
},
82+
want: "updated >= '2021/02/03 04:05' ORDER BY created ASC",
83+
},
84+
}
85+
86+
for _, tt := range tests {
87+
t.Run(tt.name, func(t *testing.T) {
88+
if got := buildJQL(tt.args.timeAfter, tt.args.latestSuccessStart, tt.args.isIncremental); got != tt.want {
89+
t.Errorf("buildJQL() = %v, want %v", got, tt.want)
90+
}
91+
})
92+
}
93+
}

0 commit comments

Comments
 (0)