Skip to content

Conversation

@nxtcoder17
Copy link
Owner

@nxtcoder17 nxtcoder17 commented Nov 17, 2024

Resolves #24

Summary by Sourcery

Add support for conditional task execution using Go template expressions, enabling tasks to be executed only when certain conditions are met. Refactor the task parsing logic to improve code maintainability.

New Features:

  • Introduce conditional execution of tasks using Go template expressions, allowing tasks to run only if specified conditions evaluate to true.

Enhancements:

  • Refactor the task parsing logic by extracting the Go template evaluation into a separate function for better code organization and reusability.

@sourcery-ai
Copy link

sourcery-ai bot commented Nov 17, 2024

Reviewer's Guide by Sourcery

This PR implements conditional command execution in tasks using a new 'if' field and refactors command parsing logic. The changes introduce a new ParsedCommandJson struct to handle evaluated conditions and improve error handling for command parsing.

Sequence diagram for evalGoTemplateCondition function

sequenceDiagram
    participant Caller
    participant evalGoTemplateCondition
    participant template
    participant bytes.Buffer

    Caller->>evalGoTemplateCondition: Call with tpl
    evalGoTemplateCondition->>template: Create new template
    evalGoTemplateCondition->>template: Parse template expression
    alt Parsing error
        evalGoTemplateCondition-->>Caller: Return TaskRequirementIncorrect error
    end
    evalGoTemplateCondition->>bytes.Buffer: Create new buffer
    evalGoTemplateCondition->>template: Execute template
    alt Execution error
        evalGoTemplateCondition-->>Caller: Return TaskRequirementIncorrect error
    end
    alt Template evaluates to "true"
        evalGoTemplateCondition-->>Caller: Return true
    else
        evalGoTemplateCondition-->>Caller: Return TaskRequirementFailed error
    end
Loading

Sequence diagram for runTask function with conditional execution

sequenceDiagram
    participant runTask
    participant logger
    participant command

    loop for each command in pt.Commands
        runTask->>logger: Debug "running command task"
        alt command.If is not nil and false
            runTask->>logger: Debug "skipping execution for failed `if`"
            runTask-->>command: Continue
        end
        alt command.Run is not empty
            runTask->>runTask: Call recursively with command.Run
            alt Error occurs
                runTask-->>caller: Return error
            end
        end
    end
Loading

Updated class diagram for ParsedTask and CommandJson

classDiagram
    class ParsedTask {
        +[]string Shell
        +string WorkingDir
        +map<string, string> Env
        +bool Interactive
        +[]ParsedCommandJson Commands
    }

    class CommandJson {
        +string Command
        +string Run
        +string Env
        +*string If
    }

    class ParsedCommandJson {
        +string Command
        +string Run
        +string Env
        +*bool If
    }

    ParsedTask --> ParsedCommandJson
    CommandJson --> ParsedCommandJson
Loading

File-Level Changes

Change Details Files
Added conditional command execution support using Go templates
  • Introduced new 'if' field in CommandJson struct for conditional execution
  • Created evalGoTemplateCondition helper function to evaluate template conditions
  • Added condition checking in runTask to skip commands when 'if' condition fails
  • Updated command parsing to handle the new conditional execution feature
pkg/runfile/task.go
pkg/runfile/task-parser.go
pkg/runfile/run.go
Refactored command parsing and structure
  • Split CommandJson into CommandJson and ParsedCommandJson structs
  • Added support for 'cmd' as an alternative to 'run' in command specification
  • Improved error messages for invalid command configurations
  • Made run target validation optional
pkg/runfile/task.go
pkg/runfile/task-parser.go
Fixed output handling in process output function
  • Changed direct stdout writes to use the provided writer interface
  • Updated prefix handling to use the writer interface consistently
pkg/runfile/run.go

Possibly linked issues

  • #12008: The PR implements conditional execution, aligning with the issue's requirement feature for run targets.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @nxtcoder17 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • There is commented out error handling code in parseCommand() that should either be uncommented or removed if not needed
  • The validation check for run targets is commented out - this should probably be restored to prevent runtime errors from invalid task references
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

pcj.Command = cj.Command

if cj.If != nil {
ok, _ := evalGoTemplateCondition(*cj.If)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Error from evalGoTemplateCondition should not be ignored

Silently ignoring template evaluation errors could mask serious problems. Consider handling or propagating the error.

}

return &cj, nil
// if _, ok := rf.Tasks[cj.Run]; !ok {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Run target existence check should be retained

Removing this check could lead to runtime failures when trying to execute non-existent tasks.

@nxtcoder17 nxtcoder17 merged commit 232520d into master Nov 17, 2024
@nxtcoder17 nxtcoder17 deleted the feat/run-if branch November 17, 2024 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEAT] run conditional commands and task, kind of like run if

2 participants