mirror of
				https://codeberg.org/forgejo/forgejo.git
				synced 2025-11-04 08:21:11 +00:00 
			
		
		
		
	- Adds a new option `[webhook].PAYLOAD_COMMIT_LIMIT` that limits the amount of commits is sent for each webhook payload, this was previously done via `[ui].FEED_MAX_COMMIT_NUM` which feels incorrect. - The default is 15 for this new option, purely arbitary. - Resolves forgejo/forgejo#6780 - Added unit testing, it's quite a lot because this the notification area is not really easy to test and rather should've been a integration test but that ends up having more complicated than trying doing an unit test. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6797 Reviewed-by: Otto <otto@codeberg.org> Reviewed-by: 0ko <0ko@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
		
			
				
	
	
		
			134 lines
		
	
	
	
		
			5.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			134 lines
		
	
	
	
		
			5.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// Copyright 2025 The Forgejo Authors. All rights reserved.
 | 
						|
// SPDX-License-Identifier: GPL-3.0-or-later
 | 
						|
 | 
						|
package webhook
 | 
						|
 | 
						|
import (
 | 
						|
	"path/filepath"
 | 
						|
	"testing"
 | 
						|
 | 
						|
	"code.gitea.io/gitea/models/db"
 | 
						|
	repo_model "code.gitea.io/gitea/models/repo"
 | 
						|
	"code.gitea.io/gitea/models/unittest"
 | 
						|
	user_model "code.gitea.io/gitea/models/user"
 | 
						|
	webhook_model "code.gitea.io/gitea/models/webhook"
 | 
						|
	"code.gitea.io/gitea/modules/git"
 | 
						|
	"code.gitea.io/gitea/modules/json"
 | 
						|
	"code.gitea.io/gitea/modules/repository"
 | 
						|
	"code.gitea.io/gitea/modules/setting"
 | 
						|
	"code.gitea.io/gitea/modules/structs"
 | 
						|
	"code.gitea.io/gitea/modules/test"
 | 
						|
 | 
						|
	"github.com/stretchr/testify/assert"
 | 
						|
	"github.com/stretchr/testify/require"
 | 
						|
)
 | 
						|
 | 
						|
func pushCommits() *repository.PushCommits {
 | 
						|
	pushCommits := repository.NewPushCommits()
 | 
						|
	pushCommits.Commits = []*repository.PushCommit{
 | 
						|
		{
 | 
						|
			Sha1:           "2c54faec6c45d31c1abfaecdab471eac6633738a",
 | 
						|
			CommitterEmail: "user2@example.com",
 | 
						|
			CommitterName:  "User2",
 | 
						|
			AuthorEmail:    "user2@example.com",
 | 
						|
			AuthorName:     "User2",
 | 
						|
			Message:        "not signed commit",
 | 
						|
		},
 | 
						|
		{
 | 
						|
			Sha1:           "205ac761f3326a7ebe416e8673760016450b5cec",
 | 
						|
			CommitterEmail: "user2@example.com",
 | 
						|
			CommitterName:  "User2",
 | 
						|
			AuthorEmail:    "user2@example.com",
 | 
						|
			AuthorName:     "User2",
 | 
						|
			Message:        "good signed commit (with not yet validated email)",
 | 
						|
		},
 | 
						|
		{
 | 
						|
			Sha1:           "1032bbf17fbc0d9c95bb5418dabe8f8c99278700",
 | 
						|
			CommitterEmail: "user2@example.com",
 | 
						|
			CommitterName:  "User2",
 | 
						|
			AuthorEmail:    "user2@example.com",
 | 
						|
			AuthorName:     "User2",
 | 
						|
			Message:        "good signed commit",
 | 
						|
		},
 | 
						|
	}
 | 
						|
	pushCommits.HeadCommit = &repository.PushCommit{Sha1: "2c54faec6c45d31c1abfaecdab471eac6633738a"}
 | 
						|
	return pushCommits
 | 
						|
}
 | 
						|
 | 
						|
