Skip to content

Commit 9bff367

Browse files
authored
impl: prefer agent name over agent id in URI handler (#585)
* impl: prefer agent name over agent id in URI handler Agent name is easier to resolve in jetbrains gateway module * impl: prefer agent name over agent id in URI handler (2) Error reporting should also prefer agent name over the agent id. * chore: update and fix UTs Test cases related to agent matching have to be updated now that agent name is preferred over agent id. * chore: refactor UTs related to agent matching in URI handling Rewrote the entire test to be easier to read and debug.
1 parent 546d317 commit 9bff367

File tree

3 files changed

+358
-161
lines changed

3 files changed

+358
-161
lines changed

src/main/kotlin/com/coder/gateway/util/LinkHandler.kt

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -327,25 +327,24 @@ internal fun getMatchingAgent(
327327
}
328328

329329
// If the agent is missing and the workspace has only one, use that.
330-
// Prefer the ID over the name if both are set.
331-
val agent =
332-
if (!parameters.agentID().isNullOrBlank()) {
333-
agents.firstOrNull { it.id.toString() == parameters.agentID() }
334-
} else if (!parameters.agentName().isNullOrBlank()) {
335-
agents.firstOrNull { it.name == parameters.agentName() }
336-
} else if (agents.size == 1) {
337-
agents.first()
338-
} else {
339-
null
340-
}
330+
// Prefer the name over the id if both are set.
331+
val agent = if (!parameters.agentName().isNullOrBlank()) {
332+
agents.firstOrNull { it.name == parameters.agentName() }
333+
} else if (!parameters.agentID().isNullOrBlank()) {
334+
agents.firstOrNull { it.id.toString() == parameters.agentID() }
335+
} else if (agents.size == 1) {
336+
agents.first()
337+
} else {
338+
null
339+
}
341340

342341
if (agent == null) {
343-
if (!parameters.agentID().isNullOrBlank()) {
344-
throw IllegalArgumentException("The workspace \"${workspace.name}\" does not have an agent with ID \"${parameters.agentID()}\"")
345-
} else if (!parameters.agentName().isNullOrBlank()) {
342+
if (!parameters.agentName().isNullOrBlank()) {
346343
throw IllegalArgumentException(
347-
"The workspace \"${workspace.name}\"does not have an agent named \"${parameters.agentName()}\"",
344+
"The workspace \"${workspace.name}\" does not have an agent named \"${parameters.agentName()}\"",
348345
)
346+
} else if (!parameters.agentID().isNullOrBlank()) {
347+
throw IllegalArgumentException("The workspace \"${workspace.name}\" does not have an agent with ID \"${parameters.agentID()}\"")
349348
} else {
350349
throw MissingArgumentException(
351350
"Unable to determine which agent to connect to; one of \"$AGENT_NAME\" or \"$AGENT_ID\" must be set because the workspace \"${workspace.name}\" has more than one agent",

src/main/kotlin/com/coder/gateway/util/LinkMap.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ fun Map<String, String>.owner() = this[OWNER]
2929

3030
fun Map<String, String?>.agentName() = this[AGENT_NAME]
3131

32+
@Deprecated("Use the agent name instead")
3233
fun Map<String, String?>.agentID() = this[AGENT_ID]
3334

3435
fun Map<String, String>.folder() = this[FOLDER]

0 commit comments

Comments
 (0)