@@ -19,6 +19,7 @@ import (
1919 auth_model "code.gitea.io/gitea/models/auth"
2020 git_model "code.gitea.io/gitea/models/git"
2121 issues_model "code.gitea.io/gitea/models/issues"
22+ "code.gitea.io/gitea/models/perm"
2223 pull_model "code.gitea.io/gitea/models/pull"
2324 repo_model "code.gitea.io/gitea/models/repo"
2425 "code.gitea.io/gitea/models/unittest"
@@ -1180,3 +1181,181 @@ func TestPullNonMergeForAdminWithBranchProtection(t *testing.T) {
11801181 session .MakeRequest (t , mergeReq , http .StatusMethodNotAllowed )
11811182 })
11821183}
1184+
1185+ func TestPullSquashMessage (t * testing.T ) {
1186+ onGiteaRun (t , func (t * testing.T , giteaURL * url.URL ) {
1187+ user2 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
1188+ user2Session := loginUser (t , user2 .Name )
1189+ user4 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 4 })
1190+ user4Session := loginUser (t , user4 .Name )
1191+
1192+ sessions := map [string ]* TestSession {
1193+ user2 .Name : user2Session ,
1194+ user4 .Name : user4Session ,
1195+ }
1196+
1197+ // Enable POPULATE_SQUASH_COMMENT_WITH_COMMIT_MESSAGES
1198+ resetFunc1 := test .MockVariableValue (& setting .Repository .PullRequest .PopulateSquashCommentWithCommitMessages , true )
1199+ defer resetFunc1 ()
1200+ // Set DEFAULT_MERGE_MESSAGE_SIZE
1201+ resetFunc2 := test .MockVariableValue (& setting .Repository .PullRequest .DefaultMergeMessageSize , 512 )
1202+ defer resetFunc2 ()
1203+
1204+ repo , err := repo_service .CreateRepository (t .Context (), user2 , user2 , repo_service.CreateRepoOptions {
1205+ Name : "squash-message-test" ,
1206+ Description : "Test squash message" ,
1207+ AutoInit : true ,
1208+ Readme : "Default" ,
1209+ DefaultBranch : "main" ,
1210+ })
1211+ assert .NoError (t , err )
1212+ doAPIAddCollaborator (NewAPITestContext (t , repo .OwnerName , repo .Name , auth_model .AccessTokenScopeWriteRepository ), user4 .Name , perm .AccessModeWrite )(t )
1213+
1214+ type commitInfo struct {
1215+ userName string
1216+ commitSummary string
1217+ commitMessage string
1218+ }
1219+
1220+ testCases := []struct {
1221+ name string
1222+ commitInfos []* commitInfo
1223+ expectedMessage string
1224+ }{
1225+ {
1226+ name : "Only summaries" ,
1227+ commitInfos : []* commitInfo {
1228+ {
1229+ userName : user2 .Name ,
1230+ commitSummary : "Implement the login endpoint" ,
1231+ },
1232+ {
1233+ userName : user2 .Name ,
1234+ commitSummary : "Validate request body" ,
1235+ },
1236+ },
1237+ expectedMessage : `* Implement the login endpoint
1238+
1239+ * Validate request body
1240+
1241+ ` ,
1242+ },
1243+ {
1244+ name : "Summaries and messages" ,
1245+ commitInfos : []* commitInfo {
1246+ {
1247+ userName : user2 .Name ,
1248+ commitSummary : "Refactor user service" ,
1249+ commitMessage : `Implement the login endpoint.
1250+ Validate request body.` ,
1251+ },
1252+ {
1253+ userName : user2 .Name ,
1254+ commitSummary : "Add email notification service" ,
1255+ commitMessage : `Implements a new email notification module.
1256+
1257+ - Supports templating
1258+ - Supports HTML and plain text modes
1259+ - Includes retry logic` ,
1260+ },
1261+ },
1262+ expectedMessage : `* Refactor user service
1263+
1264+ Implement the login endpoint.
1265+ Validate request body.
1266+
1267+ * Add email notification service
1268+
1269+ Implements a new email notification module.
1270+
1271+ - Supports templating
1272+ - Supports HTML and plain text modes
1273+ - Includes retry logic
1274+
1275+ ` ,
1276+ },
1277+ {
1278+ name : "Long Message" ,
1279+ commitInfos : []* commitInfo {
1280+ {
1281+ userName : user2 .Name ,
1282+ commitSummary : "Add advanced validation logic for user onboarding" ,
1283+ commitMessage : `This commit introduces a comprehensive validation layer for the user onboarding flow.
1284+ The primary goal is to ensure that all input data is strictly validated before being processed by downstream services.
1285+ This improves system reliability and significantly reduces runtime exceptions in the registration pipeline.
1286+
1287+ The validation logic includes:
1288+
1289+ 1. Email format checking using RFC 5322-compliant patterns.
1290+ 2. Username length and character limitation enforcement.
1291+ 3. Password strength enforcement, including:
1292+ - Minimum length checks
1293+ - Mixed character type detection
1294+ - Optional entropy-based scoring
1295+ 4. Optional phone number validation using region-specific rules.
1296+ ` ,
1297+ },
1298+ },
1299+ expectedMessage : `* Add advanced validation logic for user onboarding
1300+
1301+ This commit introduces a comprehensive validation layer for the user onboarding flow.
1302+ The primary goal is to ensure that all input data is strictly validated before being processed by downstream services.
1303+ This improves system reliability and significantly reduces runtime exceptions in the registration pipeline.
1304+
1305+ The validation logic includes:
1306+
1307+ 1. Email format checking using RFC 5322-compliant patterns.
1308+ 2. Username length and character limitation enfor...` ,
1309+ },
1310+ {
1311+ name : "Test Co-authored-by" ,
1312+ commitInfos : []* commitInfo {
1313+ {
1314+ userName : user2 .Name ,
1315+ commitSummary : "Implement the login endpoint" ,
1316+ },
1317+ {
1318+ userName : user4 .Name ,
1319+ commitSummary : "Validate request body" ,
1320+ },
1321+ },
1322+ expectedMessage : `* Implement the login endpoint
1323+
1324+ * Validate request body
1325+
1326+ ---------
1327+
1328+ Co-authored-by: user4 <[email protected] > 1329+ ` ,
1330+ },
1331+ }
1332+
1333+ for tcNum , tc := range testCases {
1334+ branchName := "test-branch-" + strconv .Itoa (tcNum )
1335+ fileName := fmt .Sprintf ("test-file-%d.txt" , tcNum )
1336+ t .Run (tc .name , func (t * testing.T ) {
1337+ for infoIdx , info := range tc .commitInfos {
1338+ content := "content-" + strconv .Itoa (infoIdx )
1339+ if infoIdx == 0 {
1340+ testCreateFileWithCommitMessage (t , sessions [info .userName ], repo .OwnerName , repo .Name , repo .DefaultBranch , branchName , fileName , content , info .commitSummary , info .commitMessage )
1341+ } else {
1342+ testEditFileWithCommitMessage (t , sessions [info .userName ], repo .OwnerName , repo .Name , branchName , fileName , content , info .commitSummary , info .commitMessage )
1343+ }
1344+ }
1345+ resp := testPullCreateDirectly (t , user2Session , createPullRequestOptions {
1346+ BaseRepoOwner : user2 .Name ,
1347+ BaseRepoName : repo .Name ,
1348+ BaseBranch : repo .DefaultBranch ,
1349+ HeadBranch : branchName ,
1350+ Title : "Pull for " + branchName ,
1351+ })
1352+ elems := strings .Split (test .RedirectURL (resp ), "/" )
1353+ pullIndex , err := strconv .Atoi (elems [4 ])
1354+ assert .NoError (t , err )
1355+ pullRequest := unittest .AssertExistsAndLoadBean (t , & issues_model.PullRequest {BaseRepoID : repo .ID , Index : int64 (pullIndex )})
1356+ squashMergeCommitMessage := pull_service .GetSquashMergeCommitMessages (t .Context (), pullRequest )
1357+ assert .Equal (t , tc .expectedMessage , squashMergeCommitMessage )
1358+ })
1359+ }
1360+ })
1361+ }
0 commit comments