-
Notifications
You must be signed in to change notification settings - Fork 274
fix(amazonq): confusing message during loading profiles #5552
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 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a6debbf
profile loading ux
Will-ShaoHua 50c9d79
cleanup
Will-ShaoHua 8bf871b
cleanup
Will-ShaoHua e3660b4
aaa
Will-ShaoHua 9652b25
formating
Will-ShaoHua 9d2c032
v2
Will-ShaoHua 58985b0
doc str
Will-ShaoHua 3dccf60
comment
Will-ShaoHua 76468bc
Merge remote-tracking branch 'upstream/main' into fix-v2
Will-ShaoHua e9ac5fb
Merge branch 'main' into fix-v2
Will-ShaoHua 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -220,6 +220,10 @@ class QWebviewBrowser(val project: Project, private val parentDisposable: Dispos | |
| ) | ||
| } | ||
|
|
||
| is BrowserMessage.ListProfiles -> { | ||
| handleListProfilesMessage() | ||
| } | ||
|
|
||
| is BrowserMessage.PublishWebviewTelemetry -> { | ||
| publishTelemetry(message) | ||
| } | ||
|
|
@@ -262,60 +266,41 @@ class QWebviewBrowser(val project: Project, private val parentDisposable: Dispos | |
| writeValueAsString(it) | ||
| } | ||
|
|
||
| // TODO: pass "REAUTH" if connection expires | ||
| // Perform the potentially blocking AWS call outside the EDT to fetch available region profiles. | ||
| ApplicationManager.getApplication().executeOnPooledThread { | ||
| val stage = if (isQExpired(project)) { | ||
| "REAUTH" | ||
| } else if (isQConnected(project) && QRegionProfileManager.getInstance().isPendingProfileSelection(project)) { | ||
| "PROFILE_SELECT" | ||
| } else { | ||
| "START" | ||
| } | ||
|
|
||
| var errorMessage: String? = null | ||
| var profiles: List<QRegionProfile> = emptyList() | ||
| val stage = if (isQExpired(project)) { | ||
| "REAUTH" | ||
| } else if (isQConnected(project) && QRegionProfileManager.getInstance().isPendingProfileSelection(project)) { | ||
| "PROFILE_SELECT" | ||
| } else { | ||
| "START" | ||
| } | ||
|
|
||
| if (stage == "PROFILE_SELECT") { | ||
| try { | ||
| profiles = QRegionProfileManager.getInstance().listRegionProfiles(project).orEmpty() | ||
| if (profiles.size == 1) { | ||
| LOG.debug { "User only have access to 1 Q profile, auto-selecting profile ${profiles.first().profileName} for ${project.name}" } | ||
| QRegionProfileManager.getInstance().switchProfile(project, profiles.first(), QProfileSwitchIntent.Update) | ||
| } | ||
| } catch (e: Exception) { | ||
| errorMessage = e.message | ||
| LOG.warn { "Failed to call listRegionProfiles API" } | ||
| val qConn = ToolkitConnectionManager.getInstance(project).activeConnectionForFeature(QConnection.getInstance()) | ||
| Telemetry.amazonq.didSelectProfile.use { span -> | ||
| span.source(QProfileSwitchIntent.Auth.value) | ||
| .amazonQProfileRegion(QRegionProfileManager.getInstance().activeProfile(project)?.region ?: "not-set") | ||
| .ssoRegion((qConn as? AwsBearerTokenConnection)?.region) | ||
| .credentialStartUrl((qConn as? AwsBearerTokenConnection)?.startUrl) | ||
| .result(MetricResult.Failed) | ||
| .reason(e.message) | ||
| when (stage) { | ||
| "PROFILE_SELECT" -> { | ||
| val jsonData = """ | ||
| { | ||
| stage: '$stage', | ||
| status: 'pending' | ||
| } | ||
| } | ||
| """.trimIndent() | ||
| executeJS("window.ideClient.prepareUi($jsonData)") | ||
| } | ||
|
|
||
| val jsonData = """ | ||
| { | ||
| stage: '$stage', | ||
| regions: $regions, | ||
| idcInfo: { | ||
| profileName: '${lastLoginIdcInfo.profileName}', | ||
| startUrl: '${lastLoginIdcInfo.startUrl}', | ||
| region: '${lastLoginIdcInfo.region}' | ||
| }, | ||
| cancellable: ${state.browserCancellable}, | ||
| feature: '${state.feature}', | ||
| existConnections: ${writeValueAsString(selectionSettings.values.map { it.currentSelection }.toList())}, | ||
| profiles: ${writeValueAsString(profiles)}, | ||
| errorMessage: ${errorMessage?.let { "\"$it\"" } ?: "null"} | ||
| } | ||
| """.trimIndent() | ||
| else -> { | ||
| val jsonData = """ | ||
| { | ||
| stage: '$stage', | ||
| regions: $regions, | ||
| idcInfo: { | ||
| profileName: '${lastLoginIdcInfo.profileName}', | ||
| startUrl: '${lastLoginIdcInfo.startUrl}', | ||
| region: '${lastLoginIdcInfo.region}' | ||
| }, | ||
| cancellable: ${state.browserCancellable}, | ||
| feature: '${state.feature}', | ||
| existConnections: ${writeValueAsString(selectionSettings.values.map { it.currentSelection }.toList())}, | ||
| } | ||
| """.trimIndent() | ||
|
|
||
| runInEdt { | ||
| executeJS("window.ideClient.prepareUi($jsonData)") | ||
| } | ||
| } | ||
|
|
@@ -330,6 +315,52 @@ class QWebviewBrowser(val project: Project, private val parentDisposable: Dispos | |
| jcefBrowser.loadHTML(getWebviewHTML(webScriptUri, query)) | ||
| } | ||
|
|
||
| private fun handleListProfilesMessage() { | ||
| ApplicationManager.getApplication().executeOnPooledThread { | ||
| var errorMessage = "" | ||
| val profiles = try { | ||
| QRegionProfileManager.getInstance().listRegionProfiles(project) | ||
| } catch (e: Exception) { | ||
| e.message?.let { | ||
| errorMessage = it | ||
| } | ||
| LOG.warn { "Failed to call listRegionProfiles API: $errorMessage" } | ||
| val qConn = ToolkitConnectionManager.getInstance(project).activeConnectionForFeature(QConnection.getInstance()) | ||
| Telemetry.amazonq.didSelectProfile.use { span -> | ||
| span.source(QProfileSwitchIntent.Auth.value) | ||
| .amazonQProfileRegion(QRegionProfileManager.getInstance().activeProfile(project)?.region ?: "not-set") | ||
| .ssoRegion((qConn as? AwsBearerTokenConnection)?.region) | ||
| .credentialStartUrl((qConn as? AwsBearerTokenConnection)?.startUrl) | ||
| .result(MetricResult.Failed) | ||
| .reason(e.message) | ||
| } | ||
|
|
||
| null | ||
| } | ||
|
|
||
| // auto-select the profile if users only have 1 and don't show the UI | ||
| if (profiles?.size == 1) { | ||
| LOG.debug { "User only have access to 1 Q profile, auto-selecting profile ${profiles.first().profileName} for ${project.name}" } | ||
| QRegionProfileManager.getInstance().switchProfile(project, profiles.first(), QProfileSwitchIntent.Update) | ||
| return@executeOnPooledThread | ||
| } | ||
|
|
||
| // required EDT as this entire block is executed on thread pool | ||
| runInEdt { | ||
| val jsonData = """ | ||
| { | ||
| stage: 'PROFILE_SELECT', | ||
| status: '${if (profiles != null) "succeeded" else "failed"}', | ||
| profiles: ${writeValueAsString(profiles ?: "")}, | ||
| errorMessage: '$errorMessage' | ||
| } | ||
|
Comment on lines
+351
to
+356
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ideally should polymorphism but haven't got enough time to figure it out |
||
| """.trimIndent() | ||
|
|
||
| executeJS("window.ideClient.prepareUi($jsonData)") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| private val LOG = getLogger<QWebviewBrowser>() | ||
| private const val WEB_SCRIPT_URI = "http://webview/js/getStart.js" | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
want to make all these messages from ide to webview typesafe as well, future work