diff --git a/cli/exec/flags.go b/cli/exec/flags.go index 0f7afb555..c9013ee67 100644 --- a/cli/exec/flags.go +++ b/cli/exec/flags.go @@ -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", diff --git a/cli/exec/metadata.go b/cli/exec/metadata.go index b757ae803..934a81d9d 100644 --- a/cli/exec/metadata.go +++ b/cli/exec/metadata.go @@ -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 diff --git a/cmd/server/openapi/docs.go b/cmd/server/openapi/docs.go index 746fe96c2..d68a3eb20 100644 --- a/cmd/server/openapi/docs.go +++ b/cmd/server/openapi/docs.go @@ -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" }, diff --git a/docs/docs/20-usage/50-environment.md b/docs/docs/20-usage/50-environment.md index 858d5dd2c..13552548b 100644 --- a/docs/docs/20-usage/50-environment.md +++ b/docs/docs/20-usage/50-environment.md @@ -48,96 +48,97 @@ 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 `/` | `john-doe/my-repo` | -| `CI_REPO_OWNER` | repository owner | `john-doe` | -| `CI_REPO_NAME` | repository name | `my-repo` | -| `CI_REPO_REMOTE_ID` | repository remote ID, is the UID it has in the forge | `82` | -| `CI_REPO_URL` | repository web URL | `https://git.example.com/john-doe/my-repo` | -| `CI_REPO_CLONE_URL` | repository clone URL | `https://git.example.com/john-doe/my-repo.git` | -| `CI_REPO_CLONE_SSH_URL` | repository SSH clone URL | `git@git.example.com:john-doe/my-repo.git` | -| `CI_REPO_DEFAULT_BRANCH` | repository default branch | `main` | -| `CI_REPO_PRIVATE` | repository is private | `true` | -| `CI_REPO_TRUSTED_NETWORK` | repository has trusted network access | `false` | -| `CI_REPO_TRUSTED_VOLUMES` | repository has trusted volumes access | `false` | -| `CI_REPO_TRUSTED_SECURITY` | repository has trusted security access | `false` | -| | **Current Commit** | | -| `CI_COMMIT_SHA` | commit SHA | `eba09b46064473a1d345da7abf28b477468e8dbd` | -| `CI_COMMIT_REF` | commit ref | `refs/heads/main` | -| `CI_COMMIT_REFSPEC` | commit ref spec | `issue-branch:main` | -| `CI_COMMIT_BRANCH` | commit branch (equals target branch for pull requests) | `main` | -| `CI_COMMIT_SOURCE_BRANCH` | commit source branch (set only for `pull_request` and `pull_request_closed` events) | `issue-branch` | -| `CI_COMMIT_TARGET_BRANCH` | commit target branch (set only for `pull_request` and `pull_request_closed` events) | `main` | -| `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_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` | -| `CI_COMMIT_PRERELEASE` | release is a pre-release (empty if event is not `release`) | `false` | -| | **Current pipeline** | | -| `CI_PIPELINE_NUMBER` | pipeline number | `8` | -| `CI_PIPELINE_PARENT` | number of parent pipeline | `0` | -| `CI_PIPELINE_EVENT` | pipeline event (see [`event`](../20-usage/20-workflow-syntax.md#event)) | `push`, `pull_request`, `pull_request_closed`, `tag`, `release`, `manual`, `cron` | -| `CI_PIPELINE_URL` | link to the web UI for the pipeline | `https://ci.example.com/repos/7/pipeline/8` | -| `CI_PIPELINE_FORGE_URL` | link to the forge's web UI for the commit(s) or tag that triggered the pipeline | `https://git.example.com/john-doe/my-repo/commit/eba09b46064473a1d345da7abf28b477468e8dbd` | -| `CI_PIPELINE_DEPLOY_TARGET` | pipeline deploy target for `deployment` events | `production` | -| `CI_PIPELINE_DEPLOY_TASK` | pipeline deploy task for `deployment` events | `migration` | -| `CI_PIPELINE_CREATED` | pipeline created UNIX timestamp | `1722617519` | -| `CI_PIPELINE_STARTED` | pipeline started UNIX timestamp | `1722617519` | -| `CI_PIPELINE_FILES` | changed files (empty if event is not `push` or `pull_request`), it is undefined if more than 500 files are touched | `[]`, `[".woodpecker.yml","README.md"]` | -| `CI_PIPELINE_AUTHOR` | pipeline author username | `octocat` | -| `CI_PIPELINE_AVATAR` | pipeline author avatar | `https://git.example.com/avatars/5dcbcadbce6f87f8abef` | -| | **Current workflow** | | -| `CI_WORKFLOW_NAME` | workflow name | `release` | -| | **Current step** | | -| `CI_STEP_NAME` | step name | `build package` | -| `CI_STEP_NUMBER` | step number | `0` | -| `CI_STEP_STARTED` | step started UNIX timestamp | `1722617519` | -| `CI_STEP_URL` | URL to step in UI | `https://ci.example.com/repos/7/pipeline/8` | -| | **Previous commit** | | -| `CI_PREV_COMMIT_SHA` | previous commit SHA | `15784117e4e103f36cba75a9e29da48046eb82c4` | -| `CI_PREV_COMMIT_REF` | previous commit ref | `refs/heads/main` | -| `CI_PREV_COMMIT_REFSPEC` | previous commit ref spec | `issue-branch:main` | -| `CI_PREV_COMMIT_BRANCH` | previous commit branch | `main` | -| `CI_PREV_COMMIT_SOURCE_BRANCH` | previous commit source branch (set only for `pull_request` and `pull_request_closed` events) | `issue-branch` | -| `CI_PREV_COMMIT_TARGET_BRANCH` | previous commit target branch (set only for `pull_request` and `pull_request_closed` events) | `main` | -| `CI_PREV_COMMIT_URL` | previous commit link in forge | `https://git.example.com/john-doe/my-repo/commit/15784117e4e103f36cba75a9e29da48046eb82c4` | -| `CI_PREV_COMMIT_MESSAGE` | previous commit message | `test` | -| `CI_PREV_COMMIT_AUTHOR` | previous commit author username | `john-doe` | -| `CI_PREV_COMMIT_AUTHOR_EMAIL` | previous commit author email address | `john-doe@example.com` | -| | **Previous pipeline** | | -| `CI_PREV_PIPELINE_NUMBER` | previous pipeline number | `7` | -| `CI_PREV_PIPELINE_PARENT` | previous pipeline number of parent pipeline | `0` | -| `CI_PREV_PIPELINE_EVENT` | previous pipeline event (see [`event`](../20-usage/20-workflow-syntax.md#event)) | `push`, `pull_request`, `pull_request_closed`, `tag`, `release`, `manual`, `cron` | -| `CI_PREV_PIPELINE_URL` | previous pipeline link in CI | `https://ci.example.com/repos/7/pipeline/7` | -| `CI_PREV_PIPELINE_FORGE_URL` | previous pipeline link to event in forge | `https://git.example.com/john-doe/my-repo/commit/15784117e4e103f36cba75a9e29da48046eb82c4` | -| `CI_PREV_PIPELINE_DEPLOY_TARGET` | previous pipeline deploy target for `deployment` events | `production` | -| `CI_PREV_PIPELINE_DEPLOY_TASK` | previous pipeline deploy task for `deployment` events | `migration` | -| `CI_PREV_PIPELINE_STATUS` | previous pipeline status | `success`, `failure` | -| `CI_PREV_PIPELINE_CREATED` | previous pipeline created UNIX timestamp | `1722610173` | -| `CI_PREV_PIPELINE_STARTED` | previous pipeline started UNIX timestamp | `1722610173` | -| `CI_PREV_PIPELINE_FINISHED` | previous pipeline finished UNIX timestamp | `1722610383` | -| `CI_PREV_PIPELINE_AUTHOR` | previous pipeline author username | `octocat` | -| `CI_PREV_PIPELINE_AVATAR` | previous pipeline author avatar | `https://git.example.com/avatars/5dcbcadbce6f87f8abef` | -| |   | | -| `CI_WORKSPACE` | Path of the workspace where source code gets cloned to | `/woodpecker/src/git.example.com/john-doe/my-repo` | -| | **System** | | -| `CI_SYSTEM_NAME` | name of the CI system | `woodpecker` | -| `CI_SYSTEM_URL` | link to CI system | `https://ci.example.com` | -| `CI_SYSTEM_HOST` | hostname of CI server | `ci.example.com` | -| `CI_SYSTEM_VERSION` | version of the server | `2.7.0` | -| | **Forge** | | -| `CI_FORGE_TYPE` | name of forge | `bitbucket` , `bitbucket_dc` , `forgejo` , `gitea` , `github` , `gitlab` | -| `CI_FORGE_URL` | root URL of configured forge | `https://git.example.com` | -| | **Internal** - Please don't use! | | -| `CI_SCRIPT` | Internal script path. Used to call pipeline step commands. | | -| `CI_NETRC_USERNAME` | Credentials for private repos to be able to clone data. (Only available for specific images) | | -| `CI_NETRC_PASSWORD` | Credentials for private repos to be able to clone data. (Only available for specific images) | | -| `CI_NETRC_MACHINE` | Credentials for private repos to be able to clone data. (Only available for specific images) | | +| NAME | Description | Example | +| ---------------------------------- | ------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------ | +| `CI` | CI environment name | `woodpecker` | +| | **Repository** | | +| `CI_REPO` | repository full name `/` | `john-doe/my-repo` | +| `CI_REPO_OWNER` | repository owner | `john-doe` | +| `CI_REPO_NAME` | repository name | `my-repo` | +| `CI_REPO_REMOTE_ID` | repository remote ID, is the UID it has in the forge | `82` | +| `CI_REPO_URL` | repository web URL | `https://git.example.com/john-doe/my-repo` | +| `CI_REPO_CLONE_URL` | repository clone URL | `https://git.example.com/john-doe/my-repo.git` | +| `CI_REPO_CLONE_SSH_URL` | repository SSH clone URL | `git@git.example.com:john-doe/my-repo.git` | +| `CI_REPO_DEFAULT_BRANCH` | repository default branch | `main` | +| `CI_REPO_PRIVATE` | repository is private | `true` | +| `CI_REPO_TRUSTED_NETWORK` | repository has trusted network access | `false` | +| `CI_REPO_TRUSTED_VOLUMES` | repository has trusted volumes access | `false` | +| `CI_REPO_TRUSTED_SECURITY` | repository has trusted security access | `false` | +| | **Current Commit** | | +| `CI_COMMIT_SHA` | commit SHA | `eba09b46064473a1d345da7abf28b477468e8dbd` | +| `CI_COMMIT_REF` | commit ref | `refs/heads/main` | +| `CI_COMMIT_REFSPEC` | commit ref spec | `issue-branch:main` | +| `CI_COMMIT_BRANCH` | commit branch (equals target branch for pull requests) | `main` | +| `CI_COMMIT_SOURCE_BRANCH` | commit source branch (set only for `pull_request` and `pull_request_closed` events) | `issue-branch` | +| `CI_COMMIT_TARGET_BRANCH` | commit target branch (set only for `pull_request` and `pull_request_closed` events) | `main` | +| `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` | +| `CI_COMMIT_PRERELEASE` | release is a pre-release (empty if event is not `release`) | `false` | +| | **Current pipeline** | | +| `CI_PIPELINE_NUMBER` | pipeline number | `8` | +| `CI_PIPELINE_PARENT` | number of parent pipeline | `0` | +| `CI_PIPELINE_EVENT` | pipeline event (see [`event`](../20-usage/20-workflow-syntax.md#event)) | `push`, `pull_request`, `pull_request_closed`, `tag`, `release`, `manual`, `cron` | +| `CI_PIPELINE_URL` | link to the web UI for the pipeline | `https://ci.example.com/repos/7/pipeline/8` | +| `CI_PIPELINE_FORGE_URL` | link to the forge's web UI for the commit(s) or tag that triggered the pipeline | `https://git.example.com/john-doe/my-repo/commit/eba09b46064473a1d345da7abf28b477468e8dbd` | +| `CI_PIPELINE_DEPLOY_TARGET` | pipeline deploy target for `deployment` events | `production` | +| `CI_PIPELINE_DEPLOY_TASK` | pipeline deploy task for `deployment` events | `migration` | +| `CI_PIPELINE_CREATED` | pipeline created UNIX timestamp | `1722617519` | +| `CI_PIPELINE_STARTED` | pipeline started UNIX timestamp | `1722617519` | +| `CI_PIPELINE_FILES` | changed files (empty if event is not `push` or `pull_request`), it is undefined if more than 500 files are touched | `[]`, `[".woodpecker.yml","README.md"]` | +| `CI_PIPELINE_AUTHOR` | pipeline author username | `octocat` | +| `CI_PIPELINE_AVATAR` | pipeline author avatar | `https://git.example.com/avatars/5dcbcadbce6f87f8abef` | +| | **Current workflow** | | +| `CI_WORKFLOW_NAME` | workflow name | `release` | +| | **Current step** | | +| `CI_STEP_NAME` | step name | `build package` | +| `CI_STEP_NUMBER` | step number | `0` | +| `CI_STEP_STARTED` | step started UNIX timestamp | `1722617519` | +| `CI_STEP_URL` | URL to step in UI | `https://ci.example.com/repos/7/pipeline/8` | +| | **Previous commit** | | +| `CI_PREV_COMMIT_SHA` | previous commit SHA | `15784117e4e103f36cba75a9e29da48046eb82c4` | +| `CI_PREV_COMMIT_REF` | previous commit ref | `refs/heads/main` | +| `CI_PREV_COMMIT_REFSPEC` | previous commit ref spec | `issue-branch:main` | +| `CI_PREV_COMMIT_BRANCH` | previous commit branch | `main` | +| `CI_PREV_COMMIT_SOURCE_BRANCH` | previous commit source branch (set only for `pull_request` and `pull_request_closed` events) | `issue-branch` | +| `CI_PREV_COMMIT_TARGET_BRANCH` | previous commit target branch (set only for `pull_request` and `pull_request_closed` events) | `main` | +| `CI_PREV_COMMIT_URL` | previous commit link in forge | `https://git.example.com/john-doe/my-repo/commit/15784117e4e103f36cba75a9e29da48046eb82c4` | +| `CI_PREV_COMMIT_MESSAGE` | previous commit message | `test` | +| `CI_PREV_COMMIT_AUTHOR` | previous commit author username | `john-doe` | +| `CI_PREV_COMMIT_AUTHOR_EMAIL` | previous commit author email address | `john-doe@example.com` | +| | **Previous pipeline** | | +| `CI_PREV_PIPELINE_NUMBER` | previous pipeline number | `7` | +| `CI_PREV_PIPELINE_PARENT` | previous pipeline number of parent pipeline | `0` | +| `CI_PREV_PIPELINE_EVENT` | previous pipeline event (see [`event`](../20-usage/20-workflow-syntax.md#event)) | `push`, `pull_request`, `pull_request_closed`, `tag`, `release`, `manual`, `cron` | +| `CI_PREV_PIPELINE_URL` | previous pipeline link in CI | `https://ci.example.com/repos/7/pipeline/7` | +| `CI_PREV_PIPELINE_FORGE_URL` | previous pipeline link to event in forge | `https://git.example.com/john-doe/my-repo/commit/15784117e4e103f36cba75a9e29da48046eb82c4` | +| `CI_PREV_PIPELINE_DEPLOY_TARGET` | previous pipeline deploy target for `deployment` events | `production` | +| `CI_PREV_PIPELINE_DEPLOY_TASK` | previous pipeline deploy task for `deployment` events | `migration` | +| `CI_PREV_PIPELINE_STATUS` | previous pipeline status | `success`, `failure` | +| `CI_PREV_PIPELINE_CREATED` | previous pipeline created UNIX timestamp | `1722610173` | +| `CI_PREV_PIPELINE_STARTED` | previous pipeline started UNIX timestamp | `1722610173` | +| `CI_PREV_PIPELINE_FINISHED` | previous pipeline finished UNIX timestamp | `1722610383` | +| `CI_PREV_PIPELINE_AUTHOR` | previous pipeline author username | `octocat` | +| `CI_PREV_PIPELINE_AVATAR` | previous pipeline author avatar | `https://git.example.com/avatars/5dcbcadbce6f87f8abef` | +| |   | | +| `CI_WORKSPACE` | Path of the workspace where source code gets cloned to | `/woodpecker/src/git.example.com/john-doe/my-repo` | +| | **System** | | +| `CI_SYSTEM_NAME` | name of the CI system | `woodpecker` | +| `CI_SYSTEM_URL` | link to CI system | `https://ci.example.com` | +| `CI_SYSTEM_HOST` | hostname of CI server | `ci.example.com` | +| `CI_SYSTEM_VERSION` | version of the server | `2.7.0` | +| | **Forge** | | +| `CI_FORGE_TYPE` | name of forge | `bitbucket` , `bitbucket_dc` , `forgejo` , `gitea` , `github` , `gitlab` | +| `CI_FORGE_URL` | root URL of configured forge | `https://git.example.com` | +| | **Internal** - Please don't use! | | +| `CI_SCRIPT` | Internal script path. Used to call pipeline step commands. | | +| `CI_NETRC_USERNAME` | Credentials for private repos to be able to clone data. (Only available for specific images) | | +| `CI_NETRC_PASSWORD` | Credentials for private repos to be able to clone data. (Only available for specific images) | | +| `CI_NETRC_MACHINE` | Credentials for private repos to be able to clone data. (Only available for specific images) | | ## Global environment variables diff --git a/pipeline/frontend/metadata/environment.go b/pipeline/frontend/metadata/environment.go index 13dfdf76b..2c595d26f 100644 --- a/pipeline/frontend/metadata/environment.go +++ b/pipeline/frontend/metadata/environment.go @@ -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 diff --git a/pipeline/frontend/metadata/types.go b/pipeline/frontend/metadata/types.go index 9a617c814..ca08e5591 100644 --- a/pipeline/frontend/metadata/types.go +++ b/pipeline/frontend/metadata/types.go @@ -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. diff --git a/server/forge/forgejo/helper.go b/server/forge/forgejo/helper.go index 9e7f243cd..7b20e35d8 100644 --- a/server/forge/forgejo/helper.go +++ b/server/forge/forgejo/helper.go @@ -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, diff --git a/server/forge/gitea/helper.go b/server/forge/gitea/helper.go index 9420d5fff..5e310da8a 100644 --- a/server/forge/gitea/helper.go +++ b/server/forge/gitea/helper.go @@ -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, diff --git a/server/forge/github/parse.go b/server/forge/github/parse.go index 67835b9f3..8d5b14fe8 100644 --- a/server/forge/github/parse.go +++ b/server/forge/github/parse.go @@ -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(), + FromFork: fromFork, } if merge { pipeline.Ref = fmt.Sprintf(mergeRefs, hook.GetPullRequest().GetNumber()) diff --git a/server/forge/gitlab/convert.go b/server/forge/gitlab/convert.go index fc1af6929..d72626392 100644 --- a/server/forge/gitlab/convert.go +++ b/server/forge/gitlab/convert.go @@ -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) { diff --git a/server/forge/gitlab/gitlab.go b/server/forge/gitlab/gitlab.go index 2ca7599be..cf46d32cd 100644 --- a/server/forge/gitlab/gitlab.go +++ b/server/forge/gitlab/gitlab.go @@ -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 } diff --git a/server/model/pipeline.go b/server/model/pipeline.go index 8d6a472d0..c4a9d1804 100644 --- a/server/model/pipeline.go +++ b/server/model/pipeline.go @@ -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. diff --git a/server/pipeline/stepbuilder/metadata.go b/server/pipeline/stepbuilder/metadata.go index badb0eb24..db549678e 100644 --- a/server/pipeline/stepbuilder/metadata.go +++ b/server/pipeline/stepbuilder/metadata.go @@ -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,