Skip to content

Commit

Permalink
refactor: use remove source workaround from repo
Browse files Browse the repository at this point in the history
  • Loading branch information
aweris committed Nov 2, 2023
1 parent 8a68f34 commit 04625be
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 52 deletions.
29 changes: 7 additions & 22 deletions daggerverse/gale/workflow_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,30 +171,15 @@ func (wr *WorkflowRun) container(ctx context.Context) (*Container, error) {
container = container.With(dag.Source().ArtifactService().BindAsService)
container = container.With(dag.Source().ArtifactCacheService().BindAsService)

// configure repo -- when *Directory can be included in to repo info, we can move source mounting to repo module as well
var (
info = dag.Repo().Info(RepoInfoOpts{
Source: wr.Config.Source,
Repo: wr.Config.Repo,
Branch: wr.Config.Branch,
Tag: wr.Config.Tag,
})
source = dag.Repo().Source(RepoSourceOpts{
Source: wr.Config.Source,
Repo: wr.Config.Repo,
Branch: wr.Config.Branch,
Tag: wr.Config.Tag,
})
)

workdir, err := info.Workdir(ctx)
if err != nil {
return nil, err
}
// configure repo
info := dag.Repo().Info(RepoInfoOpts{
Source: wr.Config.Source,
Repo: wr.Config.Repo,
Branch: wr.Config.Branch,
Tag: wr.Config.Tag,
})

container = container.With(info.Configure)
container = container.WithMountedDirectory(workdir, source).WithWorkdir(workdir)
container = container.WithEnvVariable("GITHUB_WORKSPACE", workdir)

// add env variable to the container to indicate container is configured
container = container.WithEnvVariable("GALE_CONFIGURED", "true")
Expand Down
7 changes: 5 additions & 2 deletions daggerverse/gale/workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ func (w *Workflows) List(
workflowsDir Optional[string],
) (string, error) {
// convert workflows list options to repo source options
opts := RepoSourceOpts{
opts := RepoInfoOpts{
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"))
dir := dag.Repo().
Info(opts).
Source().
Directory(workflowsDir.GetOr(".github/workflows"))

// list all entries in the workflows directory
entries, err := dir.Entries(ctx)
Expand Down
45 changes: 17 additions & 28 deletions daggerverse/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,17 @@ type Repo struct{}

// RepoInfo represents a repository information.
type RepoInfo struct {
Owner string // Owner of the repository.
Name string // Name of the repository.
NameWithOwner string // NameWithOwner combined version of owner and name. Format: owner/name.
URL string // URL of the repository.
Ref string // Ref is the branch or tag ref that triggered the workflow
RefName string // RefName is the short name (without refs/heads/ prefix) of the branch or tag ref that triggered the workflow.
RefType string // RefType is the type of ref that triggered the workflow. Possible values are branch, tag, or empty, if neither
SHA string // SHA is the commit SHA that triggered the workflow. The value of this commit SHA depends on the event that
ShortSHA string // ShortSHA is the short commit SHA that triggered the workflow. The value of this commit SHA depends on the event that
IsRemote bool // IsRemote is true if the ref is a remote ref.
}

// TODO: follow up
// this method is separate from the RepoInfo struct because we're not able to return *Directory as part of RepoInfo.
// Until it is fixed, we're returning *Directory from this method.

func (_ *Repo) Source(
// 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],
) (*Directory, error) {
return getRepoSource(source, repo, tag, branch)
Owner string // Owner of the repository.
Name string // Name of the repository.
NameWithOwner string // NameWithOwner combined version of owner and name. Format: owner/name.
URL string // URL of the repository.
Ref string // Ref is the branch or tag ref that triggered the workflow
RefName string // RefName is the short name (without refs/heads/ prefix) of the branch or tag ref that triggered the workflow.
RefType string // RefType is the type of ref that triggered the workflow. Possible values are branch, tag, or empty, if neither
SHA string // SHA is the commit SHA that triggered the workflow. The value of this commit SHA depends on the event that
ShortSHA string // ShortSHA is the short commit SHA that triggered the workflow. The value of this commit SHA depends on the event that
IsRemote bool // IsRemote is true if the ref is a remote ref.
Source *Directory // Source is the directory containing the repository source.
}

func (_ *Repo) Info(
Expand Down Expand Up @@ -102,6 +86,7 @@ func (_ *Repo) Info(
SHA: sha,
ShortSHA: shortSHA,
IsRemote: !isLocal,
Source: dir,
}, nil
}

Expand All @@ -112,7 +97,11 @@ func (ri *RepoInfo) Workdir() string {

// Configure configures the container with the repository information.
func (ri *RepoInfo) Configure(_ context.Context, c *Container) (*Container, error) {
return c.WithEnvVariable("GH_REPO", ri.NameWithOwner).
workdir := ri.Workdir()
return c.WithMountedDirectory(workdir, ri.Source).
WithWorkdir(workdir).
WithEnvVariable("GITHUB_WORKSPACE", workdir).
WithEnvVariable("GH_REPO", ri.NameWithOwner).
WithEnvVariable("GITHUB_REPOSITORY", ri.NameWithOwner).
WithEnvVariable("GITHUB_REPOSITORY_OWNER", ri.Owner).
WithEnvVariable("GITHUB_REPOSITORY_URL", ri.URL).
Expand Down

0 comments on commit 04625be

Please sign in to comment.