Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow running in place on an existing checkout #53

Merged
merged 6 commits into from
Oct 7, 2024
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
10 changes: 9 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ inputs:
actions:
description: "Actions to correct"
required: false
default: ".github/workflows"
default: ""
action_paths:
description: "Paths to search for actions"
required: false
default: ""
dockerfiles:
description: "Dockerfiles to correct"
required: false
Expand All @@ -29,6 +33,10 @@ inputs:
description: "Fail if an unpinned action/image is found"
required: false
default: "false"
repo_root:
description: "Operate on files in the specified filesystem location. If unspecified, check out files from the current repo."
required: false
default: ""
runs:
using: "docker"
image: "Dockerfile"
Expand Down
68 changes: 52 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ import (

"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/google/go-github/v60/github"
"github.com/stacklok/frizbee/pkg/replacer"
"github.com/stacklok/frizbee/pkg/utils/config"
"golang.org/x/oauth2"

"github.com/stacklok/frizbee-action/pkg/action"
)

func main() {
ctx := context.Background()
// Initialize the frizbee action
frizbeeAction, err := initAction(ctx)
frizbeeAction, err := initAction()
if err != nil {
log.Fatalf("Error initializing action: %v", err)
}
Expand All @@ -58,16 +58,13 @@ func main() {
}

// initAction initializes the frizbee action - reads the environment variables, creates the GitHub client, etc.
func initAction(ctx context.Context) (*action.FrizbeeAction, error) {
func initAction() (*action.FrizbeeAction, error) {
var repo *git.Repository
var fs billy.Filesystem
var githubClient *github.Client

// Get the GitHub token from the environment
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
return nil, errors.New("GITHUB_TOKEN environment variable is not set")
}

// Create a new GitHub client
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
tc := oauth2.NewClient(ctx, ts)

// Get the GITHUB_REPOSITORY_OWNER
repoOwner := os.Getenv("GITHUB_REPOSITORY_OWNER")
Expand All @@ -81,10 +78,28 @@ func initAction(ctx context.Context) (*action.FrizbeeAction, error) {
return nil, errors.New("GITHUB_REPOSITORY environment variable is not set")
}

// Clone the repository
fs, repo, err := cloneRepository("https://github.com/"+repoFullName, repoOwner, token)
if err != nil {
return nil, fmt.Errorf("failed to clone repository: %w", err)
repoRoot := os.Getenv("INPUT_REPO_ROOT")
if repoRoot == "" {
if token == "" {
return nil, errors.New("GITHUB_TOKEN environment variable is not set")
}

// Create a new GitHub client
githubClient = github.NewClient(nil).WithAuthToken(token)

// Clone the repository
var err error
fs, repo, err = cloneRepository("https://github.com/"+repoFullName, repoOwner, token)
if err != nil {
return nil, fmt.Errorf("failed to clone repository: %w", err)
}
} else {
fs = osfs.New(repoRoot)
var err error
repo, err = git.PlainOpen(repoRoot)
if err != nil {
return nil, fmt.Errorf("failed to open repository: %w", err)
}
}

cfg := config.DefaultConfig()
Expand All @@ -105,14 +120,19 @@ func initAction(ctx context.Context) (*action.FrizbeeAction, error) {
cfg.Images.ExcludeTags = valToStrings(excludeTags)
}

actionsPathList, err := actionsPathList()
if err != nil {
return nil, err
}

// Read the action settings from the environment and create the new frizbee replacers for actions and images
return &action.FrizbeeAction{
Client: github.NewClient(tc),
Client: githubClient,
Token: token,
RepoOwner: repoOwner,
RepoName: strings.TrimPrefix(repoFullName, repoOwner+"/"),

ActionsPath: os.Getenv("INPUT_ACTIONS"),
ActionsPaths: actionsPathList,
DockerfilesPaths: envToStrings("INPUT_DOCKERFILES"),
KubernetesPaths: envToStrings("INPUT_KUBERNETES"),
DockerComposePaths: envToStrings("INPUT_DOCKER_COMPOSE"),
Expand Down Expand Up @@ -162,3 +182,19 @@ func valToStrings(val string) []string {

return vals
}

func actionsPathList() ([]string, error) {
actions := os.Getenv("INPUT_ACTIONS")
actionsPaths := os.Getenv("INPUT_ACTIONS_PATHS")
if actions != "" && actionsPaths != "" {
return nil, errors.New("cannot set both INPUT_ACTIONS and INPUT_ACTIONS_PATHS")
} else if actions == "" && actionsPaths == "" {
// Default for actions was `.github/workflows``
actions = ".github/workflows"
}

if actions != "" {
return []string{actions}, nil
}
return valToStrings(actionsPaths), nil
}
27 changes: 12 additions & 15 deletions pkg/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type FrizbeeAction struct {
RepoOwner string
RepoName string

ActionsPath string
ActionsPaths []string
DockerfilesPaths []string
KubernetesPaths []string
DockerComposePaths []string
Expand Down Expand Up @@ -76,21 +76,18 @@ func (fa *FrizbeeAction) Run(ctx context.Context) error {

// parseWorkflowActions parses the GitHub Actions workflow files
func (fa *FrizbeeAction) parseWorkflowActions(ctx context.Context, out *replacer.ReplaceResult) error {
if fa.ActionsPath == "" {
log.Printf("Workflow path is empty")
return nil
}

log.Printf("Parsing workflow files in %s...", fa.ActionsPath)
res, err := fa.ActionsReplacer.ParsePathInFS(ctx, fa.BFS, fa.ActionsPath)
if err != nil {
return fmt.Errorf("failed to parse workflow files in %s: %w", fa.ActionsPath, err)
}
for _, path := range fa.ActionsPaths {
log.Printf("Parsing workflow files in %s...", path)
res, err := fa.ActionsReplacer.ParsePathInFS(ctx, fa.BFS, path)
if err != nil {
return fmt.Errorf("failed to parse workflow files in %s: %w", path, err)
}

// Copy the processed and modified files to the output
out.Processed = mapset.NewSet(out.Processed...).Union(mapset.NewSet(res.Processed...)).ToSlice()
for key, value := range res.Modified {
out.Modified[key] = value
// Copy the processed and modified files to the output
out.Processed = mapset.NewSet(out.Processed...).Union(mapset.NewSet(res.Processed...)).ToSlice()
for key, value := range res.Modified {
out.Modified[key] = value
}
}
return nil
}
Expand Down
Loading