19
19
import java .util .concurrent .Callable ;
20
20
21
21
import graphql .gitlab .api .WorkitemClientApi ;
22
+ import graphql .gitlab .model .WorkItem ;
22
23
import graphql .gitlab .model .WorkItemConnection ;
23
24
import graphql .gitlab .model .WorkItemDeleteInput ;
24
25
import graphql .gitlab .model .WorkItemDeletePayload ;
@@ -56,7 +57,7 @@ public class WorkItemScript implements Callable<Integer> {
56
57
@ Option (names = { "-s" , "--childId" }, description = "workitem childrenIds" )
57
58
private List <String > childrenIds ;
58
59
59
- @ Option (names = { "-r" , "--h " , "--reference" }, description = "references in the namespace" )
60
+ @ Option (names = { "-r" , "--ref " , "--reference" }, description = "references in the namespace" )
60
61
private List <String > refs ;
61
62
62
63
@ Option (names = { "-c" , "--config" }, description = "configuration file location" )
@@ -77,10 +78,18 @@ public Integer call() throws Exception {
77
78
} else {
78
79
file = configFile (Paths .get ("" ));
79
80
}
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
+ }
84
93
85
94
WorkitemClientApi api = createGraphQLWorkitemClientApi (gitLabUrl , gitLabAuthValue );
86
95
@@ -108,10 +117,16 @@ public Integer call() throws Exception {
108
117
}
109
118
110
119
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
+ }
115
130
}
116
131
117
132
private void deleteWorkItem (WorkitemClientApi api ) {
@@ -156,13 +171,19 @@ private void addChildren(WorkitemClientApi api) {
156
171
System .out .println (response );
157
172
}
158
173
174
+ private void printWorkItem (WorkItem item ) {
175
+ System .out .println (item .getId () + " " + item .getTitle () + " " + item .getWebUrl ());
176
+ }
177
+
159
178
static WorkitemClientApi createGraphQLWorkitemClientApi (String gitLabUrl , String gitlabToken ) {
160
- WorkitemClientApi gqlApi = TypesafeGraphQLClientBuilder .newBuilder ()
179
+ TypesafeGraphQLClientBuilder builder = TypesafeGraphQLClientBuilder .newBuilder ()
161
180
.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 );
166
187
}
167
188
168
189
private void ensureExists (Object value , String optionName ) {
@@ -184,14 +205,6 @@ public static Properties configProperties(final Path configFile) {
184
205
public static Path configFile (final Path root ) {
185
206
final Path configFile = root .toAbsolutePath ()
186
207
.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
- }
195
208
return configFile ;
196
209
}
197
210
0 commit comments