Skip to content

Commit

Permalink
install: Retrieve the correct action configuration from local path
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Augustus <foo@auggie.dev>
  • Loading branch information
justaugustus committed May 24, 2022
1 parent 60cd5a9 commit e0984d7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
38 changes: 24 additions & 14 deletions install/install.go
Expand Up @@ -28,7 +28,15 @@ import (
"github.com/ossf/scorecard-action/install/options"
)

const workflowFile = ".github/workflows/scorecards-analysis.yml"
const (
workflowFile = ".github/workflows/scorecards.yml"
workflowFileDeprecated = ".github/workflows/scorecards-analysis.yml"
)

var workflowFiles = []string{
workflowFile,
workflowFileDeprecated,
}

// Run adds the OpenSSF Scorecard workflow to all repositories under the given
// organization.
Expand Down Expand Up @@ -59,7 +67,7 @@ func Run(o *options.Options) error {
}

// Get yml file into byte array.
workflowContent, err := ioutil.ReadFile("scorecards-analysis.yml")
workflowContent, err := ioutil.ReadFile(o.ConfigPath)
if err != nil {
return fmt.Errorf("reading scorecard workflow file: %w", err)
}
Expand Down Expand Up @@ -101,20 +109,22 @@ func Run(o *options.Options) error {
defaultBranchSHA := defaultBranch.Commit.SHA

// Skip if scorecard file already exists in workflows folder.
scoreFileContent, _, _, err := client.GetContents(
ctx,
o.Owner,
repoName,
workflowFile,
&github.RepositoryContentGetOptions{},
)
if scoreFileContent != nil || err == nil {
log.Printf(
"skipped repo (%s) since scorecard workflow already exists",
for _, f := range workflowFiles {
scoreFileContent, _, _, err := client.GetContents(
ctx,
o.Owner,
repoName,
f,
&github.RepositoryContentGetOptions{},
)

continue
if scoreFileContent != nil || err == nil {
log.Printf(
"skipped repo (%s) since scorecard workflow already exists",
repoName,
)

continue
}
}

// Skip if branch scorecard already exists.
Expand Down
23 changes: 21 additions & 2 deletions install/options/options.go
Expand Up @@ -16,7 +16,15 @@

package options

import "errors"
import (
"errors"
"path/filepath"
)

const (
DefaultConfigDir = "starter-workflows/code-scanning"
DefaultConfigFile = "scorecards.yml"
)

var errOwnerNotSpecified = errors.New("owner not specified")

Expand All @@ -26,11 +34,16 @@ type Options struct {

// Repositories
Repositories []string

// Scorecard GitHub Action configuration path
ConfigPath string
}

// New creates a new instance of installation options.
func New() *Options {
return &Options{}
opts := &Options{}
opts.ConfigPath = GetConfigPath()
return opts
}

// Validate checks if the installation options specified are valid.
Expand All @@ -41,3 +54,9 @@ func (o *Options) Validate() error {

return nil
}

// GetConfigPath returns the local path for the scorecard action config file.
// TODO: Consider making this configurable.
func GetConfigPath() string {
return filepath.Join(DefaultConfigDir, DefaultConfigFile)
}

0 comments on commit e0984d7

Please sign in to comment.