Skip to content

Commit

Permalink
Resolve commit ref correctly. (#212)
Browse files Browse the repository at this point in the history
The command `frizbee actions <action>@<tag>` incorrectly retrieves the
tag's ref rather than the commit's one. The tag's ref should instead
be used to retrieve the details of the tag, which contain the ref of
the commit.

This change changes `getCheckSumForTag` to lookup the right field
using two subsequent call to GitHub.

Fixes #206
  • Loading branch information
blkt authored Nov 18, 2024
1 parent c2a623a commit fbef552
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
35 changes: 26 additions & 9 deletions pkg/replacer/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,23 @@ func getCheckSumForTag(ctx context.Context, restIf interfaces.REST, owner, repo,
return "", fmt.Errorf("failed to join path: %w", err)
}

return doGetReference(ctx, restIf, path)
sha, otype, err := doGetReference(ctx, restIf, path)
if err != nil {
return "", err
}

if otype == "commit" {
return sha, nil
}

// assume otype == "tag"
path, err = url.JoinPath("repos", owner, repo, "git", "tags", sha)
if err != nil {
return "", fmt.Errorf("failed to join path: %w", err)
}

sha, _, err = doGetReference(ctx, restIf, path)
return sha, err
}

func getCheckSumForBranch(ctx context.Context, restIf interfaces.REST, owner, repo, branch string) (string, error) {
Expand All @@ -323,7 +339,8 @@ func getCheckSumForBranch(ctx context.Context, restIf interfaces.REST, owner, re
return "", fmt.Errorf("failed to join path: %w", err)
}

return doGetReference(ctx, restIf, path)
sha, _, err := doGetReference(ctx, restIf, path)
return sha, err
}

func excludeBranch(excludes []string, branch string) bool {
Expand All @@ -337,10 +354,10 @@ func excludeBranch(excludes []string, branch string) bool {
return slices.Contains(excludes, branch)
}

func doGetReference(ctx context.Context, restIf interfaces.REST, path string) (string, error) {
func doGetReference(ctx context.Context, restIf interfaces.REST, path string) (string, string, error) {
req, err := restIf.NewRequest(http.MethodGet, path, nil)
if err != nil {
return "", fmt.Errorf("cannot create REST request: %w", err)
return "", "", fmt.Errorf("cannot create REST request: %w", err)
}

resp, err := restIf.Do(ctx, req)
Expand All @@ -352,20 +369,20 @@ func doGetReference(ctx context.Context, restIf interfaces.REST, path string) (s
}

if err != nil && resp.StatusCode != http.StatusNotFound {
return "", fmt.Errorf("failed to do API request: %w", err)
return "", "", fmt.Errorf("failed to do API request: %w", err)
} else if resp.StatusCode == http.StatusNotFound {
// No error, but no tag found
return "", nil
return "", "", nil
}

var t github.Reference
err = json.NewDecoder(resp.Body).Decode(&t)
if err != nil && strings.Contains(err.Error(), "cannot unmarshal array into Go value of type") {
// This is a branch, not a tag
return "", nil
return "", "", nil
} else if err != nil {
return "", fmt.Errorf("canont decode response: %w", err)
return "", "", fmt.Errorf("canont decode response: %w", err)
}

return t.GetObject().GetSHA(), nil
return t.GetObject().GetSHA(), t.GetObject().GetType(), nil
}
8 changes: 4 additions & 4 deletions pkg/replacer/replacer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ jobs:
steps:
- uses: ./minder/server.yml # this should not be replaced
- uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2
- uses: xt0rted/markdownlint-problem-matcher@c17ca40d1376f60aba7e7d38a8674a3f22f7f5b0 # v1
- uses: xt0rted/markdownlint-problem-matcher@b643b0751c371f357690337d4549221347c0e1bc # v1
- name: "Run Markdown linter"
uses: docker://index.docker.io/avtodev/markdown-lint@sha256:6aeedc2f49138ce7a1cd0adffc1b1c0321b841dc2102408967d9301c031949ee # v1
with:
Expand Down Expand Up @@ -905,7 +905,7 @@ jobs:
steps:
- uses: ./minder/server.yml # this should not be replaced
- uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2
- uses: xt0rted/markdownlint-problem-matcher@c17ca40d1376f60aba7e7d38a8674a3f22f7f5b0 # v1
- uses: xt0rted/markdownlint-problem-matcher@b643b0751c371f357690337d4549221347c0e1bc # v1
`,
modified: true,
},
Expand Down Expand Up @@ -1398,7 +1398,7 @@ jobs:
steps:
- uses: ./minder/server.yml # this should not be replaced
- uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2
- uses: xt0rted/markdownlint-problem-matcher@c17ca40d1376f60aba7e7d38a8674a3f22f7f5b0 # v1
- uses: xt0rted/markdownlint-problem-matcher@b643b0751c371f357690337d4549221347c0e1bc # v1
`,
expected: &ListResult{
Entities: []interfaces.EntityRef{
Expand All @@ -1409,7 +1409,7 @@ jobs:
},
{
Name: "xt0rted/markdownlint-problem-matcher",
Ref: "c17ca40d1376f60aba7e7d38a8674a3f22f7f5b0",
Ref: "b643b0751c371f357690337d4549221347c0e1bc",
Type: actions.ReferenceType,
},
},
Expand Down

0 comments on commit fbef552

Please sign in to comment.