Skip to content

Commit

Permalink
Refactor based on Google's Go style guide
Browse files Browse the repository at this point in the history
Adjust the existing code base based on hints from Google's style guide,
found at <https://google.github.io/styleguide/go>. The goal isn't for
this code base to perfectly match that style guide, rather to take
inspiration and incrementally improve the code base. Only guidelines
which I think make sense were adopted.

In total, three main points are included here:

https://google.github.io/styleguide/go/decisions#named-result-parameters
for cleaner APIs. (I actually found this to highlight potential problems
in the code base in certain cases, such as with `extractScript` (not
addressed)).

https://google.github.io/styleguide/go/decisions#got-before-want for
more standard test structure.

https://google.github.io/styleguide/go/decisions#use-q for better string
formatting.

Signed-off-by: Eric Cornelissen <ericornelissen@gmail.com>
  • Loading branch information
ericcornelissen committed Nov 29, 2023
1 parent aea16c1 commit fa28274
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 156 deletions.
27 changes: 16 additions & 11 deletions analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ type violation struct {

var r = regexp.MustCompile(`\$\{\{.*?\}\}`)

func analyzeManifest(manifest *Manifest) (violations []violation) {
func analyzeManifest(manifest *Manifest) []violation {
if manifest.Runs.Using == "composite" {
violations = analyzeSteps(manifest.Runs.Steps)
return analyzeSteps(manifest.Runs.Steps)
} else {
return make([]violation, 0)

Check failure on line 44 in analyze.go

View workflow job for this annotation

GitHub Actions / Mutation test

invalid argument: index -1 (constant of type int) must not be negative
}

return violations
}

func analyzeWorkflow(workflow *Workflow) (violations []violation) {
func analyzeWorkflow(workflow *Workflow) []violation {
violations := make([]violation, 0)

Check failure on line 49 in analyze.go

View workflow job for this annotation

GitHub Actions / Mutation test

invalid argument: index -1 (constant of type int) must not be negative
for id, job := range workflow.Jobs {
job := job
violations = append(violations, analyzeJob(id, &job)...)
Expand All @@ -54,12 +55,13 @@ func analyzeWorkflow(workflow *Workflow) (violations []violation) {
return violations
}

func analyzeJob(id string, job *WorkflowJob) (violations []violation) {
func analyzeJob(id string, job *WorkflowJob) []violation {
name := job.Name
if name == "" {
name = id
}

violations := make([]violation, 0)

Check failure on line 64 in analyze.go

View workflow job for this annotation

GitHub Actions / Mutation test

invalid argument: index -1 (constant of type int) must not be negative
for _, v := range analyzeSteps(job.Steps) {
v.jobId = name
violations = append(violations, v)
Expand All @@ -68,7 +70,8 @@ func analyzeJob(id string, job *WorkflowJob) (violations []violation) {
return violations
}

func analyzeSteps(steps []JobStep) (violations []violation) {
func analyzeSteps(steps []JobStep) []violation {
violations := make([]violation, 0)

Check failure on line 74 in analyze.go

View workflow job for this annotation

GitHub Actions / Mutation test

invalid argument: index -1 (constant of type int) must not be negative
for i, step := range steps {
step := step
violations = append(violations, analyzeStep(i, &step)...)
Expand All @@ -77,12 +80,13 @@ func analyzeSteps(steps []JobStep) (violations []violation) {
return violations
}

func analyzeStep(id int, step *JobStep) (violations []violation) {
func analyzeStep(id int, step *JobStep) []violation {
name := step.Name
if step.Name == "" {
name = fmt.Sprintf("#%d", id)
}

violations := make([]violation, 0)

Check failure on line 89 in analyze.go

View workflow job for this annotation

GitHub Actions / Mutation test

invalid argument: index -1 (constant of type int) must not be negative
script, kind := extractScript(step)
for _, v := range analyzeScript(script) {
v.kind = kind
Expand All @@ -93,7 +97,8 @@ func analyzeStep(id int, step *JobStep) (violations []violation) {
return violations
}

func analyzeScript(script string) (violations []violation) {
func analyzeScript(script string) []violation {
violations := make([]violation, 0)

Check failure on line 101 in analyze.go

View workflow job for this annotation

GitHub Actions / Mutation test

invalid argument: index -1 (constant of type int) must not be negative
if matches := r.FindAll([]byte(script), len(script)); matches != nil {
for _, problem := range matches {
violations = append(violations, violation{
Expand All @@ -105,14 +110,14 @@ func analyzeScript(script string) (violations []violation) {
return violations
}

func extractScript(step *JobStep) (script string, kind violationKind) {
func extractScript(step *JobStep) (string, violationKind) {
switch {
case isRunStep(step):
return step.Run, expressionInRunScript
case isActionsGitHubScriptStep(step):
return step.With.Script, expressionInActionsGithubScript
default:
return script, kind
return "", expressionInRunScript
}
}

Expand Down
Loading

0 comments on commit fa28274

Please sign in to comment.