Skip to content

Commit

Permalink
atlasaction: gitlab ci
Browse files Browse the repository at this point in the history
  • Loading branch information
noamcattan committed Sep 25, 2024
1 parent 3568c22 commit c89437e
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
3 changes: 3 additions & 0 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ func newAction(getenv func(string) string, w io.Writer) (Action, error) {
if getenv("CIRCLECI") == "true" {
return NewCircleCIOrb(getenv, w), nil
}
if getenv("GITLAB_CI") == "true" {
return NewGitlabCI(getenv, w), nil
}
return nil, errors.New("unsupported environment")
}

Expand Down
108 changes: 108 additions & 0 deletions atlasaction/gitlab_action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright 2021-present The Atlas Authors. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.

package atlasaction

import (
"ariga.io/atlas-go-sdk/atlasexec"
"fmt"
"io"
"os"
"strconv"
"strings"
)

// gitlabCI is an implementation of the Action interface for GitHub Actions.
type gitlabCI struct {
w io.Writer
getenv func(string) string
}

var _ Action = (*gitlabCI)(nil)

// NewGitlabCI returns a new Action for Gitlab CI.
func NewGitlabCI(getenv func(string) string, w io.Writer) Action {
return &gitlabCI{getenv: getenv, w: w}
}

// GetType implements the Action interface.
func (g *gitlabCI) GetType() atlasexec.TriggerType {
return "GITLAB_CI"
}

// GetInput implements the Action interface.
func (g *gitlabCI) GetInput(name string) string {
e := strings.ReplaceAll(name, " ", "_")
e = strings.ReplaceAll(e, "-", "_")
return strings.TrimSpace(g.getenv(e))
}

// SetOutput implements the Action interface.
func (g *gitlabCI) SetOutput(name, value string) {
f, err := os.OpenFile("output.env", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return
}
defer f.Close()
f.WriteString(name + "=" + value)
}

// GetTriggerContext implements the Action interface.
func (g *gitlabCI) GetTriggerContext() (*TriggerContext, error) {
ctx := &TriggerContext{
SCM: SCM{
Type: atlasexec.SCMType("GITLAB_CI"),
APIURL: g.getenv("CI_API_V4_URL"),
},
Repo: g.getenv("CI_PROJECT_NAME"),
RepoURL: g.getenv("CI_PROJECT_URL"),
Branch: g.getenv("CI_COMMIT_REF_NAME"),
Commit: g.getenv("CI_COMMIT_SHA"),
Actor: &Actor{Name: g.getenv("GITLAB_USER_NAME"), ID: g.getenv("GITLAB_USER_ID")},
}
if mr := g.getenv("CI_MERGE_REQUEST_IID"); mr != "" {
num, err := strconv.Atoi(mr)
if err != nil {
return nil, err
}
ctx.PullRequest = &PullRequest{
Commit: g.getenv("CI_COMMIT_SHA"),
Number: num,
URL: g.getenv("CI_MERGE_REQUEST_REF_PATH"),
}
}
return ctx, nil
}

// Infof implements the Logger interface.
func (g *gitlabCI) Infof(msg string, args ...any) {
fmt.Fprintf(g.w, msg+EOF, args...)
}

// Warningf implements the Logger interface.
func (g *gitlabCI) Warningf(msg string, args ...any) {
fmt.Fprintf(g.w, msg+EOF, args...)
}

// Errorf implements the Logger interface.
func (g *gitlabCI) Errorf(msg string, args ...any) {
fmt.Fprintf(g.w, msg+EOF, args...)
}

// Fatalf implements the Logger interface.
func (g *gitlabCI) Fatalf(msg string, args ...any) {
g.Errorf(msg, args...)
os.Exit(1)
}

// WithFieldsMap implements the Logger interface.
func (g *gitlabCI) WithFieldsMap(map[string]string) Logger {
// unsupported
return g
}

// AddStepSummary implements the Action interface.
func (g *gitlabCI) AddStepSummary(summary string) {
// unsupported
}

0 comments on commit c89437e

Please sign in to comment.