Skip to content

Commit

Permalink
refactor: move ghx.core to common.model
Browse files Browse the repository at this point in the history
  • Loading branch information
aweris committed Oct 23, 2023
1 parent 4362fe8 commit 52d2371
Show file tree
Hide file tree
Showing 21 changed files with 174 additions and 174 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package model

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion ghx/core/doc.go → common/model/doc.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Package core provides the core functionality of the gale library. It is used by the all components of the gale.
package core
package model
2 changes: 1 addition & 1 deletion ghx/core/enum.go → common/model/enum.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package model

// Conclusion is outcome of the operation
type Conclusion string
Expand Down
2 changes: 1 addition & 1 deletion ghx/core/job.go → common/model/job.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package model

import "gopkg.in/yaml.v3"

Expand Down
2 changes: 1 addition & 1 deletion ghx/core/job_matrix.go → common/model/job_matrix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package model

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package model

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion ghx/core/step.go → common/model/step.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package model

import "strings"

Expand Down
2 changes: 1 addition & 1 deletion ghx/core/workflow.go → common/model/workflow.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package model

// Workflow represents a GitHub Actions workflow.
//
Expand Down
20 changes: 10 additions & 10 deletions ghx/context/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package context
import (
"dagger.io/dagger"

"github.com/aweris/gale/ghx/core"
"github.com/aweris/gale/common/model"
)

