From aae8b7a496417d6c8267bc679d74febbc911eb6b Mon Sep 17 00:00:00 2001 From: Michelangelo Mori Date: Fri, 15 Nov 2024 21:16:48 +0100 Subject: [PATCH] Resolve commit ref correctly. The command `frizbee actions @` 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 --- pkg/replacer/actions/actions.go | 35 ++++++++++++++++++++++++--------- pkg/replacer/replacer_test.go | 8 ++++---- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/pkg/replacer/actions/actions.go b/pkg/replacer/actions/actions.go index 251211f..43651f4 100644 --- a/pkg/replacer/actions/actions.go +++ b/pkg/replacer/actions/actions.go @@ -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) { @@ -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 { @@ -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) @@ -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 } diff --git a/pkg/replacer/replacer_test.go b/pkg/replacer/replacer_test.go index 26974be..bcf69bc 100644 --- a/pkg/replacer/replacer_test.go +++ b/pkg/replacer/replacer_test.go @@ -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: @@ -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, }, @@ -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{ @@ -1409,7 +1409,7 @@ jobs: }, { Name: "xt0rted/markdownlint-problem-matcher", - Ref: "c17ca40d1376f60aba7e7d38a8674a3f22f7f5b0", + Ref: "b643b0751c371f357690337d4549221347c0e1bc", Type: actions.ReferenceType, }, },