Skip to content

Commit 00d635a

Browse files
authored
Support unsharded notes blobs (#216)
This commit adds support for reading notes stored as top-level files in addition to sharded subdirectories. Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]>
1 parent 3386996 commit 00d635a

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

sourcetool/pkg/ghcontrol/notes.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
"github.com/google/go-github/v69/github"
99
)
1010

11-
// GetNotesForCommit returns the unparsed notes blob for a commit as stored in git via the GitHub API.
11+
// GetNotesForCommit returns the unparsed notes blob for a commit as stored in
12+
// git via the GitHub API. If no notes data can be found at the specified commit
13+
// GetNotesForCommit returns a blank string (and no error).
1214
func (ghc *GitHubConnection) GetNotesForCommit(ctx context.Context, commit string) (string, error) {
1315
// We can find the notes for a given commit fairly easily.
1416
// They'll be in the path <co/mmit> within ref `refs/notes/commits`
@@ -19,8 +21,19 @@ func (ghc *GitHubConnection) GetNotesForCommit(ctx context.Context, commit strin
1921
ctx, ghc.Owner(), ghc.Repo(), path, &github.RepositoryContentGetOptions{Ref: "refs/notes/commits"})
2022

2123
if resp.StatusCode == http.StatusNotFound {
22-
// Don't freak out if it's not there.
23-
return "", nil
24+
// If we got a 404, look for the note in a file at the top-level
25+
// directory of refs/notes/commits. We brute force this call after
26+
// trying the sharded path above as notes will be found using this
27+
// path only when there is a small number of notes in the repo.
28+
//
29+
// See https://github.com/slsa-framework/slsa-source-poc/issues/215
30+
contents, _, resp, err = ghc.Client().Repositories.GetContents(
31+
ctx, ghc.Owner(), ghc.Repo(), commit,
32+
&github.RepositoryContentGetOptions{Ref: "refs/notes/commits"},
33+
)
34+
if resp.StatusCode == http.StatusNotFound {
35+
return "", nil
36+
}
2437
}
2538
if err != nil {
2639
return "", fmt.Errorf("cannot get note contents for commit %s: %w", commit, err)
@@ -29,6 +42,5 @@ func (ghc *GitHubConnection) GetNotesForCommit(ctx context.Context, commit strin
2942
// No notes stored for this commit.
3043
return "", nil
3144
}
32-
3345
return contents.GetContent()
3446
}

0 commit comments

Comments
 (0)