Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/patch-aw-yml-shared-js-resources.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion docs/src/content/docs/reference/aw-yml-package-manifest.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,12 @@ If no installable workflow files are resolved, validation fails.

## Resources

The `resources` field installs inert repository assets. Each entry maps a package-relative `source` to a repository-relative `destination`. Supported destinations are:
The `resources` field copies repository assets as-is without executing them during installation. Each entry maps a package-relative `source` to a repository-relative `destination`. Supported destinations are:

- Direct children of `.github/ISSUE_TEMPLATE/` with a `.yml` or `.yaml` extension
- `.github/CODEOWNERS`
- Files under `.github/aw/`
- `.mjs` and `.cjs` helper scripts under `.github/workflows/shared/`

Resource destinations must be unique, including case-insensitive comparisons. Path traversal, symbolic links, non-regular local files, and destinations outside the allowlist are rejected. Installed resources are tracked with package-scoped ownership metadata in `.github/aw/packages/*.json`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ Resource destinations are restricted to non-hook repository asset namespaces:
- `.github/ISSUE_TEMPLATE/*.yaml`
- `.github/CODEOWNERS`
- `.github/aw/**`
- `.mjs` files under `.github/workflows/shared/`
- `.cjs` files under `.github/workflows/shared/`

Implementations MUST reject duplicate or case-insensitive duplicate resource destinations before writing files. Resources are copied as inert content from the selected package ref; installers MUST NOT execute package-provided scripts or expose configured secrets to package content during installation.

Expand Down
52 changes: 52 additions & 0 deletions pkg/cli/add_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,58 @@ func TestAddWorkflowsWithTracking_PackageResourceWritesOwnershipRecord(t *testin
assert.Contains(t, string(record), `"sha256":`)
}

func TestAddWorkflowsWithTracking_PackageSharedScriptResourceWritesOwnershipRecord(t *testing.T) {
tempDir := testutil.TempDir(t, "test-package-shared-script-resource-*")
setupMinimalGitRepo(t, tempDir)

resourceContent := []byte("export function run() {}\n")
workflows := []*ResolvedWorkflow{
{
Spec: &WorkflowSpec{
RepoSpec: RepoSpec{
RepoSlug: "owner/repo",
Version: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
PackagePath: "packages/repo-assist",
},
WorkflowPath: "packages/repo-assist/shared/runtime.mjs",
WorkflowName: "runtime",
DestinationPath: ".github/workflows/shared/runtime.mjs",
FromRepositoryManifest: true,
IsPackageResourceFile: true,
},
Content: resourceContent,
SourceInfo: &FetchedWorkflow{
Content: resourceContent,
CommitSHA: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
IsLocal: false,
SourcePath: "packages/repo-assist/shared/runtime.mjs",
},
IsPackageResourceFile: true,
},
}

err := addWorkflowsWithTracking(context.Background(), workflows, NewFileTracker(), AddOptions{
NoGitattributes: true,
DisableSecurityScanner: true,
Quiet: true,
})
require.NoError(t, err)

resourcePath := filepath.Join(tempDir, ".github", "workflows", "shared", "runtime.mjs")
written, err := os.ReadFile(resourcePath)
require.NoError(t, err)
assert.Equal(t, resourceContent, written)

recordFiles, err := filepath.Glob(filepath.Join(tempDir, ".github", "aw", "packages", "*.json"))
require.NoError(t, err)
require.Len(t, recordFiles, 1)
record, err := os.ReadFile(recordFiles[0])
require.NoError(t, err)
assert.Contains(t, string(record), `"source": "owner/repo/packages/repo-assist@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"`)
assert.Contains(t, string(record), `"destination": ".github/workflows/shared/runtime.mjs"`)
assert.Contains(t, string(record), `"sha256":`)
}

