Skip to content

Commit 8bd18b3

Browse files
authored
Fix file path when fetching notes (#184)
* Fix file path when fetching notes This commit fixes the path we request from github to retrieve a commit's notes. Signed-off-by: Adolfo Garcia Veytia (puerco) <puerco@carabiner.dev> * Add local GetNotesForCommit unit test This commit adds a unit test for the GetNotesForCommit func that connects to the github api. It will be skipped if no token is set in the environment. Signed-off-by: Adolfo Garcia Veytia (puerco) <puerco@carabiner.dev> * Fix bug passing token from env Signed-off-by: Adolfo Garcia Veytia (puerco) <puerco@carabiner.dev> --------- Signed-off-by: Adolfo Garcia Veytia (puerco) <puerco@carabiner.dev>
1 parent ebb42c5 commit 8bd18b3

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

sourcetool/pkg/gh_control/connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func NewGhConnectionWithClient(owner, repo, ref string, client *github.Client) *
3737
owner: owner,
3838
repo: repo,
3939
ref: ref,
40-
Options: defaultOptions,
40+
Options: opts,
4141
}
4242
}
4343

sourcetool/pkg/gh_control/notes.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ 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.
1112
func (ghc *GitHubConnection) GetNotesForCommit(ctx context.Context, commit string) (string, error) {
1213
// We can find the notes for a given commit fairly easily.
13-
// They'll be in the path <commit> within ref `refs/notes/commits`
14-
14+
// They'll be in the path <co/mmit> within ref `refs/notes/commits`
15+
// where the first two characters of the commit sha are separated from the
16+
// rest with a slash, eg e5/73149ab3e574abc2e5a151a04acfaf2a59b453.
17+
path := commit[0:2] + "/" + commit[2:]
1518
contents, _, resp, err := ghc.Client().Repositories.GetContents(
16-
ctx, ghc.Owner(), ghc.Repo(), commit, &github.RepositoryContentGetOptions{Ref: "refs/notes/commits"})
19+
ctx, ghc.Owner(), ghc.Repo(), path, &github.RepositoryContentGetOptions{Ref: "refs/notes/commits"})
1720

1821
if resp.StatusCode == http.StatusNotFound {
1922
// Don't freak out if it's not there.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package gh_control
2+
3+
import (
4+
"context"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestGetNotesForCommit(t *testing.T) {
12+
if tk := os.Getenv(tokenEnvVar); tk == "" {
13+
t.Log("Skipping API test as no token is set")
14+
t.Skip()
15+
}
16+
t.Parallel()
17+
for _, tc := range []struct {
18+
name string
19+
owner string
20+
repo string
21+
commit string
22+
mustBeEmpty bool
23+
mustErr bool
24+
}{
25+
{name: "success", owner: "slsa-framework", repo: "slsa-source-poc", commit: "e573149ab3e574abc2e5a151a04acfaf2a59b453", mustBeEmpty: false, mustErr: false},
26+
{name: "non-existent", owner: "kjsdhi373iuh", repo: "lksjdhfk3773", commit: "invalid", mustBeEmpty: true, mustErr: false},
27+
} {
28+
t.Run(tc.name, func(t *testing.T) {
29+
t.Parallel()
30+
ghc := NewGhConnection(tc.owner, tc.repo, "main")
31+
notes, err := ghc.GetNotesForCommit(context.Background(), tc.commit)
32+
if tc.mustErr {
33+
require.Error(t, err)
34+
return
35+
}
36+
require.NoError(t, err, tc.commit)
37+
if tc.mustBeEmpty {
38+
require.Empty(t, notes, "Commit: %s", tc.commit)
39+
} else {
40+
require.NotEmpty(t, notes, "Commit: %s", tc.commit)
41+
}
42+
})
43+
}
44+
}

0 commit comments

Comments
 (0)