Skip to content

Commit

Permalink
fix: rename actions to jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Dec 19, 2024
1 parent f890121 commit 82319c0
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package action
package jobs

import (
"fmt"
Expand All @@ -22,7 +22,7 @@ type template struct {
cnt int
}

func buildCommand(params *Params) (*Action, error) {
func buildCommand(params *Params) (*Job, error) {
if err := params.validateCommand(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -124,7 +124,7 @@ func buildCommand(params *Params) (*Action, error) {
}

if config.HookUsesStagedFiles(params.HookName) {
ok, err := canSkipAction(params, filterParams, templates[config.SubStagedFiles], params.Repo.StagedFiles)
ok, err := canSkipJob(params, filterParams, templates[config.SubStagedFiles], params.Repo.StagedFiles)
if err != nil {
return nil, err
}
Expand All @@ -134,7 +134,7 @@ func buildCommand(params *Params) (*Action, error) {
}

if config.HookUsesPushFiles(params.HookName) {
ok, err := canSkipAction(params, filterParams, templates[config.SubPushFiles], params.Repo.PushFiles)
ok, err := canSkipJob(params, filterParams, templates[config.SubPushFiles], params.Repo.PushFiles)
if err != nil {
return nil, err
}
Expand All @@ -146,7 +146,7 @@ func buildCommand(params *Params) (*Action, error) {
return result, nil
}

func canSkipAction(params *Params, filterParams filters.Params, template *template, filesFn func() ([]string, error)) (bool, error) {
func canSkipJob(params *Params, filterParams filters.Params, template *template, filesFn func() ([]string, error)) (bool, error) {
if template != nil {
return len(template.files) == 0, nil
}
Expand Down Expand Up @@ -184,9 +184,9 @@ func escapeFiles(files []string) []string {
return filesEsc
}

func replaceInChunks(str string, templates map[string]*template, maxlen int) *Action {
func replaceInChunks(str string, templates map[string]*template, maxlen int) *Job {
if len(templates) == 0 {
return &Action{
return &Job{
Execs: []string{str},
}
}
Expand Down Expand Up @@ -232,7 +232,7 @@ func replaceInChunks(str string, templates map[string]*template, maxlen int) *Ac
}
}

return &Action{
return &Job{
Execs: commands,
Files: allFiles,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package action
package jobs

import (
"fmt"
Expand Down Expand Up @@ -70,7 +70,7 @@ func Test_replaceInChunks(t *testing.T) {
str string
templates map[string]*template
maxlen int
action *Action
job *Job
}{
{
str: "echo {staged_files}",
Expand All @@ -81,7 +81,7 @@ func Test_replaceInChunks(t *testing.T) {
},
},
maxlen: 300,
action: &Action{
job: &Job{
Execs: []string{"echo file1 file2 file3"},
Files: []string{"file1", "file2", "file3"},
},
Expand All @@ -95,7 +95,7 @@ func Test_replaceInChunks(t *testing.T) {
},
},
maxlen: 10,
action: &Action{
job: &Job{
Execs: []string{
"echo file1",
"echo file2",
Expand All @@ -113,7 +113,7 @@ func Test_replaceInChunks(t *testing.T) {
},
},
maxlen: 49, // (49 - 17(len of command without templates)) / 2 = 16, but we need 17 (3 words + 2 spaces)
action: &Action{
job: &Job{
Execs: []string{
"echo file1 file2 && git add file1 file2",
"echo file3 && git add file3",
Expand All @@ -130,7 +130,7 @@ func Test_replaceInChunks(t *testing.T) {
},
},
maxlen: 51,
action: &Action{
job: &Job{
Execs: []string{
"echo file1 file2 file3 && git add file1 file2 file3",
},
Expand All @@ -150,7 +150,7 @@ func Test_replaceInChunks(t *testing.T) {
},
},
maxlen: 10,
action: &Action{
job: &Job{
Execs: []string{
"echo push-file && git add file1",
"echo push-file && git add file2",
Expand All @@ -171,7 +171,7 @@ func Test_replaceInChunks(t *testing.T) {
},
},
maxlen: 27,
action: &Action{
job: &Job{
Execs: []string{
"echo push1 && git add file1",
"echo push2 && git add file2",
Expand All @@ -183,10 +183,10 @@ func Test_replaceInChunks(t *testing.T) {
} {
t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) {
assert := assert.New(t)
action := replaceInChunks(tt.str, tt.templates, tt.maxlen)
job := replaceInChunks(tt.str, tt.templates, tt.maxlen)

assert.ElementsMatch(action.Files, tt.action.Files)
assert.Equal(action.Execs, tt.action.Execs)
assert.ElementsMatch(job.Files, tt.job.Files)
assert.Equal(job.Execs, tt.job.Execs)
})
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package action
package jobs

import (
"fmt"
Expand All @@ -24,7 +24,7 @@ func (s scriptNotExistsError) Error() string {
return fmt.Sprintf("script does not exist: %s", s.scriptPath)
}

func buildScript(params *Params) (*Action, error) {
func buildScript(params *Params) (*Job, error) {
if err := params.validateScript(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -73,7 +73,7 @@ func buildScript(params *Params) (*Action, error) {
return nil, scriptNotExistsError{params.Script}
}

return &Action{
return &Job{
Execs: execs,
Files: []string{},
}, nil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package action
package jobs

import (
"github.com/evilmartians/lefthook/internal/config"
Expand Down Expand Up @@ -28,12 +28,12 @@ type Params struct {
Skip interface{}
}

type Action struct {
type Job struct {
Execs []string
Files []string
}

func New(name string, params *Params) (*Action, error) {
func New(name string, params *Params) (*Job, error) {
if params.skip() {
return nil, SkipError{"settings"}
}
Expand All @@ -47,18 +47,18 @@ func New(name string, params *Params) (*Action, error) {
}

var err error
var action *Action
var job *Job
if len(params.Run) != 0 {
action, err = buildCommand(params)
job, err = buildCommand(params)
} else {
action, err = buildScript(params)
job, err = buildScript(params)
}

if err != nil {
return nil, err
}

return action, nil
return job, nil
}

func (p *Params) skip() bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package action
package jobs

// SkipError implements error interface but indicates that the execution needs to be skipped.
type SkipError struct {
Expand Down
52 changes: 26 additions & 26 deletions internal/lefthook/runner/run_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"sync/atomic"

"github.com/evilmartians/lefthook/internal/config"
"github.com/evilmartians/lefthook/internal/lefthook/runner/action"
"github.com/evilmartians/lefthook/internal/lefthook/runner/exec"
"github.com/evilmartians/lefthook/internal/lefthook/runner/filters"
"github.com/evilmartians/lefthook/internal/lefthook/runner/jobs"
"github.com/evilmartians/lefthook/internal/log"
)

Expand Down Expand Up @@ -95,35 +95,35 @@ func (r *Runner) runJob(ctx context.Context, domain *domain, id string, job *con
return failed(job.PrintableName(id), "don't know how to run job")
}

func (r *Runner) runSingleJob(ctx context.Context, domain *domain, id string, act *config.Job) Result {
name := act.PrintableName(id)
func (r *Runner) runSingleJob(ctx context.Context, domain *domain, id string, job *config.Job) Result {
name := job.PrintableName(id)

root := first(act.Root, domain.root)
glob := first(act.Glob, domain.glob)
runAction, err := action.New(name, &action.Params{
root := first(job.Root, domain.root)
glob := first(job.Glob, domain.glob)
executionJob, err := jobs.New(name, &jobs.Params{
Repo: r.Repo,
Hook: r.Hook,
HookName: r.HookName,
ForceFiles: r.Files,
Force: r.Force,
SourceDirs: r.SourceDirs,
GitArgs: r.GitArgs,
Run: act.Run,
Run: job.Run,
Root: root,
Runner: act.Runner,
Script: act.Script,
Runner: job.Runner,
Script: job.Script,
Glob: glob,
Files: act.Files,
FileTypes: act.FileTypes,
Tags: act.Tags,
Exclude: act.Exclude,
Only: act.Only,
Skip: act.Skip,
Files: job.Files,
FileTypes: job.FileTypes,
Tags: job.Tags,
Exclude: job.Exclude,
Only: job.Only,
Skip: job.Skip,
})
if err != nil {
r.logSkip(name, err.Error())

var skipErr action.SkipError
var skipErr jobs.SkipError
if errors.As(err, &skipErr) {
return skipped(name)
}
Expand All @@ -134,20 +134,20 @@ func (r *Runner) runSingleJob(ctx context.Context, domain *domain, id string, ac

ok := r.run(ctx, exec.Options{
Name: name,
Root: filepath.Join(r.Repo.RootPath, act.Root),
Commands: runAction.Execs,
Interactive: act.Interactive && !r.DisableTTY,
UseStdin: act.UseStdin,
Env: act.Env,
Root: filepath.Join(r.Repo.RootPath, root),
Commands: executionJob.Execs,
Interactive: job.Interactive && !r.DisableTTY,
UseStdin: job.UseStdin,
Env: job.Env,
}, r.Hook.Follow)

if !ok {
domain.failed.Store(true)
return failed(name, act.FailText)
return failed(name, job.FailText)
}

if config.HookUsesStagedFiles(r.HookName) && act.StageFixed {
files := runAction.Files
if config.HookUsesStagedFiles(r.HookName) && job.StageFixed {
files := executionJob.Files

if len(files) == 0 {
var err error
Expand All @@ -160,8 +160,8 @@ func (r *Runner) runSingleJob(ctx context.Context, domain *domain, id string, ac
files = filters.Apply(r.Repo.Fs, files, filters.Params{
Glob: glob,
Root: root,
Exclude: act.Exclude,
FileTypes: act.FileTypes,
Exclude: job.Exclude,
FileTypes: job.FileTypes,
})
}

Expand Down
16 changes: 8 additions & 8 deletions internal/lefthook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (

"github.com/evilmartians/lefthook/internal/config"
"github.com/evilmartians/lefthook/internal/git"
"github.com/evilmartians/lefthook/internal/lefthook/runner/action"
"github.com/evilmartians/lefthook/internal/lefthook/runner/exec"
"github.com/evilmartians/lefthook/internal/lefthook/runner/filters"
"github.com/evilmartians/lefthook/internal/lefthook/runner/jobs"
"github.com/evilmartians/lefthook/internal/log"
"github.com/evilmartians/lefthook/internal/system"
)
Expand Down Expand Up @@ -332,7 +332,7 @@ func (r *Runner) runScripts(ctx context.Context, dir string) []Result {
}

func (r *Runner) runScript(ctx context.Context, script *config.Script, file os.FileInfo) Result {
scriptAction, err := action.New(file.Name(), &action.Params{
job, err := jobs.New(file.Name(), &jobs.Params{
Repo: r.Repo,
Hook: r.Hook,
HookName: r.HookName,
Expand All @@ -349,7 +349,7 @@ func (r *Runner) runScript(ctx context.Context, script *config.Script, file os.F
if err != nil {
r.logSkip(file.Name(), err.Error())

var skipErr action.SkipError
var skipErr jobs.SkipError
if errors.As(err, &skipErr) {
return skipped(file.Name())
}
Expand All @@ -366,7 +366,7 @@ func (r *Runner) runScript(ctx context.Context, script *config.Script, file os.F
ok := r.run(ctx, exec.Options{
Name: file.Name(),
Root: r.Repo.RootPath,
Commands: scriptAction.Execs,
Commands: job.Execs,
Interactive: script.Interactive && !r.DisableTTY,
UseStdin: script.UseStdin,
Env: script.Env,
Expand Down Expand Up @@ -450,7 +450,7 @@ func (r *Runner) runCommands(ctx context.Context) []Result {
}

func (r *Runner) runCommand(ctx context.Context, name string, command *config.Command) Result {
runAction, err := action.New(name, &action.Params{
job, err := jobs.New(name, &jobs.Params{
Repo: r.Repo,
Hook: r.Hook,
HookName: r.HookName,
Expand All @@ -470,7 +470,7 @@ func (r *Runner) runCommand(ctx context.Context, name string, command *config.Co
if err != nil {
r.logSkip(name, err.Error())

var skipErr action.SkipError
var skipErr jobs.SkipError
if errors.As(err, &skipErr) {
return skipped(name)
}
Expand All @@ -487,7 +487,7 @@ func (r *Runner) runCommand(ctx context.Context, name string, command *config.Co
ok := r.run(ctx, exec.Options{
Name: name,
Root: filepath.Join(r.Repo.RootPath, command.Root),
Commands: runAction.Execs,
Commands: job.Execs,
Interactive: command.Interactive && !r.DisableTTY,
UseStdin: command.UseStdin,
Env: command.Env,
Expand All @@ -501,7 +501,7 @@ func (r *Runner) runCommand(ctx context.Context, name string, command *config.Co
result := succeeded(name)

if config.HookUsesStagedFiles(r.HookName) && command.StageFixed {
files := runAction.Files
files := job.Files

if len(files) == 0 {
var err error
Expand Down

0 comments on commit 82319c0

Please sign in to comment.