func TestSyncPushCommits(t *testing.T) {
 | 
						|
	defer unittest.OverrideFixtures(
 | 
						|
		unittest.FixturesOptions{
 | 
						|
			Dir:  filepath.Join(setting.AppWorkPath, "models/fixtures/"),
 | 
						|
			Base: setting.AppWorkPath,
 | 
						|
			Dirs: []string{"services/webhook/TestPushCommits"},
 | 
						|
		},
 | 
						|
	)()
 | 
						|
	require.NoError(t, unittest.PrepareTestDatabase())
 | 
						|
 | 
						|
	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
 | 
						|
	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2, OwnerID: user.ID})
 | 
						|
 | 
						|
	t.Run("All commits", func(t *testing.T) {
 | 
						|
		defer test.MockVariableValue(&setting.Webhook.PayloadCommitLimit, 10)()
 | 
						|
 | 
						|
		NewNotifier().SyncPushCommits(db.DefaultContext, user, repo, &repository.PushUpdateOptions{RefFullName: git.RefNameFromBranch("master-1")}, pushCommits())
 | 
						|
 | 
						|
		hookTask := unittest.AssertExistsAndLoadBean(t, &webhook_model.HookTask{}, unittest.Cond("payload_content LIKE '%master-1%'"))
 | 
						|
 | 
						|
		var payloadContent structs.PushPayload
 | 
						|
		require.NoError(t, json.Unmarshal([]byte(hookTask.PayloadContent), &payloadContent))
 | 
						|
		assert.Len(t, payloadContent.Commits, 3)
 | 
						|
	})
 | 
						|
 | 
						|
	t.Run("Only one commit", func(t *testing.T) {
 | 
						|
		defer test.MockVariableValue(&setting.Webhook.PayloadCommitLimit, 1)()
 | 
						|
 | 
						|
		NewNotifier().SyncPushCommits(db.DefaultContext, user, repo, &repository.PushUpdateOptions{RefFullName: git.RefNameFromBranch("main-1")}, pushCommits())
 | 
						|
 | 
						|
		hookTask := unittest.AssertExistsAndLoadBean(t, &webhook_model.HookTask{}, unittest.Cond("payload_content LIKE '%main-1%'"))
 | 
						|
 | 
						|
		var payloadContent structs.PushPayload
 | 
						|
		require.NoError(t, json.Unmarshal([]byte(hookTask.PayloadContent), &payloadContent))
 | 
						|
		assert.Len(t, payloadContent.Commits, 1)
 | 
						|
		assert.EqualValues(t, "2c54faec6c45d31c1abfaecdab471eac6633738a", payloadContent.Commits[0].ID)
 | 
						|
	})
 | 
						|
}
 | 
						|
 | 
						|
func TestPushCommits(t *testing.T) {
 | 
						|
	defer unittest.OverrideFixtures(
 | 
						|
		unittest.FixturesOptions{
 | 
						|
			Dir:  filepath.Join(setting.AppWorkPath, "models/fixtures/"),
 | 
						|
			Base: setting.AppWorkPath,
 | 
						|
			Dirs: []string{"services/webhook/TestPushCommits"},
 | 
						|
		},
 | 
						|
	)()
 | 
						|
	require.NoError(t, unittest.PrepareTestDatabase())
 | 
						|
 | 
						|
	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
 | 
						|
	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2, OwnerID: user.ID})
 | 
						|
 | 
						|
	t.Run("All commits", func(t *testing.T) {
 | 
						|
		defer test.MockVariableValue(&setting.Webhook.PayloadCommitLimit, 10)()
 | 
						|
 | 
						|
		NewNotifier().PushCommits(db.DefaultContext, user, repo, &repository.PushUpdateOptions{RefFullName: git.RefNameFromBranch("master-2")}, pushCommits())
 | 
						|
 | 
						|
		hookTask := unittest.AssertExistsAndLoadBean(t, &webhook_model.HookTask{}, unittest.Cond("payload_content LIKE '%master-2%'"))
 | 
						|
 | 
						|
		var payloadContent structs.PushPayload
 | 
						|
		require.NoError(t, json.Unmarshal([]byte(hookTask.PayloadContent), &payloadContent))
 | 
						|
		assert.Len(t, payloadContent.Commits, 3)
 | 
						|
	})
 | 
						|
 | 
						|
	t.Run("Only one commit", func(t *testing.T) {
 | 
						|
		defer test.MockVariableValue(&setting.Webhook.PayloadCommitLimit, 1)()
 | 
						|
 | 
						|
		NewNotifier().PushCommits(db.DefaultContext, user, repo, &repository.PushUpdateOptions{RefFullName: git.RefNameFromBranch("main-2")}, pushCommits())
 | 
						|
 | 
						|
		hookTask := unittest.AssertExistsAndLoadBean(t, &webhook_model.HookTask{}, unittest.Cond("payload_content LIKE '%main-2%'"))
 | 
						|
 | 
						|
		var payloadContent structs.PushPayload
 | 
						|
		require.NoError(t, json.Unmarshal([]byte(hookTask.PayloadContent), &payloadContent))
 | 
						|
		assert.Len(t, payloadContent.Commits, 1)
 | 
						|
		assert.EqualValues(t, "2c54faec6c45d31c1abfaecdab471eac6633738a", payloadContent.Commits[0].ID)
 | 
						|
	})
 | 
						|
}
 |