Add pull request labels as environment variable (#1321)

Closes #1308 

Co-authored-by: Anbraten <anton@ju60.de>
This commit is contained in:
Lukas
2023-03-17 03:43:04 +01:00
committed by GitHub
parent 7e3bf2202c
commit 1b43b0bf20
9 changed files with 64 additions and 22 deletions

View File

@@ -475,6 +475,13 @@ when:
- evaluate: 'not (CI_COMMIT_MESSAGE contains "please ignore me")' - evaluate: 'not (CI_COMMIT_MESSAGE contains "please ignore me")'
``` ```
Run on pull requests with the label `deploy`:
```yaml
when:
- evaluate: 'CI_COMMIT_PULL_REQUEST_LABELS contains "deploy"'
```
### `group` - Parallel execution ### `group` - Parallel execution
Woodpecker supports parallel step execution for same-machine fan-in and fan-out. Parallel steps are configured using the `group` attribute. This instructs the pipeline runner to execute the named group in parallel. Woodpecker supports parallel step execution for same-machine fan-in and fan-out. Parallel steps are configured using the `group` attribute. This instructs the pipeline runner to execute the named group in parallel.

View File

@@ -68,6 +68,7 @@ This is the reference list of all environment variables available to your pipeli
| `CI_COMMIT_TARGET_BRANCH` | commit target branch | | `CI_COMMIT_TARGET_BRANCH` | commit target branch |
| `CI_COMMIT_TAG` | commit tag name (empty if event is not `tag`) | | `CI_COMMIT_TAG` | commit tag name (empty if event is not `tag`) |
| `CI_COMMIT_PULL_REQUEST` | commit pull request number (empty if event is not `pull_request`) | | `CI_COMMIT_PULL_REQUEST` | commit pull request number (empty if event is not `pull_request`) |
| `CI_COMMIT_PULL_REQUEST_LABELS` | labels assigned to pull request (empty if event is not `pull_request`) |
| `CI_COMMIT_LINK` | commit link in forge | | `CI_COMMIT_LINK` | commit link in forge |
| `CI_COMMIT_MESSAGE` | commit message | | `CI_COMMIT_MESSAGE` | commit message |
| `CI_COMMIT_AUTHOR` | commit author username | | `CI_COMMIT_AUTHOR` | commit author username |

View File

@@ -86,6 +86,7 @@ type (
Message string `json:"message,omitempty"` Message string `json:"message,omitempty"`
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"`
} }
// Author defines runtime metadata for a commit author. // Author defines runtime metadata for a commit author.
@@ -167,6 +168,7 @@ func (m *Metadata) Environ() map[string]string {
"CI_COMMIT_AUTHOR_AVATAR": m.Curr.Commit.Author.Avatar, "CI_COMMIT_AUTHOR_AVATAR": m.Curr.Commit.Author.Avatar,
"CI_COMMIT_TAG": "", // will be set if event is tag "CI_COMMIT_TAG": "", // will be set if event is tag
"CI_COMMIT_PULL_REQUEST": "", // will be set if event is pr "CI_COMMIT_PULL_REQUEST": "", // will be set if event is pr
"CI_COMMIT_PULL_REQUEST_LABELS": "", // will be set if event is pr
"CI_PIPELINE_NUMBER": strconv.FormatInt(m.Curr.Number, 10), "CI_PIPELINE_NUMBER": strconv.FormatInt(m.Curr.Number, 10),
"CI_PIPELINE_PARENT": strconv.FormatInt(m.Curr.Parent, 10), "CI_PIPELINE_PARENT": strconv.FormatInt(m.Curr.Parent, 10),
@@ -244,6 +246,7 @@ func (m *Metadata) Environ() map[string]string {
} }
if m.Curr.Event == EventPull { if m.Curr.Event == EventPull {
params["CI_COMMIT_PULL_REQUEST"] = pullRegexp.FindString(m.Curr.Commit.Ref) params["CI_COMMIT_PULL_REQUEST"] = pullRegexp.FindString(m.Curr.Commit.Ref)
params["CI_COMMIT_PULL_REQUEST_LABELS"] = strings.Join(m.Curr.Commit.PullRequestLabels, ",")
} }
return params return params

View File

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

View File

@@ -160,6 +160,7 @@ func pipelineFromPullRequest(hook *pullRequestHook) *model.Pipeline {
hook.PullRequest.Head.Ref, hook.PullRequest.Head.Ref,
hook.PullRequest.Base.Ref, hook.PullRequest.Base.Ref,
), ),
PullRequestLabels: convertLabels(hook.PullRequest.Labels),
} }
return pipeline return pipeline
} }
@@ -229,3 +230,11 @@ func matchingHooks(hooks []*gitea.Hook, rawurl string) *gitea.Hook {
} }
return nil return nil
} }
func convertLabels(from []*gitea.Label) []string {
labels := make([]string, len(from))
for i, label := range from {
labels[i] = label.Name
}
return labels
}

View File

@@ -164,3 +164,13 @@ func convertRepoHook(eventRepo *github.PushEventRepository) *model.Repo {
} }
return repo return repo
} }
// covertLabels is a helper function used to convert a GitHub label list to
// the common Woodpecker label structure.
func convertLabels(from []*github.Label) []string {
labels := make([]string, len(from))
for i, label := range from {
labels[i] = *label.Name
}
return labels
}

View File

@@ -170,6 +170,7 @@ func parsePullHook(hook *github.PullRequestEvent, merge bool) (*github.PullReque
hook.GetPullRequest().GetHead().GetRef(), hook.GetPullRequest().GetHead().GetRef(),
hook.GetPullRequest().GetBase().GetRef(), hook.GetPullRequest().GetBase().GetRef(),
), ),
PullRequestLabels: convertLabels(hook.GetPullRequest().Labels),
} }
if merge { if merge {
pipeline.Ref = fmt.Sprintf(mergeRefs, hook.GetPullRequest().GetNumber()) pipeline.Ref = fmt.Sprintf(mergeRefs, hook.GetPullRequest().GetNumber())

View File

@@ -129,6 +129,7 @@ func convertMergeRequestHook(hook *gitlab.MergeEvent, req *http.Request) (int, *
pipeline.Title = obj.Title pipeline.Title = obj.Title
pipeline.Link = obj.URL pipeline.Link = obj.URL
pipeline.PullRequestLabels = convertLabels(hook.Labels)
return obj.IID, repo, pipeline, nil return obj.IID, repo, pipeline, nil
} }
@@ -250,3 +251,11 @@ func extractFromPath(str string) (string, string, error) {
} }
return s[0], s[1], nil return s[0], s[1], nil
} }
func convertLabels(from []*gitlab.Label) []string {
labels := make([]string, len(from))
for i, label := range from {
labels[i] = label.Name
}
return labels
}

View File

@@ -52,6 +52,7 @@ type Pipeline struct {
Files []*File `json:"files,omitempty" xorm:"-"` Files []*File `json:"files,omitempty" xorm:"-"`
ChangedFiles []string `json:"changed_files,omitempty" xorm:"json 'changed_files'"` ChangedFiles []string `json:"changed_files,omitempty" xorm:"json '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'"`
} }
// TableName return database table name for xorm // TableName return database table name for xorm