Skip to content

Commit

Permalink
fix: use local git to find git sha
Browse files Browse the repository at this point in the history
Not all files and commits exist remotely. Use local git to look for commit sha
  • Loading branch information
aweris committed Aug 10, 2023
1 parent 8a84b34 commit 1240366
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
4 changes: 2 additions & 2 deletions internal/core/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ type GithubWorkflowContext struct {
}

// NewGithubWorkflowContext creates a new GithubWorkflowContext from the given workflow.
func NewGithubWorkflowContext(repo *Repository, workflow *Workflow, runID string) GithubWorkflowContext {
func NewGithubWorkflowContext(repo *Repository, workflow *Workflow, runID, workflowSHA string) GithubWorkflowContext {
return GithubWorkflowContext{
Workflow: workflow.Name,
WorkflowRef: fmt.Sprintf("%s/%s@%s", repo.NameWithOwner, workflow.Path, repo.CurrentRef),
WorkflowSHA: workflow.SHA,
WorkflowSHA: workflowSHA,
RunID: runID,
RunNumber: "1", // TODO: fill this value
RunAttempt: "1", // TODO: fill this value
Expand Down
11 changes: 0 additions & 11 deletions internal/core/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,5 @@ func (r *Repository) loadWorkflow(ctx context.Context, path string, file *dagger
workflow.Name = path
}

// TODO: this is an expensive operation. We should move this to a separate method and call it only when needed.

api := fmt.Sprintf("repos/%s/contents/%s?ref=%s", r.NameWithOwner, path, r.CurrentRef)

stdout, stderr, err := gh.Exec("api", api, "--jq", ".sha")
if err != nil {
return nil, fmt.Errorf("failed to get current repository: %w stderr: %s", err, stderr.String())
}

workflow.SHA = strings.TrimSpace(stdout.String())

return &workflow, nil
}
1 change: 0 additions & 1 deletion internal/core/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ type Workflow struct {
Name string `yaml:"name"` // Name is the name of the workflow.
Env map[string]string `yaml:"env"` // Env is the environment variables used in the workflow
Jobs map[string]Job `yaml:"jobs"` // Jobs is the list of jobs in the workflow.
SHA string `yaml:"-"` // SHA is the commit SHA for the workflow file. Set by gale when loading the workflow.

// TBD: add more fields when needed
}
16 changes: 15 additions & 1 deletion pkg/gale/gale.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package gale

import (
"context"
"strings"

"dagger.io/dagger"

"github.com/aweris/gale/internal/config"
"github.com/aweris/gale/internal/core"
"github.com/aweris/gale/internal/dagger/helpers"
"github.com/aweris/gale/internal/dagger/services"
Expand Down Expand Up @@ -70,12 +72,24 @@ func Run(ctx context.Context, workflow, job string, opts ...RunOpts) dagger.With
return helpers.FailPipeline(container, err)
}

//TODO: not sure if this is the best way to get the sha, probably we're missing some scenarios. However, it works for now.

sha, err := config.Client().Container().
From("alpine/git").
WithMountedDirectory("/workdir", repo.Dir).
WithWorkdir("/workdir").
WithExec([]string{"log", "-1", "--follow", "--format=%H", "--", wf.Path}).
Stdout(ctx)
if err != nil {
return helpers.FailPipeline(container, err)
}

// context configuration
container = container.With(core.NewRunnerContext().WithContainerFunc())
container = container.With(core.NewGithubRepositoryContext(repo).WithContainerFunc())
container = container.With(core.NewGithubSecretsContext(token).WithContainerFunc())
container = container.With(core.NewGithubURLContext().WithContainerFunc())
container = container.With(core.NewGithubWorkflowContext(repo, wf, workflowRunID).WithContainerFunc())
container = container.With(core.NewGithubWorkflowContext(repo, wf, workflowRunID, strings.TrimSpace(sha)).WithContainerFunc())
container = container.With(core.NewGithubJobInfoContext(job).WithContainerFunc())
container = container.With(core.NewSecretsContext(token, opt.Secrets).WithContainerFunc())

Expand Down

0 comments on commit 1240366

Please sign in to comment.