Skip to content

Commit

Permalink
Ensure pinning is idempotent (#22)
Browse files Browse the repository at this point in the history
Previously, running "pin" on an already-pinned file would lose the original ratchet references.
  • Loading branch information
sethvargo authored May 27, 2022
1 parent d3b4f23 commit bc83ec3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
2 changes: 1 addition & 1 deletion command/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ hashed version for the given input file:
actions/checkout@v3 -> actions/checkout@2541b1294d2704b0964813337f...
The original unpinned version is preserved in a comment, next to the pinned
version.
version. If a version is already pinned, it does nothing.
To update versions that are already pinned, use the "update" command instead.
Expand Down
17 changes: 8 additions & 9 deletions parser/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,16 @@ func (a *Actions) Parse(m *yaml.Node) (*RefsList, error) {
for k, property := range step.Content {
if property.Value == "uses" {
uses := step.Content[k+1]
// Only include references to remove workflows. This could be a
// local workflow, which should not be pinned.
// Only include references to remove workflows. This could be
// a local workflow, which should not be pinned.
switch {
case strings.HasPrefix(uses.Value, "docker://"):
ref := resolver.NormalizeContainerRef(uses.Value)
refs.Add(ref, uses)
case strings.Contains(uses.Value, "@"):
ref := resolver.NormalizeActionsRef(uses.Value)
refs.Add(ref, uses)
case strings.Contains(uses.Value, "docker://"):
ref := resolver.NormalizeContainerRef(uses.Value)
refs.Add(ref, uses)
}
break
}
}
}
Expand All @@ -114,12 +113,12 @@ func (a *Actions) Parse(m *yaml.Node) (*RefsList, error) {
// Only include references to remove workflows. This could be a
// local workflow, which should not be pinned.
switch {
case strings.HasPrefix(uses.Value, "docker://"):
ref := resolver.NormalizeContainerRef(uses.Value)
refs.Add(ref, uses)
case strings.Contains(uses.Value, "@"):
ref := resolver.NormalizeActionsRef(uses.Value)
refs.Add(ref, uses)
case strings.Contains(uses.Value, "docker://"):
ref := resolver.NormalizeContainerRef(uses.Value)
refs.Add(ref, uses)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions parser/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
steps:
- uses: 'actions/checkout@v3'
- uses: 'docker://ubuntu:20.04'
- uses: 'docker://ubuntu@sha256:47f14534bda344d9fe6ffd6effb95eefe579f4be0d508b7445cf77f61a0e5724'
with:
uses: 'foo/bar@v0'
other_job:
Expand All @@ -39,6 +40,7 @@ jobs:
"actions://actions/checkout@v3",
"actions://org/repo/.github/workflows/other@v0",
"container://ubuntu:20.04",
"container://ubuntu@sha256:47f14534bda344d9fe6ffd6effb95eefe579f4be0d508b7445cf77f61a0e5724",
},
},
{
Expand Down
8 changes: 8 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ func Pin(ctx context.Context, res resolver.Resolver, parser Parser, m *yaml.Node
}
refs := refsList.All()

// Remove any absolute references from the list. We do not want to pin
// absolute references since they are already pinned.
for ref := range refs {
if isAbsolute(ref) {
delete(refs, ref)
}
}

sem := semaphore.NewWeighted(concurrency)

var merrLock sync.Mutex
Expand Down
27 changes: 26 additions & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,15 @@ func TestPin(t *testing.T) {
ctx := context.Background()

res, err := resolver.NewTest(map[string]*resolver.TestResult{
"actions://good/repo@v0": {Resolved: "good/repo@a12a3943"},
"actions://good/repo@v0": {
Resolved: "good/repo@a12a3943",
},
"actions://good/repo@2541b1294d2704b0964813337f33b291d3f8596b": {
Resolved: "good/repo@2541b1294d2704b0964813337f33b291d3f8596b",
},
"container://ubuntu@sha256:47f14534bda344d9fe6ffd6effb95eefe579f4be0d508b7445cf77f61a0e5724": {
Resolved: "ubuntu@sha256:47f14534bda344d9fe6ffd6effb95eefe579f4be0d508b7445cf77f61a0e5724",
},
})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -138,6 +146,23 @@ jobs:
my_job:
steps:
- uses: 'good/repo@a12a3943' # this is a comment ratchet:good/repo@v0
`,
},
{
name: "already_pinned",
in: `
jobs:
my_job:
steps:
- uses: 'good/repo@2541b1294d2704b0964813337f33b291d3f8596b' # ratchet:good/repo@v0
- uses: 'docker://ubuntu@sha256:47f14534bda344d9fe6ffd6effb95eefe579f4be0d508b7445cf77f61a0e5724' # ratchet:docker://ubuntu:20.04
`,
exp: `
jobs:
my_job:
steps:
- uses: 'good/repo@2541b1294d2704b0964813337f33b291d3f8596b' # ratchet:good/repo@v0
- uses: 'docker://ubuntu@sha256:47f14534bda344d9fe6ffd6effb95eefe579f4be0d508b7445cf77f61a0e5724' # ratchet:docker://ubuntu:20.04
`,
},
{
Expand Down

0 comments on commit bc83ec3

Please sign in to comment.