Skip to content

Commit 2cfeb0b

Browse files
committed
github-style format
1 parent adece92 commit 2cfeb0b

File tree

3 files changed

+214
-14
lines changed

3 files changed

+214
-14
lines changed

services/pull/pull.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,8 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequ
837837
authors := make([]string, 0, len(commits))
838838
stringBuilder := strings.Builder{}
839839

840-
if !setting.Repository.PullRequest.PopulateSquashCommentWithCommitMessages {
840+
populateWithCommits := setting.Repository.PullRequest.PopulateSquashCommentWithCommitMessages
841+
if !populateWithCommits {
841842
message := strings.TrimSpace(pr.Issue.Content)
842843
stringBuilder.WriteString(message)
843844
if stringBuilder.Len() > 0 {
@@ -849,33 +850,25 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequ
849850
}
850851

851852
// commits list is in reverse chronological order
852-
first := true
853853
for i := len(commits) - 1; i >= 0; i-- {
854854
commit := commits[i]
855855

856-
if setting.Repository.PullRequest.PopulateSquashCommentWithCommitMessages {
856+
if populateWithCommits {
857857
maxSize := setting.Repository.PullRequest.DefaultMergeMessageSize
858858
if maxSize < 0 || stringBuilder.Len() < maxSize {
859-
var toWrite []byte
860-
if first {
861-
first = false
862-
toWrite = []byte(strings.TrimPrefix(commit.CommitMessage, pr.Issue.Title))
863-
} else {
864-
toWrite = []byte(commit.CommitMessage)
859+
if strings.TrimSpace(commit.CommitMessage) == "" {
860+
continue
865861
}
866862

863+
toWrite := fmt.Appendf(nil, "* %s\n", commit.CommitMessage)
864+
867865
if len(toWrite) > maxSize-stringBuilder.Len() && maxSize > -1 {
868866
toWrite = append(toWrite[:maxSize-stringBuilder.Len()], "..."...)
869867
}
870868
if _, err := stringBuilder.Write(toWrite); err != nil {
871869
log.Error("Unable to write commit message Error: %v", err)
872870
return ""
873871
}
874-
875-
if _, err := stringBuilder.WriteRune('\n'); err != nil {
876-
log.Error("Unable to write commit message Error: %v", err)
877-
return ""
878-
}
879872
}
880873
}
881874

@@ -916,6 +909,10 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequ
916909
}
917910
}
918911

912+
if populateWithCommits && stringBuilder.Len() > 0 && len(authors) > 0 {
913+
stringBuilder.WriteString("---------\n\n")
914+
}
915+
919916
for _, author := range authors {
920917
if _, err := stringBuilder.WriteString("Co-authored-by: "); err != nil {
921918
log.Error("Unable to write to string builder Error: %v", err)

tests/integration/editor_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,21 @@ func testCreateFile(t *testing.T, session *TestSession, user, repo, baseBranchNa
8383
})
8484
}
8585

86+
func testCreateFileWithCommitMessage(t *testing.T, session *TestSession, user, repo, baseBranchName, newBranchName, filePath, content, commitSummary, commitMessage string) {
87+
commitChoice := "direct"
88+
if newBranchName != "" && newBranchName != baseBranchName {
89+
commitChoice = "commit-to-new-branch"
90+
}
91+
testEditorActionEdit(t, session, user, repo, "_new", baseBranchName, "", map[string]string{
92+
"tree_path": filePath,
93+
"content": content,
94+
"commit_choice": commitChoice,
95+
"new_branch_name": newBranchName,
96+
"commit_summary": commitSummary,
97+
"commit_message": commitMessage,
98+
})
99+
}
100+
86101
func testEditorProtectedBranch(t *testing.T) {
87102
session := loginUser(t, "user2")
88103
// Change the "master" branch to "protected"
@@ -139,6 +154,15 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa
139154
})
140155
}
141156

157+
func testEditFileWithCommitMessage(t *testing.T, session *TestSession, user, repo, branch, filePath, newContent, commitSummary, commitMessage string) {
158+
testEditorActionEdit(t, session, user, repo, "_edit", branch, filePath, map[string]string{
159+
"content": newContent,
160+
"commit_choice": "direct",
161+
"commit_summary": commitSummary,
162+
"commit_message": commitMessage,
163+
})
164+
}
165+
142166
func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, branch, targetBranch, filePath, newContent string) {
143167
testEditorActionEdit(t, session, user, repo, "_edit", branch, filePath, map[string]string{
144168
"content": newContent,

tests/integration/pull_merge_test.go

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)