Compare commits

...

5 Commits

Author SHA1 Message Date
Lunny Xiao b7f9e74e8d
Merge 95cecbb294 into bb0e4ce581 2024-05-05 00:57:26 +02:00
Neal Caffery bb0e4ce581
Update README.md (#30856)
fix typo for the Docker README
2024-05-03 23:53:18 -04:00
wxiaoguang c7bb3aa034
Fix markdown URL parsing for commit ID (#30812) 2024-05-04 09:48:16 +08:00
Lunny Xiao 95cecbb294
Add NewIssue events 2024-04-05 14:18:51 +08:00
Lunny Xiao 64e0ba10de
Add project workflow feature so users can define how to execute steps when project related events fired 2024-03-31 11:57:14 +08:00
11 changed files with 415 additions and 79 deletions

View File

@ -1,7 +1,7 @@
# Gitea - Docker
Dockerfile is found in root of repository.
Dockerfile is found in the root of the repository.
Docker image can be found on [docker hub](https://hub.docker.com/r/gitea/gitea)
Docker image can be found on [docker hub](https://hub.docker.com/r/gitea/gitea).
Documentation on using docker image can be found on [Gitea Docs site](https://docs.gitea.com/installation/install-with-docker-rootless)
Documentation on using docker image can be found on [Gitea Docs site](https://docs.gitea.com/installation/install-with-docker-rootless).

View File

@ -217,6 +217,18 @@ func GetBoard(ctx context.Context, boardID int64) (*Board, error) {
return board, nil
}
func GetBoardByProjectIDAndBoardName(ctx context.Context, projectID int64, boardName string) (*Board, error) {
board := new(Board)
has, err := db.GetEngine(ctx).Where("project_id=? AND title=?", projectID, boardName).Get(board)
if err != nil {
return nil, err
} else if !has {
return nil, ErrProjectBoardNotExist{ProjectID: projectID, Name: boardName}
}
return board, nil
}
// UpdateBoard updates a project board
func UpdateBoard(ctx context.Context, board *Board) error {
var fieldToUpdate []string

View File

@ -75,6 +75,19 @@ func (p *Project) NumOpenIssues(ctx context.Context) int {
return int(c)
}
func AddIssueToBoard(ctx context.Context, issueID int64, newBoard *Board) error {
return db.Insert(ctx, &ProjectIssue{
IssueID: issueID,
ProjectID: newBoard.ProjectID,
ProjectBoardID: newBoard.ID,
})
}
func MoveIssueToAnotherBoard(ctx context.Context, issueID int64, newBoard *Board) error {
_, err := db.GetEngine(ctx).Exec("UPDATE `project_issue` SET project_board_id=? WHERE issue_id=?", newBoard.ID, issueID)
return err
}
// MoveIssuesOnProjectBoard moves or keeps issues in a column and sorts them inside that column
func MoveIssuesOnProjectBoard(ctx context.Context, board *Board, sortedIssueIDs map[int64]int64) error {
return db.WithTx(ctx, func(ctx context.Context) error {

View File

@ -52,6 +52,7 @@ const (
type ErrProjectNotExist struct {
ID int64
RepoID int64
Name string
}
// IsErrProjectNotExist checks if an error is a ErrProjectNotExist
@ -61,6 +62,9 @@ func IsErrProjectNotExist(err error) bool {
}
func (err ErrProjectNotExist) Error() string {
if err.RepoID > 0 && len(err.Name) > 0 {
return fmt.Sprintf("projects does not exist [repo_id: %d, name: %s]", err.RepoID, err.Name)
}
return fmt.Sprintf("projects does not exist [id: %d]", err.ID)
}
@ -70,7 +74,9 @@ func (err ErrProjectNotExist) Unwrap() error {
// ErrProjectBoardNotExist represents a "ProjectBoardNotExist" kind of error.
type ErrProjectBoardNotExist struct {
BoardID int64
BoardID int64
ProjectID int64
Name string
}
// IsErrProjectBoardNotExist checks if an error is a ErrProjectBoardNotExist
@ -80,6 +86,9 @@ func IsErrProjectBoardNotExist(err error) bool {
}
func (err ErrProjectBoardNotExist) Error() string {
if err.ProjectID > 0 && len(err.Name) > 0 {
return fmt.Sprintf("project board does not exist [project_id: %d, name: %s]", err.ProjectID, err.Name)
}
return fmt.Sprintf("project board does not exist [id: %d]", err.BoardID)
}
@ -293,6 +302,19 @@ func GetProjectByID(ctx context.Context, id int64) (*Project, error) {
return p, nil
}
// GetProjectByName returns the projects in a repository
func GetProjectByName(ctx context.Context, repoID int64, name string) (*Project, error) {
p := new(Project)
has, err := db.GetEngine(ctx).Where("repo_id=? AND title=?", repoID, name).Get(p)
if err != nil {
return nil, err
} else if !has {
return nil, ErrProjectNotExist{RepoID: repoID, Name: name}
}
return p, nil
}
// GetProjectForRepoByID returns the projects in a repository
func GetProjectForRepoByID(ctx context.Context, repoID, id int64) (*Project, error) {
p := new(Project)

View File

@ -10,6 +10,7 @@ import (
"path"
"path/filepath"
"regexp"
"slices"
"strings"
"sync"
@ -54,7 +55,7 @@ var (
shortLinkPattern = regexp.MustCompile(`\[\[(.*?)\]\](\w*)`)
// anyHashPattern splits url containing SHA into parts
anyHashPattern = regexp.MustCompile(`https?://(?:\S+/){4,5}([0-9a-f]{40,64})(/[-+~_%.a-zA-Z0-9/]+)?(#[-+~_%.a-zA-Z0-9]+)?`)
anyHashPattern = regexp.MustCompile(`https?://(?:\S+/){4,5}([0-9a-f]{40,64})(/[-+~%./\w]+)?(\?[-+~%.\w&=]+)?(#[-+~%.\w]+)?`)
// comparePattern matches "http://domain/org/repo/compare/COMMIT1...COMMIT2#hash"
comparePattern = regexp.MustCompile(`https?://(?:\S+/){4,5}([0-9a-f]{7,64})(\.\.\.?)([0-9a-f]{7,64})?(#[-+~_%.a-zA-Z0-9]+)?`)
@ -591,7 +592,8 @@ func replaceContentList(node *html.Node, i, j int, newNodes []*html.Node) {
func mentionProcessor(ctx *RenderContext, node *html.Node) {
start := 0
for node != nil {
nodeStop := node.NextSibling
for node != nodeStop {
found, loc := references.FindFirstMentionBytes(util.UnsafeStringToBytes(node.Data[start:]))
if !found {
node = node.NextSibling
@ -962,57 +964,68 @@ func commitCrossReferencePatternProcessor(ctx *RenderContext, node *html.Node) {
}
}
type anyHashPatternResult struct {
PosStart int
PosEnd int
FullURL string
CommitID string
SubPath string
QueryHash string
}
func anyHashPatternExtract(s string) (ret anyHashPatternResult, ok bool) {
m := anyHashPattern.FindStringSubmatchIndex(s)
if m == nil {
return ret, false
}
ret.PosStart, ret.PosEnd = m[0], m[1]
ret.FullURL = s[ret.PosStart:ret.PosEnd]
if strings.HasSuffix(ret.FullURL, ".") {
// if url ends in '.', it's very likely that it is not part of the actual url but used to finish a sentence.
ret.PosEnd--
ret.FullURL = ret.FullURL[:len(ret.FullURL)-1]
for i := 0; i < len(m); i++ {
m[i] = min(m[i], ret.PosEnd)
}
}
ret.CommitID = s[m[2]:m[3]]
if m[5] > 0 {
ret.SubPath = s[m[4]:m[5]]
}
lastStart, lastEnd := m[len(m)-2], m[len(m)-1]
if lastEnd > 0 {
ret.QueryHash = s[lastStart:lastEnd][1:]
}
return ret, true
}
// fullHashPatternProcessor renders SHA containing URLs
func fullHashPatternProcessor(ctx *RenderContext, node *html.Node) {
if ctx.Metas == nil {
return
}
next := node.NextSibling
for node != nil && node != next {
m := anyHashPattern.FindStringSubmatchIndex(node.Data)
if m == nil {
return
nodeStop := node.NextSibling
for node != nodeStop {
if node.Type != html.TextNode {
node = node.NextSibling
continue
}
urlFull := node.Data[m[0]:m[1]]
text := base.ShortSha(node.Data[m[2]:m[3]])
// 3rd capture group matches a optional path
subpath := ""
if m[5] > 0 {
subpath = node.Data[m[4]:m[5]]
ret, ok := anyHashPatternExtract(node.Data)
if !ok {
node = node.NextSibling
continue
}
// 4th capture group matches a optional url hash
hash := ""
if m[7] > 0 {
hash = node.Data[m[6]:m[7]][1:]
text := base.ShortSha(ret.CommitID)
if ret.SubPath != "" {
text += ret.SubPath
}
start := m[0]
end := m[1]
// If url ends in '.', it's very likely that it is not part of the
// actual url but used to finish a sentence.
if strings.HasSuffix(urlFull, ".") {
end--
urlFull = urlFull[:len(urlFull)-1]
if hash != "" {
hash = hash[:len(hash)-1]
} else if subpath != "" {
subpath = subpath[:len(subpath)-1]
}
if ret.QueryHash != "" {
text += " (" + ret.QueryHash + ")"
}
if subpath != "" {
text += subpath
}
if hash != "" {
text += " (" + hash + ")"
}
replaceContent(node, start, end, createCodeLink(urlFull, text, "commit"))
replaceContent(node, ret.PosStart, ret.PosEnd, createCodeLink(ret.FullURL, text, "commit"))
node = node.NextSibling.NextSibling
}
}
@ -1021,19 +1034,16 @@ func comparePatternProcessor(ctx *RenderContext, node *html.Node) {
if ctx.Metas == nil {
return
}
next := node.NextSibling
for node != nil && node != next {
m := comparePattern.FindStringSubmatchIndex(node.Data)
if m == nil {
return
nodeStop := node.NextSibling
for node != nodeStop {
if node.Type != html.TextNode {
node = node.NextSibling
continue
}
// Ensure that every group (m[0]...m[7]) has a match
for i := 0; i < 8; i++ {
if m[i] == -1 {
return
}
m := comparePattern.FindStringSubmatchIndex(node.Data)
if m == nil || slices.Contains(m[:8], -1) { // ensure that every group (m[0]...m[7]) has a match
node = node.NextSibling
continue
}
urlFull := node.Data[m[0]:m[1]]

View File

@ -60,7 +60,8 @@ func renderCodeBlock(ctx *RenderContext, node *html.Node) (urlPosStart, urlPosSt
}
func codePreviewPatternProcessor(ctx *RenderContext, node *html.Node) {
for node != nil {
nodeStop := node.NextSibling
for node != nodeStop {
if node.Type != html.TextNode {
node = node.NextSibling
continue

View File

@ -399,36 +399,61 @@ func TestRegExp_sha1CurrentPattern(t *testing.T) {
}
func TestRegExp_anySHA1Pattern(t *testing.T) {
testCases := map[string][]string{
testCases := map[string]anyHashPatternResult{
"https://github.com/jquery/jquery/blob/a644101ed04d0beacea864ce805e0c4f86ba1cd1/test/unit/event.js#L2703": {
"a644101ed04d0beacea864ce805e0c4f86ba1cd1",
"/test/unit/event.js",
"#L2703",
CommitID: "a644101ed04d0beacea864ce805e0c4f86ba1cd1",
SubPath: "/test/unit/event.js",
QueryHash: "L2703",
},
"https://github.com/jquery/jquery/blob/a644101ed04d0beacea864ce805e0c4f86ba1cd1/test/unit/event.js": {
"a644101ed04d0beacea864ce805e0c4f86ba1cd1",
"/test/unit/event.js",
"",
CommitID: "a644101ed04d0beacea864ce805e0c4f86ba1cd1",
SubPath: "/test/unit/event.js",
},
"https://github.com/jquery/jquery/commit/0705be475092aede1eddae01319ec931fb9c65fc": {
"0705be475092aede1eddae01319ec931fb9c65fc",
"",
"",
CommitID: "0705be475092aede1eddae01319ec931fb9c65fc",
},
"https://github.com/jquery/jquery/tree/0705be475092aede1eddae01319ec931fb9c65fc/src": {
"0705be475092aede1eddae01319ec931fb9c65fc",
"/src",
"",
CommitID: "0705be475092aede1eddae01319ec931fb9c65fc",
SubPath: "/src",
},
"https://try.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2": {
"d8a994ef243349f321568f9e36d5c3f444b99cae",
"",
"#diff-2",
CommitID: "d8a994ef243349f321568f9e36d5c3f444b99cae",
QueryHash: "diff-2",
},
"non-url": {},
"http://a/b/c/d/e/1234567812345678123456781234567812345678123456781234567812345678?a=b#L1-L2": {
CommitID: "1234567812345678123456781234567812345678123456781234567812345678",
QueryHash: "L1-L2",
},
"http://a/b/c/d/e/1234567812345678123456781234567812345678123456781234567812345678.": {
CommitID: "1234567812345678123456781234567812345678123456781234567812345678",
},
"http://a/b/c/d/e/1234567812345678123456781234567812345678123456781234567812345678/sub.": {
CommitID: "1234567812345678123456781234567812345678123456781234567812345678",
SubPath: "/sub",
},
"http://a/b/c/d/e/1234567812345678123456781234567812345678123456781234567812345678?a=b.": {
CommitID: "1234567812345678123456781234567812345678123456781234567812345678",
},
"http://a/b/c/d/e/1234567812345678123456781234567812345678123456781234567812345678?a=b&c=d": {
CommitID: "1234567812345678123456781234567812345678123456781234567812345678",
},
"http://a/b/c/d/e/1234567812345678123456781234567812345678123456781234567812345678#hash.": {
CommitID: "1234567812345678123456781234567812345678123456781234567812345678",
QueryHash: "hash",
},
}
for k, v := range testCases {
assert.Equal(t, anyHashPattern.FindStringSubmatch(k)[1:], v)
ret, ok := anyHashPatternExtract(k)
if v.CommitID == "" {
assert.False(t, ok)
} else {
assert.EqualValues(t, strings.TrimSuffix(k, "."), ret.FullURL)
assert.EqualValues(t, v.CommitID, ret.CommitID)
assert.EqualValues(t, v.SubPath, ret.SubPath)
assert.EqualValues(t, v.QueryHash, ret.QueryHash)
}
}
}

View File

@ -124,6 +124,11 @@ func TestRender_CrossReferences(t *testing.T) {
test(
util.URLJoin(markup.TestAppURL, "gogitea", "some-repo-name", "issues", "12345"),
`<p><a href="`+util.URLJoin(markup.TestAppURL, "gogitea", "some-repo-name", "issues", "12345")+`" class="ref-issue" rel="nofollow">gogitea/some-repo-name#12345</a></p>`)
inputURL := "https://host/a/b/commit/0123456789012345678901234567890123456789/foo.txt?a=b#L2-L3"
test(
inputURL,
`<p><a href="`+inputURL+`" rel="nofollow"><code>0123456789/foo.txt (L2-L3)</code></a></p>`)
}
func TestMisc_IsSameDomain(t *testing.T) {
@ -695,7 +700,7 @@ func TestIssue18471(t *testing.T) {
}, strings.NewReader(data), &res)
assert.NoError(t, err)
assert.Equal(t, "<a href=\"http://domain/org/repo/compare/783b039...da951ce\" class=\"compare\"><code class=\"nohighlight\">783b039...da951ce</code></a>", res.String())
assert.Equal(t, `<a href="http://domain/org/repo/compare/783b039...da951ce" class="compare"><code class="nohighlight">783b039...da951ce</code></a>`, res.String())
}
func TestIsFullURL(t *testing.T) {

View File

@ -0,0 +1,47 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package projects
// Action represents an action that can be taken in a workflow
type Action struct {
SetValue string
}
const (
// Project workflow event names
EventItemAddedToProject = "item_added_to_project"
EventItemClosed = "item_closed"
EventItem
)
type Event struct {
Name string
Types []string
Actions []Action
}
type Workflow struct {
Name string
Events []Event
ProjectID int64
}
func ParseWorkflow(content string) (*Workflow, error) {
return &Workflow{}, nil
}
func (w *Workflow) FireAction(evtName string, f func(action Action) error) error {
for _, evt := range w.Events {
if evt.Name == evtName {
for _, action := range evt.Actions {
// Do something with action
if err := f(action); err != nil {
return err
}
}
break
}
}
return nil
}

View File

@ -0,0 +1,46 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package projects
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseWorkflow(t *testing.T) {
workflowFile := `
name: Test Workflow
on:
item_added_to_project:
types: [issue, pull_request]
action:
- set_value: "status=Todo"
item_closed:
types: [issue, pull_request]
action:
- remove_label: ""
item_reopened:
action:
code_changes_requested:
action:
code_review_approved:
action:
pull_request_merged:
action:
auto_add_to_project:
action:
`
wf, err := ParseWorkflow(workflowFile)
assert.NoError(t, err)
assert.Equal(t, "Test Workflow", wf.Name)
}

View File

@ -0,0 +1,155 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package projects
import (
"context"
"strings"
issues_model "code.gitea.io/gitea/models/issues"
project_model "code.gitea.io/gitea/models/project"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/log"
project_module "code.gitea.io/gitea/modules/projects"
notify_service "code.gitea.io/gitea/services/notify"
)
func init() {
notify_service.RegisterNotifier(&workflowNotifier{})
}
type workflowNotifier struct {
notify_service.NullNotifier
}
var _ notify_service.Notifier = &workflowNotifier{}
// NewNotifier create a new workflowNotifier notifier
func NewNotifier() notify_service.Notifier {
return &workflowNotifier{}
}
func findRepoProjectsWorkflows(ctx context.Context, repo *repo_model.Repository) ([]*project_module.Workflow, error) {
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
if err != nil {
log.Error("IssueChangeStatus: OpenRepository: %v", err)
return nil, err
}
defer gitRepo.Close()
// Get the commit object for the ref
commit, err := gitRepo.GetCommit(repo.DefaultBranch)
if err != nil {
log.Error("gitRepo.GetCommit: %w", err)
return nil, err
}
tree, err := commit.SubTree(".gitea/projects")
if _, ok := err.(git.ErrNotExist); ok {
return nil, nil
}
if err != nil {
log.Error("commit.SubTree: %w", err)
return nil, err
}
entries, err := tree.ListEntriesRecursiveFast()
if err != nil {
log.Error("tree.ListEntriesRecursiveFast: %w", err)
return nil, err
}
ret := make(git.Entries, 0, len(entries))
for _, entry := range entries {
if strings.HasSuffix(entry.Name(), ".yml") || strings.HasSuffix(entry.Name(), ".yaml") {
ret = append(ret, entry)
}
}
if len(ret) == 0 {
return nil, nil
}
wfs := make([]*project_module.Workflow, 0, len(ret))
for _, entry := range ret {
workflowContent, err := commit.GetFileContent(".gitea/projects/"+entry.Name(), 1024*1024)
if err != nil {
log.Error("gitRepo.GetCommit: %w", err)
return nil, err
}
wf, err := project_module.ParseWorkflow(workflowContent)
if err != nil {
log.Error("IssueChangeStatus: OpenRepository: %v", err)
return nil, err
}
projectName := strings.TrimSuffix(strings.TrimSuffix(entry.Name(), ".yml"), ".yaml")
project, err := project_model.GetProjectByName(ctx, repo.ID, projectName)
if err != nil {
log.Error("IssueChangeStatus: GetProjectByName: %v", err)
return nil, err
}
wf.ProjectID = project.ID
wfs = append(wfs, wf)
}
return wfs, nil
}
func (m *workflowNotifier) NewIssue(ctx context.Context, issue *issues_model.Issue, mentions []*user_model.User) {
if err := issue.LoadRepo(ctx); err != nil {
log.Error("NewIssue: LoadRepo: %v", err)
return
}
wfs, err := findRepoProjectsWorkflows(ctx, issue.Repo)
if err != nil {
log.Error("NewIssue: findRepoProjectsWorkflows: %v", err)
return
}
for _, wf := range wfs {
if err := wf.FireAction(project_module.EventItemClosed, func(action project_module.Action) error {
board, err := project_model.GetBoardByProjectIDAndBoardName(ctx, wf.ProjectID, action.SetValue)
if err != nil {
log.Error("NewIssue: GetBoardByProjectIDAndBoardName: %v", err)
return err
}
return project_model.AddIssueToBoard(ctx, issue.ID, board)
}); err != nil {
log.Error("NewIssue: FireAction: %v", err)
return
}
}
}
}
func (m *workflowNotifier) IssueChangeStatus(ctx context.Context, doer *user_model.User, commitID string, issue *issues_model.Issue, actionComment *issues_model.Comment, isClosed bool) {
if isClosed {
if err := issue.LoadRepo(ctx); err != nil {
log.Error("IssueChangeStatus: LoadRepo: %v", err)
return
}
wfs, err := findRepoProjectsWorkflows(ctx, issue.Repo)
if err != nil {
log.Error("IssueChangeStatus: findRepoProjectsWorkflows: %v", err)
return
}
for _, wf := range wfs {
if err := wf.FireAction(project_module.EventItemClosed, func(action project_module.Action) error {
board, err := project_model.GetBoardByProjectIDAndBoardName(ctx, wf.ProjectID, action.SetValue)
if err != nil {
log.Error("IssueChangeStatus: GetBoardByProjectIDAndBoardName: %v", err)
return err
}
return project_model.MoveIssueToAnotherBoard(ctx, issue.ID, board)
}); err != nil {
log.Error("IssueChangeStatus: FireAction: %v", err)
return
}
}
}
}