Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
riteshnoronha committed Feb 8, 2025
1 parent 69fdd91 commit 2253530
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 167 deletions.
68 changes: 0 additions & 68 deletions cmd/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"fmt"
"os"
"strings"

"github.com/interlynk-io/sbommv/pkg/engine"
"github.com/interlynk-io/sbommv/pkg/mvtypes"
Expand Down Expand Up @@ -51,12 +50,6 @@ sbommv transfer --input-adapter=github --in-github-url="https://github.com/sigst
func init() {
rootCmd.AddCommand(transferCmd)

// custom usage of command
transferCmd.SetUsageFunc(func(cmd *cobra.Command) error {
fmt.Print(customUsageFunc(cmd))
return nil
})

// Input adapter flags
transferCmd.Flags().String("input-adapter", "", "input adapter type (github)")
transferCmd.MarkFlagRequired("input-adapter")
Expand Down Expand Up @@ -143,64 +136,3 @@ func parseConfig(cmd *cobra.Command) (mvtypes.Config, error) {

return config, nil
}

// Custom usage function for transferCmd
func customUsageFunc(_ *cobra.Command) string {
builder := &strings.Builder{}

builder.WriteString("Usage:\n")
builder.WriteString(" transfer [flags]\n\n")
builder.WriteString("Flags:\n")

// Input Adapters
builder.WriteString("Input Adapters:\n")
inputAdapters := map[string][]struct {
Name string
Usage string
}{
"github": {
{"--in-github-url", "URL for input adapter github (required)"},
{"--in-github-method", "Method for input adapter github (optional)"},
{"--in-github-include-repos", "Comma-separated list of repositories to include (optional)"},
{"--in-github-exclude-repos", "Comma-separated list of repositories to exclude (optional)"},
},
}

for adapter, flags := range inputAdapters {
builder.WriteString(fmt.Sprintf(" %s:\n", adapter))
for _, flag := range flags {
builder.WriteString(fmt.Sprintf(" %s %s\n", flag.Name, flag.Usage))
}
builder.WriteString("\n")
}

// Output Adapters
builder.WriteString("Output Adapters:\n")
outputAdapters := map[string][]struct {
Name string
Usage string
}{
"interlynk": {
{"--out-interlynk-url", "URL for output adapter interlynk (optional)"},
{"--out-interlynk-project-id", "Project ID for output adapter interlynk (optional)"},
},
}

for adapter, flags := range outputAdapters {
builder.WriteString(fmt.Sprintf(" %s:\n", adapter))
for _, flag := range flags {
builder.WriteString(fmt.Sprintf(" %s %s\n", flag.Name, flag.Usage))
}
builder.WriteString("\n")
}

// Other Flags
builder.WriteString("Other Flags:\n")
builder.WriteString(" -D, --debug Enable debug logging\n")
builder.WriteString(" --dry-run Enable dry run mode\n")
builder.WriteString(" -h, --help help for transfer\n")
builder.WriteString(" --input-adapter Input adapter type (github) (required)\n")
builder.WriteString(" --output-adapter Output adapter type (interlynk) (required)\n")

return builder.String()
}
6 changes: 3 additions & 3 deletions pkg/source/github/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ const (
// AddCommandParams adds GitHub-specific CLI flags
func (g *GitHubAdapter) AddCommandParams(cmd *cobra.Command) {
cmd.Flags().String("in-github-url", "", "GitHub repository URL")
cmd.Flags().String("in-github-method", "release", "GitHub method: release, api, or tool")
cmd.Flags().String("in-github-method", "api", "GitHub method: release, api, or tool")

// Updated to StringSlice to support multiple values (comma-separated)
cmd.Flags().StringSlice("in-github-include-repos", nil, "Comma-separated list of repositories to include")
cmd.Flags().StringSlice("in-github-exclude-repos", nil, "Comma-separated list of repositories to exclude")
cmd.Flags().StringSlice("in-github-include-repos", nil, "Include only these repositories e.g sbomqs,sbomasm")
cmd.Flags().StringSlice("in-github-exclude-repos", nil, "Exclude these repositories e.g sbomqs,sbomasm")

// (Optional) If you plan to fetch **all versions** of a repo
// cmd.Flags().Bool("in-github-all-versions", false, "Fetch SBOMs from all versions")
Expand Down
111 changes: 33 additions & 78 deletions pkg/target/interlynk/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"
"io"
"net/http"
"strings"

"github.com/interlynk-io/sbommv/pkg/iterator"
"github.com/interlynk-io/sbommv/pkg/logger"
Expand All @@ -31,17 +30,15 @@ import (
// InterlynkAdapter manages SBOM uploads to the Interlynk service.
type InterlynkAdapter struct {
// Config fields
ProjectID string
BaseURL string
ApiKey string
Role types.AdapterRole
ProjectName string
ProjectEnv string

// HTTP client for API requests
client *http.Client
BaseURL string
ApiKey string
Role types.AdapterRole

// Repository info
RepoURL string
Version string
// HTTP client for API requests
client *http.Client
settings UploadSettings
}

Expand All @@ -60,39 +57,45 @@ type UploadSettings struct {

// AddCommandParams adds GitHub-specific CLI flags
func (i *InterlynkAdapter) AddCommandParams(cmd *cobra.Command) {
cmd.Flags().String("out-interlynk-url", "", "Interlynk API URL")
cmd.Flags().String("out-interlynk-project-id", "", "Interlynk Project ID")
cmd.Flags().String("out-interlynk-url", "https://api.interlynk.io/lynkapi", "Interlynk API URL")
cmd.Flags().String("out-interlynk-project-name", "", "Interlynk Project Name")
cmd.Flags().String("out-interlynk-project-env", "default", "Interlynk Project Environment")
cmd.Flags().String("in-interlynk-url", "https://api.interlynk.io/lynkapi", "Interlynk API URL")
cmd.Flags().String("in-interlynk-project-name", "", "Interlynk Project Name")
cmd.Flags().String("in-interlynk-project-env", "default", "Interlynk Project Environment")
}

// ParseAndValidateParams validates the GitHub adapter params
func (i *InterlynkAdapter) ParseAndValidateParams(cmd *cobra.Command) error {
var urlFlag, projectIDFlag string
var urlFlag, projectNameFlag, projectEnvFlag string

if i.Role == types.InputAdapter {
urlFlag = "in-interlynk-url"
projectIDFlag = "in-interlynk-project-id"
projectNameFlag = "in-interlynk-project-name"
projectEnvFlag = "in-interlynk-project-env"
} else {
urlFlag = "out-interlynk-url"
projectIDFlag = "out-interlynk-project-id"
projectNameFlag = "out-interlynk-project-name"
projectEnvFlag = "out-interlynk-project-env"
}

url, _ := cmd.Flags().GetString(urlFlag)
if url == "" {
return fmt.Errorf("missing or invalid flag: %s", urlFlag)
}

projectID, _ := cmd.Flags().GetString(projectIDFlag)
if projectID == "" {
logger.LogDebug(cmd.Context(), "No project ID provided, a new project will be created")
}
projectName, _ := cmd.Flags().GetString(projectNameFlag)
projectEnv, _ := cmd.Flags().GetString(projectEnvFlag)

token := viper.GetString("INTERLYNK_SECURITY_TOKEN")
if token == "" {
return fmt.Errorf("INTERLYNK_SECURITY_TOKEN environment variable is required")
}

i.BaseURL = url
i.ProjectID = projectID
if url == "" {
i.BaseURL = "https://api.interlynk.io/lynkapi"
} else {
i.BaseURL = url
}

i.ProjectName = projectName
i.ProjectEnv = projectEnv
i.ApiKey = token
i.settings = UploadSettings{
ProcessingMode: UploadSequential,
Expand Down Expand Up @@ -150,47 +153,12 @@ func (i *InterlynkAdapter) uploadSequential(ctx *tcontext.TransferMetadata, sbom

// Initialize Interlynk API client
client := NewClient(Config{
Token: i.ApiKey,
APIURL: i.BaseURL,
ProjectID: i.ProjectID,
Token: i.ApiKey,
APIURL: i.BaseURL,
ProjectName: i.ProjectName,
ProjectEnv: i.ProjectEnv,
})

// // Retrieve metadata from context
// repoURL, _ := ctx.Value("repo_url").(string)
// repoVersion, _ := ctx.Value("repo_version").(string)
// totalSBOMs, _ := ctx.Value("total_sboms").(int)

// if repoVersion == "" {
// repoVersion = "all-version"
// }

// repoName := sanitizeRepoName(repoURL)

// // Create project if needed
// if client.ProjectID == "" {
// projectName := fmt.Sprintf("%s", repoName)

// projectID, err := client.FindProjectGroup(ctx, projectName, "default")

// if err != nil {
// logger.LogDebug(ctx.Context, "Project not found, creating new project", "name", projectName)
// projectID, err = client.CreateProjectGroup(ctx, projectName, "default")
// if err != nil {
// return fmt.Errorf("failed to create project: %w", err)
// }
// }

// logger.LogDebug(ctx.Context, "Found Project", "name", projectName, "ID", projectID)
// client.SetProjectID(projectID)
// }

// if totalSBOMs == 0 {
// return errors.New("no SBOMs to upload")
// }
//

// Initialize progress bar
// bar := progressbar.Default(int64(0), "🚀 Uploading SBOMs")
errorCount := 0
maxRetries := 5

Expand All @@ -213,7 +181,7 @@ func (i *InterlynkAdapter) uploadSequential(ctx *tcontext.TransferMetadata, sbom

logger.LogDebug(ctx.Context, "Uploading SBOM", "repo", sbom.Repo, "version", sbom.Version, "data size", len(sbom.Data))

projectID, err := client.FindOrCreateProjectGroup(ctx, sbom.Repo, "default")
projectID, err := client.FindOrCreateProjectGroup(ctx, sbom.Repo)
if err != nil {
logger.LogError(ctx.Context, err, "Failed to create project", "repo", sbom.Repo)
continue
Expand All @@ -226,20 +194,7 @@ func (i *InterlynkAdapter) uploadSequential(ctx *tcontext.TransferMetadata, sbom
} else {
logger.LogDebug(ctx.Context, "Successfully uploaded SBOM", "repo", sbom.Repo, "version", sbom.Version)
}

// // Update progress bar
// if err := bar.Add(1); err != nil {
// logger.LogError(ctx.Context, err, "Error updating progress bar")
// }
}

return nil
}

func sanitizeRepoName(repoURL string) string {
repoParts := strings.Split(repoURL, "/")
if len(repoParts) < 2 {
return "unknown"
}
return repoParts[len(repoParts)-1]
}
50 changes: 32 additions & 18 deletions pkg/target/interlynk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,19 @@ const (

// Client handles interactions with the Interlynk API
type Client struct {
ApiURL string
token string
client *http.Client
ProjectID string
ApiURL string
token string
client *http.Client
ProjectName string
ProjectEnv string
}

// Config holds the configuration for the Interlynk client
type Config struct {
APIURL string
Token string
ProjectID string
ProjectName string
ProjectEnv string
Timeout time.Duration
MaxAttempts int
}
Expand All @@ -79,28 +81,40 @@ func NewClient(config Config) *Client {
}

return &Client{
ApiURL: config.APIURL,
token: config.Token,
ProjectID: config.ProjectID,
ApiURL: config.APIURL,
token: config.Token,
ProjectName: config.ProjectName,
ProjectEnv: config.ProjectEnv,
client: &http.Client{
Timeout: config.Timeout,
},
}
}

// SetProjectID updates the project ID for the client
func (c *Client) SetProjectID(projectID string) {
c.ProjectID = projectID
}
func (c *Client) FindOrCreateProjectGroup(ctx *tcontext.TransferMetadata, repoName string) (string, error) {
projectName := ""
if c.ProjectName != "" {
projectName = c.ProjectName
} else {
projectName = fmt.Sprintf("%s", repoName)
}

func (c *Client) FindOrCreateProjectGroup(ctx *tcontext.TransferMetadata, repoName, env string) (string, error) {
projectName := fmt.Sprintf("%s", repoName)
projectID, err := c.FindProjectGroup(ctx, projectName, env)
env := ""
if c.ProjectEnv != "" {
env = c.ProjectEnv
} else {
env = "default"
}

projectID, err := c.FindProjectGroup(ctx, projectName, env)
if err != nil {
projectID, err = c.CreateProjectGroup(ctx, projectName, env)
if err != nil {
return "", fmt.Errorf("failed to create project: %w", err)
if c.ProjectName != "" {
return "", fmt.Errorf("failed to find project: %w", err)
} else {
projectID, err = c.CreateProjectGroup(ctx, projectName, env)
if err != nil {
return "", fmt.Errorf("failed to create project: %w", err)
}
}
}

Expand Down

0 comments on commit 2253530

Please sign in to comment.