-
Couldn't load subscription status.
- Fork 55
MMF-4988 - Send user ID & server ID for SQ:C - WIP #1531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...end/server-api/src/main/java/org/sonarsource/sonarlint/core/serverapi/users/UsersApi.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * SonarLint Core - Server API | ||
| * Copyright (C) 2016-2025 SonarSource SA | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 3 of the License, or (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public License | ||
| * along with this program; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
| */ | ||
| package org.sonarsource.sonarlint.core.serverapi.users; | ||
|
|
||
| import com.google.gson.Gson; | ||
| import javax.annotation.CheckForNull; | ||
| import org.sonarsource.sonarlint.core.commons.log.SonarLintLogger; | ||
| import org.sonarsource.sonarlint.core.commons.progress.SonarLintCancelMonitor; | ||
| import org.sonarsource.sonarlint.core.serverapi.ServerApiHelper; | ||
|
|
||
| public class UsersApi { | ||
| private static final SonarLintLogger LOG = SonarLintLogger.get(); | ||
|
|
||
| private final ServerApiHelper helper; | ||
|
|
||
| public UsersApi(ServerApiHelper helper) { | ||
| this.helper = helper; | ||
| } | ||
|
|
||
| /** | ||
| * Fetch the current user info on SonarQube Cloud (SQC) using api/users/current. | ||
| * Returns null on SonarQube Server or if the response cannot be parsed. | ||
| */ | ||
| @CheckForNull | ||
| public String getCurrentUserId(SonarLintCancelMonitor cancelMonitor) { | ||
| if (!helper.isSonarCloud()) { | ||
| return null; | ||
| } | ||
| try (var response = helper.get("/api/users/current", cancelMonitor)) { | ||
| var body = response.bodyAsString(); | ||
| try { | ||
| var userResponse = new Gson().fromJson(body, CurrentUserResponse.class); | ||
| return userResponse == null ? null : userResponse.id; | ||
| } catch (Exception e) { | ||
| LOG.error("Error while parsing /api/users/current response", e); | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static class CurrentUserResponse { | ||
| String id; | ||
| } | ||
| } | ||
|
|
||
|
|
||
84 changes: 84 additions & 0 deletions
84
...erver-api/src/test/java/org/sonarsource/sonarlint/core/serverapi/users/UsersApiTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * SonarLint Core - Server API | ||
| * Copyright (C) 2016-2025 SonarSource SA | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 3 of the License, or (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public License | ||
| * along with this program; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
| */ | ||
| package org.sonarsource.sonarlint.core.serverapi.users; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
| import org.sonarsource.sonarlint.core.commons.log.SonarLintLogTester; | ||
| import org.sonarsource.sonarlint.core.commons.progress.SonarLintCancelMonitor; | ||
| import org.sonarsource.sonarlint.core.serverapi.MockWebServerExtensionWithProtobuf; | ||
| import org.sonarsource.sonarlint.core.serverapi.ServerApiHelper; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class UsersApiTests { | ||
|
|
||
| @RegisterExtension | ||
| private static final SonarLintLogTester logTester = new SonarLintLogTester(); | ||
|
|
||
| @RegisterExtension | ||
| static MockWebServerExtensionWithProtobuf mockServer = new MockWebServerExtensionWithProtobuf(); | ||
|
|
||
| private UsersApi underTest; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| // default with SonarCloud organization to trigger api base URL and isSonarCloud = true | ||
| ServerApiHelper helper = mockServer.serverApiHelper("orgKey"); | ||
| underTest = new UsersApi(helper); | ||
| } | ||
|
|
||
| @Test | ||
| void should_return_user_id_on_sonarcloud() { | ||
| mockServer.addStringResponse("/api/users/current", """ | ||
| { | ||
| "isLoggedIn": true, | ||
| "id": "16c9b3b3-3f7e-4d61-91fe-31d731456c08", | ||
| "login": "obiwan.kenobi" | ||
| }"""); | ||
|
|
||
| var id = underTest.getCurrentUserId(new SonarLintCancelMonitor()); | ||
|
|
||
| assertThat(id).isEqualTo("16c9b3b3-3f7e-4d61-91fe-31d731456c08"); | ||
| } | ||
|
|
||
| @Test | ||
| void should_return_null_on_sonarqube_server() { | ||
| var helperSqs = mockServer.serverApiHelper(null); // isSonarCloud = false | ||
| var api = new UsersApi(helperSqs); | ||
|
|
||
| var id = api.getCurrentUserId(new SonarLintCancelMonitor()); | ||
|
|
||
| assertThat(id).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void should_return_null_on_malformed_response() { | ||
| mockServer.addStringResponse("/api/users/current", "{}"); | ||
|
|
||
| var id = underTest.getCurrentUserId(new SonarLintCancelMonitor()); | ||
|
|
||
| assertThat(id).isNull(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...ction/src/main/java/org/sonarsource/sonarlint/core/serverconnection/UserSynchronizer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * SonarLint Core - Server Connection | ||
| * Copyright (C) 2016-2025 SonarSource SA | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 3 of the License, or (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public License | ||
| * along with this program; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
| */ | ||
| package org.sonarsource.sonarlint.core.serverconnection; | ||
|
|
||
| import org.sonarsource.sonarlint.core.commons.log.SonarLintLogger; | ||
| import org.sonarsource.sonarlint.core.commons.progress.SonarLintCancelMonitor; | ||
| import org.sonarsource.sonarlint.core.serverapi.ServerApi; | ||
|
|
||
| public class UserSynchronizer { | ||
| private static final SonarLintLogger LOG = SonarLintLogger.get(); | ||
| private final ConnectionStorage storage; | ||
|
|
||
| public UserSynchronizer(ConnectionStorage storage) { | ||
| this.storage = storage; | ||
| } | ||
|
|
||
| /** | ||
| * Fetches and stores the user id on SQC. On SQS, does nothing (file won't be created). | ||
| */ | ||
| public void synchronize(ServerApi serverApi, SonarLintCancelMonitor cancelMonitor) { | ||
| if (!serverApi.isSonarCloud()) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| var userId = serverApi.users().getCurrentUserId(cancelMonitor); | ||
| if (userId != null && !userId.trim().isEmpty()) { | ||
| storage.user().store(userId.trim()); | ||
| } | ||
| } catch (Exception e) { | ||
| LOG.warn("Failed to synchronize user id from server: {}", e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.