From b12e93617de7e80dcb848c6b2cf7a011e11a288c Mon Sep 17 00:00:00 2001 From: Matteo Mara Date: Thu, 30 Jun 2022 17:55:56 +0200 Subject: [PATCH] SONAR-16567 update API api/user_tokens/search for returning the expiration date --- .../sonar/server/usertoken/ws/SearchAction.java | 2 ++ .../server/usertoken/ws/search-example.json | 1 + .../server/usertoken/ws/SearchActionTest.java | 17 +++++++++++++++++ .../ws/client/usertokens/GenerateRequest.java | 10 ++++++++++ .../ws/client/usertokens/UserTokensService.java | 3 ++- sonar-ws/src/main/protobuf/ws-user_tokens.proto | 1 + 6 files changed, 33 insertions(+), 1 deletion(-) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usertoken/ws/SearchAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usertoken/ws/SearchAction.java index 0a04977cc9f..653c700e704 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usertoken/ws/SearchAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usertoken/ws/SearchAction.java @@ -58,6 +58,7 @@ public class SearchAction implements UserTokensWsAction { "Field 'lastConnectionDate' is only updated every hour, so it may not be accurate, for instance when a user is using a token many times in less than one hour.
" + "It requires administration permissions to specify a 'login' and list the tokens of another user. Otherwise, tokens for the current user are listed.
" + "Authentication is required for this API endpoint") + .setChangelog(new Change("9.6", "New field 'expirationDate' is added to response")) .setChangelog(new Change("7.7", "New field 'lastConnectionDate' is added to response")) .setResponseExample(getClass().getResource("search-example.json")) .setSince("5.3") @@ -93,6 +94,7 @@ public class SearchAction implements UserTokensWsAction { .setCreatedAt(formatDateTime(userTokenDto.getCreatedAt())) .setType(userTokenDto.getType()); ofNullable(userTokenDto.getLastConnectionDate()).ifPresent(date -> userTokenBuilder.setLastConnectionDate(formatDateTime(date))); + ofNullable(userTokenDto.getExpirationDate()).ifPresent(expirationDate -> userTokenBuilder.setExpirationDate(formatDateTime(expirationDate))); if (!isNullOrEmpty(userTokenDto.getProjectKey()) && !isNullOrEmpty(userTokenDto.getProjectName())) { Project.Builder projectBuilder = newBuilder().getProjectBuilder() diff --git a/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/usertoken/ws/search-example.json b/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/usertoken/ws/search-example.json index 34f35268527..4279e2891be 100644 --- a/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/usertoken/ws/search-example.json +++ b/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/usertoken/ws/search-example.json @@ -9,6 +9,7 @@ { "name": "Project scan on Jenkins", "createdAt": "2015-04-08T21:57:47+0200", + "expirationDate": "2022-07-14T00:00:00+0200", "type": "PROJECT_ANALYSIS_TOKEN", "project": { "key": "project-1", diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usertoken/ws/SearchActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usertoken/ws/SearchActionTest.java index 1b165a2367c..6b1d152d521 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usertoken/ws/SearchActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usertoken/ws/SearchActionTest.java @@ -75,6 +75,7 @@ public class SearchActionTest { db.users().insertToken(user1, t -> t.setName("Project scan on AppVeyor").setCreatedAt(1438523067221L)); db.users().insertProjectAnalysisToken(user1, t -> t.setName("Project scan on Jenkins") .setCreatedAt(1428523067221L) + .setExpirationDate(1657749600000L) .setProjectKey(project1.getKey())); db.users().insertProjectAnalysisToken(user2, t -> t.setName("Project scan on Travis") .setCreatedAt(141456787123L) @@ -118,6 +119,22 @@ public class SearchActionTest { tuple(token2.getName(), false, "")); } + @Test + public void expiration_date_is_returned_only_when_set() { + UserDto user = db.users().insertUser(); + UserTokenDto token1 = db.users().insertToken(user, t -> t.setExpirationDate(10_000_000_000L)); + UserTokenDto token2 = db.users().insertToken(user); + logInAsSystemAdministrator(); + + SearchWsResponse response = newRequest(user.getLogin()); + + assertThat(response.getUserTokensList()) + .extracting(UserToken::getName, UserToken::getExpirationDate) + .containsExactlyInAnyOrder( + tuple(token1.getName(), formatDateTime(10_000_000_000L)), + tuple(token2.getName(), "")); + } + @Test public void fail_when_login_does_not_exist() { logInAsSystemAdministrator(); diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/usertokens/GenerateRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/usertokens/GenerateRequest.java index c0b3214b7d8..f2e2b5de0c3 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/usertokens/GenerateRequest.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/usertokens/GenerateRequest.java @@ -35,6 +35,7 @@ public class GenerateRequest { private String name; private String type; private String projectKey; + private String expirationDate; /** * Example value: "g.hopper" @@ -78,4 +79,13 @@ public class GenerateRequest { this.projectKey = projectKey; return this; } + + public String getExpirationDate() { + return expirationDate; + } + + public GenerateRequest setExpirationDate(String expirationDate) { + this.expirationDate = expirationDate; + return this; + } } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/usertokens/UserTokensService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/usertokens/UserTokensService.java index 1539acc65f2..afe2f57fe16 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/usertokens/UserTokensService.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/usertokens/UserTokensService.java @@ -51,7 +51,8 @@ public class UserTokensService extends BaseService { .setParam("login", request.getLogin()) .setParam("name", request.getName()) .setParam("type", request.getType()) - .setParam("projectKey", request.getProjectKey()), + .setParam("projectKey", request.getProjectKey()) + .setParam("expirationDate", request.getExpirationDate()), GenerateWsResponse.parser()); } diff --git a/sonar-ws/src/main/protobuf/ws-user_tokens.proto b/sonar-ws/src/main/protobuf/ws-user_tokens.proto index 786a3bfc5ff..a96d9551ffd 100644 --- a/sonar-ws/src/main/protobuf/ws-user_tokens.proto +++ b/sonar-ws/src/main/protobuf/ws-user_tokens.proto @@ -46,6 +46,7 @@ message SearchWsResponse { optional string lastConnectionDate = 3; optional string type = 4; optional Project project = 5; + optional string expirationDate = 6; message Project { optional string key = 1; -- 2.39.5