Skip to content

Commit 002b597

Browse files
richmahnzeripath
authored andcommitted
Fixes #7152 - Allow create/update/delete message to be empty, use default message (#7324)
* Fixes #7152 - Allow create/update/delete message to be empty, use default message * Linting fix * Fix to delete integration tests
1 parent 462284e commit 002b597

File tree

7 files changed

+104
-27
lines changed

7 files changed

+104
-27
lines changed

integrations/api_repo_file_create_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func getCreateFileOptions() api.CreateFileOptions {
2828
FileOptions: api.FileOptions{
2929
BranchName: "master",
3030
NewBranchName: "master",
31-
Message: "Creates new/file.txt",
31+
Message: "Making this new file new/file.txt",
3232
Author: api.Identity{
3333
Name: "John Doe",
3434
@@ -150,6 +150,19 @@ func TestAPICreateFile(t *testing.T) {
150150
assert.EqualValues(t, expectedSHA, fileResponse.Content.SHA)
151151
assert.EqualValues(t, expectedHTMLURL, fileResponse.Content.HTMLURL)
152152
assert.EqualValues(t, expectedDownloadURL, fileResponse.Content.DownloadURL)
153+
assert.EqualValues(t, createFileOptions.Message+"\n", fileResponse.Commit.Message)
154+
155+
// Test creating a file without a message
156+
createFileOptions = getCreateFileOptions()
157+
createFileOptions.Message = ""
158+
fileID++
159+
treePath = fmt.Sprintf("new/file%d.txt", fileID)
160+
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
161+
req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
162+
resp = session.MakeRequest(t, req, http.StatusCreated)
163+
DecodeJSON(t, resp, &fileResponse)
164+
expectedMessage := "Add '" + treePath + "'\n"
165+
assert.EqualValues(t, expectedMessage, fileResponse.Commit.Message)
153166

154167
// Test trying to create a file that already exists, should fail
155168
createFileOptions = getCreateFileOptions()

integrations/api_repo_file_delete_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func getDeleteFileOptions() *api.DeleteFileOptions {
2323
FileOptions: api.FileOptions{
2424
BranchName: "master",
2525
NewBranchName: "master",
26-
Message: "Updates new/file.txt",
26+
Message: "Removing the file new/file.txt",
2727
Author: api.Identity{
2828
Name: "John Doe",
2929
@@ -89,6 +89,20 @@ func TestAPIDeleteFile(t *testing.T) {
8989
DecodeJSON(t, resp, &fileResponse)
9090
assert.NotNil(t, fileResponse)
9191
assert.Nil(t, fileResponse.Content)
92+
assert.EqualValues(t, deleteFileOptions.Message+"\n", fileResponse.Commit.Message)
93+
94+
// Test deleting file without a message
95+
fileID++
96+
treePath = fmt.Sprintf("delete/file%d.txt", fileID)
97+
createFile(user2, repo1, treePath)
98+
deleteFileOptions = getDeleteFileOptions()
99+
deleteFileOptions.Message = ""
100+
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
101+
req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
102+
resp = session.MakeRequest(t, req, http.StatusOK)
103+
DecodeJSON(t, resp, &fileResponse)
104+
expectedMessage := "Delete '" + treePath + "'\n"
105+
assert.EqualValues(t, expectedMessage, fileResponse.Commit.Message)
92106

93107
// Test deleting a file with the wrong SHA
94108
fileID++

integrations/api_repo_file_update_test.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,23 @@ func getUpdateFileOptions() *api.UpdateFileOptions {
2525
content := "This is updated text"
2626
contentEncoded := base64.StdEncoding.EncodeToString([]byte(content))
2727
return &api.UpdateFileOptions{
28-
DeleteFileOptions: *getDeleteFileOptions(),
29-
Content: contentEncoded,
28+
DeleteFileOptions: api.DeleteFileOptions{
29+
FileOptions: api.FileOptions{
30+
BranchName: "master",
31+
NewBranchName: "master",
32+
Message: "My update of new/file.txt",
33+
Author: api.Identity{
34+
Name: "John Doe",
35+
36+
},
37+
Committer: api.Identity{
38+
Name: "Jane Doe",
39+
40+
},
41+
},
42+
SHA: "103ff9234cefeee5ec5361d22b49fbb04d385885",
43+
},
44+
Content: contentEncoded,
3045
}
3146
}
3247

@@ -67,7 +82,7 @@ func getExpectedFileResponseForUpdate(commitID, treePath string) *api.FileRespon
6782
6883
},
6984
},
70-
Message: "Updates README.md\n",
85+
Message: "My update of README.md\n",
7186
},
7287
Verification: &api.PayloadCommitVerification{
7388
Verified: false,
@@ -140,6 +155,7 @@ func TestAPIUpdateFile(t *testing.T) {
140155
assert.EqualValues(t, expectedSHA, fileResponse.Content.SHA)
141156
assert.EqualValues(t, expectedHTMLURL, fileResponse.Content.HTMLURL)
142157
assert.EqualValues(t, expectedDownloadURL, fileResponse.Content.DownloadURL)
158+
assert.EqualValues(t, updateFileOptions.Message+"\n", fileResponse.Commit.Message)
143159

144160
// Test updating a file and renaming it
145161
updateFileOptions = getUpdateFileOptions()
@@ -160,6 +176,20 @@ func TestAPIUpdateFile(t *testing.T) {
160176
assert.EqualValues(t, expectedHTMLURL, fileResponse.Content.HTMLURL)
161177
assert.EqualValues(t, expectedDownloadURL, fileResponse.Content.DownloadURL)
162178

179+
// Test updating a file without a message
180+
updateFileOptions = getUpdateFileOptions()
181+
updateFileOptions.Message = ""
182+
updateFileOptions.BranchName = repo1.DefaultBranch
183+
fileID++
184+
treePath = fmt.Sprintf("update/file%d.txt", fileID)
185+
createFile(user2, repo1, treePath)
186+
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
187+
req = NewRequestWithJSON(t, "PUT", url, &updateFileOptions)
188+
resp = session.MakeRequest(t, req, http.StatusOK)
189+
DecodeJSON(t, resp, &fileResponse)
190+
expectedMessage := "Update '" + treePath + "'\n"
191+
assert.EqualValues(t, expectedMessage, fileResponse.Commit.Message)
192+
163193
// Test updating a file with the wrong SHA
164194
fileID++
165195
treePath = fmt.Sprintf("update/file%d.txt", fileID)

integrations/repofiles_delete_test.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,32 @@ func getDeleteRepoFileOptions(repo *models.Repository) *repofiles.DeleteRepoFile
2424
TreePath: "README.md",
2525
Message: "Deletes README.md",
2626
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
27-
Author: nil,
28-
Committer: nil,
27+
Author: &repofiles.IdentityOptions{
28+
Name: "Bob Smith",
29+
30+
},
31+
Committer: nil,
2932
}
3033
}
3134

3235
func getExpectedDeleteFileResponse(u *url.URL) *api.FileResponse {
36+
// Just returns fields that don't change, i.e. fields with commit SHAs and dates can't be determined
3337
return &api.FileResponse{
3438
Content: nil,
3539
Commit: &api.FileCommitResponse{
36-
CommitMeta: api.CommitMeta{
37-
URL: u.String() + "api/v1/repos/user2/repo1/git/commits/65f1bf27bc3bf70f64657658635e66094edbcb4d",
38-
SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
39-
},
40-
HTMLURL: u.String() + "user2/repo1/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d",
4140
Author: &api.CommitUser{
4241
Identity: api.Identity{
43-
Name: "user1",
44-
Email: "address1@example.com",
42+
Name: "Bob Smith",
43+
Email: "bob@smith.com",
4544
},
46-
Date: "2017-03-19T20:47:59Z",
4745
},
4846
Committer: &api.CommitUser{
4947
Identity: api.Identity{
50-
Name: "Ethan Koenig",
51-
Email: "ethantkoenig@gmail.com",
48+
Name: "Bob Smith",
49+
Email: "bob@smith.com",
5250
},
53-
Date: "2017-03-19T20:47:59Z",
54-
},
55-
Parents: []*api.CommitMeta{},
56-
Message: "Initial commit\n",
57-
Tree: &api.CommitMeta{
58-
URL: u.String() + "api/v1/repos/user2/repo1/git/trees/2a2f1d4670728a2e10049e345bd7a276468beab6",
59-
SHA: "2a2f1d4670728a2e10049e345bd7a276468beab6",
6051
},
52+
Message: "Deletes README.md\n",
6153
},
6254
Verification: &api.PayloadCommitVerification{
6355
Verified: false,
@@ -89,7 +81,12 @@ func testDeleteRepoFile(t *testing.T, u *url.URL) {
8981
fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
9082
assert.Nil(t, err)
9183
expectedFileResponse := getExpectedDeleteFileResponse(u)
92-
assert.EqualValues(t, expectedFileResponse, fileResponse)
84+
assert.NotNil(t, fileResponse)
85+
assert.Nil(t, fileResponse.Content)
86+
assert.EqualValues(t, expectedFileResponse.Commit.Message, fileResponse.Commit.Message)
87+
assert.EqualValues(t, expectedFileResponse.Commit.Author.Identity, fileResponse.Commit.Author.Identity)
88+
assert.EqualValues(t, expectedFileResponse.Commit.Committer.Identity, fileResponse.Commit.Committer.Identity)
89+
assert.EqualValues(t, expectedFileResponse.Verification, fileResponse.Verification)
9390
})
9491

9592
t.Run("Verify README.md has been deleted", func(t *testing.T) {
@@ -124,7 +121,12 @@ func testDeleteRepoFileWithoutBranchNames(t *testing.T, u *url.URL) {
124121
fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
125122
assert.Nil(t, err)
126123
expectedFileResponse := getExpectedDeleteFileResponse(u)
127-
assert.EqualValues(t, expectedFileResponse, fileResponse)
124+
assert.NotNil(t, fileResponse)
125+
assert.Nil(t, fileResponse.Content)
126+
assert.EqualValues(t, expectedFileResponse.Commit.Message, fileResponse.Commit.Message)
127+
assert.EqualValues(t, expectedFileResponse.Commit.Author.Identity, fileResponse.Commit.Author.Identity)
128+
assert.EqualValues(t, expectedFileResponse.Commit.Committer.Identity, fileResponse.Commit.Committer.Identity)
129+
assert.EqualValues(t, expectedFileResponse.Verification, fileResponse.Verification)
128130
})
129131
}
130132

modules/repofiles/delete.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ func DeleteRepoFile(repo *models.Repository, doer *models.User, opts *DeleteRepo
198198
return nil, fmt.Errorf("PushUpdate: %v", err)
199199
}
200200

201+
commit, err = t.GetCommit(commitHash)
202+
if err != nil {
203+
return nil, err
204+
}
205+
201206
file, err := GetFileResponseFromCommit(repo, commit, opts.NewBranch, treePath)
202207
if err != nil {
203208
return nil, err

modules/structs/repo_file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package structs
88
// FileOptions options for all file APIs
99
type FileOptions struct {
1010
// message (optional) for the commit of this file. if not supplied, a default message will be used
11-
Message string `json:"message" binding:"Required"`
11+
Message string `json:"message"`
1212
// branch (optional) to base this file from. if not given, the default branch is used
1313
BranchName string `json:"branch"`
1414
// new_branch (optional) will make a new branch from `branch` before creating the file

routers/api/v1/repo/file.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ func CreateFile(ctx *context.APIContext, apiOpts api.CreateFileOptions) {
204204
Email: apiOpts.Author.Email,
205205
},
206206
}
207+
208+
if opts.Message == "" {
209+
opts.Message = ctx.Tr("repo.editor.add", opts.TreePath)
210+
}
211+
207212
if fileResponse, err := createOrUpdateFile(ctx, opts); err != nil {
208213
ctx.Error(http.StatusInternalServerError, "CreateFile", err)
209214
} else {
@@ -264,6 +269,10 @@ func UpdateFile(ctx *context.APIContext, apiOpts api.UpdateFileOptions) {
264269
},
265270
}
266271

272+
if opts.Message == "" {
273+
opts.Message = ctx.Tr("repo.editor.update", opts.TreePath)
274+
}
275+
267276
if fileResponse, err := createOrUpdateFile(ctx, opts); err != nil {
268277
ctx.Error(http.StatusInternalServerError, "UpdateFile", err)
269278
} else {
@@ -346,6 +355,10 @@ func DeleteFile(ctx *context.APIContext, apiOpts api.DeleteFileOptions) {
346355
},
347356
}
348357

358+
if opts.Message == "" {
359+
opts.Message = ctx.Tr("repo.editor.delete", opts.TreePath)
360+
}
361+
349362
if fileResponse, err := repofiles.DeleteRepoFile(ctx.Repo.Repository, ctx.User, opts); err != nil {
350363
ctx.Error(http.StatusInternalServerError, "DeleteFile", err)
351364
} else {

0 commit comments

Comments
 (0)