Skip to content

Commit 418a7bb

Browse files
committed
Add tests for project workflow execution
1 parent fdd3bf0 commit 418a7bb

File tree

7 files changed

+883
-41
lines changed

7 files changed

+883
-41
lines changed

routers/web/repo/projects.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ func MoveIssues(ctx *context.Context) {
685685
form := &movedIssuesForm{}
686686
if err = json.NewDecoder(ctx.Req.Body).Decode(&form); err != nil {
687687
ctx.ServerError("DecodeMovedIssuesForm", err)
688+
return
688689
}
689690

690691
issueIDs := make([]int64, 0, len(form.Issues))

services/issue/label.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"code.gitea.io/gitea/models/db"
1010
issues_model "code.gitea.io/gitea/models/issues"
11-
access_model "code.gitea.io/gitea/models/perm/access"
1211
user_model "code.gitea.io/gitea/models/user"
1312
notify_service "code.gitea.io/gitea/services/notify"
1413
)
@@ -47,21 +46,6 @@ func AddLabels(ctx context.Context, issue *issues_model.Issue, doer *user_model.
4746
// RemoveLabel removes a label from issue by given ID.
4847
func RemoveLabel(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, label *issues_model.Label) error {
4948
if err := db.WithTx(ctx, func(ctx context.Context) error {
50-
if err := issue.LoadRepo(ctx); err != nil {
51-
return err
52-
}
53-
54-
perm, err := access_model.GetUserRepoPermission(ctx, issue.Repo, doer)
55-
if err != nil {
56-
return err
57-
}
58-
if !perm.CanWriteIssuesOrPulls(issue.IsPull) {
59-
if label.OrgID > 0 {
60-
return issues_model.ErrOrgLabelNotExist{}
61-
}
62-
return issues_model.ErrRepoLabelNotExist{}
63-
}
64-
6549
return issues_model.DeleteIssueLabel(ctx, issue, label, doer)
6650
}); err != nil {
6751
return err

services/projects/workflow_notifier.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,27 @@ func (m *workflowNotifier) IssueChangeStatus(ctx context.Context, doer *user_mod
9898
}
9999

100100
func (*workflowNotifier) IssueChangeProjects(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, newProject *project_model.Project) {
101-
if newProject == nil {
101+
if newProject == nil { // removed from project
102+
if err := issue.LoadProject(ctx); err != nil {
103+
log.Error("LoadProject: %v", err)
104+
return
105+
}
106+
if issue.Project == nil {
107+
return
108+
}
109+
110+
workflows, err := project_model.FindWorkflowsByProjectID(ctx, issue.Project.ID)
111+
if err != nil {
112+
log.Error("IssueChangeStatus: FindWorkflowsByProjectID: %v", err)
113+
return
114+
}
115+
116+
// Find workflows for the ItemOpened event
117+
for _, workflow := range workflows {
118+
if workflow.WorkflowEvent == project_model.WorkflowEventItemRemovedFromProject {
119+
fireIssueWorkflow(ctx, workflow, issue, 0, 0)
120+
}
121+
}
102122
return
103123
}
104124

@@ -381,15 +401,13 @@ func executeWorkflowActions(ctx context.Context, workflow *project_model.Workflo
381401
log.Error("ReopenIssue: %v", err)
382402
continue
383403
}
384-
issue.IsClosed = false
385404
}
386405
} else if strings.EqualFold(action.Value, "close") {
387406
if !issue.IsClosed {
388407
if err := issue_service.CloseIssue(ctx, issue, user_model.NewProjectWorkflowsUser(), ""); err != nil {
389408
log.Error("CloseIssue: %v", err)
390409
continue
391410
}
392-
issue.IsClosed = true
393411
}
394412
}
395413
default:

tests/integration/issue_test.go

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,34 @@ func TestNoLoginViewIssue(t *testing.T) {
122122
MakeRequest(t, req, http.StatusOK)
123123
}
124124

125-
func testNewIssue(t *testing.T, session *TestSession, user, repo, title, content string) string {
125+
type newIssueOptions struct {
126+
Title string
127+
Content string
128+
ProjectID int64
129+
LabelIDs []int64
130+
}
131+
132+
func testNewIssue(t *testing.T, session *TestSession, user, repo string, opts newIssueOptions) string {
126133
req := NewRequest(t, "GET", path.Join(user, repo, "issues", "new"))
127134
resp := session.MakeRequest(t, req, http.StatusOK)
128135

129136
htmlDoc := NewHTMLParser(t, resp.Body)
130137
link, exists := htmlDoc.doc.Find("form.ui.form").Attr("action")
131138
assert.True(t, exists, "The template has changed")
139+
var labelIDs string
140+
for i, id := range opts.LabelIDs {
141+
labelIDs += strconv.FormatInt(id, 10)
142+
if i < len(opts.LabelIDs)-1 {
143+
labelIDs += ","
144+
}
145+
}
146+
132147
req = NewRequestWithValues(t, "POST", link, map[string]string{
133-
"_csrf": htmlDoc.GetCSRF(),
134-
"title": title,
135-
"content": content,
148+
"_csrf": htmlDoc.GetCSRF(),
149+
"title": opts.Title,
150+
"content": opts.Content,
151+
"project_id": strconv.FormatInt(opts.ProjectID, 10),
152+
"label_ids": labelIDs,
136153
})
137154
resp = session.MakeRequest(t, req, http.StatusOK)
138155

@@ -142,9 +159,9 @@ func testNewIssue(t *testing.T, session *TestSession, user, repo, title, content
142159

143160
htmlDoc = NewHTMLParser(t, resp.Body)
144161
val := htmlDoc.doc.Find("#issue-title-display").Text()
145-
assert.Contains(t, val, title)
162+
assert.Contains(t, val, opts.Title)
146163
val = htmlDoc.doc.Find(".comment .render-content p").First().Text()
147-
assert.Equal(t, content, val)
164+
assert.Equal(t, opts.Content, val)
148165

149166
return issueURL
150167
}
@@ -210,13 +227,19 @@ func testIssueChangeMilestone(t *testing.T, session *TestSession, repoLink strin
210227
func TestNewIssue(t *testing.T) {
211228
defer tests.PrepareTestEnv(t)()
212229
session := loginUser(t, "user2")
213-
testNewIssue(t, session, "user2", "repo1", "Title", "Description")
230+
testNewIssue(t, session, "user2", "repo1", newIssueOptions{
231+
Title: "Title",
232+
Content: "Description",
233+
})
214234
}
215235

216236
func TestEditIssue(t *testing.T) {
217237
defer tests.PrepareTestEnv(t)()
218238
session := loginUser(t, "user2")
219-
issueURL := testNewIssue(t, session, "user2", "repo1", "Title", "Description")
239+
issueURL := testNewIssue(t, session, "user2", "repo1", newIssueOptions{
240+
Title: "Title",
241+
Content: "Description",
242+
})
220243

221244
req := NewRequestWithValues(t, "POST", issueURL+"/content", map[string]string{
222245
"_csrf": GetUserCSRFToken(t, session),
@@ -244,7 +267,10 @@ func TestEditIssue(t *testing.T) {
244267
func TestIssueCommentClose(t *testing.T) {
245268
defer tests.PrepareTestEnv(t)()
246269
session := loginUser(t, "user2")
247-
issueURL := testNewIssue(t, session, "user2", "repo1", "Title", "Description")
270+
issueURL := testNewIssue(t, session, "user2", "repo1", newIssueOptions{
271+
Title: "Title",
272+
Content: "Description",
273+
})
248274
testIssueAddComment(t, session, issueURL, "Test comment 1", "")
249275
testIssueAddComment(t, session, issueURL, "Test comment 2", "")
250276
testIssueAddComment(t, session, issueURL, "Test comment 3", "close")
@@ -260,7 +286,10 @@ func TestIssueCommentClose(t *testing.T) {
260286
func TestIssueCommentDelete(t *testing.T) {
261287
defer tests.PrepareTestEnv(t)()
262288
session := loginUser(t, "user2")
263-
issueURL := testNewIssue(t, session, "user2", "repo1", "Title", "Description")
289+
issueURL := testNewIssue(t, session, "user2", "repo1", newIssueOptions{
290+
Title: "Title",
291+
Content: "Description",
292+
})
264293
comment1 := "Test comment 1"
265294
commentID := testIssueAddComment(t, session, issueURL, comment1, "")
266295
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: commentID})
@@ -281,7 +310,10 @@ func TestIssueCommentDelete(t *testing.T) {
281310
func TestIssueCommentUpdate(t *testing.T) {
282311
defer tests.PrepareTestEnv(t)()
283312
session := loginUser(t, "user2")
284-
issueURL := testNewIssue(t, session, "user2", "repo1", "Title", "Description")
313+
issueURL := testNewIssue(t, session, "user2", "repo1", newIssueOptions{
314+
Title: "Title",
315+
Content: "Description",
316+
})
285317
comment1 := "Test comment 1"
286318
commentID := testIssueAddComment(t, session, issueURL, comment1, "")
287319

@@ -310,7 +342,10 @@ func TestIssueCommentUpdate(t *testing.T) {
310342
func TestIssueCommentUpdateSimultaneously(t *testing.T) {
311343
defer tests.PrepareTestEnv(t)()
312344
session := loginUser(t, "user2")
313-
issueURL := testNewIssue(t, session, "user2", "repo1", "Title", "Description")
345+
issueURL := testNewIssue(t, session, "user2", "repo1", newIssueOptions{
346+
Title: "Title",
347+
Content: "Description",
348+
})
314349
comment1 := "Test comment 1"
315350
commentID := testIssueAddComment(t, session, issueURL, comment1, "")
316351

@@ -348,7 +383,10 @@ func TestIssueCommentUpdateSimultaneously(t *testing.T) {
348383
func TestIssueReaction(t *testing.T) {
349384
defer tests.PrepareTestEnv(t)()
350385
session := loginUser(t, "user2")
351-
issueURL := testNewIssue(t, session, "user2", "repo1", "Title", "Description")
386+
issueURL := testNewIssue(t, session, "user2", "repo1", newIssueOptions{
387+
Title: "Title",
388+
Content: "Description",
389+
})
352390

353391
req := NewRequest(t, "GET", issueURL)
354392
resp := session.MakeRequest(t, req, http.StatusOK)
@@ -448,7 +486,10 @@ func TestIssueCrossReference(t *testing.T) {
448486

449487
func testIssueWithBean(t *testing.T, user string, repoID int64, title, content string) (string, *issues_model.Issue) {
450488
session := loginUser(t, user)
451-
issueURL := testNewIssue(t, session, user, fmt.Sprintf("repo%d", repoID), title, content)
489+
issueURL := testNewIssue(t, session, user, fmt.Sprintf("repo%d", repoID), newIssueOptions{
490+
Title: title,
491+
Content: content,
492+
})
452493
indexStr := issueURL[strings.LastIndexByte(issueURL, '/')+1:]
453494
index, err := strconv.Atoi(indexStr)
454495
assert.NoError(t, err, "Invalid issue href: %s", issueURL)

0 commit comments

Comments
 (0)