1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-18 05:18:24 +02:00
sap-jenkins-library/pkg/github/commit.go
Oliver Nocon d640d72dc6
feat: improve vulnerability reporting via GitHub issues (#3924)
* feat: improve vulnerability reporting via GitHub issues

* feat: update reports

* chore: add tls cert links

* only write log on error

* chore: update formatting

* chore: update handling of direct dependencies

* chore: fix linting issue

* chore: minor updates
2022-08-02 08:26:26 +02:00

46 lines
1.5 KiB
Go

package github
import (
"github.com/google/go-github/v45/github"
"github.com/pkg/errors"
)
// FetchCommitOptions to configure the lookup
type FetchCommitOptions struct {
APIURL string `json:"apiUrl,omitempty"`
Owner string `json:"owner,omitempty"`
Repository string `json:"repository,omitempty"`
Token string `json:"token,omitempty"`
SHA string `json:"sha,omitempty"`
TrustedCerts []string `json:"trustedCerts,omitempty"`
}
// FetchCommitResult to handle the lookup result
type FetchCommitResult struct {
Files int
Total int
Additions int
Deletions int
}
// https://docs.github.com/en/rest/reference/commits#get-a-commit
// FetchCommitStatistics looks up the statistics for a certain commit SHA.
func FetchCommitStatistics(options *FetchCommitOptions) (FetchCommitResult, error) {
// create GitHub client
ctx, client, err := NewClient(options.Token, options.APIURL, "", options.TrustedCerts)
if err != nil {
return FetchCommitResult{}, errors.Wrap(err, "failed to get GitHub client")
}
// fetch commit by SAH
result, _, err := client.Repositories.GetCommit(ctx, options.Owner, options.Repository, options.SHA, &github.ListOptions{})
if err != nil {
return FetchCommitResult{}, errors.Wrap(err, "failed to get GitHub commit")
}
return FetchCommitResult{
Files: len(result.Files),
Total: result.Stats.GetTotal(),
Additions: result.Stats.GetAdditions(),
Deletions: result.Stats.GetDeletions(),
}, nil
}