Skip to content

Commit

Permalink
Finishing Touches
Browse files Browse the repository at this point in the history
Signed-off-by: nathannaveen <42319948+nathannaveen@users.noreply.github.com>
  • Loading branch information
nathannaveen committed Dec 9, 2023
1 parent 7c130c4 commit 1e8f2b3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 42 deletions.
91 changes: 57 additions & 34 deletions cmd/guaccollect/cmd/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ import (
"github.com/spf13/viper"
)

const (
githubMode = "github-mode"
githubSbom = "github-sbom"
githubWorkflowFile = "github-workflow-file"
)

type githubOptions struct {
// datasource for the collector
dataSource datasource.CollectSource
Expand All @@ -54,20 +60,22 @@ type githubOptions struct {
}

var githubCmd = &cobra.Command{
Use: "github [flags] release_url1 release_url2...",
Short: "takes github repos and tags to download metadata documents stored in Github releases to add to GUAC graph",
Args: cobra.MinimumNArgs(0),
Use: "github <subject> is in the form of <release_url> for release mode or <owner>/<repo> for workflow mode",
Short: "Takes github repos and tags to download metadata documents stored in Github releases to add to GUAC graph.",
Long: `Takes github repos and tags to download metadata documents stored in Github releases to add to GUAC graph.
<github-mode> must be either "workflow", "release", or "". If "", then the default is "release".
if <github-mode> is "workflow", then <owner-repo> must be specified.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
ctx := logging.WithLogger(context.Background())
logger := logging.FromContext(ctx)

opts, err := validateGithubFlags(
viper.GetString(githubMode),
viper.GetString(githubSbom),
viper.GetString(githubWorkflowFile),
viper.GetString("nats-addr"),
viper.GetString("csub-addr"),
viper.GetString("github-mode"),
viper.GetString("github-sbom"),
viper.GetString("github-workflow-file"),
viper.GetString("owner-repo"),
viper.GetBool("csub-tls"),
viper.GetBool("csub-tls-skip-verify"),
viper.GetBool("use-csub"),
Expand Down Expand Up @@ -100,10 +108,16 @@ var githubCmd = &cobra.Command{
if opts.poll {
collectorOpts = append(collectorOpts, github.WithPolling(30*time.Second))
}

if opts.ownerRepoName != "" {
collectorOpts = append(collectorOpts, github.WithOwner(opts.ownerRepoName[:strings.Index(opts.ownerRepoName, "/")])) // the owner name is everything before the slash
collectorOpts = append(collectorOpts, github.WithRepo(opts.ownerRepoName[strings.Index(opts.ownerRepoName, "/")+1:])) // the repo name is everything after the slash
if strings.Index(opts.ownerRepoName, "/") == -1 {
logger.Errorf("owner-repo flag must be in the format <owner>/<repo>")
} else {
collectorOpts = append(collectorOpts, github.WithOwner(opts.ownerRepoName[:strings.Index(opts.ownerRepoName, "/")])) // the owner name is everything before the slash
collectorOpts = append(collectorOpts, github.WithRepo(opts.ownerRepoName[strings.Index(opts.ownerRepoName, "/")+1:])) // the repo name is everything after the slash
}
}

githubCollector, err := github.NewGithubCollector(collectorOpts...)
if err != nil {
logger.Errorf("unable to create Github collector: %v", err)
Expand All @@ -117,14 +131,13 @@ var githubCmd = &cobra.Command{
},
}

func validateGithubFlags(natsAddr, csubAddr, githubMode, sbomName, workflowFileName, ownerRepoName string, csubTls, csubTlsSkipVerify, useCsub, poll bool, args []string) (githubOptions, error) {
func validateGithubFlags(githubMode, sbomName, workflowFileName, natsAddr, csubAddr string, csubTls, csubTlsSkipVerify, useCsub, poll bool, args []string) (githubOptions, error) {
var opts githubOptions
opts.natsAddr = natsAddr
opts.poll = poll
opts.githubMode = githubMode
opts.sbomName = sbomName
opts.workflowFileName = workflowFileName
opts.ownerRepoName = ownerRepoName

if useCsub {
csubOpts, err := client.ValidateCsubClientFlags(csubAddr, csubTls, csubTlsSkipVerify)
Expand All @@ -139,39 +152,49 @@ func validateGithubFlags(natsAddr, csubAddr, githubMode, sbomName, workflowFileN
return opts, err
}

// else direct CLI call
if len(args) < 1 {
return opts, fmt.Errorf("expected positional argument(s) for release_url(s)")
}
// Otherwise direct CLI call

sources := []datasource.Source{}
for _, arg := range args {
// TODO (mlieberman85): Below should be a github url parser helper instead of in the github collector
if _, _, err := github.ParseGithubReleaseDataSource(datasource.Source{
Value: arg,
}); err != nil {
return opts, fmt.Errorf("release_url parsing error. require format https://github.com/<org>/<repo>/releases/<optional_tag>: %v", err)
if githubMode == "" || githubMode == "release" {
if len(args) < 1 {
return opts, fmt.Errorf("expected positional argument(s) for release_url(s)")
}
sources = append(sources, datasource.Source{
Value: arg,

sources := []datasource.Source{}
for _, arg := range args {
// TODO (mlieberman85): Below should be a github url parser helper instead of in the github collector
if _, _, err := github.ParseGithubReleaseDataSource(datasource.Source{
Value: arg,
}); err != nil {
return opts, fmt.Errorf("release_url parsing error. require format https://github.com/<org>/<repo>/releases/<optional_tag>: %v", err)
}
sources = append(sources, datasource.Source{
Value: arg,
})
}

var err error
opts.dataSource, err = inmemsource.NewInmemDataSources(&datasource.DataSources{
GithubReleaseDataSources: sources,
})
}
if err != nil {
return opts, err
}
} else {
if len(args) != 1 {
return opts, fmt.Errorf("expected positional argument for owner-repo in the format <owner>/<repo>")
}
opts.ownerRepoName = args[0]

var err error
opts.dataSource, err = inmemsource.NewInmemDataSources(&datasource.DataSources{
GithubReleaseDataSources: sources,
})
if err != nil {
return opts, err
if opts.ownerRepoName == "" {
return opts, fmt.Errorf("owner-repo flag must be in the format <owner>/<repo>")
}
}

// TODO (nathan): Add support for workflow mode

return opts, nil
}

func init() {
set, err := cli.BuildFlags([]string{"github-mode", "github-sbom", "github-workflow-file", "owner-repo"})
set, err := cli.BuildFlags([]string{githubMode, githubSbom, githubWorkflowFile})
if err != nil {
fmt.Fprintf(os.Stderr, "failed to setup flag: %v", err)
os.Exit(1)
Expand Down
1 change: 0 additions & 1 deletion pkg/cli/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ func init() {
set.String("github-mode", "release", "mode to run github collector in: [release | workflow]")
set.String("github-sbom", "", "name of sbom file to look for in github release.")
set.String("github-workflow-file", "", "name of workflow file to look for in github workflow. \nThis will be the name of the actual file, not the workflow name (i.e. ci.yaml).")
set.String("owner-repo", "", "owner/repo name to look for in github workflow, this is only for the workflow mode. \nThe format of this should be <owner>/<repo>.")

set.VisitAll(func(f *pflag.Flag) {
flagStore[f.Name] = f
Expand Down
4 changes: 1 addition & 3 deletions pkg/handler/collector/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func NewGithubCollector(opts ...Opt) (*githubCollector, error) {
if len(g.assetSuffixes) == 0 {
return nil, fmt.Errorf("no asset suffixes for github collector")
}
if len(g.repoToReleaseTags) == 0 && g.collectDataSource == nil {
if g.isRelease && len(g.repoToReleaseTags) == 0 && g.collectDataSource == nil {
return nil, fmt.Errorf("no repos and releases to collect nor any data source for future subscriptions")
}
return g, nil
Expand Down Expand Up @@ -310,8 +310,6 @@ func (g *githubCollector) fetchWorkflowRunArtifacts(ctx context.Context, owner s
continue
}

fmt.Println("run.RunId: ", run.RunId)

artifacts, err := g.client.GetWorkflowRunArtifacts(ctx, owner, repo, g.sbomName, g.workflowFileName)
if err != nil {
logger.Warnf("unable to fetch workflow run artifacts for run %v: %v", run.RunId, err)
Expand Down
10 changes: 6 additions & 4 deletions pkg/handler/collector/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build integration

package github

import (
Expand Down Expand Up @@ -695,14 +697,14 @@ func Test_parseGitDataSource(t *testing.T) {
name: "parse valid tag git uri",
args: args{
source: datasource.Source{
Value: "git+https://github.com/guacsec/guac@v0.3.0",
Value: "git+https://github.com/mock/repo@v1",
},
},
want: &client.Repo{
Owner: "guacsec",
Repo: "guac",
Owner: "mock",
Repo: "repo",
},
want1: "v0.3.0",
want1: "v1",
wantErr: false,
},
{
Expand Down

0 comments on commit 1e8f2b3

Please sign in to comment.