Skip to content
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
11 changes: 10 additions & 1 deletion sourcetool/cmd/checklevelprov.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,17 @@ func doCheckLevelProv(checkLevelProvArgs CheckLevelProvArgs) {
ver_options.ExpectedSan = checkLevelProvArgs.expectedSan
}

prevCommit := checkLevelProvArgs.prevCommit
var err error
if prevCommit == "" {
prevCommit, err = gh_connection.GetPriorCommit(ctx, checkLevelProvArgs.commit)
if err != nil {
log.Fatal(err)
}
}

pa := attest.NewProvenanceAttestor(gh_connection, ver_options)
prov, err := pa.CreateSourceProvenance(ctx, checkLevelProvArgs.prevBundlePath, checkLevelProvArgs.commit, checkLevelProvArgs.prevCommit)
prov, err := pa.CreateSourceProvenance(ctx, checkLevelProvArgs.prevBundlePath, checkLevelProvArgs.commit, prevCommit)
if err != nil {
log.Fatal(err)
}
Expand Down
20 changes: 20 additions & 0 deletions sourcetool/pkg/gh_control/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,23 @@ func (ghc *GitHubConnection) GetLatestCommit(ctx context.Context) (string, error
}
return *branch.Commit.SHA, nil
}

// Gets the previous commit to 'sha' if it has one.
// If there are more than one parents this fails with an error.
// (This tool generally operates in an environment of linear history)
func (ghc *GitHubConnection) GetPriorCommit(ctx context.Context, sha string) (string, error) {
commit, _, err := ghc.Client.Git.GetCommit(ctx, ghc.Owner, ghc.Repo, sha)
if err != nil {
return "", fmt.Errorf("cannot get commit data for %s: %w", sha, err)
}

if len(commit.Parents) == 0 {
return "", fmt.Errorf("there is no commit earlier than %s, that isn't yet supported", sha)
}

if len(commit.Parents) > 1 {
return "", fmt.Errorf("commit %s has more than one parent (%v), which is not supported", sha, commit.Parents)
}

return *commit.Parents[0].SHA, nil
}