func TestAddWorkflowsWithTracking_PackageResourceRejectsLocalDrift(t *testing.T) {
tempDir := testutil.TempDir(t, "test-package-resource-drift-*")
setupMinimalGitRepo(t, tempDir)
Expand Down
7 changes: 6 additions & 1 deletion pkg/cli/add_package_manifest_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func parseManifestResourceMapping(mapping map[string]any, manifestPath string) (

func validateManifestResourceDestination(destination string) error {
switch {
case strings.HasPrefix(destination, constants.WorkflowsDirSlash+"shared/"):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[/codebase-design] The new shared/ case duplicates the extension-matching logic already present in isSharedWorkflowScriptDestination (in add_package_ownership.go) with slightly different mechanics (strings.HasPrefix+TrimPrefix here vs. filepath.Clean+ToSlash there). Two independent implementations of "what counts as a shared JS resource" is a seam that will drift silently if one is updated later.

💡 Suggested consolidation

Extract a single helper, e.g. isSharedWorkflowScriptPath(destination string) (remaining string, ok bool), used by both validateManifestResourceDestination and isSharedWorkflowScriptDestination/isPackageResourceDestination. This keeps the acceptance rule (prefix + .mjs/.cjs suffix) defined in exactly one place, matching the deep-module principle of a single source of truth for a piece of domain logic reused across the manifest-parsing and ownership-tracking modules.

@copilot please address this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validateManifestResourceDestination now delegates to isSharedWorkflowScriptDestination for the shared-script acceptance check, so the prefix+extension rule is defined in one place (7730687).

if !isSharedWorkflowScriptDestination(destination) {
return errorsForResourceDestination()
}
return nil
case strings.HasPrefix(destination, constants.GithubDir+"ISSUE_TEMPLATE/"):
remaining := strings.TrimPrefix(destination, constants.GithubDir+"ISSUE_TEMPLATE/")
if remaining == "" || strings.Contains(remaining, "/") {
Expand All @@ -92,7 +97,7 @@ func validateManifestResourceDestination(destination string) error {
}

func errorsForResourceDestination() error {
return errors.New("destinations must be .github/CODEOWNERS, .github/ISSUE_TEMPLATE/*.yml, .github/ISSUE_TEMPLATE/*.yaml, or under .github/aw/")
return errors.New("destinations must be .github/CODEOWNERS, .github/ISSUE_TEMPLATE/*.yml, .github/ISSUE_TEMPLATE/*.yaml, .github/workflows/shared/**/*.mjs, .github/workflows/shared/**/*.cjs, or under .github/aw/")
}

func normalizePackageResourcePaths(resources []repositoryPackageResource, packagePath string) []resolvedPackageResource {
Expand Down
46 changes: 46 additions & 0 deletions pkg/cli/add_package_manifest_resources_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//go:build !integration

package cli

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

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

for _, destination := range []string{
".github/workflows/shared/runtime.mjs",
".github/workflows/shared/lib/runtime.cjs",
".github/workflows/shared/runtime.MJS",
} {
t.Run(destination, func(t *testing.T) {
t.Parallel()
require.NoError(t, validateManifestResourceDestination(destination))
assert.True(t, isPackageResourceDestination(destination))
})
}

for _, destination := range []string{
".github/workflows/shared/runtime.js",
".github/workflows/shared/runtime.json",
".github/workflows/shared/runtime.md",
".github/workflows/shared/",
".github/workflows/shared-adjacent/runtime.mjs",
} {
t.Run(destination, func(t *testing.T) {
t.Parallel()
require.Error(t, validateManifestResourceDestination(destination))
assert.False(t, isPackageResourceDestination(destination))
})
}

_, err := parseManifestResourceMapping(map[string]any{
"source": "runtime.mjs",
"destination": ".github/workflows/shared/../runtime.mjs",
}, "aw.yml")
require.Error(t, err)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[/tdd] Good unit coverage for validateManifestResourceDestination, but there's no end-to-end test proving a .mjs/.cjs resource actually gets written to disk and recorded in package ownership metadata (the existing TestAddWorkflowsWithTracking_PackageResourceWritesOwnershipRecord pattern in add_command_test.go only covers the ISSUE_TEMPLATE destination type).

💡 Suggested addition

Add a variant of TestAddWorkflowsWithTracking_PackageResourceWritesOwnershipRecord (or extend it via table-driven cases) using DestinationPath: ".github/workflows/shared/runtime.mjs", asserting the file is written verbatim and the ownership record's destination field matches. This closes the gap between "destination string is accepted" and "the new resource type round-trips through install + ownership + removal correctly," which is the actual behavior users depend on.

@copilot please address this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added TestAddWorkflowsWithTracking_PackageSharedScriptResourceWritesOwnershipRecord covering the .mjs destination end-to-end (write to disk + ownership record) in 7730687.

9 changes: 9 additions & 0 deletions pkg/cli/add_package_ownership.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,9 +583,18 @@ func isPackageResourceDestination(destination string) bool {
return strings.EqualFold(destination, constants.GithubDir+"CODEOWNERS") ||
strings.HasPrefix(destination, constants.GithubDir+"ISSUE_TEMPLATE/") ||
strings.HasPrefix(destination, constants.GithubDir+"aw/") ||
isSharedWorkflowScriptDestination(destination) ||
workflow.IsValidOperationalValueEvaluatorRunPath(destination)
}

func isSharedWorkflowScriptDestination(destination string) bool {
cleaned := filepath.ToSlash(filepath.Clean(destination))
lower := strings.ToLower(cleaned)
prefix := constants.WorkflowsDirSlash + "shared/"
return strings.HasPrefix(cleaned, prefix) &&
(strings.HasSuffix(lower, ".mjs") || strings.HasSuffix(lower, ".cjs"))
}

func removePackageOwnedFilesIfUnused(packageBase string) error {
gitRoot, err := gitutil.FindGitRoot()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/parser/schemas/aw_manifest_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
},
"resources": {
"type": "array",
"description": "Declarative repository assets installed as-is from immutable package content. Each entry maps a package-relative source to an allowlisted repository destination.",
"description": "Repository assets copied as-is from immutable package content. Each entry maps a package-relative source to an allowlisted repository destination, including .mjs and .cjs helper scripts under .github/workflows/shared/.",
"items": {
"type": "object",
"required": ["source", "destination"],
Expand Down