Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve commit ref correctly. #212

Merged
merged 1 commit into from
Nov 18, 2024
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
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
Loading