Skip to content

Commit f8580a4

Browse files
authored
Merge branch 'main' into dependabot/github_actions/actions/checkout-4.2.0
2 parents f15ba4a + 4ab6687 commit f8580a4

File tree

3 files changed

+73
-32
lines changed

3 files changed

+73
-32
lines changed

action.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ inputs:
88
actions:
99
description: "Actions to correct"
1010
required: false
11-
default: ".github/workflows"
11+
default: ""
12+
action_paths:
13+
description: "Paths to search for actions"
14+
required: false
15+
default: ""
1216
dockerfiles:
1317
description: "Dockerfiles to correct"
1418
required: false
@@ -29,6 +33,10 @@ inputs:
2933
description: "Fail if an unpinned action/image is found"
3034
required: false
3135
default: "false"
36+
repo_root:
37+
description: "Operate on files in the specified filesystem location. If unspecified, check out files from the current repo."
38+
required: false
39+
default: ""
3240
runs:
3341
using: "docker"
3442
image: "Dockerfile"

main.go

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@ import (
2727

2828
"github.com/go-git/go-billy/v5"
2929
"github.com/go-git/go-billy/v5/memfs"
30+
"github.com/go-git/go-billy/v5/osfs"
3031
"github.com/go-git/go-git/v5"
3132
"github.com/go-git/go-git/v5/plumbing/transport/http"
3233
"github.com/go-git/go-git/v5/storage/memory"
3334
"github.com/google/go-github/v60/github"
3435
"github.com/stacklok/frizbee/pkg/replacer"
3536
"github.com/stacklok/frizbee/pkg/utils/config"
36-
"golang.org/x/oauth2"
3737

3838
"github.com/stacklok/frizbee-action/pkg/action"
3939
)
4040

4141
func main() {
4242
ctx := context.Background()
4343
// Initialize the frizbee action
44-
frizbeeAction, err := initAction(ctx)
44+
frizbeeAction, err := initAction()
4545
if err != nil {
4646
log.Fatalf("Error initializing action: %v", err)
4747
}
@@ -58,16 +58,13 @@ func main() {
5858
}
5959

6060
// initAction initializes the frizbee action - reads the environment variables, creates the GitHub client, etc.
61-
func initAction(ctx context.Context) (*action.FrizbeeAction, error) {
61+
func initAction() (*action.FrizbeeAction, error) {
62+
var repo *git.Repository
63+
var fs billy.Filesystem
64+
var githubClient *github.Client
65+
6266
// Get the GitHub token from the environment
6367
token := os.Getenv("GITHUB_TOKEN")
64-
if token == "" {
65-
return nil, errors.New("GITHUB_TOKEN environment variable is not set")
66-
}
67-
68-
// Create a new GitHub client
69-
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
70-
tc := oauth2.NewClient(ctx, ts)
7168

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

84-
// Clone the repository
85-
fs, repo, err := cloneRepository("https://github.com/"+repoFullName, repoOwner, token)
86-
if err != nil {
87-
return nil, fmt.Errorf("failed to clone repository: %w", err)
81+
repoRoot := os.Getenv("INPUT_REPO_ROOT")
82+
if repoRoot == "" {
83+
if token == "" {
84+
return nil, errors.New("GITHUB_TOKEN environment variable is not set")
85+
}
86+
87+
// Create a new GitHub client
88+
githubClient = github.NewClient(nil).WithAuthToken(token)
89+
90+
// Clone the repository
91+
var err error
92+
fs, repo, err = cloneRepository("https://github.com/"+repoFullName, repoOwner, token)
93+
if err != nil {
94+
return nil, fmt.Errorf("failed to clone repository: %w", err)
95+
}
96+
} else {
97+
fs = osfs.New(repoRoot)
98+
var err error
99+
repo, err = git.PlainOpen(repoRoot)
100+
if err != nil {
101+
return nil, fmt.Errorf("failed to open repository: %w", err)
102+
}
88103
}
89104

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

123+
actionsPathList, err := actionsPathList()
124+
if err != nil {
125+
return nil, err
126+
}
127+
108128
// Read the action settings from the environment and create the new frizbee replacers for actions and images
109129
return &action.FrizbeeAction{
110-
Client: github.NewClient(tc),
130+
Client: githubClient,
111131
Token: token,
112132
RepoOwner: repoOwner,
113133
RepoName: strings.TrimPrefix(repoFullName, repoOwner+"/"),
114134

115-
ActionsPath: os.Getenv("INPUT_ACTIONS"),
135+
ActionsPaths: actionsPathList,
116136
DockerfilesPaths: envToStrings("INPUT_DOCKERFILES"),
117137
KubernetesPaths: envToStrings("INPUT_KUBERNETES"),
118138
DockerComposePaths: envToStrings("INPUT_DOCKER_COMPOSE"),
@@ -162,3 +182,19 @@ func valToStrings(val string) []string {
162182

163183
return vals
164184
}
185+
186+
func actionsPathList() ([]string, error) {
187+
actions := os.Getenv("INPUT_ACTIONS")
188+
actionsPaths := os.Getenv("INPUT_ACTIONS_PATHS")
189+
if actions != "" && actionsPaths != "" {
190+
return nil, errors.New("cannot set both INPUT_ACTIONS and INPUT_ACTIONS_PATHS")
191+
} else if actions == "" && actionsPaths == "" {
192+
// Default for actions was `.github/workflows``
193+
actions = ".github/workflows"
194+
}
195+
196+
if actions != "" {
197+
return []string{actions}, nil
198+
}
199+
return valToStrings(actionsPaths), nil
200+
}

pkg/action/action.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type FrizbeeAction struct {
4242
RepoOwner string
4343
RepoName string
4444

45-
ActionsPath string
45+
ActionsPaths []string
4646
DockerfilesPaths []string
4747
KubernetesPaths []string
4848
DockerComposePaths []string
@@ -76,21 +76,18 @@ func (fa *FrizbeeAction) Run(ctx context.Context) error {
7676

7777
// parseWorkflowActions parses the GitHub Actions workflow files
7878
func (fa *FrizbeeAction) parseWorkflowActions(ctx context.Context, out *replacer.ReplaceResult) error {
79-
if fa.ActionsPath == "" {
80-
log.Printf("Workflow path is empty")
81-
return nil
82-
}
83-
84-
log.Printf("Parsing workflow files in %s...", fa.ActionsPath)
85-
res, err := fa.ActionsReplacer.ParsePathInFS(ctx, fa.BFS, fa.ActionsPath)
86-
if err != nil {
87-
return fmt.Errorf("failed to parse workflow files in %s: %w", fa.ActionsPath, err)
88-
}
79+
for _, path := range fa.ActionsPaths {
80+
log.Printf("Parsing workflow files in %s...", path)
81+
res, err := fa.ActionsReplacer.ParsePathInFS(ctx, fa.BFS, path)
82+
if err != nil {
83+
return fmt.Errorf("failed to parse workflow files in %s: %w", path, err)
84+
}
8985

90-
// Copy the processed and modified files to the output
91-
out.Processed = mapset.NewSet(out.Processed...).Union(mapset.NewSet(res.Processed...)).ToSlice()
92-
for key, value := range res.Modified {
93-
out.Modified[key] = value
86+
// Copy the processed and modified files to the output
87+
out.Processed = mapset.NewSet(out.Processed...).Union(mapset.NewSet(res.Processed...)).ToSlice()
88+
for key, value := range res.Modified {
89+
out.Modified[key] = value
90+
}
9491
}
9592
return nil
9693
}

0 commit comments

Comments
 (0)