aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine
diff options
context:
space:
mode:
authorZipeng WU <zipeng.wu@sonarsource.com>2022-07-15 15:24:01 +0200
committersonartech <sonartech@sonarsource.com>2022-07-18 20:03:26 +0000
commit30c1d8d6a91d9e18633314ee2cb636bd70811d10 (patch)
treefad373d464a75e95ec594ed5570e758d5e02600e /sonar-scanner-engine
parent27a39bde6814a0498fa3aeb86b9e2163f2007bd0 (diff)
downloadsonarqube-30c1d8d6a91d9e18633314ee2cb636bd70811d10.tar.gz
sonarqube-30c1d8d6a91d9e18633314ee2cb636bd70811d10.zip
SONAR-16567 Use user-friendly date format and improve notification message
Diffstat (limited to 'sonar-scanner-engine')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/DefaultScannerWsClient.java24
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/DefaultScannerWsClientTest.java7
2 files changed, 17 insertions, 14 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/DefaultScannerWsClient.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/DefaultScannerWsClient.java
index 0be294a4f99..b1ec13c6732 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/DefaultScannerWsClient.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/DefaultScannerWsClient.java
@@ -23,7 +23,6 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
-import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
@@ -55,7 +54,8 @@ import static org.sonar.api.utils.Preconditions.checkState;
public class DefaultScannerWsClient implements ScannerWsClient {
private static final int MAX_ERROR_MSG_LEN = 128;
- private static final String SQ_TOKEN_EXPIRATION_HEADER = "sq-authentication-token-expiration";
+ private static final String SQ_TOKEN_EXPIRATION_HEADER = "SonarQube-Authentication-Token-Expiration";
+ private static final DateTimeFormatter USER_FRIENDLY_DATETIME_FORMAT = DateTimeFormatter.ofPattern("MMMM dd, yyyy");
private static final Logger LOG = Loggers.get(DefaultScannerWsClient.class);
private final Set<String> warningMessages = new HashSet<>();
@@ -129,28 +129,30 @@ public class DefaultScannerWsClient implements ScannerWsClient {
private void checkAuthenticationWarnings(WsResponse response) {
if (response.code() == HTTP_OK) {
response.header(SQ_TOKEN_EXPIRATION_HEADER).ifPresent(expirationDate -> {
- if (isTokenExpiringInOneWeek(expirationDate)) {
- addAnalysisWarning(expirationDate);
+ var datetimeInUTC = ZonedDateTime.from(DateTimeFormatter.ofPattern(DATETIME_FORMAT)
+ .parse(expirationDate)).withZoneSameInstant(ZoneOffset.UTC);
+ if (isTokenExpiringInOneWeek(datetimeInUTC)) {
+ addAnalysisWarning(datetimeInUTC);
}
});
}
}
- private static boolean isTokenExpiringInOneWeek(String expirationDate) {
+ private static boolean isTokenExpiringInOneWeek(ZonedDateTime expirationDate) {
ZonedDateTime localDateTime = ZonedDateTime.now(ZoneOffset.UTC);
- ZonedDateTime headerDateTime = LocalDateTime.from(DateTimeFormatter.ofPattern(DATETIME_FORMAT)
- .parse(expirationDate)).minusDays(7).atZone(ZoneOffset.UTC);
+ ZonedDateTime headerDateTime = expirationDate.minusDays(7);
return localDateTime.isAfter(headerDateTime);
}
- private void addAnalysisWarning(String tokenExpirationDate) {
- String warningMessage = "The token used for this analysis will expire on: " + tokenExpirationDate;
+ private void addAnalysisWarning(ZonedDateTime tokenExpirationDate) {
+ String warningMessage = "The token used for this analysis will expire on: " + tokenExpirationDate.format(USER_FRIENDLY_DATETIME_FORMAT);
if (!warningMessages.contains(warningMessage)) {
warningMessages.add(warningMessage);
LOG.warn(warningMessage);
- LOG.warn("Analysis executed with this token after the expiration date will fail.");
+ LOG.warn("Analysis executed with this token will fail after the expiration date.");
}
- analysisWarnings.addUnique(warningMessage + "\nAnalysis executed with this token after the expiration date will fail.");
+ analysisWarnings.addUnique(warningMessage + "\nAfter this date, the token can no longer be used to execute the analysis. "
+ + "Please consider generating a new token and updating it in the locations where it is in use.");
}
/**
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/DefaultScannerWsClientTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/DefaultScannerWsClientTest.java
index 3da98be8901..a1075f4b100 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/DefaultScannerWsClientTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/DefaultScannerWsClientTest.java
@@ -133,9 +133,10 @@ public class DefaultScannerWsClientTest {
@Test
public void warnings_are_added_when_expiration_approaches() {
WsRequest request = newRequest();
+ var fiveDaysLatter = LocalDateTime.now().atZone(ZoneOffset.UTC).plusDays(5);
String expirationDate = DateTimeFormatter
.ofPattern(DATETIME_FORMAT)
- .format(LocalDateTime.now().atOffset(ZoneOffset.UTC).plusDays(5));
+ .format(fiveDaysLatter);
WsResponse response = newResponse()
.setCode(200)
.setExpirationDate(expirationDate);
@@ -150,8 +151,8 @@ public class DefaultScannerWsClientTest {
// check logs
List<String> warningLogs = logTester.logs(LoggerLevel.WARN);
assertThat(warningLogs).hasSize(2);
- assertThat(warningLogs.get(0)).contains("The token used for this analysis will expire on: " + expirationDate);
- assertThat(warningLogs.get(1)).contains("Analysis executed with this token after the expiration date will fail.");
+ assertThat(warningLogs.get(0)).contains("The token used for this analysis will expire on: " + fiveDaysLatter.format(DateTimeFormatter.ofPattern("MMMM dd, yyyy")));
+ assertThat(warningLogs.get(1)).contains("Analysis executed with this token will fail after the expiration date.");
}
@Test