Skip to content

Commit

Permalink
refactor: remove deprecated opts structs from modules
Browse files Browse the repository at this point in the history
  • Loading branch information
aweris committed Oct 26, 2023
1 parent ebbd914 commit 9f72279
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 248 deletions.
5 changes: 3 additions & 2 deletions daggerverse/gale/dagger.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "gale",
"root": "../..",
"sdk": "go",
"dependencies": [
"github.com/aweris/gale/daggerverse/repo@main",
"github.com/aweris/gale/daggerverse/source@main"
"../repo",
"../source"
]
}
22 changes: 2 additions & 20 deletions daggerverse/gale/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,11 @@ import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"runtime"

"gopkg.in/yaml.v3"
)

// root returns the root directory of the project.
func root() string {
// get location of current file
_, current, _, _ := runtime.Caller(0)

println(current)

return filepath.Join(filepath.Dir(current), "../..")
}

// root returns the root directory of the project.
func (h *Host) root(opts ...HostDirectoryOpts) *Directory {
return h.Directory(root(), opts...)
}

// unmarshalContentsToYAML unmarshal the contents of the file as YAML into the given value.
func (f *File) unmarshalContentsToYAML(ctx context.Context, v interface{}) error {
func unmarshalContentsToYAML(ctx context.Context, f *File, v interface{}) error {
stdout, err := f.Contents(ctx)
if err != nil {
return fmt.Errorf("%w: failed to get file contents", err)
Expand All @@ -41,7 +23,7 @@ func (f *File) unmarshalContentsToYAML(ctx context.Context, v interface{}) error
}

// unmarshalContentsToJSON unmarshal the contents of the file as JSON into the given value.
func (f *File) unmarshalContentsToJSON(ctx context.Context, v interface{}) error {
func unmarshalContentsToJSON(ctx context.Context, f *File, v interface{}) error {
stdout, err := f.Contents(ctx)
if err != nil {
return fmt.Errorf("%w: failed to get file contents", err)
Expand Down
103 changes: 0 additions & 103 deletions daggerverse/gale/opts.go

This file was deleted.

84 changes: 75 additions & 9 deletions daggerverse/gale/workflow_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,111 @@ import (
)

type WorkflowRun struct {
// Configuration for the workflow run.
Config WorkflowRunConfig
}

// Sync evaluates the workflow run and returns the container that executed the workflow.
// WorkflowRunConfig holds the configuration of a workflow run.
type WorkflowRunConfig struct {
// Directory containing the repository source.
Source *Directory

// Name of the repository. Format: owner/name.
Repo string

// Branch name to check out. Only one of branch or tag can be used. Precedence: tag, branch.
Branch string

// Tag name to check out. Only one of branch or tag can be used. Precedence: tag, branch.
Tag string

// Path to the workflow directory.
WorkflowsDir string

// WorkflowFile is external workflow file to run.
WorkflowFile *File

// Workflow to run.
Workflow string

// Job name to run. If empty, all jobs will be run.
Job string

// Name of the event that triggered the workflow. e.g. push
Event string

// File with the complete webhook event payload.
EventFile *File

// Image to use for the runner.
RunnerImage string

// Enables debug mode.
RunnerDebug bool

// GitHub token to use for authentication.
Token *Secret
}

// FIXME: add jobs to WorkflowRunReport when dagger supports map type

// WorkflowRunReport represents the result of a workflow run.
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 string `json:"conclusion"` // Conclusion is the result of a completed workflow run after continue-on-error is applied
}

// Sync runs the workflow and returns the container that ran the workflow.
func (wr *WorkflowRun) Sync(ctx context.Context) (*Container, error) {
container, err := wr.run(ctx)
if err != nil {
return nil, err
}

return container.Sync(ctx)
return container, nil
}

// Directory returns the directory of the workflow run information.
func (wr *WorkflowRun) Directory(ctx context.Context, opts WorkflowRunDirectoryOpts) (*Directory, error) {
func (wr *WorkflowRun) Directory(
ctx context.Context,
// Adds the repository source to the exported directory. (default: false)
includeRepo Optional[bool],
// Adds the mounted secrets to the exported directory. (default: false)
includeSecrets Optional[bool],
// Adds the event file to the exported directory. (default: false)
includeEvent Optional[bool],
// Adds the uploaded artifacts to the exported directory. (default: false)
includeArtifacts Optional[bool],
) (*Directory, error) {
container, err := wr.run(ctx)
if err != nil {
return nil, err
}

dir := dag.Directory().WithDirectory("run", container.Directory("/home/runner/_temp/ghx/run"))

if opts.IncludeRepo {
if includeRepo.GetOr(false) {
dir = dir.WithDirectory("repo", container.Directory("."))
}

if opts.IncludeSecrets {
if includeSecrets.GetOr(false) {
dir = dir.WithDirectory("secrets", container.Directory("/home/runner/_temp/ghx/secrets"))
}

if opts.IncludeEvent && wr.Config.EventFile != nil {
if includeEvent.GetOr(false) && wr.Config.EventFile != nil {
dir = dir.WithFile("event.json", container.File("/home/runner/_temp/_github_workflow/event.json"))
}

if opts.IncludeArtifacts {
if includeArtifacts.GetOr(false) {
var report WorkflowRunReport

err := dir.File("run/workflow_run.json").unmarshalContentsToJSON(ctx, &report)
err := unmarshalContentsToJSON(ctx, dir.File("run/workflow_run.json"), &report)
if err != nil {
return nil, err
}
Expand Down
85 changes: 67 additions & 18 deletions daggerverse/gale/workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,31 @@ import (

type Workflows struct{}

func (w *Workflows) List(ctx context.Context, repoOpts RepoOpts, dirOpts WorkflowsDirOpts) (string, error) {
dir := dag.Repo().Source((RepoSourceOpts)(repoOpts)).Directory(dirOpts.WorkflowsDir)
func (w *Workflows) List(
ctx context.Context,
// The directory containing the repository source. If source is provided, rest of the options are ignored.
source Optional[*Directory],
// The name of the repository. Format: owner/name.
repo Optional[string],
// Tag name to check out. Only one of branch or tag can be used. Precedence is as follows: tag, branch.
tag Optional[string],
// Branch name to check out. Only one of branch or tag can be used. Precedence is as follows: tag, branch.
branch Optional[string],
// Path to the workflows directory. (default: .github/workflows)
workflowsDir Optional[string],
) (string, error) {
// convert workflows list options to repo source options
opts := RepoSourceOpts{
Source: source.GetOr(nil),
Repo: repo.GetOr(""),
Tag: tag.GetOr(""),
Branch: branch.GetOr(""),
}

// get the repository source working directory from the options
dir := dag.Repo().Source(opts).Directory(workflowsDir.GetOr(".github/workflows"))

// list all entries in the workflows directory
entries, err := dir.Entries(ctx)
if err != nil {
return "", err
Expand All @@ -23,7 +45,7 @@ func (w *Workflows) List(ctx context.Context, repoOpts RepoOpts, dirOpts Workflo
// load only .yaml and .yml files
if strings.HasSuffix(entry, ".yaml") || strings.HasSuffix(entry, ".yml") {
file := dir.File(entry)
path := filepath.Join(dirOpts.WorkflowsDir, entry)
path := filepath.Join(workflowsDir.GetOr(".github/workflows"), entry)

// dagger do not support maps yet, so we're defining anonymous struct to unmarshal the yaml file to avoid
// hit this limitation.
Expand All @@ -33,7 +55,7 @@ func (w *Workflows) List(ctx context.Context, repoOpts RepoOpts, dirOpts Workflo
Jobs map[string]interface{} `yaml:"jobs"`
}

if err := file.unmarshalContentsToYAML(ctx, &workflow); err != nil {
if err := unmarshalContentsToYAML(ctx, file, &workflow); err != nil {
return "", err
}

Expand All @@ -57,22 +79,49 @@ func (w *Workflows) List(ctx context.Context, repoOpts RepoOpts, dirOpts Workflo
return sb.String(), nil
}

func (w *Workflows) Run(repoOpts RepoOpts, pathOpts WorkflowsDirOpts, runOpts WorkflowsRunOpts) *WorkflowRun {
func (w *Workflows) Run(
// The directory containing the repository source. If source is provided, rest of the options are ignored.
source Optional[*Directory],
// The name of the repository. Format: owner/name.
repo Optional[string],
// Tag name to check out. Only one of branch or tag can be used. Precedence is as follows: tag, branch.
tag Optional[string],
// Branch name to check out. Only one of branch or tag can be used. Precedence is as follows: tag, branch.
branch Optional[string],
// Path to the workflows directory. (default: .github/workflows)
workflowsDir Optional[string],
// External workflow file to run.
workflowFile Optional[*File],
// Name of the workflow to run.
workflow Optional[string],
// Name of the job to run. If empty, all jobs will be run.
job Optional[string],
// Name of the event that triggered the workflow. e.g. push
event Optional[string],
// File with the complete webhook event payload.
eventFile Optional[*File],
// Image to use for the runner.
runnerImage Optional[string],
// Enables debug mode.
runnerDebug Optional[bool],
// GitHub token to use for authentication.
token Optional[*Secret],
) *WorkflowRun {
return &WorkflowRun{
Config: WorkflowRunConfig{
Source: repoOpts.Source,
Repo: repoOpts.Repo,
Branch: repoOpts.Branch,
Tag: repoOpts.Tag,
WorkflowsDir: pathOpts.WorkflowsDir,
WorkflowFile: runOpts.WorkflowFile,
Workflow: runOpts.Workflow,
Job: runOpts.Job,
Event: runOpts.Event,
EventFile: runOpts.EventFile,
RunnerImage: runOpts.RunnerImage,
RunnerDebug: runOpts.RunnerDebug,
Token: runOpts.Token,
Source: source.GetOr(nil),
Repo: repo.GetOr(""),
Branch: branch.GetOr(""),
Tag: tag.GetOr(""),
WorkflowsDir: workflowsDir.GetOr(".github/workflows"),
WorkflowFile: workflowFile.GetOr(nil),
Workflow: workflow.GetOr(""),
Job: job.GetOr(""),
Event: event.GetOr("push"),
EventFile: eventFile.GetOr(nil),
RunnerImage: runnerImage.GetOr("ghcr.io/catthehacker/ubuntu:act-latest"),
RunnerDebug: runnerDebug.GetOr(false),
Token: token.GetOr(nil),
},
}
}
Loading

0 comments on commit 9f72279

Please sign in to comment.