Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 5 additions & 0 deletions cli/exec/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ var flags = []cli.Flag{
Name: "commit-pull-labels",
Usage: "Set the metadata environment variable \"CI_COMMIT_PULL_REQUEST_LABELS\".",
},
&cli.StringFlag{
Sources: cli.EnvVars("CI_COMMIT_PULL_REQUEST_MILESTONE"),
Name: "commit-pull-milestone",
Usage: "Set the metadata environment variable \"CI_COMMIT_PULL_REQUEST_MILESTONE\".",
},
&cli.BoolFlag{
Sources: cli.EnvVars("CI_COMMIT_PRERELEASE"),
Name: "commit-release-is-pre",
Expand Down
1 change: 1 addition & 0 deletions cli/exec/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func metadataFromContext(_ context.Context, c *cli.Command, axis matrix.Axis, w
metadataFileAndOverrideOrDefault(c, "commit-author-avatar", func(s string) { m.Curr.Commit.Author.Avatar = s }, c.String)

metadataFileAndOverrideOrDefault(c, "commit-pull-labels", func(sl []string) { m.Curr.Commit.PullRequestLabels = sl }, c.StringSlice)
metadataFileAndOverrideOrDefault(c, "commit-pull-milestone", func(s string) { m.Curr.Commit.PullRequestMilestone = s }, c.String)
metadataFileAndOverrideOrDefault(c, "commit-release-is-pre", func(b bool) { m.Curr.Commit.IsPrerelease = b }, c.Bool)

// Previous Pipeline
Expand Down
6 changes: 6 additions & 0 deletions cmd/server/openapi/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4946,6 +4946,9 @@ const docTemplate = `{
"type": "string"
}
},
"pr_milestone": {
"type": "string"
},
"ref": {
"type": "string"
},
Expand Down Expand Up @@ -5554,6 +5557,9 @@ const docTemplate = `{
"message": {
"type": "string"
},
"milestone": {
"type": "string"
},
"ref": {
"type": "string"
},
Expand Down
181 changes: 91 additions & 90 deletions docs/docs/20-usage/50-environment.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pipeline/frontend/metadata/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (m *Metadata) Environ() map[string]string {
setNonEmptyEnvVar(params, "CI_COMMIT_TARGET_BRANCH", targetBranch)
setNonEmptyEnvVar(params, "CI_COMMIT_PULL_REQUEST", pullRegexp.FindString(pipeline.Commit.Ref))
setNonEmptyEnvVar(params, "CI_COMMIT_PULL_REQUEST_LABELS", strings.Join(pipeline.Commit.PullRequestLabels, ","))
setNonEmptyEnvVar(params, "CI_COMMIT_PULL_REQUEST_MILESTONE", pipeline.Commit.PullRequestMilestone)
}

// Only export changed files if maxChangedFiles is not exceeded
Expand Down
19 changes: 10 additions & 9 deletions pipeline/frontend/metadata/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,16 @@ type (

// Commit defines runtime metadata for a commit.
Commit struct {
Sha string `json:"sha,omitempty"`
Ref string `json:"ref,omitempty"`
Refspec string `json:"refspec,omitempty"`
Branch string `json:"branch,omitempty"`
Message string `json:"message,omitempty"`
Author Author `json:"author,omitempty"`
ChangedFiles []string `json:"changed_files,omitempty"`
PullRequestLabels []string `json:"labels,omitempty"`
IsPrerelease bool `json:"is_prerelease,omitempty"`
Sha string `json:"sha,omitempty"`
Ref string `json:"ref,omitempty"`
Refspec string `json:"refspec,omitempty"`
Branch string `json:"branch,omitempty"`
Message string `json:"message,omitempty"`
Author Author `json:"author,omitempty"`
ChangedFiles []string `json:"changed_files,omitempty"`
PullRequestLabels []string `json:"labels,omitempty"`
PullRequestMilestone string `json:"milestone,omitempty"`
IsPrerelease bool `json:"is_prerelease,omitempty"`
}

// Author defines runtime metadata for a commit author.
Expand Down
12 changes: 10 additions & 2 deletions server/forge/forgejo/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,21 @@ func pipelineFromPullRequest(hook *pullRequestHook) *model.Pipeline {
hook.PullRequest.Head.Ref,
hook.PullRequest.Base.Ref,
),
PullRequestLabels: convertLabels(hook.PullRequest.Labels),
FromFork: hook.PullRequest.Head.RepoID != hook.PullRequest.Base.RepoID,
PullRequestLabels: convertLabels(hook.PullRequest.Labels),
PullRequestMilestone: convertMilestone(hook.PullRequest.Milestone),
FromFork: hook.PullRequest.Head.RepoID != hook.PullRequest.Base.RepoID,
}

return pipeline
}

func convertMilestone(milestone *forgejo.Milestone) string {
if milestone == nil || milestone.ID == 0 {
return ""
}
return milestone.Title
}

func pipelineFromRelease(hook *releaseHook) *model.Pipeline {
avatar := expandAvatar(
hook.Repo.HTMLURL,
Expand Down
12 changes: 10 additions & 2 deletions server/forge/gitea/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,21 @@ func pipelineFromPullRequest(hook *pullRequestHook) *model.Pipeline {
hook.PullRequest.Head.Ref,
hook.PullRequest.Base.Ref,
),
PullRequestLabels: convertLabels(hook.PullRequest.Labels),
FromFork: hook.PullRequest.Head.RepoID != hook.PullRequest.Base.RepoID,
PullRequestLabels: convertLabels(hook.PullRequest.Labels),
PullRequestMilestone: convertMilestone(hook.PullRequest.Milestone),
FromFork: hook.PullRequest.Head.RepoID != hook.PullRequest.Base.RepoID,
}

return pipeline
}

func convertMilestone(milestone *gitea.Milestone) string {
if milestone == nil || milestone.ID == 0 {
return ""
}
return milestone.Title
}

func pipelineFromRelease(hook *releaseHook) *model.Pipeline {
avatar := expandAvatar(
hook.Repo.HTMLURL,
Expand Down
5 changes: 3 additions & 2 deletions server/forge/github/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ func parsePullHook(hook *github.PullRequestEvent, merge bool) (*github.PullReque
hook.GetPullRequest().GetHead().GetRef(),
hook.GetPullRequest().GetBase().GetRef(),
),
PullRequestLabels: convertLabels(hook.GetPullRequest().Labels),
FromFork: fromFork,
PullRequestLabels: convertLabels(hook.GetPullRequest().Labels),
PullRequestMilestone: hook.GetPullRequest().GetMilestone().GetTitle(),
Comment thread
6543 marked this conversation as resolved.
FromFork: fromFork,
}
if merge {
pipeline.Ref = fmt.Sprintf(mergeRefs, hook.GetPullRequest().GetNumber())
Expand Down
16 changes: 8 additions & 8 deletions server/forge/gitlab/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,27 @@ func (g *GitLab) convertGitLabRepo(_repo *gitlab.Project, projectMember *gitlab.
return repo, nil
}

func convertMergeRequestHook(hook *gitlab.MergeEvent, req *http.Request) (int, *model.Repo, *model.Pipeline, error) {
repo := &model.Repo{}
pipeline := &model.Pipeline{}
func convertMergeRequestHook(hook *gitlab.MergeEvent, req *http.Request) (mergeID, milestoneID int, repo *model.Repo, pipeline *model.Pipeline, err error) {
repo = &model.Repo{}
pipeline = &model.Pipeline{}

target := hook.ObjectAttributes.Target
source := hook.ObjectAttributes.Source
obj := hook.ObjectAttributes

switch {
case target == nil && source == nil:
return 0, nil, nil, fmt.Errorf("target and source keys expected in merge request hook")
return 0, 0, nil, nil, fmt.Errorf("target and source keys expected in merge request hook")
case target == nil:
return 0, nil, nil, fmt.Errorf("target key expected in merge request hook")
return 0, 0, nil, nil, fmt.Errorf("target key expected in merge request hook")
case source == nil:
return 0, nil, nil, fmt.Errorf("source key expected in merge request hook")
return 0, 0, nil, nil, fmt.Errorf("source key expected in merge request hook")
}

if target.PathWithNamespace != "" {
var err error
if repo.Owner, repo.Name, err = extractFromPath(target.PathWithNamespace); err != nil {
return 0, nil, nil, err
return 0, 0, nil, nil, err
}
repo.FullName = target.PathWithNamespace
} else {
Expand Down Expand Up @@ -140,7 +140,7 @@ func convertMergeRequestHook(hook *gitlab.MergeEvent, req *http.Request) (int, *
pipeline.PullRequestLabels = convertLabels(hook.Labels)
pipeline.FromFork = target.PathWithNamespace != source.PathWithNamespace

return obj.IID, repo, pipeline, nil
return obj.IID, hook.ObjectAttributes.MilestoneID, repo, pipeline, nil
}

func convertPushHook(hook *gitlab.PushEvent) (*model.Repo, *model.Pipeline, error) {
Expand Down
16 changes: 12 additions & 4 deletions server/forge/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,12 +650,12 @@ func (g *GitLab) Hook(ctx context.Context, req *http.Request) (*model.Repo, *mod
event.ObjectAttributes.Action != "reopen" {
return nil, nil, &forge_types.ErrIgnoreEvent{Event: string(eventType), Reason: "no code changes"}
}
mergeIID, repo, pipeline, err := convertMergeRequestHook(event, req)
mergeID, milestoneID, repo, pipeline, err := convertMergeRequestHook(event, req)
if err != nil {
return nil, nil, err
}

if pipeline, err = g.loadChangedFilesFromMergeRequest(ctx, repo, pipeline, mergeIID); err != nil {
if pipeline, err = g.loadMetadataFromMergeRequest(ctx, repo, pipeline, mergeID, milestoneID); err != nil {
return nil, nil, err
}

Expand Down Expand Up @@ -780,7 +780,7 @@ func (g *GitLab) Org(ctx context.Context, u *model.User, owner string) (*model.O
}, nil
}

func (g *GitLab) loadChangedFilesFromMergeRequest(ctx context.Context, tmpRepo *model.Repo, pipeline *model.Pipeline, mergeIID int) (*model.Pipeline, error) {
func (g *GitLab) loadMetadataFromMergeRequest(ctx context.Context, tmpRepo *model.Repo, pipeline *model.Pipeline, mergeID, milestoneID int) (*model.Pipeline, error) {
_store, ok := store.TryFromContext(ctx)
if !ok {
log.Error().Msg("could not get store from context")
Expand All @@ -807,7 +807,7 @@ func (g *GitLab) loadChangedFilesFromMergeRequest(ctx context.Context, tmpRepo *
return nil, err
}

changes, _, err := client.MergeRequests.ListMergeRequestDiffs(_repo.ID, mergeIID, &gitlab.ListMergeRequestDiffsOptions{}, gitlab.WithContext(ctx))
changes, _, err := client.MergeRequests.ListMergeRequestDiffs(_repo.ID, mergeID, &gitlab.ListMergeRequestDiffsOptions{}, gitlab.WithContext(ctx))
if err != nil {
return nil, err
}
Expand All @@ -818,5 +818,13 @@ func (g *GitLab) loadChangedFilesFromMergeRequest(ctx context.Context, tmpRepo *
}
pipeline.ChangedFiles = utils.DeduplicateStrings(files)

if milestoneID != 0 {
milestone, _, err := client.Milestones.GetMilestone(_repo.ID, milestoneID)
if err != nil {
return nil, err
}
pipeline.PullRequestMilestone = milestone.Title
}

return pipeline, nil
}
67 changes: 34 additions & 33 deletions server/model/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,40 @@ import (
)

type Pipeline struct {
ID int64 `json:"id" xorm:"pk autoincr 'id'"`
RepoID int64 `json:"-" xorm:"UNIQUE(s) INDEX 'repo_id'"`
Number int64 `json:"number" xorm:"UNIQUE(s) 'number'"`
Author string `json:"author" xorm:"INDEX 'author'"`
Parent int64 `json:"parent" xorm:"parent"`
Event WebhookEvent `json:"event" xorm:"event"`
Status StatusValue `json:"status" xorm:"INDEX 'status'"`
Errors []*types.PipelineError `json:"errors" xorm:"json 'errors'"`
Created int64 `json:"created" xorm:"'created' NOT NULL DEFAULT 0 created"`
Updated int64 `json:"updated" xorm:"'updated' NOT NULL DEFAULT 0 updated"`
Started int64 `json:"started" xorm:"started"`
Finished int64 `json:"finished" xorm:"finished"`
DeployTo string `json:"deploy_to" xorm:"deploy"`
DeployTask string `json:"deploy_task" xorm:"deploy_task"`
Commit string `json:"commit" xorm:"commit"`
Branch string `json:"branch" xorm:"branch"`
Ref string `json:"ref" xorm:"ref"`
Refspec string `json:"refspec" xorm:"refspec"`
Title string `json:"title" xorm:"title"`
Message string `json:"message" xorm:"TEXT 'message'"`
Timestamp int64 `json:"timestamp" xorm:"'timestamp'"`
Sender string `json:"sender" xorm:"sender"` // uses reported user for webhooks and name of cron for cron pipelines
Avatar string `json:"author_avatar" xorm:"varchar(500) avatar"`
Email string `json:"author_email" xorm:"varchar(500) email"`
ForgeURL string `json:"forge_url" xorm:"forge_url"`
Reviewer string `json:"reviewed_by" xorm:"reviewer"`
Reviewed int64 `json:"reviewed" xorm:"reviewed"`
Workflows []*Workflow `json:"workflows,omitempty" xorm:"-"`
ChangedFiles []string `json:"changed_files,omitempty" xorm:"LONGTEXT 'changed_files'"`
AdditionalVariables map[string]string `json:"variables,omitempty" xorm:"json 'additional_variables'"`
PullRequestLabels []string `json:"pr_labels,omitempty" xorm:"json 'pr_labels'"`
IsPrerelease bool `json:"is_prerelease,omitempty" xorm:"is_prerelease"`
FromFork bool `json:"from_fork,omitempty" xorm:"from_fork"`
ID int64 `json:"id" xorm:"pk autoincr 'id'"`
RepoID int64 `json:"-" xorm:"UNIQUE(s) INDEX 'repo_id'"`
Number int64 `json:"number" xorm:"UNIQUE(s) 'number'"`
Author string `json:"author" xorm:"INDEX 'author'"`
Parent int64 `json:"parent" xorm:"parent"`
Event WebhookEvent `json:"event" xorm:"event"`
Status StatusValue `json:"status" xorm:"INDEX 'status'"`
Errors []*types.PipelineError `json:"errors" xorm:"json 'errors'"`
Created int64 `json:"created" xorm:"'created' NOT NULL DEFAULT 0 created"`
Updated int64 `json:"updated" xorm:"'updated' NOT NULL DEFAULT 0 updated"`
Started int64 `json:"started" xorm:"started"`
Finished int64 `json:"finished" xorm:"finished"`
DeployTo string `json:"deploy_to" xorm:"deploy"`
DeployTask string `json:"deploy_task" xorm:"deploy_task"`
Commit string `json:"commit" xorm:"commit"`
Branch string `json:"branch" xorm:"branch"`
Ref string `json:"ref" xorm:"ref"`
Refspec string `json:"refspec" xorm:"refspec"`
Title string `json:"title" xorm:"title"`
Message string `json:"message" xorm:"TEXT 'message'"`
Timestamp int64 `json:"timestamp" xorm:"'timestamp'"`
Sender string `json:"sender" xorm:"sender"` // uses reported user for webhooks and name of cron for cron pipelines
Avatar string `json:"author_avatar" xorm:"varchar(500) avatar"`
Email string `json:"author_email" xorm:"varchar(500) email"`
ForgeURL string `json:"forge_url" xorm:"forge_url"`
Reviewer string `json:"reviewed_by" xorm:"reviewer"`
Reviewed int64 `json:"reviewed" xorm:"reviewed"`
Workflows []*Workflow `json:"workflows,omitempty" xorm:"-"`
ChangedFiles []string `json:"changed_files,omitempty" xorm:"LONGTEXT 'changed_files'"`
AdditionalVariables map[string]string `json:"variables,omitempty" xorm:"json 'additional_variables'"`
PullRequestLabels []string `json:"pr_labels,omitempty" xorm:"json 'pr_labels'"`
PullRequestMilestone string `json:"pr_milestone,omitempty" xorm:"pr_milestone"`
IsPrerelease bool `json:"is_prerelease,omitempty" xorm:"is_prerelease"`
FromFork bool `json:"from_fork,omitempty" xorm:"from_fork"`
} // @name Pipeline

// TableName return database table name for xorm.
Expand Down
7 changes: 4 additions & 3 deletions server/pipeline/stepbuilder/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,10 @@ func metadataPipelineFromModelPipeline(pipeline *model.Pipeline, includeParent b
Email: pipeline.Email,
Avatar: pipeline.Avatar,
},
ChangedFiles: pipeline.ChangedFiles,
PullRequestLabels: pipeline.PullRequestLabels,
IsPrerelease: pipeline.IsPrerelease,
ChangedFiles: pipeline.ChangedFiles,
PullRequestLabels: pipeline.PullRequestLabels,
PullRequestMilestone: pipeline.PullRequestMilestone,
IsPrerelease: pipeline.IsPrerelease,
},
Cron: cron,
Author: pipeline.Author,
Expand Down