|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "os" |
5 | 6 | "path/filepath" |
6 | 7 | "strings" |
7 | 8 | "testing" |
8 | 9 |
|
| 10 | + "github.com/github/gh-actions-lock/cmd/gh-actions-lock/format" |
9 | 11 | "github.com/github/gh-actions-lock/internal/ghapi/httpmock" |
10 | 12 | "github.com/stretchr/testify/assert" |
11 | 13 | "github.com/stretchr/testify/require" |
@@ -275,3 +277,82 @@ jobs: |
275 | 277 | assert.NotContains(t, got, "$/", "opt-out means no migration to $/") |
276 | 278 | }) |
277 | 279 | } |
| 280 | + |
| 281 | +func TestMigrateLocalActions_UnreadableWorkflowAborts(t *testing.T) { |
| 282 | + dir := t.TempDir() |
| 283 | + require.NoError(t, os.Mkdir(filepath.Join(dir, ".git"), 0o755)) |
| 284 | + write := func(rel, content string) string { |
| 285 | + full := filepath.Join(dir, rel) |
| 286 | + require.NoError(t, os.MkdirAll(filepath.Dir(full), 0o755)) |
| 287 | + require.NoError(t, os.WriteFile(full, []byte(content), 0o644)) |
| 288 | + return full |
| 289 | + } |
| 290 | + write("local/action.yml", "runs:\n using: composite\n") |
| 291 | + const good = "jobs:\n build:\n steps:\n - uses: ./local\n" |
| 292 | + goodPath := write(".github/workflows/good.yml", good) |
| 293 | + lockedPath := write(".github/workflows/locked.yml", good) |
| 294 | + require.NoError(t, os.Chmod(lockedPath, 0o000)) |
| 295 | + t.Cleanup(func() { _ = os.Chmod(lockedPath, 0o644) }) |
| 296 | + if _, err := os.ReadFile(lockedPath); err == nil { |
| 297 | + t.Skip("file permissions not enforced for this user") |
| 298 | + } |
| 299 | + |
| 300 | + total, _, err := migrateLocalActions([]string{goodPath, lockedPath}) |
| 301 | + |
| 302 | + require.Error(t, err) |
| 303 | + assert.ErrorIs(t, err, os.ErrPermission) |
| 304 | + assert.Zero(t, total) |
| 305 | + got, readErr := os.ReadFile(goodPath) |
| 306 | + require.NoError(t, readErr) |
| 307 | + assert.Equal(t, good, string(got)) |
| 308 | +} |
| 309 | + |
| 310 | +// TestMigrateLocalActions_UnparseableWorkflowIsAFinding: a workflow that |
| 311 | +// won't parse doesn't abort migration; it's reported like a default run. |
| 312 | +func TestMigrateLocalActions_UnparseableWorkflowIsAFinding(t *testing.T) { |
| 313 | + reg := &httpmock.Registry{} |
| 314 | + defer reg.Verify(t) |
| 315 | + setupGoSHA := "4a3601121dd01d1626a1e23e37211e3254c1c06c" |
| 316 | + reg.Register( |
| 317 | + httpmock.GraphQLForRepo("actions", "setup-go"), |
| 318 | + httpmock.JSONResponse(map[string]any{ |
| 319 | + "data": map[string]any{ |
| 320 | + "a0": testRepoResponse("actions/setup-go", setupGoSHA, nodeActionYAML), |
| 321 | + }, |
| 322 | + }), |
| 323 | + ) |
| 324 | + |
| 325 | + dir := t.TempDir() |
| 326 | + write := func(rel, content string) { |
| 327 | + full := filepath.Join(dir, rel) |
| 328 | + require.NoError(t, os.MkdirAll(filepath.Dir(full), 0o755)) |
| 329 | + require.NoError(t, os.WriteFile(full, []byte(content), 0o600)) |
| 330 | + } |
| 331 | + require.NoError(t, os.Mkdir(filepath.Join(dir, ".git"), 0o755)) |
| 332 | + const good = "name: CI\non: push\njobs:\n build:\n runs-on: ubuntu-latest\n steps:\n - uses: ./.github/actions/foo\n" |
| 333 | + write(".github/workflows/good.yml", good) |
| 334 | + write(".github/workflows/bad.yml", "name: [\n") |
| 335 | + write(".github/actions/foo/action.yml", |
| 336 | + "name: Foo\nruns:\n using: composite\n steps:\n - uses: actions/setup-go@v6\n") |
| 337 | + t.Chdir(dir) |
| 338 | + |
| 339 | + stdout, _, err := runCommandWithHTTP(t, reg, "--migrate-local-actions", "--json=valid,findings") |
| 340 | + |
| 341 | + require.ErrorIs(t, err, errSilent, "the load error blocks the write like a default run") |
| 342 | + var payload struct { |
| 343 | + Valid bool `json:"valid"` |
| 344 | + Findings []format.Finding `json:"findings"` |
| 345 | + } |
| 346 | + require.NoError(t, json.Unmarshal([]byte(stdout), &payload)) |
| 347 | + assert.False(t, payload.Valid) |
| 348 | + var loadErrors []string |
| 349 | + for _, f := range payload.Findings { |
| 350 | + if strings.Contains(f.Detail, "failed to load workflow") { |
| 351 | + loadErrors = append(loadErrors, f.Workflow) |
| 352 | + } |
| 353 | + } |
| 354 | + assert.Equal(t, []string{".github/workflows/bad.yml"}, loadErrors) |
| 355 | + got, readErr := os.ReadFile(filepath.Join(dir, ".github", "workflows", "good.yml")) |
| 356 | + require.NoError(t, readErr) |
| 357 | + assert.Equal(t, good, string(got), "a blocked run rolls back the migration") |
| 358 | +} |
0 commit comments