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",
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",

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-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

View File

@@ -4946,6 +4946,9 @@ const docTemplate = `{
"type": "string"
}
},
"pr_milestone": {
"type": "string"
},
"ref": {
"type": "string"
},
@@ -5554,6 +5557,9 @@ const docTemplate = `{
"message": {
"type": "string"
},
"milestone": {
"type": "string"
},
"ref": {
"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.
| NAME | Description | Example |
| -------------------------------- | ------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------ |
| ---------------------------------- | ------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------ |
| `CI` | CI environment name | `woodpecker` |
| | **Repository** | |
| `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_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_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_AUTHOR` | commit author username | `john-doe` |
| `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_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

View File

@@ -69,6 +69,7 @@ type (
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"`
}

View File

@@ -170,12 +170,20 @@ func pipelineFromPullRequest(hook *pullRequestHook) *model.Pipeline {
hook.PullRequest.Base.Ref,
),
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,

View File

@@ -171,12 +171,20 @@ func pipelineFromPullRequest(hook *pullRequestHook) *model.Pipeline {
hook.PullRequest.Base.Ref,
),
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,

View File

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

View File

@@ -63,9 +63,9 @@ 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
@@ -73,17 +73,17 @@ func convertMergeRequestHook(hook *gitlab.MergeEvent, req *http.Request) (int, *
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 {
@@ -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) {

View File

@@ -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
}
@@ -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")
@@ -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
}
@@ -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
}

View File

@@ -51,6 +51,7 @@ type Pipeline struct {
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

View File

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