Add milestone to metadata (#5174)

This commit is contained in:
6543
2025-09-01 16:12:54 +02:00
committed by GitHub
parent 3366c4c118
commit c5a5bbea5d
13 changed files with 195 additions and 153 deletions

View File

@@ -290,6 +290,11 @@ var flags = []cli.Flag{
Name: "commit-pull-labels", Name: "commit-pull-labels",
Usage: "Set the metadata environment variable \"CI_COMMIT_PULL_REQUEST_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{ &cli.BoolFlag{
Sources: cli.EnvVars("CI_COMMIT_PRERELEASE"), Sources: cli.EnvVars("CI_COMMIT_PRERELEASE"),
Name: "commit-release-is-pre", Name: "commit-release-is-pre",

View File

@@ -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-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-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) metadataFileAndOverrideOrDefault(c, "commit-release-is-pre", func(b bool) { m.Curr.Commit.IsPrerelease = b }, c.Bool)
// Previous Pipeline // Previous Pipeline

View File

@@ -4946,6 +4946,9 @@ const docTemplate = `{
"type": "string" "type": "string"
} }
}, },
"pr_milestone": {
"type": "string"
},
"ref": { "ref": {
"type": "string" "type": "string"
}, },
@@ -5554,6 +5557,9 @@ const docTemplate = `{
"message": { "message": {
"type": "string" "type": "string"
}, },
"milestone": {
"type": "string"
},
"ref": { "ref": {
"type": "string" "type": "string"
}, },

View File

@@ -49,7 +49,7 @@ Please note that the environment section is not able to expand environment varia
This is the reference list of all environment variables available to your pipeline containers. These are injected into your pipeline step and plugins containers, at runtime. This is the reference list of all environment variables available to your pipeline containers. These are injected into your pipeline step and plugins containers, at runtime.
| NAME | Description | Example | | NAME | Description | Example |
| -------------------------------- | ------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------ | | ---------------------------------- | ------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------ |
| `CI` | CI environment name | `woodpecker` | | `CI` | CI environment name | `woodpecker` |
| | **Repository** | | | | **Repository** | |
| `CI_REPO` | repository full name `<owner>/<name>` | `john-doe/my-repo` | | `CI_REPO` | repository full name `<owner>/<name>` | `john-doe/my-repo` |
@@ -74,6 +74,7 @@ This is the reference list of all environment variables available to your pipeli
| `CI_COMMIT_TAG` | commit tag name (empty if event is not `tag`) | `v1.10.3` | | `CI_COMMIT_TAG` | commit tag name (empty if event is not `tag`) | `v1.10.3` |
| `CI_COMMIT_PULL_REQUEST` | commit pull request number (set only for `pull_request` and `pull_request_closed` events) | `1` | | `CI_COMMIT_PULL_REQUEST` | commit pull request number (set only for `pull_request` and `pull_request_closed` events) | `1` |
| `CI_COMMIT_PULL_REQUEST_LABELS` | labels assigned to pull request (set only for `pull_request` and `pull_request_closed` events) | `server` | | `CI_COMMIT_PULL_REQUEST_LABELS` | labels assigned to pull request (set only for `pull_request` and `pull_request_closed` events) | `server` |
| `CI_COMMIT_PULL_REQUEST_MILESTONE` | milestone assigned to pull request (set only for `pull_request` and `pull_request_closed` events) | `summer-sprint` |
| `CI_COMMIT_MESSAGE` | commit message | `Initial commit` | | `CI_COMMIT_MESSAGE` | commit message | `Initial commit` |
| `CI_COMMIT_AUTHOR` | commit author username | `john-doe` | | `CI_COMMIT_AUTHOR` | commit author username | `john-doe` |
| `CI_COMMIT_AUTHOR_EMAIL` | commit author email address | `john-doe@example.com` | | `CI_COMMIT_AUTHOR_EMAIL` | commit author email address | `john-doe@example.com` |

View File

@@ -108,6 +108,7 @@ func (m *Metadata) Environ() map[string]string {
setNonEmptyEnvVar(params, "CI_COMMIT_TARGET_BRANCH", targetBranch) setNonEmptyEnvVar(params, "CI_COMMIT_TARGET_BRANCH", targetBranch)
setNonEmptyEnvVar(params, "CI_COMMIT_PULL_REQUEST", pullRegexp.FindString(pipeline.Commit.Ref)) 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_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 // Only export changed files if maxChangedFiles is not exceeded

View File

@@ -69,6 +69,7 @@ type (
Author Author `json:"author,omitempty"` Author Author `json:"author,omitempty"`
ChangedFiles []string `json:"changed_files,omitempty"` ChangedFiles []string `json:"changed_files,omitempty"`
PullRequestLabels []string `json:"labels,omitempty"` PullRequestLabels []string `json:"labels,omitempty"`
PullRequestMilestone string `json:"milestone,omitempty"`
IsPrerelease bool `json:"is_prerelease,omitempty"` IsPrerelease bool `json:"is_prerelease,omitempty"`
} }

View File

@@ -170,12 +170,20 @@ func pipelineFromPullRequest(hook *pullRequestHook) *model.Pipeline {
hook.PullRequest.Base.Ref, hook.PullRequest.Base.Ref,
), ),
PullRequestLabels: convertLabels(hook.PullRequest.Labels), PullRequestLabels: convertLabels(hook.PullRequest.Labels),
PullRequestMilestone: convertMilestone(hook.PullRequest.Milestone),
FromFork: hook.PullRequest.Head.RepoID != hook.PullRequest.Base.RepoID, FromFork: hook.PullRequest.Head.RepoID != hook.PullRequest.Base.RepoID,
} }
return pipeline return pipeline
} }
func convertMilestone(milestone *forgejo.Milestone) string {
if milestone == nil || milestone.ID == 0 {
return ""
}
return milestone.Title
}
func pipelineFromRelease(hook *releaseHook) *model.Pipeline { func pipelineFromRelease(hook *releaseHook) *model.Pipeline {
avatar := expandAvatar( avatar := expandAvatar(
hook.Repo.HTMLURL, hook.Repo.HTMLURL,

View File

@@ -171,12 +171,20 @@ func pipelineFromPullRequest(hook *pullRequestHook) *model.Pipeline {
hook.PullRequest.Base.Ref, hook.PullRequest.Base.Ref,
), ),
PullRequestLabels: convertLabels(hook.PullRequest.Labels), PullRequestLabels: convertLabels(hook.PullRequest.Labels),
PullRequestMilestone: convertMilestone(hook.PullRequest.Milestone),
FromFork: hook.PullRequest.Head.RepoID != hook.PullRequest.Base.RepoID, FromFork: hook.PullRequest.Head.RepoID != hook.PullRequest.Base.RepoID,
} }
return pipeline return pipeline
} }
func convertMilestone(milestone *gitea.Milestone) string {
if milestone == nil || milestone.ID == 0 {
return ""
}
return milestone.Title
}
func pipelineFromRelease(hook *releaseHook) *model.Pipeline { func pipelineFromRelease(hook *releaseHook) *model.Pipeline {
avatar := expandAvatar( avatar := expandAvatar(
hook.Repo.HTMLURL, hook.Repo.HTMLURL,

View File

@@ -179,6 +179,7 @@ func parsePullHook(hook *github.PullRequestEvent, merge bool) (*github.PullReque
hook.GetPullRequest().GetBase().GetRef(), hook.GetPullRequest().GetBase().GetRef(),
), ),
PullRequestLabels: convertLabels(hook.GetPullRequest().Labels), PullRequestLabels: convertLabels(hook.GetPullRequest().Labels),
PullRequestMilestone: hook.GetPullRequest().GetMilestone().GetTitle(),
FromFork: fromFork, FromFork: fromFork,
} }
if merge { if merge {

View File

@@ -63,9 +63,9 @@ func (g *GitLab) convertGitLabRepo(_repo *gitlab.Project, projectMember *gitlab.
return repo, nil return repo, nil
} }
func convertMergeRequestHook(hook *gitlab.MergeEvent, req *http.Request) (int, *model.Repo, *model.Pipeline, error) { func convertMergeRequestHook(hook *gitlab.MergeEvent, req *http.Request) (mergeID, milestoneID int, repo *model.Repo, pipeline *model.Pipeline, err error) {
repo := &model.Repo{} repo = &model.Repo{}
pipeline := &model.Pipeline{} pipeline = &model.Pipeline{}
target := hook.ObjectAttributes.Target target := hook.ObjectAttributes.Target
source := hook.ObjectAttributes.Source source := hook.ObjectAttributes.Source
@@ -73,17 +73,17 @@ func convertMergeRequestHook(hook *gitlab.MergeEvent, req *http.Request) (int, *
switch { switch {
case target == nil && source == nil: 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: 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: 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 != "" { if target.PathWithNamespace != "" {
var err error var err error
if repo.Owner, repo.Name, err = extractFromPath(target.PathWithNamespace); err != nil { 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 repo.FullName = target.PathWithNamespace
} else { } else {
@@ -140,7 +140,7 @@ func convertMergeRequestHook(hook *gitlab.MergeEvent, req *http.Request) (int, *
pipeline.PullRequestLabels = convertLabels(hook.Labels) pipeline.PullRequestLabels = convertLabels(hook.Labels)
pipeline.FromFork = target.PathWithNamespace != source.PathWithNamespace 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) { func convertPushHook(hook *gitlab.PushEvent) (*model.Repo, *model.Pipeline, error) {

View File

@@ -650,12 +650,12 @@ func (g *GitLab) Hook(ctx context.Context, req *http.Request) (*model.Repo, *mod
event.ObjectAttributes.Action != "reopen" { event.ObjectAttributes.Action != "reopen" {
return nil, nil, &forge_types.ErrIgnoreEvent{Event: string(eventType), Reason: "no code changes"} 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 { if err != nil {
return nil, nil, err 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 return nil, nil, err
} }
@@ -780,7 +780,7 @@ func (g *GitLab) Org(ctx context.Context, u *model.User, owner string) (*model.O
}, nil }, 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) _store, ok := store.TryFromContext(ctx)
if !ok { if !ok {
log.Error().Msg("could not get store from context") log.Error().Msg("could not get store from context")
@@ -807,7 +807,7 @@ func (g *GitLab) loadChangedFilesFromMergeRequest(ctx context.Context, tmpRepo *
return nil, err 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 { if err != nil {
return nil, err return nil, err
} }
@@ -818,5 +818,13 @@ func (g *GitLab) loadChangedFilesFromMergeRequest(ctx context.Context, tmpRepo *
} }
pipeline.ChangedFiles = utils.DeduplicateStrings(files) 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 return pipeline, nil
} }

View File

@@ -51,6 +51,7 @@ type Pipeline struct {
ChangedFiles []string `json:"changed_files,omitempty" xorm:"LONGTEXT 'changed_files'"` ChangedFiles []string `json:"changed_files,omitempty" xorm:"LONGTEXT 'changed_files'"`
AdditionalVariables map[string]string `json:"variables,omitempty" xorm:"json 'additional_variables'"` AdditionalVariables map[string]string `json:"variables,omitempty" xorm:"json 'additional_variables'"`
PullRequestLabels []string `json:"pr_labels,omitempty" xorm:"json 'pr_labels'"` 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"` IsPrerelease bool `json:"is_prerelease,omitempty" xorm:"is_prerelease"`
FromFork bool `json:"from_fork,omitempty" xorm:"from_fork"` FromFork bool `json:"from_fork,omitempty" xorm:"from_fork"`
} // @name Pipeline } // @name Pipeline

View File

@@ -134,6 +134,7 @@ func metadataPipelineFromModelPipeline(pipeline *model.Pipeline, includeParent b
}, },
ChangedFiles: pipeline.ChangedFiles, ChangedFiles: pipeline.ChangedFiles,
PullRequestLabels: pipeline.PullRequestLabels, PullRequestLabels: pipeline.PullRequestLabels,
PullRequestMilestone: pipeline.PullRequestMilestone,
IsPrerelease: pipeline.IsPrerelease, IsPrerelease: pipeline.IsPrerelease,
}, },
Cron: cron, Cron: cron,