mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 16:01:32 +01:00 
			
		
		
		
	Add issue delete notifier (#34592)
Fixes https://github.com/go-gitea/gitea/issues/34591 A reference regarding the deletion of issue webhooks on GitHub: https://docs.github.com/en/webhooks/webhook-events-and-payloads?actionType=deleted#issues
This commit is contained in:
		
							parent
							
								
									75aa23a665
								
							
						
					
					
						commit
						c67a8397ff
					
				| @ -286,6 +286,8 @@ const ( | ||||
| 	HookIssueReOpened HookIssueAction = "reopened" | ||||
| 	// HookIssueEdited edited | ||||
| 	HookIssueEdited HookIssueAction = "edited" | ||||
| 	// HookIssueDeleted is an issue action for deleting an issue | ||||
| 	HookIssueDeleted HookIssueAction = "deleted" | ||||
| 	// HookIssueAssigned assigned | ||||
| 	HookIssueAssigned HookIssueAction = "assigned" | ||||
| 	// HookIssueUnassigned unassigned | ||||
|  | ||||
| @ -2384,7 +2384,7 @@ settings.event_repository = Repository | ||||
| settings.event_repository_desc = Repository created or deleted. | ||||
| settings.event_header_issue = Issue Events | ||||
| settings.event_issues = Issues | ||||
| settings.event_issues_desc = Issue opened, closed, reopened, or edited. | ||||
| settings.event_issues_desc = Issue opened, closed, reopened, edited or deleted. | ||||
| settings.event_issue_assign = Issue Assigned | ||||
| settings.event_issue_assign_desc = Issue assigned or unassigned. | ||||
| settings.event_issue_label = Issue Labeled | ||||
| @ -2395,7 +2395,7 @@ settings.event_issue_comment = Issue Comment | ||||
| settings.event_issue_comment_desc = Issue comment created, edited, or deleted. | ||||
| settings.event_header_pull_request = Pull Request Events | ||||
| settings.event_pull_request = Pull Request | ||||
| settings.event_pull_request_desc = Pull request opened, closed, reopened, or edited. | ||||
| settings.event_pull_request_desc = Pull request opened, closed, reopened, edited or deleted. | ||||
| settings.event_pull_request_assign = Pull Request Assigned | ||||
| settings.event_pull_request_assign_desc = Pull request assigned or unassigned. | ||||
| settings.event_pull_request_label = Pull Request Labeled | ||||
|  | ||||
| @ -294,6 +294,43 @@ func (m *webhookNotifier) NewIssue(ctx context.Context, issue *issues_model.Issu | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (m *webhookNotifier) DeleteIssue(ctx context.Context, doer *user_model.User, issue *issues_model.Issue) { | ||||
| 	permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, doer) | ||||
| 	if issue.IsPull { | ||||
| 		if err := issue.LoadPullRequest(ctx); err != nil { | ||||
| 			log.Error("LoadPullRequest: %v", err) | ||||
| 			return | ||||
| 		} | ||||
| 		if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{ | ||||
| 			Action:      api.HookIssueDeleted, | ||||
| 			Index:       issue.Index, | ||||
| 			PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, doer), | ||||
| 			Repository:  convert.ToRepo(ctx, issue.Repo, permission), | ||||
| 			Sender:      convert.ToUser(ctx, doer, nil), | ||||
| 		}); err != nil { | ||||
| 			log.Error("PrepareWebhooks: %v", err) | ||||
| 		} | ||||
| 	} else { | ||||
| 		if err := issue.LoadRepo(ctx); err != nil { | ||||
| 			log.Error("issue.LoadRepo: %v", err) | ||||
| 			return | ||||
| 		} | ||||
| 		if err := issue.LoadPoster(ctx); err != nil { | ||||
| 			log.Error("issue.LoadPoster: %v", err) | ||||
| 			return | ||||
| 		} | ||||
| 		if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, &api.IssuePayload{ | ||||
| 			Action:     api.HookIssueDeleted, | ||||
| 			Index:      issue.Index, | ||||
| 			Issue:      convert.ToAPIIssue(ctx, issue.Poster, issue), | ||||
| 			Repository: convert.ToRepo(ctx, issue.Repo, permission), | ||||
| 			Sender:     convert.ToUser(ctx, doer, nil), | ||||
| 		}); err != nil { | ||||
| 			log.Error("PrepareWebhooks: %v", err) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (m *webhookNotifier) NewPullRequest(ctx context.Context, pull *issues_model.PullRequest, mentions []*user_model.User) { | ||||
| 	if err := pull.LoadIssue(ctx); err != nil { | ||||
| 		log.Error("pull.LoadIssue: %v", err) | ||||
|  | ||||
| @ -148,6 +148,13 @@ func testNewIssue(t *testing.T, session *TestSession, user, repo, title, content | ||||
| 	return issueURL | ||||
| } | ||||
| 
 | ||||
| func testIssueDelete(t *testing.T, session *TestSession, issueURL string) { | ||||
| 	req := NewRequestWithValues(t, "POST", path.Join(issueURL, "delete"), map[string]string{ | ||||
| 		"_csrf": GetUserCSRFToken(t, session), | ||||
| 	}) | ||||
| 	session.MakeRequest(t, req, http.StatusSeeOther) | ||||
| } | ||||
| 
 | ||||
| func testIssueAssign(t *testing.T, session *TestSession, repoLink string, issueID, assigneeID int64) { | ||||
| 	req := NewRequestWithValues(t, "POST", fmt.Sprintf(repoLink+"/issues/assignee?issue_ids=%d", issueID), map[string]string{ | ||||
| 		"_csrf":  GetUserCSRFToken(t, session), | ||||
|  | ||||
| @ -9,6 +9,7 @@ import ( | ||||
| 	"net/http" | ||||
| 	"net/http/httptest" | ||||
| 	"net/url" | ||||
| 	"path" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| 
 | ||||
| @ -450,6 +451,39 @@ func Test_WebhookIssue(t *testing.T) { | ||||
| 	}) | ||||
| } | ||||
| 
 | ||||
| func Test_WebhookIssueDelete(t *testing.T) { | ||||
| 	onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { | ||||
| 		var payloads []api.IssuePayload | ||||
| 		var triggeredEvent string | ||||
| 		provider := newMockWebhookProvider(func(r *http.Request) { | ||||
| 			content, _ := io.ReadAll(r.Body) | ||||
| 			var payload api.IssuePayload | ||||
| 			err := json.Unmarshal(content, &payload) | ||||
| 			assert.NoError(t, err) | ||||
| 			payloads = append(payloads, payload) | ||||
| 			triggeredEvent = "issue" | ||||
| 		}, http.StatusOK) | ||||
| 		defer provider.Close() | ||||
| 
 | ||||
| 		// 1. create a new webhook with special webhook for repo1 | ||||
| 		session := loginUser(t, "user2") | ||||
| 		testAPICreateWebhookForRepo(t, session, "user2", "repo1", provider.URL(), "issues") | ||||
| 		issueURL := testNewIssue(t, session, "user2", "repo1", "Title1", "Description1") | ||||
| 
 | ||||
| 		// 2. trigger the webhook | ||||
| 		testIssueDelete(t, session, issueURL) | ||||
| 
 | ||||
| 		// 3. validate the webhook is triggered | ||||
| 		assert.Equal(t, "issue", triggeredEvent) | ||||
| 		require.Len(t, payloads, 2) | ||||
| 		assert.EqualValues(t, "deleted", payloads[1].Action) | ||||
| 		assert.Equal(t, "repo1", payloads[1].Issue.Repo.Name) | ||||
| 		assert.Equal(t, "user2/repo1", payloads[1].Issue.Repo.FullName) | ||||
| 		assert.Equal(t, "Title1", payloads[1].Issue.Title) | ||||
| 		assert.Equal(t, "Description1", payloads[1].Issue.Body) | ||||
| 	}) | ||||
| } | ||||
| 
 | ||||
| func Test_WebhookIssueAssign(t *testing.T) { | ||||
| 	onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { | ||||
| 		var payloads []api.PullRequestPayload | ||||
| @ -596,6 +630,44 @@ func Test_WebhookPullRequest(t *testing.T) { | ||||
| 	}) | ||||
| } | ||||
| 
 | ||||
| func Test_WebhookPullRequestDelete(t *testing.T) { | ||||
| 	onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { | ||||
| 		var payloads []api.PullRequestPayload | ||||
| 		var triggeredEvent string | ||||
| 		provider := newMockWebhookProvider(func(r *http.Request) { | ||||
| 			content, _ := io.ReadAll(r.Body) | ||||
| 			var payload api.PullRequestPayload | ||||
| 			err := json.Unmarshal(content, &payload) | ||||
| 			assert.NoError(t, err) | ||||
| 			payloads = append(payloads, payload) | ||||
| 			triggeredEvent = "pull_request" | ||||
| 		}, http.StatusOK) | ||||
| 		defer provider.Close() | ||||
| 
 | ||||
| 		// 1. create a new webhook with special webhook for repo1 | ||||
| 		session := loginUser(t, "user2") | ||||
| 		testAPICreateWebhookForRepo(t, session, "user2", "repo1", provider.URL(), "pull_request") | ||||
| 
 | ||||
| 		testAPICreateBranch(t, session, "user2", "repo1", "master", "master2", http.StatusCreated) | ||||
| 
 | ||||
| 		repo1 := unittest.AssertExistsAndLoadBean(t, &repo.Repository{ID: 1}) | ||||
| 		issueURL := testCreatePullToDefaultBranch(t, session, repo1, repo1, "master2", "first pull request") | ||||
| 
 | ||||
| 		// 2. trigger the webhook | ||||
| 		testIssueDelete(t, session, path.Join(repo1.Link(), "pulls", issueURL)) | ||||
| 
 | ||||
| 		// 3. validate the webhook is triggered | ||||
| 		assert.Equal(t, "pull_request", triggeredEvent) | ||||
| 		require.Len(t, payloads, 2) | ||||
| 		assert.EqualValues(t, "deleted", payloads[1].Action) | ||||
| 		assert.Equal(t, "repo1", payloads[1].PullRequest.Base.Repository.Name) | ||||
| 		assert.Equal(t, "user2/repo1", payloads[1].PullRequest.Base.Repository.FullName) | ||||
| 		assert.Equal(t, 0, *payloads[1].PullRequest.Additions) | ||||
| 		assert.Equal(t, 0, *payloads[1].PullRequest.ChangedFiles) | ||||
| 		assert.Equal(t, 0, *payloads[1].PullRequest.Deletions) | ||||
| 	}) | ||||
| } | ||||
| 
 | ||||
| func Test_WebhookPullRequestComment(t *testing.T) { | ||||
| 	onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { | ||||
| 		var payloads []api.IssueCommentPayload | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user