Skip to content

Commit 88b0b3d

Browse files
committed
Improve WorkItemScript
1 parent 82cbb9d commit 88b0b3d

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

gitlab4j-test/WorkItemScript.java

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.concurrent.Callable;
2020

2121
import graphql.gitlab.api.WorkitemClientApi;
22+
import graphql.gitlab.model.WorkItem;
2223
import graphql.gitlab.model.WorkItemConnection;
2324
import graphql.gitlab.model.WorkItemDeleteInput;
2425
import graphql.gitlab.model.WorkItemDeletePayload;
@@ -56,7 +57,7 @@ public class WorkItemScript implements Callable<Integer> {
5657
@Option(names = { "-s", "--childId" }, description = "workitem childrenIds")
5758
private List<String> childrenIds;
5859

59-
@Option(names = { "-r", "--h", "--reference" }, description = "references in the namespace")
60+
@Option(names = { "-r", "--ref", "--reference" }, description = "references in the namespace")
6061
private List<String> refs;
6162

6263
@Option(names = { "-c", "--config" }, description = "configuration file location")
@@ -77,10 +78,18 @@ public Integer call() throws Exception {
7778
} else {
7879
file = configFile(Paths.get(""));
7980
}
80-
System.out.println("Reading config: " + file.toAbsolutePath());
81-
final Properties prop = configProperties(file);
82-
final String gitLabUrl = readProperty(prop, "GITLAB_URL", "https://gitlab.com");
83-
final String gitLabAuthValue = readProperty(prop, "GITLAB_AUTH_VALUE");
81+
final String gitLabUrl;
82+
final String gitLabAuthValue;
83+
if (Files.isRegularFile(file)) {
84+
System.out.println("Reading config: " + file.toAbsolutePath());
85+
final Properties prop = configProperties(file);
86+
gitLabUrl = readProperty(prop, "GITLAB_URL", "https://gitlab.com");
87+
gitLabAuthValue = readProperty(prop, "GITLAB_AUTH_VALUE");
88+
} else {
89+
System.out.println("Config file does not exists: " + file.toAbsolutePath());
90+
gitLabUrl = "https://gitlab.com";
91+
gitLabAuthValue = null;
92+
}
8493

8594
WorkitemClientApi api = createGraphQLWorkitemClientApi(gitLabUrl, gitLabAuthValue);
8695

@@ -108,10 +117,16 @@ public Integer call() throws Exception {
108117
}
109118

110119
private void getWorkItem(WorkitemClientApi api) {
111-
ensureExists(namespace, "namespace");
112-
ensureExists(refs, "reference");
113-
WorkItemConnection response = api.workItemsByReference(namespace, refs, null);
114-
System.out.println(response);
120+
if (id != null) {
121+
WorkItem response = api.workItem(new WorkItemID(id));
122+
//System.out.println(response);
123+
printWorkItem(response);
124+
} else {
125+
ensureExists(namespace, "namespace");
126+
ensureExists(refs, "reference");
127+
WorkItemConnection response = api.workItemsByReference(namespace, refs, null);
128+
System.out.println(response);
129+
}
115130
}
116131

117132
private void deleteWorkItem(WorkitemClientApi api) {
@@ -156,13 +171,19 @@ private void addChildren(WorkitemClientApi api) {
156171
System.out.println(response);
157172
}
158173

174+
private void printWorkItem(WorkItem item) {
175+
System.out.println(item.getId() + " " + item.getTitle() + " " + item.getWebUrl());
176+
}
177+
159178
static WorkitemClientApi createGraphQLWorkitemClientApi(String gitLabUrl, String gitlabToken) {
160-
WorkitemClientApi gqlApi = TypesafeGraphQLClientBuilder.newBuilder()
179+
TypesafeGraphQLClientBuilder builder = TypesafeGraphQLClientBuilder.newBuilder()
161180
.endpoint(gitLabUrl + "/api/graphql")
162-
.header("Authorization", "Bearer " + gitlabToken)
163-
.allowUnexpectedResponseFields(true)
164-
.build(WorkitemClientApi.class);
165-
return gqlApi;
181+
.allowUnexpectedResponseFields(true);
182+
if (gitlabToken != null) {
183+
builder.header("Authorization", "Bearer " + gitlabToken);
184+
}
185+
186+
return builder.build(WorkitemClientApi.class);
166187
}
167188

168189
private void ensureExists(Object value, String optionName) {
@@ -184,14 +205,6 @@ public static Properties configProperties(final Path configFile) {
184205
public static Path configFile(final Path root) {
185206
final Path configFile = root.toAbsolutePath()
186207
.resolve("gitlab-config.properties");
187-
if (!Files.isRegularFile(configFile)) {
188-
try {
189-
Files.writeString(configFile, CONFIG_FILE_INITIAL_CONTENT);
190-
throw new IllegalStateException(String.format("Configuration file '%s' does not exist. An empty configuration file was created", configFile.toAbsolutePath()));
191-
} catch (final IOException e) {
192-
throw new IllegalStateException("Can not write initial config file", e);
193-
}
194-
}
195208
return configFile;
196209
}
197210

0 commit comments

Comments
 (0)