mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-26 06:16:56 +09:00
This PR introduces project workflows, allowing users to configure event-based automations directly from the UI. Users can define filters and actions for each workflow event, and duplicate existing events when needed. ### Concepts Every workflow is composed of `event`, `filters`, and `actions`. - **Event** - An Event defines when a workflow should be triggered. It represents specific activities such as an issue being added to the project, a pull request being merged, or a project column being changed. - **Filter** - A Filter refines when an event should take effect. It allows users to specify conditions like issue type, labels, or project columns to ensure actions only run in targeted scenarios. - **Action** - An Action defines what happens when the event and filters match. It performs operations such as adding labels, moving issues between columns, or changing the issue or pull request state. ### Available Events Currently, the following eight events are supported: - On issues/pull requests closed - On issues/pull requests reopened - On issues/pull requests was added to the project, including when created in it - On issues/pull requests was removed from the project - On issues/pull requests' column was changed - On code changes requested - On code review approved - On pull request merged Each event type supports its own set of filters and actions. ### Available Filters There are four types of filters that can be configured for an event: - **Apply to** - Specify the target type: `issues only`, `pull requests only`, or `issues and pull requests`. - **Labels** - Trigger the event only if the issue or pull request contains one or more specified labels. - **Move from column** - Filter events based on the original project column. - **Move to column** - Filter events based on the destination project column. ### Available Actions There are four types of actions that can be executed when an event is triggered: - **Add labels** - Automatically add one or more labels to the issue or pull request. - **Remove labels** - Remove one or more specified labels. - **Move to column** - Move the issue or pull request to a designated project column. - **Issue state** - Close or reopen the issue. Pull requests can only be closed. ### Enable/Disable an event workflow Each event workflow can be individually enabled or disabled. When disabled, the workflow will be ignored even if the event occurs. This feature is useful when you want to temporarily stop certain automated actions without deleting the workflow configuration. Workflows of closed projects don't run either. ### Clone an event workflow When you need multiple workflows for the same event, you can clone an existing event to create a new one with different filters and actions. This allows you to customize how the same event behaves under different conditions, making workflows more flexible and powerful. ### Permissions Permissions are checked when workflows are managed, not when they run: - Viewing a project's workflows needs read access to the project - Managing them needs write access to the project plus write access to both issues and pull requests of a repository project's repository, being an owner of an organization project's organization or a site administrator, or being the user of a user project. Tokens limited to public repositories can't manage owner-level projects' workflows - A workflow runs when its event happens to an item of an open project that can hold the item's repository, unless that repository is archived, no matter who caused the event, and applies its actions without further permission checks. Pull requests are never reopened - Changes show as made by "Project workflow" on behalf of the user who triggered the event and don't trigger other workflows - Only users who can read an item can put it on a board or move its card The management rule is also documented on `CanManageWorkflows` in `services/projects/workflow.go`. Fixes: #14359 Fixes: #26704 Fixes: #27990 Replaces: #28745 Documentation PR: https://gitea.com/gitea/docs/pulls/308 Co-authored-by: bircni <bircni@icloud.com> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Giteabot <teabot@gitea.io>
218 lines
6.6 KiB
Go
218 lines
6.6 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package convert
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
project_model "gitea.dev/models/project"
|
|
user_model "gitea.dev/models/user"
|
|
"gitea.dev/modules/container"
|
|
"gitea.dev/modules/httplib"
|
|
"gitea.dev/modules/log"
|
|
api "gitea.dev/modules/structs"
|
|
"gitea.dev/modules/timeutil"
|
|
)
|
|
|
|
func projectTemplateTypeToString(t project_model.TemplateType) string {
|
|
switch t {
|
|
case project_model.TemplateTypeBasicKanban:
|
|
return "basic_kanban"
|
|
case project_model.TemplateTypeBugTriage:
|
|
return "bug_triage"
|
|
default:
|
|
return "none"
|
|
}
|
|
}
|
|
|
|
func ProjectTemplateTypeFromString(s string) (project_model.TemplateType, error) {
|
|
switch s {
|
|
case "", "none":
|
|
return project_model.TemplateTypeNone, nil
|
|
case "basic_kanban":
|
|
return project_model.TemplateTypeBasicKanban, nil
|
|
case "bug_triage":
|
|
return project_model.TemplateTypeBugTriage, nil
|
|
default:
|
|
return 0, fmt.Errorf("invalid template_type %q (expected none, basic_kanban, bug_triage)", s)
|
|
}
|
|
}
|
|
|
|
func projectCardTypeToString(t project_model.CardType) string {
|
|
switch t {
|
|
case project_model.CardTypeImagesAndText:
|
|
return "images_and_text"
|
|
default:
|
|
return "text_only"
|
|
}
|
|
}
|
|
|
|
func ProjectCardTypeFromString(s string) (project_model.CardType, error) {
|
|
switch s {
|
|
case "", "text_only":
|
|
return project_model.CardTypeTextOnly, nil
|
|
case "images_and_text":
|
|
return project_model.CardTypeImagesAndText, nil
|
|
default:
|
|
return 0, fmt.Errorf("invalid card_type %q (expected text_only, images_and_text)", s)
|
|
}
|
|
}
|
|
|
|
func projectTypeToString(t project_model.Type) string {
|
|
switch t {
|
|
case project_model.TypeIndividual:
|
|
return "individual"
|
|
case project_model.TypeRepository:
|
|
return "repository"
|
|
case project_model.TypeOrganization:
|
|
return "organization"
|
|
default:
|
|
return ""
|
|
}
|
|
}
|
|
|
|
// loadProjectCreators batch-fetches the creators of the given projects and columns, keyed by
|
|
// user ID. Enrichment is best-effort: on a lookup failure, or for creators that no longer
|
|
// exist, the creator field stays nil rather than failing the whole conversion.
|
|
func loadProjectCreators(ctx context.Context, projects []*project_model.Project, columns []*project_model.Column) map[int64]*user_model.User {
|
|
idSet := container.Set[int64]{}
|
|
for _, p := range projects {
|
|
if p.CreatorID > 0 {
|
|
idSet.Add(p.CreatorID)
|
|
}
|
|
}
|
|
for _, c := range columns {
|
|
if c.CreatorID > 0 {
|
|
idSet.Add(c.CreatorID)
|
|
}
|
|
}
|
|
if len(idSet) == 0 {
|
|
return nil
|
|
}
|
|
creators, err := user_model.GetUsersMapByIDs(ctx, idSet.Values())
|
|
if err != nil {
|
|
log.Error("GetUsersMapByIDs: %v", err)
|
|
return nil
|
|
}
|
|
return creators
|
|
}
|
|
|
|
// timeStampPtr returns nil for the zero timestamp, so a missing timestamp is not
|
|
// reported to API clients as the unix epoch.
|
|
func timeStampPtr(ts timeutil.TimeStamp) *time.Time {
|
|
if ts == 0 {
|
|
return nil
|
|
}
|
|
return ts.AsTimePtr()
|
|
}
|
|
|
|
// ToProject converts a project_model.Project to api.Project.
|
|
// Caller is expected to preload p.Repo / p.Owner to avoid N+1 lookups.
|
|
func ToProject(ctx context.Context, p *project_model.Project, doer *user_model.User) *api.Project {
|
|
creators := loadProjectCreators(ctx, []*project_model.Project{p}, nil)
|
|
return toProject(ctx, p, doer, creators)
|
|
}
|
|
|
|
func toProject(ctx context.Context, p *project_model.Project, doer *user_model.User, creators map[int64]*user_model.User) *api.Project {
|
|
state, closedAt := api.StateOpen, (*time.Time)(nil)
|
|
if p.IsClosed {
|
|
// changeProjectStatus stamps ClosedDateUnix on reopen too, so it only means
|
|
// anything while the project is closed
|
|
state, closedAt = api.StateClosed, timeStampPtr(p.ClosedDateUnix)
|
|
}
|
|
|
|
project := &api.Project{
|
|
ID: p.ID,
|
|
Title: p.Title,
|
|
Description: p.Description,
|
|
OwnerID: p.OwnerID,
|
|
RepoID: p.RepoID,
|
|
CreatorID: p.CreatorID, //nolint:staticcheck // deprecated but useful to API response
|
|
State: state,
|
|
IsClosed: p.IsClosed, //nolint:staticcheck // deprecated but useful to API response
|
|
TemplateType: projectTemplateTypeToString(p.TemplateType),
|
|
CardType: projectCardTypeToString(p.CardType),
|
|
Type: projectTypeToString(p.Type),
|
|
NumOpenIssues: p.NumOpenIssues,
|
|
NumClosedIssues: p.NumClosedIssues,
|
|
NumIssues: p.NumIssues,
|
|
CreatedAt: p.CreatedUnix.AsTime(),
|
|
UpdatedAt: timeStampPtr(p.UpdatedUnix),
|
|
ClosedAt: closedAt,
|
|
}
|
|
|
|
if creator, ok := creators[p.CreatorID]; ok {
|
|
project.Creator = ToUser(ctx, creator, doer)
|
|
}
|
|
|
|
// the caller preloads Repo/Owner, so Link stays free of lazy lookups
|
|
if link := p.Link(ctx); link != "" {
|
|
project.HTMLURL = httplib.MakeAbsoluteURL(ctx, link)
|
|
}
|
|
|
|
return project
|
|
}
|
|
|
|
func ToProjectColumn(ctx context.Context, column *project_model.Column, doer *user_model.User) *api.ProjectColumn {
|
|
creators := loadProjectCreators(ctx, nil, []*project_model.Column{column})
|
|
return toProjectColumn(ctx, column, doer, creators)
|
|
}
|
|
|
|
func toProjectColumn(ctx context.Context, column *project_model.Column, doer *user_model.User, creators map[int64]*user_model.User) *api.ProjectColumn {
|
|
apiColumn := &api.ProjectColumn{
|
|
ID: column.ID,
|
|
Title: column.Title,
|
|
Default: column.Default,
|
|
Sorting: int(column.Sorting),
|
|
Color: column.Color,
|
|
ProjectID: column.ProjectID,
|
|
CreatedAt: column.CreatedUnix.AsTime(),
|
|
UpdatedAt: timeStampPtr(column.UpdatedUnix),
|
|
}
|
|
if creator, ok := creators[column.CreatorID]; ok {
|
|
apiColumn.Creator = ToUser(ctx, creator, doer)
|
|
}
|
|
return apiColumn
|
|
}
|
|
|
|
func ToProjectList(ctx context.Context, projects []*project_model.Project, doer *user_model.User) []*api.Project {
|
|
creators := loadProjectCreators(ctx, projects, nil)
|
|
result := make([]*api.Project, len(projects))
|
|
for i, p := range projects {
|
|
result[i] = toProject(ctx, p, doer, creators)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func ToProjectColumnList(ctx context.Context, columns []*project_model.Column, doer *user_model.User) []*api.ProjectColumn {
|
|
creators := loadProjectCreators(ctx, nil, columns)
|
|
result := make([]*api.ProjectColumn, len(columns))
|
|
for i, column := range columns {
|
|
result[i] = toProjectColumn(ctx, column, doer, creators)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func ToProjectWorkflow(wf *project_model.Workflow) *api.ProjectWorkflow {
|
|
return &api.ProjectWorkflow{
|
|
ID: wf.ID,
|
|
Event: string(wf.WorkflowEvent),
|
|
Enabled: wf.Enabled,
|
|
Filters: api.ProjectWorkflowFilters(wf.WorkflowFilters),
|
|
Actions: api.ProjectWorkflowActions(wf.WorkflowActions),
|
|
Created: wf.CreatedUnix.AsTime(),
|
|
Updated: wf.UpdatedUnix.AsTime(),
|
|
}
|
|
}
|
|
|
|
func ToProjectWorkflowList(workflows []*project_model.Workflow) []*api.ProjectWorkflow {
|
|
result := make([]*api.ProjectWorkflow, len(workflows))
|
|
for i, wf := range workflows {
|
|
result[i] = ToProjectWorkflow(wf)
|
|
}
|
|
return result
|
|
}
|