Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust cli exec metadata structure to equal server metadata #4119

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cli/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ func runExec(ctx context.Context, c *cli.Command, file, repoPath string) error {
}

func execWithAxis(ctx context.Context, c *cli.Command, file, repoPath string, axis matrix.Axis) error {
metadata := metadataFromContext(ctx, c, axis)
metadata, err := metadataFromContext(ctx, c, axis)
if err != nil {
return fmt.Errorf("could not create metadata: %w", err)
}
environ := metadata.Environ()
var secrets []compiler.Secret
for key, val := range metadata.Workflow.Matrix {
Expand Down
48 changes: 42 additions & 6 deletions cli/exec/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ var flags = []cli.Flag{
Sources: cli.EnvVars("CI_SYSTEM_PLATFORM"),
Name: "system-platform",
},
&cli.StringFlag{
Sources: cli.EnvVars("CI_SYSTEM_HOST"),
Name: "system-host",
},
&cli.StringFlag{
Sources: cli.EnvVars("CI_SYSTEM_NAME"),
Name: "system-name",
Expand All @@ -149,6 +153,16 @@ var flags = []cli.Flag{
Sources: cli.EnvVars("CI_REPO_URL"),
Name: "repo-url",
},
&cli.StringFlag{
Sources: cli.EnvVars("CI_REPO_SCM"),
Name: "repo-scm",
Value: "git",
},
&cli.StringFlag{
Sources: cli.EnvVars("CI_REPO_DEFAULT_BRANCH"),
Name: "repo-default-branch",
Value: "main",
},
&cli.StringFlag{
Sources: cli.EnvVars("CI_REPO_CLONE_URL"),
Name: "repo-clone-url",
Expand Down Expand Up @@ -187,17 +201,22 @@ var flags = []cli.Flag{
Value: "manual",
},
&cli.StringFlag{
Sources: cli.EnvVars("CI_PIPELINE_URL"),
Sources: cli.EnvVars("CI_PIPELINE_FORGE_URL"),
Name: "pipeline-url",
},
&cli.StringFlag{
Sources: cli.EnvVars("CI_PIPELINE_DEPLOY_TARGET", "CI_PIPELINE_TARGET"), // TODO: remove CI_PIPELINE_TARGET in 3.x
Sources: cli.EnvVars("CI_PIPELINE_DEPLOY_TARGET"),
Name: "pipeline-deploy-to",
},
&cli.StringFlag{
Sources: cli.EnvVars("CI_PIPELINE_DEPLOY_TASK", "CI_PIPELINE_TASK"), // TODO: remove CI_PIPELINE_TASK in 3.x
Sources: cli.EnvVars("CI_PIPELINE_DEPLOY_TASK"),
Name: "pipeline-deploy-task",
},
&cli.StringFlag{
Sources: cli.EnvVars("CI_PIPELINE_FILES"),
Usage: "either json formatted list of strings, or comma separated string list",
Name: "pipeline-files",
},
&cli.StringFlag{
Sources: cli.EnvVars("CI_COMMIT_SHA"),
Name: "commit-sha",
Expand All @@ -213,13 +232,14 @@ var flags = []cli.Flag{
&cli.StringFlag{
Sources: cli.EnvVars("CI_COMMIT_BRANCH"),
Name: "commit-branch",
Value: "main",
},
&cli.StringFlag{
Sources: cli.EnvVars("CI_COMMIT_MESSAGE"),
Name: "commit-message",
},
&cli.StringFlag{
Sources: cli.EnvVars("CI_COMMIT_AUTHOR_NAME"),
Sources: cli.EnvVars("CI_COMMIT_AUTHOR"),
Name: "commit-author-name",
},
&cli.StringFlag{
Expand All @@ -230,6 +250,14 @@ var flags = []cli.Flag{
Sources: cli.EnvVars("CI_COMMIT_AUTHOR_EMAIL"),
Name: "commit-author-email",
},
&cli.StringSliceFlag{
Sources: cli.EnvVars("CI_COMMIT_PULL_REQUEST_LABELS"),
Name: "commit-pull-labels",
},
&cli.BoolFlag{
Sources: cli.EnvVars("CI_COMMIT_PRERELEASE"),
Name: "commit-release-is-pre",
},
&cli.IntFlag{
Sources: cli.EnvVars("CI_PREV_PIPELINE_NUMBER"),
Name: "prev-pipeline-number",
Expand All @@ -255,9 +283,17 @@ var flags = []cli.Flag{
Name: "prev-pipeline-event",
},
&cli.StringFlag{
Sources: cli.EnvVars("CI_PREV_PIPELINE_URL"),
Sources: cli.EnvVars("CI_PREV_PIPELINE_FORGE_URL"),
Name: "prev-pipeline-url",
},
&cli.StringFlag{
Sources: cli.EnvVars("CI_PREV_PIPELINE_DEPLOY_TARGET"),
Name: "prev-pipeline-deploy-to",
},
&cli.StringFlag{
Sources: cli.EnvVars("CI_PREV_PIPELINE_DEPLOY_TASK"),
Name: "prev-pipeline-deploy-task",
},
&cli.StringFlag{
Sources: cli.EnvVars("CI_PREV_COMMIT_SHA"),
Name: "prev-commit-sha",
Expand All @@ -279,7 +315,7 @@ var flags = []cli.Flag{
Name: "prev-commit-message",
},
&cli.StringFlag{
Sources: cli.EnvVars("CI_PREV_COMMIT_AUTHOR_NAME"),
Sources: cli.EnvVars("CI_PREV_COMMIT_AUTHOR"),
Name: "prev-commit-author-name",
},
&cli.StringFlag{
Expand Down
24 changes: 22 additions & 2 deletions cli/exec/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package exec

import (
"context"
"encoding/json"
"fmt"
"runtime"
"strings"

Expand All @@ -27,7 +29,7 @@ import (
)

// return the metadata from the cli context.
func metadataFromContext(_ context.Context, c *cli.Command, axis matrix.Axis) metadata.Metadata {
func metadataFromContext(_ context.Context, c *cli.Command, axis matrix.Axis) (metadata.Metadata, error) {
platform := c.String("system-platform")
if platform == "" {
platform = runtime.GOOS + "/" + runtime.GOARCH
Expand All @@ -41,12 +43,26 @@ func metadataFromContext(_ context.Context, c *cli.Command, axis matrix.Axis) me
repoName = fullRepoName[idx+1:]
}

var changedFiles []string
changedFilesRaw := c.String("pipeline-files")
if changedFilesRaw[0] == '[' {
if err := json.Unmarshal([]byte(changedFilesRaw), &changedFiles); err != nil {
return metadata.Metadata{}, fmt.Errorf("pipeline-files detected json but could not parse it: %w", err)
}
} else {
for _, file := range strings.Split(changedFilesRaw, ",") {
changedFiles = append(changedFiles, strings.TrimSpace(file))
}
}

return metadata.Metadata{
Repo: metadata.Repo{
Name: repoName,
Owner: repoOwner,
RemoteID: c.String("repo-remote-id"),
ForgeURL: c.String("repo-url"),
SCM: c.String("repo-scm"),
Branch: c.String("repo-default-branch"),
CloneURL: c.String("repo-clone-url"),
CloneSSHURL: c.String("repo-clone-ssh-url"),
Private: c.Bool("repo-private"),
Expand Down Expand Up @@ -74,6 +90,9 @@ func metadataFromContext(_ context.Context, c *cli.Command, axis matrix.Axis) me
Email: c.String("commit-author-email"),
Avatar: c.String("commit-author-avatar"),
},
PullRequestLabels: c.StringSlice("commit-pull-labels"),
IsPrerelease: c.Bool("commit-release-is-pre"),
ChangedFiles: changedFiles,
},
},
Prev: metadata.Pipeline{
Expand Down Expand Up @@ -109,12 +128,13 @@ func metadataFromContext(_ context.Context, c *cli.Command, axis matrix.Axis) me
Sys: metadata.System{
Name: c.String("system-name"),
URL: c.String("system-url"),
Host: c.String("system-host"),
Platform: platform,
Version: version.Version,
},
Forge: metadata.Forge{
Type: c.String("forge-type"),
URL: c.String("forge-url"),
},
}
}, nil
}
2 changes: 1 addition & 1 deletion pipeline/frontend/metadata/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (m *Metadata) Environ() map[string]string {
"CI_REPO_NAME": m.Repo.Name,
"CI_REPO_OWNER": m.Repo.Owner,
"CI_REPO_REMOTE_ID": m.Repo.RemoteID,
"CI_REPO_SCM": "git",
"CI_REPO_SCM": m.Repo.SCM,
"CI_REPO_URL": m.Repo.ForgeURL,
"CI_REPO_CLONE_URL": m.Repo.CloneURL,
"CI_REPO_CLONE_SSH_URL": m.Repo.CloneSSHURL,
Expand Down
1 change: 1 addition & 0 deletions pipeline/frontend/metadata/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type (
Owner string `json:"owner,omitempty"`
RemoteID string `json:"remote_id,omitempty"`
ForgeURL string `json:"forge_url,omitempty"`
SCM string `json:"scm,omitempty"`
CloneURL string `json:"clone_url,omitempty"`
CloneSSHURL string `json:"clone_url_ssh,omitempty"`
Private bool `json:"private,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions server/pipeline/stepbuilder/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func MetadataFromStruct(forge metadata.ServerForge, repo *model.Repo, pipeline,
Owner: repo.Owner,
RemoteID: fmt.Sprint(repo.ForgeRemoteID),
ForgeURL: repo.ForgeURL,
SCM: string(repo.SCMKind),
CloneURL: repo.Clone,
CloneSSHURL: repo.CloneSSH,
Private: repo.IsSCMPrivate,
Expand Down