Skip to content

Commit

Permalink
refactor: pass github context by value
Browse files Browse the repository at this point in the history
... to avoid side effects on passed values to each other
  • Loading branch information
aweris committed Jul 13, 2023
1 parent 58c8234 commit 1305fab
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions internal/model/github_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ type GithubContext struct {
}

// NewGithubContextFromEnv creates a new GithubContext from environment variables.
func NewGithubContextFromEnv() *GithubContext {
func NewGithubContextFromEnv() GithubContext {
//nolint:goconst // keeping "true" here as string makes it easier to read and understand. No need to create a const.
return &GithubContext{
return GithubContext{
CI: os.Getenv("CI") == "true",
Actions: os.Getenv("GITHUB_ACTIONS") == "true",
Action: os.Getenv("GITHUB_ACTION"),
Expand Down Expand Up @@ -221,7 +221,7 @@ func NewGithubContextFromEnv() *GithubContext {

// ToEnv converts the GithubContext to a map of environment variables.
// More info: https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
func (g *GithubContext) ToEnv() map[string]string {
func (g GithubContext) ToEnv() map[string]string {
return map[string]string{
"CI": strconv.FormatBool(g.CI),
"GITHUB_ACTION": g.Action,
Expand Down
2 changes: 1 addition & 1 deletion pkg/gale/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type ExecResult struct {
Container *dagger.Container

// contexts
github *model.GithubContext
github model.GithubContext

// services
artifactService *services.ArtifactService
Expand Down
12 changes: 6 additions & 6 deletions pkg/gale/gale.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Gale struct {

// contexts

github *model.GithubContext
github model.GithubContext

// services

Expand Down Expand Up @@ -156,7 +156,7 @@ func (g *Gale) loadCurrentRepository() *Gale {
}

// WithGithubContext sets the github and runner contexts for the gale instance.
func (g *Gale) WithGithubContext(github *model.GithubContext) *Gale {
func (g *Gale) WithGithubContext(github model.GithubContext) *Gale {
return g.WithModifier(func(container *dagger.Container) (*dagger.Container, error) {
// keep reference to the github context in the gale instance to be able to access it later on.
g.github = github
Expand Down Expand Up @@ -253,7 +253,7 @@ func (g *Gale) Container() (container *dagger.Container, err error) {

// TODO: we should find a better way to get the github contexts. This is a temporary solution.

func getInitialGithubContext() (*model.GithubContext, error) {
func getInitialGithubContext() (model.GithubContext, error) {
github := model.NewGithubContextFromEnv()

// if we're running in github actions, we can get the github context from the environment variables.
Expand All @@ -264,7 +264,7 @@ func getInitialGithubContext() (*model.GithubContext, error) {
// update user related information
user, err := gh.CurrentUser()
if err != nil {
return nil, err
return model.GithubContext{}, err
}

github.Actor = user.Login
Expand All @@ -274,7 +274,7 @@ func getInitialGithubContext() (*model.GithubContext, error) {
// update repository related information
repo, err := gh.CurrentRepository()
if err != nil {
return nil, err
return model.GithubContext{}, err
}

github.Repository = repo.NameWithOwner
Expand All @@ -287,7 +287,7 @@ func getInitialGithubContext() (*model.GithubContext, error) {
// update token
token, err := gh.GetToken()
if err != nil {
return nil, err
return model.GithubContext{}, err
}

github.Token = token
Expand Down

0 comments on commit 1305fab

Please sign in to comment.