8
8
"github.com/google/go-github/v69/github"
9
9
)
10
10
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).
12
14
func (ghc * GitHubConnection ) GetNotesForCommit (ctx context.Context , commit string ) (string , error ) {
13
15
// We can find the notes for a given commit fairly easily.
14
16
// 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
19
21
ctx , ghc .Owner (), ghc .Repo (), path , & github.RepositoryContentGetOptions {Ref : "refs/notes/commits" })
20
22
21
23
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
+ }
24
37
}
25
38
if err != nil {
26
39
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
29
42
// No notes stored for this commit.
30
43
return "" , nil
31
44
}
32
-
33
45
return contents .GetContent ()
34
46
}
0 commit comments