Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(agent/agentcontainers): fall back to workspace folder name#18466

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
DanielleMaywood wants to merge4 commits intomain
base:main
Choose a base branch
Loading
fromdm-devcontainer-folder-name
Draft
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 35 additions & 27 deletionsagent/agentcontainers/api.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
package agentcontainers

import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"runtime"
"slices"
"strings"
Expand DownExpand Up@@ -585,10 +584,10 @@ func (api *API) processUpdatedContainersLocked(ctx context.Context, updated code
if dc.Container != nil {
if !api.devcontainerNames[dc.Name] {
// If the devcontainer name wasn't set via terraform, we
//use the containers friendlynameas a fallback which
//will keep changing as the devcontainer is recreated.
//TODO(mafredri): Parse the container label (i.e. devcontainer.json) for customization.
dc.Name =safeFriendlyName(dc.Container.FriendlyName)
//will attempt to create an agentnamebased on the workspace
//folder's name. If that is not possible, we will fall back
//to using the container's friendly name.
dc.Name =safeAgentName(filepath.Base(dc.WorkspaceFolder),dc.Container.FriendlyName)
}
}

Expand DownExpand Up@@ -633,6 +632,34 @@ func (api *API) processUpdatedContainersLocked(ctx context.Context, updated code
api.containersErr = nil
}

var consecutiveHyphenRegex = regexp.MustCompile("-+")

// `safeAgentName` returns a safe agent name derived from a folder name,
// falling back to the container’s friendly name if needed.
func safeAgentName(name string, friendlyName string) string {
// Keep only ASCII letters and digits, replacing everything
// else with a hyphen.
var sb strings.Builder
for _, r := range strings.ToLower(name) {
if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') {
_, _ = sb.WriteRune(r)
} else {
_, _ = sb.WriteRune('-')
}
}

// Remove any consecutive hyphens, and then trim any leading
// and trailing hyphens.
name = consecutiveHyphenRegex.ReplaceAllString(sb.String(), "-")
name = strings.Trim(name, "-")

if name == "" {
return safeFriendlyName(friendlyName)
}

return name
}

// safeFriendlyName returns a API safe version of the container's
// friendly name.
//
Expand DownExpand Up@@ -1114,27 +1141,6 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c
if proc.agent.ID == uuid.Nil || maybeRecreateSubAgent {
subAgentConfig.Architecture = arch

// Detect workspace folder by executing `pwd` in the container.
// NOTE(mafredri): This is a quick and dirty way to detect the
// workspace folder inside the container. In the future we will
// rely more on `devcontainer read-configuration`.
var pwdBuf bytes.Buffer
err = api.dccli.Exec(ctx, dc.WorkspaceFolder, dc.ConfigPath, "pwd", []string{},
WithExecOutput(&pwdBuf, io.Discard),
WithExecContainerID(container.ID),
)
if err != nil {
return xerrors.Errorf("check workspace folder in container: %w", err)
}
directory := strings.TrimSpace(pwdBuf.String())
if directory == "" {
logger.Warn(ctx, "detected workspace folder is empty, using default workspace folder",
slog.F("default_workspace_folder", DevcontainerDefaultContainerWorkspaceFolder),
)
directory = DevcontainerDefaultContainerWorkspaceFolder
}
subAgentConfig.Directory = directory

displayAppsMap := map[codersdk.DisplayApp]bool{
// NOTE(DanielleMaywood):
// We use the same defaults here as set in terraform-provider-coder.
Expand DownExpand Up@@ -1167,6 +1173,8 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c
return err
}

subAgentConfig.Directory = config.Workspace.WorkspaceFolder

// NOTE(DanielleMaywood):
// We only want to take an agent name specified in the root customization layer.
// This restricts the ability for a feature to specify the agent name. We may revisit
Expand Down
195 changes: 195 additions & 0 deletionsagent/agentcontainers/api_internal_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
package agentcontainers

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/coder/coder/v2/provisioner"
)

func TestSafeAgentName(t *testing.T) {
t.Parallel()

tests := []struct {
name string
folderName string
expected string
}{
// Basic valid names
{
folderName: "simple",
expected: "simple",
},
{
folderName: "with-hyphens",
expected: "with-hyphens",
},
{
folderName: "123numbers",
expected: "123numbers",
},
{
folderName: "mixed123",
expected: "mixed123",
},

// Names that need transformation
{
folderName: "With_Underscores",
expected: "with-underscores",
},
{
folderName: "With Spaces",
expected: "with-spaces",
},
{
folderName: "UPPERCASE",
expected: "uppercase",
},
{
folderName: "Mixed_Case-Name",
expected: "mixed-case-name",
},

// Names with special characters that get replaced
{
folderName: "special@#$chars",
expected: "special-chars",
},
{
folderName: "dots.and.more",
expected: "dots-and-more",
},
{
folderName: "multiple___underscores",
expected: "multiple-underscores",
},
{
folderName: "multiple---hyphens",
expected: "multiple-hyphens",
},

// Edge cases with leading/trailing special chars
{
folderName: "-leading-hyphen",
expected: "leading-hyphen",
},
{
folderName: "trailing-hyphen-",
expected: "trailing-hyphen",
},
{
folderName: "_leading_underscore",
expected: "leading-underscore",
},
{
folderName: "trailing_underscore_",
expected: "trailing-underscore",
},
{
folderName: "---multiple-leading",
expected: "multiple-leading",
},
{
folderName: "trailing-multiple---",
expected: "trailing-multiple",
},

// Complex transformation cases
{
folderName: "___very---complex@@@name___",
expected: "very-complex-name",
},
{
folderName: "my.project-folder_v2",
expected: "my-project-folder-v2",
},

// Empty and fallback cases - now correctly uses friendlyName fallback
{
folderName: "",
expected: "friendly-fallback",
},
{
folderName: "---",
expected: "friendly-fallback",
},
{
folderName: "___",
expected: "friendly-fallback",
},
{
folderName: "@#$",
expected: "friendly-fallback",
},

// Additional edge cases
{
folderName: "a",
expected: "a",
},
{
folderName: "1",
expected: "1",
},
{
folderName: "a1b2c3",
expected: "a1b2c3",
},
{
folderName: "CamelCase",
expected: "camelcase",
},
{
folderName: "snake_case_name",
expected: "snake-case-name",
},
{
folderName: "kebab-case-name",
expected: "kebab-case-name",
},
{
folderName: "mix3d_C4s3-N4m3",
expected: "mix3d-c4s3-n4m3",
},
{
folderName: "123-456-789",
expected: "123-456-789",
},
{
folderName: "abc123def456",
expected: "abc123def456",
},
{
folderName: " spaces everywhere ",
expected: "spaces-everywhere",
},
{
folderName: "unicode-café-naïve",
expected: "unicode-caf-na-ve",
},
{
folderName: "path/with/slashes",
expected: "path-with-slashes",
},
{
folderName: "file.tar.gz",
expected: "file-tar-gz",
},
{
folderName: "version-1.2.3-alpha",
expected: "version-1-2-3-alpha",
},
}

for _, tt := range tests {
t.Run(tt.folderName, func(t *testing.T) {
t.Parallel()
name := safeAgentName(tt.folderName, "friendly-fallback")

assert.Equal(t, tt.expected, name)
assert.True(t, provisioner.AgentNameRegex.Match([]byte(name)))
})
}
}
Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp