1
1
///usr/bin/env jbang "$0" "$@" ; exit $?
2
2
3
3
//DEPS info.picocli:picocli:4.6.3
4
- //DEPS org. gitlab4j:gitlab4j -api:6.0.0-rc.8
4
+ //DEPS https://github.com/jmini/ gitlab4j-api/commit/1627d509400bcd4f9de092e9e8c30cceddc62da4
5
5
//JAVA 17
6
6
7
7
import java .io .FileInputStream ;
10
10
import java .nio .file .Files ;
11
11
import java .nio .file .Path ;
12
12
import java .nio .file .Paths ;
13
+ import java .time .LocalDate ;
14
+ import java .time .ZoneId ;
15
+ import java .util .ArrayList ;
16
+ import java .util .Date ;
17
+ import java .util .List ;
13
18
import java .util .Properties ;
14
19
import java .util .concurrent .Callable ;
15
20
16
21
import org .gitlab4j .api .GitLabApi ;
22
+ import org .gitlab4j .api .models .ImpersonationToken .Scope ;
17
23
18
24
import picocli .CommandLine ;
19
25
import picocli .CommandLine .Command ;
@@ -28,17 +34,35 @@ public class AccessTokenScript implements Callable<Integer> {
28
34
GITLAB_AUTH_VALUE=
29
35
""" ;
30
36
31
- @ Parameters (index = "0" , description = "action to execute" , defaultValue = "GET " )
37
+ @ Parameters (index = "0" , description = "action to execute" , defaultValue = "GET_SELF " )
32
38
private Action action ;
33
39
40
+ @ Option (names = { "-u" , "--user" }, description = "user id or username" )
41
+ String userIdOrUsername ;
42
+
43
+ @ Option (names = { "-i" , "--tokenId" }, description = "token id" )
44
+ String tokenId ;
45
+
46
+ @ Option (names = { "-n" , "--tokenName" }, description = "token name" )
47
+ String tokenName ;
48
+
49
+ @ Option (names = { "-d" , "--description" }, description = "token description" )
50
+ String description ;
51
+
52
+ @ Option (names = { "-e" , "--expiresAt" }, description = "token expiration date" )
53
+ Date expiresAt ;
54
+
55
+ @ Option (names = { "-s" , "--scope" }, description = "token scopes" )
56
+ List <Scope > scopes = new ArrayList <>();
57
+
34
58
@ Option (names = { "-c" , "--config" }, description = "configuration file location" )
35
59
String configFile ;
36
60
37
61
@ Option (names = { "-v" , "--verbose" }, description = "log http trafic" )
38
62
Boolean logHttp ;
39
63
40
64
private static enum Action {
41
- GET
65
+ GET_SELF , LIST , CREATE , ROTATE
42
66
}
43
67
44
68
@ Override
@@ -59,18 +83,41 @@ public Integer call() throws Exception {
59
83
gitLabApi .enableRequestResponseLogging (java .util .logging .Level .INFO , 2000000000 );
60
84
}
61
85
switch (action ) {
62
- case GET :
86
+ case GET_SELF :
63
87
var selfPersonalAccessToken = gitLabApi .getPersonalAccessTokenApi ()
64
88
.getPersonalAccessToken ();
65
89
System .out .println (selfPersonalAccessToken );
66
90
break ;
91
+ case LIST :
92
+ var tokens = gitLabApi .getPersonalAccessTokenApi ()
93
+ .getPersonalAccessTokens ();
94
+ System .out .println (tokens );
95
+ break ;
96
+ case CREATE :
97
+ ensureExists (userIdOrUsername , "user" );
98
+ var newToken = gitLabApi .getUserApi ()
99
+ .createPersonalAccessToken (idOrPath (userIdOrUsername ), tokenName , description , expiresAt , scopes .toArray (new Scope [] {}));
100
+ System .out .println (newToken );
101
+ break ;
102
+ case ROTATE :
103
+ ensureExists (tokenId , "tokenId" );
104
+ var rotated = gitLabApi .getPersonalAccessTokenApi ()
105
+ .rotatePersonalAccessToken (tokenId , toDate (LocalDate .now ()
106
+ .plusDays (30 )));
107
+ System .out .println (rotated );
108
+ break ;
67
109
default :
68
110
throw new IllegalArgumentException ("Unexpected value: " + action );
69
111
}
70
112
}
71
113
return 0 ;
72
114
}
73
115
116
+ private Date toDate (LocalDate localDate ) {
117
+ return Date .from (localDate .atStartOfDay (ZoneId .systemDefault ())
118
+ .toInstant ());
119
+ }
120
+
74
121
private GitLabApi createGitLabApi (String gitLabUrl , String gitLabAuthValue ) {
75
122
return new GitLabApi (gitLabUrl , gitLabAuthValue );
76
123
}
0 commit comments