11///usr/bin/env jbang "$0" "$@" ; exit $?
22
33//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
55//JAVA 17
66
77import java .io .FileInputStream ;
1010import java .nio .file .Files ;
1111import java .nio .file .Path ;
1212import 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 ;
1318import java .util .Properties ;
1419import java .util .concurrent .Callable ;
1520
1621import org .gitlab4j .api .GitLabApi ;
22+ import org .gitlab4j .api .models .ImpersonationToken .Scope ;
1723
1824import picocli .CommandLine ;
1925import picocli .CommandLine .Command ;
@@ -28,17 +34,35 @@ public class AccessTokenScript implements Callable<Integer> {
2834 GITLAB_AUTH_VALUE=
2935 """ ;
3036
31- @ Parameters (index = "0" , description = "action to execute" , defaultValue = "GET " )
37+ @ Parameters (index = "0" , description = "action to execute" , defaultValue = "GET_SELF " )
3238 private Action action ;
3339
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+
3458 @ Option (names = { "-c" , "--config" }, description = "configuration file location" )
3559 String configFile ;
3660
3761 @ Option (names = { "-v" , "--verbose" }, description = "log http trafic" )
3862 Boolean logHttp ;
3963
4064 private static enum Action {
41- GET
65+ GET_SELF , LIST , CREATE , ROTATE
4266 }
4367
4468 @ Override
@@ -59,18 +83,41 @@ public Integer call() throws Exception {
5983 gitLabApi .enableRequestResponseLogging (java .util .logging .Level .INFO , 2000000000 );
6084 }
6185 switch (action ) {
62- case GET :
86+ case GET_SELF :
6387 var selfPersonalAccessToken = gitLabApi .getPersonalAccessTokenApi ()
6488 .getPersonalAccessToken ();
6589 System .out .println (selfPersonalAccessToken );
6690 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 ;
67109 default :
68110 throw new IllegalArgumentException ("Unexpected value: " + action );
69111 }
70112 }
71113 return 0 ;
72114 }
73115
116+ private Date toDate (LocalDate localDate ) {
117+ return Date .from (localDate .atStartOfDay (ZoneId .systemDefault ())
118+ .toInstant ());
119+ }
120+
74121 private GitLabApi createGitLabApi (String gitLabUrl , String gitLabAuthValue ) {
75122 return new GitLabApi (gitLabUrl , gitLabAuthValue );
76123 }
0 commit comments