|
@@ -1,11 +1,12 @@
|
1
|
1
|
package com.armstrongconsulting.acprotasks;
|
2
|
2
|
|
|
3
|
+import org.apache.commons.lang.StringUtils;
|
3
|
4
|
import org.jetbrains.annotations.NotNull;
|
4
|
5
|
import org.jetbrains.annotations.Nullable;
|
5
|
|
-import org.slf4j.Logger;
|
6
|
|
-import org.slf4j.LoggerFactory;
|
7
|
6
|
|
8
|
7
|
import com.armstrongconsulting.acprotasks.client.AcproConnector;
|
|
8
|
+import com.armstrongconsulting.acprotasks.client.model.ItemsContainer;
|
|
9
|
+import com.intellij.ide.util.PropertiesComponent;
|
9
|
10
|
import com.intellij.openapi.progress.ProgressIndicator;
|
10
|
11
|
import com.intellij.tasks.Task;
|
11
|
12
|
import com.intellij.tasks.TaskRepositoryType;
|
|
@@ -13,14 +14,12 @@ import com.intellij.tasks.impl.BaseRepository;
|
13
|
14
|
import com.intellij.util.xmlb.annotations.Tag;
|
14
|
15
|
|
15
|
16
|
import feign.FeignException;
|
|
17
|
+import lombok.extern.slf4j.Slf4j;
|
16
|
18
|
|
17
|
19
|
@Tag(AcproRepositoryType.NAME)
|
|
20
|
+@Slf4j
|
18
|
21
|
public class AcproRepository extends BaseRepository
|
19
|
22
|
{
|
20
|
|
- private final Logger logger = LoggerFactory.getLogger(AcproRepository.class);
|
21
|
|
-
|
22
|
|
- private String apiKey = null;
|
23
|
|
-
|
24
|
23
|
public AcproRepository(TaskRepositoryType type)
|
25
|
24
|
{
|
26
|
25
|
super(type);
|
|
@@ -29,7 +28,8 @@ public class AcproRepository extends BaseRepository
|
29
|
28
|
public AcproRepository(AcproRepository other)
|
30
|
29
|
{
|
31
|
30
|
super(other);
|
32
|
|
- this.apiKey = other.apiKey;
|
|
31
|
+ setRepositoryType(other.getRepositoryType());
|
|
32
|
+ setApiKey(other.getApiKey());
|
33
|
33
|
}
|
34
|
34
|
|
35
|
35
|
public AcproRepository()
|
|
@@ -52,14 +52,22 @@ public class AcproRepository extends BaseRepository
|
52
|
52
|
public Task[] getIssues(@Nullable String query, int offset, int limit, boolean withClosed,
|
53
|
53
|
@NotNull ProgressIndicator cancelled) throws Exception
|
54
|
54
|
{
|
55
|
|
- if (apiKey == null)
|
|
55
|
+ if (StringUtils.isBlank(query))
|
|
56
|
+ {
|
|
57
|
+ return new Task[0];
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ ItemsContainer response = AcproConnector.getClient(this).getItems(query, offset, limit);
|
|
61
|
+ Task[] result = new Task[0];
|
|
62
|
+ if (response.getItems() != null && response.getItems().getItem() != null)
|
56
|
63
|
{
|
57
|
|
- apiKey = AcproConnector.getClient(getUrl()).getApiKey(getUsername(), getPassword(), 28800);
|
|
64
|
+ result = response.getItems().getItem().stream().map(AcproTask::new).toArray(Task[]::new);
|
58
|
65
|
}
|
59
|
|
- return new Task[0];/*AcproConnector.getClient(getUrl()).getItems(query, offset, limit, apiKey).stream().peek(a -> logger.warn(a.toString())).map(AcproTask::new)
|
60
|
|
- .toArray(AcproTask[]::new);*/
|
|
66
|
+ return result;
|
61
|
67
|
}
|
62
|
68
|
|
|
69
|
+ private final AcproRepository self = this;
|
|
70
|
+
|
63
|
71
|
@Override
|
64
|
72
|
public @Nullable CancellableConnection createCancellableConnection()
|
65
|
73
|
{
|
|
@@ -68,22 +76,20 @@ public class AcproRepository extends BaseRepository
|
68
|
76
|
@Override
|
69
|
77
|
protected void doTest() throws Exception
|
70
|
78
|
{
|
|
79
|
+ // do login and set apiKey
|
71
|
80
|
try
|
72
|
81
|
{
|
73
|
|
- if (apiKey != null)
|
74
|
|
- {
|
75
|
|
- return;
|
76
|
|
- }
|
77
|
|
-
|
78
|
|
- apiKey = AcproConnector.getClient(getUrl()).getApiKey(getUsername(), getPassword(), 28800);
|
|
82
|
+ setApiKey(
|
|
83
|
+ AcproConnector.getClient(self).getApiKey(getUsername(), getPassword()));
|
|
84
|
+ log.warn("Fetched api key: {}", getApiKey());
|
79
|
85
|
}
|
80
|
86
|
catch (FeignException.Unauthorized e)
|
81
|
87
|
{
|
82
|
|
- throw new RuntimeException("Wrong username or password");
|
|
88
|
+ throw new RuntimeException("Failed to authenticate", e);
|
83
|
89
|
}
|
84
|
|
- catch (FeignException.FeignClientException e)
|
|
90
|
+ catch (FeignException e)
|
85
|
91
|
{
|
86
|
|
- throw new RuntimeException("Connection failed");
|
|
92
|
+ throw new RuntimeException("Failed to connect to server", e);
|
87
|
93
|
}
|
88
|
94
|
}
|
89
|
95
|
|
|
@@ -95,9 +101,13 @@ public class AcproRepository extends BaseRepository
|
95
|
101
|
};
|
96
|
102
|
}
|
97
|
103
|
|
98
|
|
- @Override
|
99
|
|
- public void initializeRepository()
|
|
104
|
+ public String getApiKey()
|
|
105
|
+ {
|
|
106
|
+ return PropertiesComponent.getInstance().getValue("api_key");
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+ public void setApiKey(String apiKey)
|
100
|
110
|
{
|
101
|
|
- logger.info("Initialize repository");
|
|
111
|
+ PropertiesComponent.getInstance().setValue("api_key", apiKey);
|
102
|
112
|
}
|
103
|
113
|
}
|