type GhxConfig struct {
Expand All @@ -28,16 +28,16 @@ type DaggerContext struct {

type ExecutionContext struct {
// Workflow is the current workflow that is being executed.
WorkflowRun *core.WorkflowRun
WorkflowRun *model.WorkflowRun

// Job is the current job that is being executed.
JobRun *core.JobRun
JobRun *model.JobRun

// Step is the current step that is being executed.
StepRun *core.StepRun
StepRun *model.StepRun

// CurrentAction is the current action that is being executed. This is only available on step level if the step is uses a custom action.
CurrentAction *core.CustomAction
CurrentAction *model.CustomAction
}

// ActionsContext is the context for the internal services configuration for used by GitHub Actions.
Expand Down Expand Up @@ -173,7 +173,7 @@ type InputsContext map[string]string
//
// See: https://docs.github.com/en/actions/learn-github-actions/contexts#job-context
type JobContext struct {
Status core.Conclusion `json:"status"` // Status is the current status of the job. Possible values are success, failure, or cancelled.
Status model.Conclusion `json:"status"` // Status is the current status of the job. Possible values are success, failure, or cancelled.

// TODO: add other fields when needed.
}
Expand All @@ -185,7 +185,7 @@ type NeedsContext map[string]NeedContext

// NeedContext is a context that contains information about dependent job.
type NeedContext struct {
Result core.Conclusion `json:"result"` // Result is conclusion of the job. It can be success, failure, skipped or cancelled.
Result model.Conclusion `json:"result"` // Result is conclusion of the job. It can be success, failure, skipped or cancelled.
Outputs map[string]string `json:"outputs"` // Outputs of the job
}

Expand Down Expand Up @@ -231,10 +231,10 @@ type StepsContext map[string]StepContext
// This context created per step execution.
type StepContext struct {
// Conclusion is the result of a completed step after continue-on-error is applied
Conclusion core.Conclusion `json:"conclusion"`
Conclusion model.Conclusion `json:"conclusion"`

// Outcome is the result of a completed step before continue-on-error is applied
Outcome core.Conclusion `json:"outcome"`
Outcome model.Conclusion `json:"outcome"`

// Outputs is a map of output name to output value
Outputs map[string]string `json:"outputs"`
Expand All @@ -254,4 +254,4 @@ type EnvContext map[string]string
// MatrixContext is a context that contains matrix information.
//
// See: https://docs.github.com/en/actions/learn-github-actions/contexts#matrix-context
type MatrixContext core.MatrixCombination
type MatrixContext model.MatrixCombination
20 changes: 10 additions & 10 deletions ghx/context/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (

"github.com/aweris/gale/common/fs"
"github.com/aweris/gale/common/log"
"github.com/aweris/gale/ghx/core"
"github.com/aweris/gale/common/model"
)

// SetWorkflow creates a new execution context with the given workflow and sets it to the context.
func (c *Context) SetWorkflow(wr *core.WorkflowRun) error {
func (c *Context) SetWorkflow(wr *model.WorkflowRun) error {
// set the workflow run to the execution context
c.Execution = ExecutionContext{WorkflowRun: wr}

Expand All @@ -25,7 +25,7 @@ func (c *Context) SetWorkflow(wr *core.WorkflowRun) error {
c.Github.WorkflowSHA = c.Github.SHA

// set workflow conclusion to success explicitly
c.Execution.WorkflowRun.Conclusion = core.ConclusionSuccess
c.Execution.WorkflowRun.Conclusion = model.ConclusionSuccess

// sync github context with env values
syncWithEnvValues(&c.Github)
Expand Down Expand Up @@ -60,7 +60,7 @@ func (c *Context) UnsetWorkflow(result RunResult) {
}

// SetJob sets the given job to the execution context.
func (c *Context) SetJob(jr *core.JobRun) error {
func (c *Context) SetJob(jr *model.JobRun) error {
if c.Execution.WorkflowRun == nil {
return errors.New("no workflow is set")
}
Expand Down Expand Up @@ -109,7 +109,7 @@ func (c *Context) UnsetJob(result RunResult) {
c.Execution.WorkflowRun.Jobs[jr.Job.ID] = *jr

// update workflow conclusion
if c.Execution.WorkflowRun.Conclusion == core.ConclusionSuccess && jr.Conclusion != core.ConclusionSuccess {
if c.Execution.WorkflowRun.Conclusion == model.ConclusionSuccess && jr.Conclusion != model.ConclusionSuccess {
c.Execution.WorkflowRun.Conclusion = jr.Conclusion
}
// unset the job run from the github context
Expand All @@ -136,7 +136,7 @@ func (c *Context) UnsetJob(result RunResult) {
}

// SetJobResults sets the status of the job.
func (c *Context) SetJobResults(conclusion, outcome core.Conclusion, outputs map[string]string) error {
func (c *Context) SetJobResults(conclusion, outcome model.Conclusion, outputs map[string]string) error {
if c.Execution.JobRun == nil {
return errors.New("no job is set")
}
Expand All @@ -153,7 +153,7 @@ func (c *Context) SetJobResults(conclusion, outcome core.Conclusion, outputs map
}

// SetStep sets the given step to the execution context.
func (c *Context) SetStep(sr *core.StepRun) error {
func (c *Context) SetStep(sr *model.StepRun) error {
if c.Execution.JobRun == nil {
return errors.New("no job is set")
}
Expand Down Expand Up @@ -204,7 +204,7 @@ func (c *Context) UnsetStep(result RunResult) {
c.Steps[sr.Step.ID] = sc

// only export the result of the main stage
if c.Execution.StepRun.Stage == core.StepStageMain {
if c.Execution.StepRun.Stage == model.StepStageMain {
// write the job run result to the file system
// ignoring error since directory must exist at this point of execution
dir, _ := c.GetStepRunPath()
Expand All @@ -225,7 +225,7 @@ func (c *Context) UnsetStep(result RunResult) {
c.Execution.StepRun = nil
}

func (c *Context) SetStepResults(conclusion, outcome core.Conclusion) error {
func (c *Context) SetStepResults(conclusion, outcome model.Conclusion) error {
if c.Execution.StepRun == nil {
return errors.New("no step is set")
}
Expand Down Expand Up @@ -290,7 +290,7 @@ func (c *Context) SetStepEnv(key, value string) error {
return nil
}

func (c *Context) SetAction(action *core.CustomAction) {
func (c *Context) SetAction(action *model.CustomAction) {
c.Execution.CurrentAction = action
}

Expand Down
66 changes: 33 additions & 33 deletions ghx/context/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@ package context
import (
"time"

"github.com/aweris/gale/ghx/core"
"github.com/aweris/gale/common/model"
)

// RunResult is the result of the task execution
type RunResult struct {
Ran bool `json:"ran"` // Ran indicates if the execution ran
Conclusion core.Conclusion `json:"conclusion"` // Conclusion of the execution
Duration time.Duration `json:"duration"` // Duration of the execution
Ran bool `json:"ran"` // Ran indicates if the execution ran
Conclusion model.Conclusion `json:"conclusion"` // Conclusion of the execution
Duration time.Duration `json:"duration"` // Duration of the execution
}

type WorkflowRunReport struct {
Ran bool `json:"ran"` // Ran indicates if the execution ran
Duration string `json:"duration"` // Duration of the execution
Name string `json:"name"` // Name is the name of the workflow
Path string `json:"path"` // Path is the path of the workflow
RunID string `json:"run_id"` // RunID is the ID of the run
RunNumber string `json:"run_number"` // RunNumber is the number of the run
RunAttempt string `json:"run_attempt"` // RunAttempt is the attempt number of the run
RetentionDays string `json:"retention_days"` // RetentionDays is the number of days to keep the run logs
Conclusion core.Conclusion `json:"conclusion"` // Conclusion is the result of a completed workflow run after continue-on-error is applied
Jobs map[string]core.Conclusion `json:"jobs"` // Jobs is map of the job run id to its result
Ran bool `json:"ran"` // Ran indicates if the execution ran
Duration string `json:"duration"` // Duration of the execution
Name string `json:"name"` // Name is the name of the workflow
Path string `json:"path"` // Path is the path of the workflow
RunID string `json:"run_id"` // RunID is the ID of the run
RunNumber string `json:"run_number"` // RunNumber is the number of the run
RunAttempt string `json:"run_attempt"` // RunAttempt is the attempt number of the run
RetentionDays string `json:"retention_days"` // RetentionDays is the number of days to keep the run logs
Conclusion model.Conclusion `json:"conclusion"` // Conclusion is the result of a completed workflow run after continue-on-error is applied
Jobs map[string]model.Conclusion `json:"jobs"` // Jobs is map of the job run id to its result
}

// NewWorkflowRunReport creates a new workflow run report from the given workflow run.
func NewWorkflowRunReport(result *RunResult, wr *core.WorkflowRun) *WorkflowRunReport {
func NewWorkflowRunReport(result *RunResult, wr *model.WorkflowRun) *WorkflowRunReport {
report := &WorkflowRunReport{
Ran: result.Ran,
Duration: result.Duration.String(),
Expand All @@ -38,7 +38,7 @@ func NewWorkflowRunReport(result *RunResult, wr *core.WorkflowRun) *WorkflowRunR
RunNumber: wr.RunNumber,
RunAttempt: wr.RunAttempt,
RetentionDays: wr.RetentionDays,
Jobs: make(map[string]core.Conclusion),
Jobs: make(map[string]model.Conclusion),
}

for id, job := range wr.Jobs {
Expand All @@ -49,26 +49,26 @@ func NewWorkflowRunReport(result *RunResult, wr *core.WorkflowRun) *WorkflowRunR
}

type JobRunReport struct {
Ran bool `json:"ran"` // Ran indicates if the execution ran
Duration string `json:"duration"` // Duration of the execution
Name string `json:"name"` // Name is the name of the job
RunID string `json:"run_id"` // RunID is the ID of the run
Conclusion core.Conclusion `json:"conclusion"` // Conclusion is the result of a completed job after continue-on-error is applied
Outcome core.Conclusion `json:"outcome"` // Outcome is the result of a completed job before continue-on-error is applied
Outputs map[string]string `json:"outputs,omitempty"` // Outputs is the outputs generated by the job
Matrix core.MatrixCombination `json:"matrix,omitempty"` // Matrix is the matrix parameters used to run the job
Steps []StepRunSummary `json:"steps"` // Steps is the list of steps in the job
Ran bool `json:"ran"` // Ran indicates if the execution ran
Duration string `json:"duration"` // Duration of the execution
Name string `json:"name"` // Name is the name of the job
RunID string `json:"run_id"` // RunID is the ID of the run
Conclusion model.Conclusion `json:"conclusion"` // Conclusion is the result of a completed job after continue-on-error is applied
Outcome model.Conclusion `json:"outcome"` // Outcome is the result of a completed job before continue-on-error is applied
Outputs map[string]string `json:"outputs,omitempty"` // Outputs is the outputs generated by the job
Matrix model.MatrixCombination `json:"matrix,omitempty"` // Matrix is the matrix parameters used to run the job
Steps []StepRunSummary `json:"steps"` // Steps is the list of steps in the job
}

type StepRunSummary struct {
ID string `json:"id"` // ID is the unique identifier of the step.
Name string `json:"name,omitempty"` // Name is the name of the step
Stage core.StepStage `json:"stage"` // Stage is the stage of the step during the execution of the job. Possible values are: setup, pre, main, post, complete.
Conclusion core.Conclusion `json:"conclusion"` // Conclusion is the result of a completed job after continue-on-error is applied
ID string `json:"id"` // ID is the unique identifier of the step.
Name string `json:"name,omitempty"` // Name is the name of the step
Stage model.StepStage `json:"stage"` // Stage is the stage of the step during the execution of the job. Possible values are: setup, pre, main, post, complete.
Conclusion model.Conclusion `json:"conclusion"` // Conclusion is the result of a completed job after continue-on-error is applied
}

// NewJobRunReport creates a new job run report from the given job run.
func NewJobRunReport(result *RunResult, jr *core.JobRun) *JobRunReport {
func NewJobRunReport(result *RunResult, jr *model.JobRun) *JobRunReport {
report := &JobRunReport{
Ran: result.Ran,
Duration: result.Duration.String(),
Expand Down Expand Up @@ -99,16 +99,16 @@ type StepRunReport struct {
Duration string `json:"duration"` // Duration of the execution
ID string `json:"id"` // ID is the unique identifier of the step.
Name string `json:"name,omitempty"` // Name is the name of the step
Conclusion core.Conclusion `json:"conclusion"` // Conclusion is the result of a completed job after continue-on-error is applied
Outcome core.Conclusion `json:"outcome"` // Outcome is the result of a completed job before continue-on-error is applied
Conclusion model.Conclusion `json:"conclusion"` // Conclusion is the result of a completed job after continue-on-error is applied
Outcome model.Conclusion `json:"outcome"` // Outcome is the result of a completed job before continue-on-error is applied
Outputs map[string]string `json:"outputs,omitempty"` // Outputs is the outputs generated by the job
State map[string]string `json:"state,omitempty"` // State is a map of step state variables.
Env map[string]string `json:"env,omitempty"` // Env is the extra environment variables set by the step.
Path []string `json:"path,omitempty"` // Path is extra PATH items set by the step.
}

// NewStepRunReport creates a new step run report from the given step run.
func NewStepRunReport(result *RunResult, sr *core.StepRun) *StepRunReport {
func NewStepRunReport(result *RunResult, sr *model.StepRun) *StepRunReport {
return &StepRunReport{
Ran: result.Ran,
Duration: result.Duration.String(),
Expand Down
10 changes: 5 additions & 5 deletions ghx/custom_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import (

"github.com/aweris/gale/common/fs"
"github.com/aweris/gale/common/log"
"github.com/aweris/gale/ghx/core"
"github.com/aweris/gale/common/model"
)

// LoadActionFromSource loads an action from given source to the target directory. If the source is a local action,
// the target directory will be the same as the source. If the source is a remote action, the action will be downloaded
// to the target directory using the source as the reference(e.g. {target}/{owner}/{repo}/{path}@{ref}).
func LoadActionFromSource(ctx context.Context, client *dagger.Client, source, targetDir string) (*core.CustomAction, error) {
func LoadActionFromSource(ctx context.Context, client *dagger.Client, source, targetDir string) (*model.CustomAction, error) {
var target string

repo, path, ref, err := parseRepoRef(source)
Expand Down Expand Up @@ -54,7 +54,7 @@ func LoadActionFromSource(ctx context.Context, client *dagger.Client, source, ta
return nil, err
}

return &core.CustomAction{Meta: meta, Path: target}, nil
return &model.CustomAction{Meta: meta, Path: target}, nil
}

// isLocalAction checks if the given source is a local action
Expand Down Expand Up @@ -111,8 +111,8 @@ func ensureActionExistsLocally(source, repo, ref, target string) error {
}

// getCustomActionMeta returns the meta information about the custom action from the action directory.
func getCustomActionMeta(ctx context.Context, actionDir *dagger.Directory) (core.CustomActionMeta, error) {
var meta core.CustomActionMeta
func getCustomActionMeta(ctx context.Context, actionDir *dagger.Directory) (model.CustomActionMeta, error) {
var meta model.CustomActionMeta

file, err := findActionMetadataFileName(ctx, actionDir)
if err != nil {
Expand Down
Loading

0 comments on commit 52d2371

Please sign in to comment.