Support choose email when creating a commit via web UI (more) (#33445)

Follow #33432
This commit is contained in:
wxiaoguang 2025-01-31 10:36:18 +08:00 committed by GitHub
parent b57d9f41d4
commit 5a7b42dac7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 228 additions and 176 deletions

View File

@ -1694,19 +1694,6 @@
is_fsck_enabled: true is_fsck_enabled: true
close_issues_via_commit_in_any_branch: false close_issues_via_commit_in_any_branch: false
-
id: 59
owner_id: 2
owner_name: user2
lower_name: test_commit_revert
name: test_commit_revert
default_branch: main
is_empty: false
is_archived: false
is_private: true
status: 0
num_issues: 0
- -
id: 60 id: 60
owner_id: 40 owner_id: 40

View File

@ -67,7 +67,7 @@
num_followers: 2 num_followers: 2
num_following: 1 num_following: 1
num_stars: 2 num_stars: 2
num_repos: 15 num_repos: 14
num_teams: 0 num_teams: 0
num_members: 0 num_members: 0
visibility: 0 visibility: 0

View File

@ -114,11 +114,19 @@ func CherryPickPost(ctx *context.Context) {
message += "\n\n" + form.CommitMessage message += "\n\n" + form.CommitMessage
} }
gitCommitter, valid := WebGitOperationGetCommitChosenEmailIdentity(ctx, form.CommitEmail)
if !valid {
ctx.Data["Err_CommitEmail"] = true
ctx.RenderWithErr(ctx.Tr("repo.editor.invalid_commit_email"), tplCherryPick, &form)
return
}
opts := &files.ApplyDiffPatchOptions{ opts := &files.ApplyDiffPatchOptions{
LastCommitID: form.LastCommit, LastCommitID: form.LastCommit,
OldBranch: ctx.Repo.BranchName, OldBranch: ctx.Repo.BranchName,
NewBranch: branchName, NewBranch: branchName,
Message: message, Message: message,
Author: gitCommitter,
Committer: gitCommitter,
} }
// First lets try the simple plain read-tree -m approach // First lets try the simple plain read-tree -m approach

View File

@ -13,7 +13,6 @@ import (
git_model "code.gitea.io/gitea/models/git" git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/charset"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/json"
@ -103,18 +102,6 @@ func getParentTreeFields(treePath string) (treeNames, treePaths []string) {
return treeNames, treePaths return treeNames, treePaths
} }
func getCandidateEmailAddresses(ctx *context.Context) []string {
emails, err := user_model.GetActivatedEmailAddresses(ctx, ctx.Doer.ID)
if err != nil {
log.Error("getCandidateEmailAddresses: GetActivatedEmailAddresses: %v", err)
}
if ctx.Doer.KeepEmailPrivate {
emails = append([]string{ctx.Doer.GetPlaceholderEmail()}, emails...)
}
return emails
}
func editFileCommon(ctx *context.Context, isNewFile bool) { func editFileCommon(ctx *context.Context, isNewFile bool) {
ctx.Data["PageIsEdit"] = true ctx.Data["PageIsEdit"] = true
ctx.Data["IsNewFile"] = isNewFile ctx.Data["IsNewFile"] = isNewFile
@ -123,8 +110,6 @@ func editFileCommon(ctx *context.Context, isNewFile bool) {
ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",") ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
ctx.Data["IsEditingFileOnly"] = ctx.FormString("return_uri") != "" ctx.Data["IsEditingFileOnly"] = ctx.FormString("return_uri") != ""
ctx.Data["ReturnURI"] = ctx.FormString("return_uri") ctx.Data["ReturnURI"] = ctx.FormString("return_uri")
ctx.Data["CommitCandidateEmails"] = getCandidateEmailAddresses(ctx)
ctx.Data["CommitDefaultEmail"] = ctx.Doer.GetEmail()
} }
func editFile(ctx *context.Context, isNewFile bool) { func editFile(ctx *context.Context, isNewFile bool) {
@ -287,15 +272,11 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
message += "\n\n" + form.CommitMessage message += "\n\n" + form.CommitMessage
} }
gitCommitter := &files_service.IdentityOptions{} gitCommitter, valid := WebGitOperationGetCommitChosenEmailIdentity(ctx, form.CommitEmail)
if form.CommitEmail != "" { if !valid {
if util.SliceContainsString(getCandidateEmailAddresses(ctx), form.CommitEmail, true) { ctx.Data["Err_CommitEmail"] = true
gitCommitter.GitUserEmail = form.CommitEmail ctx.RenderWithErr(ctx.Tr("repo.editor.invalid_commit_email"), tplEditFile, &form)
} else { return
ctx.Data["Err_CommitEmail"] = true
ctx.RenderWithErr(ctx.Tr("repo.editor.invalid_commit_email"), tplEditFile, &form)
return
}
} }
operation := "update" operation := "update"
@ -515,6 +496,13 @@ func DeleteFilePost(ctx *context.Context) {
message += "\n\n" + form.CommitMessage message += "\n\n" + form.CommitMessage
} }
gitCommitter, valid := WebGitOperationGetCommitChosenEmailIdentity(ctx, form.CommitEmail)
if !valid {
ctx.Data["Err_CommitEmail"] = true
ctx.RenderWithErr(ctx.Tr("repo.editor.invalid_commit_email"), tplDeleteFile, &form)
return
}
if _, err := files_service.ChangeRepoFiles(ctx, ctx.Repo.Repository, ctx.Doer, &files_service.ChangeRepoFilesOptions{ if _, err := files_service.ChangeRepoFiles(ctx, ctx.Repo.Repository, ctx.Doer, &files_service.ChangeRepoFilesOptions{
LastCommitID: form.LastCommit, LastCommitID: form.LastCommit,
OldBranch: ctx.Repo.BranchName, OldBranch: ctx.Repo.BranchName,
@ -525,8 +513,10 @@ func DeleteFilePost(ctx *context.Context) {
TreePath: ctx.Repo.TreePath, TreePath: ctx.Repo.TreePath,
}, },
}, },
Message: message, Message: message,
Signoff: form.Signoff, Signoff: form.Signoff,
Author: gitCommitter,
Committer: gitCommitter,
}); err != nil { }); err != nil {
// This is where we handle all the errors thrown by repofiles.DeleteRepoFile // This is where we handle all the errors thrown by repofiles.DeleteRepoFile
if git.IsErrNotExist(err) || files_service.IsErrRepoFileDoesNotExist(err) { if git.IsErrNotExist(err) || files_service.IsErrRepoFileDoesNotExist(err) {
@ -726,6 +716,13 @@ func UploadFilePost(ctx *context.Context) {
message += "\n\n" + form.CommitMessage message += "\n\n" + form.CommitMessage
} }
gitCommitter, valid := WebGitOperationGetCommitChosenEmailIdentity(ctx, form.CommitEmail)
if !valid {
ctx.Data["Err_CommitEmail"] = true
ctx.RenderWithErr(ctx.Tr("repo.editor.invalid_commit_email"), tplUploadFile, &form)
return
}
if err := files_service.UploadRepoFiles(ctx, ctx.Repo.Repository, ctx.Doer, &files_service.UploadRepoFileOptions{ if err := files_service.UploadRepoFiles(ctx, ctx.Repo.Repository, ctx.Doer, &files_service.UploadRepoFileOptions{
LastCommitID: ctx.Repo.CommitID, LastCommitID: ctx.Repo.CommitID,
OldBranch: oldBranchName, OldBranch: oldBranchName,
@ -734,6 +731,8 @@ func UploadFilePost(ctx *context.Context) {
Message: message, Message: message,
Files: form.Files, Files: form.Files,
Signoff: form.Signoff, Signoff: form.Signoff,
Author: gitCommitter,
Committer: gitCommitter,
}); err != nil { }); err != nil {
if git_model.IsErrLFSFileLocked(err) { if git_model.IsErrLFSFileLocked(err) {
ctx.Data["Err_TreePath"] = true ctx.Data["Err_TreePath"] = true

View File

@ -66,7 +66,7 @@ func NewDiffPatchPost(ctx *context.Context) {
return return
} }
// Cannot commit to a an existing branch if user doesn't have rights // Cannot commit to an existing branch if user doesn't have rights
if branchName == ctx.Repo.BranchName && !canCommit { if branchName == ctx.Repo.BranchName && !canCommit {
ctx.Data["Err_NewBranchName"] = true ctx.Data["Err_NewBranchName"] = true
ctx.Data["commit_choice"] = frmCommitChoiceNewBranch ctx.Data["commit_choice"] = frmCommitChoiceNewBranch
@ -86,12 +86,21 @@ func NewDiffPatchPost(ctx *context.Context) {
message += "\n\n" + form.CommitMessage message += "\n\n" + form.CommitMessage
} }
gitCommitter, valid := WebGitOperationGetCommitChosenEmailIdentity(ctx, form.CommitEmail)
if !valid {
ctx.Data["Err_CommitEmail"] = true
ctx.RenderWithErr(ctx.Tr("repo.editor.invalid_commit_email"), tplPatchFile, &form)
return
}
fileResponse, err := files.ApplyDiffPatch(ctx, ctx.Repo.Repository, ctx.Doer, &files.ApplyDiffPatchOptions{ fileResponse, err := files.ApplyDiffPatch(ctx, ctx.Repo.Repository, ctx.Doer, &files.ApplyDiffPatchOptions{
LastCommitID: form.LastCommit, LastCommitID: form.LastCommit,
OldBranch: ctx.Repo.BranchName, OldBranch: ctx.Repo.BranchName,
NewBranch: branchName, NewBranch: branchName,
Message: message, Message: message,
Content: strings.ReplaceAll(form.Content, "\r", ""), Content: strings.ReplaceAll(form.Content, "\r", ""),
Author: gitCommitter,
Committer: gitCommitter,
}) })
if err != nil { if err != nil {
if git_model.IsErrBranchAlreadyExists(err) { if git_model.IsErrBranchAlreadyExists(err) {

View File

@ -0,0 +1,40 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repo
import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/services/context"
files_service "code.gitea.io/gitea/services/repository/files"
)
func WebGitOperationCommonData(ctx *context.Context) {
// TODO: more places like "wiki page" and "merging a pull request or creating an auto merge merging task"
emails, err := user_model.GetActivatedEmailAddresses(ctx, ctx.Doer.ID)
if err != nil {
log.Error("WebGitOperationCommonData: GetActivatedEmailAddresses: %v", err)
}
if ctx.Doer.KeepEmailPrivate {
emails = append([]string{ctx.Doer.GetPlaceholderEmail()}, emails...)
}
ctx.Data["CommitCandidateEmails"] = emails
ctx.Data["CommitDefaultEmail"] = ctx.Doer.GetEmail()
}
func WebGitOperationGetCommitChosenEmailIdentity(ctx *context.Context, email string) (_ *files_service.IdentityOptions, valid bool) {
if ctx.Data["CommitCandidateEmails"] == nil {
setting.PanicInDevOrTesting("no CommitCandidateEmails in context data")
}
emails, _ := ctx.Data["CommitCandidateEmails"].([]string)
if email == "" {
return nil, true
}
if util.SliceContainsString(emails, email, true) {
return &files_service.IdentityOptions{GitUserEmail: email}, true
}
return nil, false
}

View File

@ -1294,21 +1294,20 @@ func registerRoutes(m *web.Router) {
m.Group("/{username}/{reponame}", func() { // repo code m.Group("/{username}/{reponame}", func() { // repo code
m.Group("", func() { m.Group("", func() {
m.Group("", func() { m.Group("", func() {
m.Post("/_preview/*", web.Bind(forms.EditPreviewDiffForm{}), repo.DiffPreviewPost)
m.Combo("/_edit/*").Get(repo.EditFile). m.Combo("/_edit/*").Get(repo.EditFile).
Post(web.Bind(forms.EditRepoFileForm{}), repo.EditFilePost) Post(web.Bind(forms.EditRepoFileForm{}), repo.EditFilePost)
m.Combo("/_new/*").Get(repo.NewFile). m.Combo("/_new/*").Get(repo.NewFile).
Post(web.Bind(forms.EditRepoFileForm{}), repo.NewFilePost) Post(web.Bind(forms.EditRepoFileForm{}), repo.NewFilePost)
m.Post("/_preview/*", web.Bind(forms.EditPreviewDiffForm{}), repo.DiffPreviewPost)
m.Combo("/_delete/*").Get(repo.DeleteFile). m.Combo("/_delete/*").Get(repo.DeleteFile).
Post(web.Bind(forms.DeleteRepoFileForm{}), repo.DeleteFilePost) Post(web.Bind(forms.DeleteRepoFileForm{}), repo.DeleteFilePost)
m.Combo("/_upload/*", repo.MustBeAbleToUpload). m.Combo("/_upload/*", repo.MustBeAbleToUpload).Get(repo.UploadFile).
Get(repo.UploadFile).
Post(web.Bind(forms.UploadRepoFileForm{}), repo.UploadFilePost) Post(web.Bind(forms.UploadRepoFileForm{}), repo.UploadFilePost)
m.Combo("/_diffpatch/*").Get(repo.NewDiffPatch). m.Combo("/_diffpatch/*").Get(repo.NewDiffPatch).
Post(web.Bind(forms.EditRepoFileForm{}), repo.NewDiffPatchPost) Post(web.Bind(forms.EditRepoFileForm{}), repo.NewDiffPatchPost)
m.Combo("/_cherrypick/{sha:([a-f0-9]{7,64})}/*").Get(repo.CherryPick). m.Combo("/_cherrypick/{sha:([a-f0-9]{7,64})}/*").Get(repo.CherryPick).
Post(web.Bind(forms.CherryPickForm{}), repo.CherryPickPost) Post(web.Bind(forms.CherryPickForm{}), repo.CherryPickPost)
}, context.RepoRefByType(git.RefTypeBranch), context.CanWriteToBranch()) }, context.RepoRefByType(git.RefTypeBranch), context.CanWriteToBranch(), repo.WebGitOperationCommonData)
m.Group("", func() { m.Group("", func() {
m.Post("/upload-file", repo.UploadFileToServer) m.Post("/upload-file", repo.UploadFileToServer)
m.Post("/upload-remove", web.Bind(forms.RemoveUploadFileForm{}), repo.RemoveUploadFileFromServer) m.Post("/upload-remove", web.Bind(forms.RemoveUploadFileForm{}), repo.RemoveUploadFileFromServer)

View File

@ -756,6 +756,7 @@ type CherryPickForm struct {
LastCommit string LastCommit string
Revert bool Revert bool
Signoff bool Signoff bool
CommitEmail string
} }
// Validate validates the fields // Validate validates the fields
@ -781,6 +782,7 @@ type UploadRepoFileForm struct {
NewBranchName string `binding:"GitRefName;MaxSize(100)"` NewBranchName string `binding:"GitRefName;MaxSize(100)"`
Files []string Files []string
Signoff bool Signoff bool
CommitEmail string
} }
// Validate validates the fields // Validate validates the fields
@ -815,6 +817,7 @@ type DeleteRepoFileForm struct {
NewBranchName string `binding:"GitRefName;MaxSize(100)"` NewBranchName string `binding:"GitRefName;MaxSize(100)"`
LastCommit string LastCommit string
Signoff bool Signoff bool
CommitEmail string
} }
// Validate validates the fields // Validate validates the fields

View File

@ -27,6 +27,8 @@ type UploadRepoFileOptions struct {
Message string Message string
Files []string // In UUID format. Files []string // In UUID format.
Signoff bool Signoff bool
Author *IdentityOptions
Committer *IdentityOptions
} }
type uploadInfo struct { type uploadInfo struct {
@ -130,11 +132,13 @@ func UploadRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
// Now commit the tree // Now commit the tree
commitOpts := &CommitTreeUserOptions{ commitOpts := &CommitTreeUserOptions{
ParentCommitID: opts.LastCommitID, ParentCommitID: opts.LastCommitID,
TreeHash: treeHash, TreeHash: treeHash,
CommitMessage: opts.Message, CommitMessage: opts.Message,
SignOff: opts.Signoff, SignOff: opts.Signoff,
DoerUser: doer, DoerUser: doer,
AuthorIdentity: opts.Author,
CommitterIdentity: opts.Committer,
} }
commitHash, err := t.CommitTree(commitOpts) commitHash, err := t.CommitTree(commitOpts)
if err != nil { if err != nil {

View File

@ -1 +0,0 @@
ref: refs/heads/main

View File

@ -1,8 +0,0 @@
[core]
repositoryformatversion = 0
filemode = true
bare = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://try.gitea.io/me-heer/test_commit_revert.git

View File

@ -1,3 +0,0 @@
# pack-refs with: peeled fully-peeled sorted
46aa6ab2c881ae90e15d9ccfc947d1625c892ce5 refs/heads/develop
deebcbc752e540bab4ce3ee713d3fc8fdc35b2f7 refs/heads/main

View File

@ -1 +0,0 @@
deebcbc752e540bab4ce3ee713d3fc8fdc35b2f7

View File

@ -4,7 +4,10 @@
package integration package integration
import ( import (
"bytes"
"fmt" "fmt"
"io"
"mime/multipart"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
@ -181,101 +184,155 @@ func TestEditFileToNewBranch(t *testing.T) {
}) })
} }
func TestEditFileCommitEmail(t *testing.T) { func TestWebGitCommitEmail(t *testing.T) {
onGiteaRun(t, func(t *testing.T, _ *url.URL) { onGiteaRun(t, func(t *testing.T, _ *url.URL) {
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
assert.True(t, user.KeepEmailPrivate) require.True(t, user.KeepEmailPrivate)
session := loginUser(t, user.Name) repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
link := "/user2/repo1/_edit/master/README.md" gitRepo, _ := git.OpenRepository(git.DefaultContext, repo1.RepoPath())
defer gitRepo.Close()
getLastCommitID := func(t *testing.T) string { getLastCommit := func(t *testing.T) *git.Commit {
req := NewRequest(t, "GET", link) c, err := gitRepo.GetBranchCommit("master")
resp := session.MakeRequest(t, req, http.StatusOK) require.NoError(t, err)
htmlDoc := NewHTMLParser(t, resp.Body) return c
lastCommit := htmlDoc.GetInputValueByName("last_commit")
require.NotEmpty(t, lastCommit)
return lastCommit
} }
newReq := func(t *testing.T, session *TestSession, email, content string) *RequestWrapper { session := loginUser(t, user.Name)
req := NewRequestWithValues(t, "POST", link, map[string]string{
"_csrf": GetUserCSRFToken(t, session), makeReq := func(t *testing.T, link string, params map[string]string, expectedUserName, expectedEmail string) *httptest.ResponseRecorder {
"last_commit": getLastCommitID(t), lastCommit := getLastCommit(t)
"tree_path": "README.md", params["_csrf"] = GetUserCSRFToken(t, session)
"content": content, params["last_commit"] = lastCommit.ID.String()
"commit_choice": "direct", params["commit_choice"] = "direct"
"commit_email": email, req := NewRequestWithValues(t, "POST", link, params)
}) resp := session.MakeRequest(t, req, NoExpectedStatus)
return req newCommit := getLastCommit(t)
if expectedUserName == "" {
require.Equal(t, lastCommit.ID.String(), newCommit.ID.String())
htmlDoc := NewHTMLParser(t, resp.Body)
errMsg := htmlDoc.doc.Find(".ui.negative.message").Text()
assert.Contains(t, errMsg, translation.NewLocale("en-US").Tr("repo.editor.invalid_commit_email"))
} else {
require.NotEqual(t, lastCommit.ID.String(), newCommit.ID.String())
assert.EqualValues(t, expectedUserName, newCommit.Author.Name)
assert.EqualValues(t, expectedEmail, newCommit.Author.Email)
assert.EqualValues(t, expectedUserName, newCommit.Committer.Name)
assert.EqualValues(t, expectedEmail, newCommit.Committer.Email)
}
return resp
}
uploadFile := func(t *testing.T, name, content string) string {
body := &bytes.Buffer{}
uploadForm := multipart.NewWriter(body)
file, _ := uploadForm.CreateFormFile("file", name)
_, _ = io.Copy(file, bytes.NewBufferString(content))
_ = uploadForm.WriteField("_csrf", GetUserCSRFToken(t, session))
_ = uploadForm.Close()
req := NewRequestWithBody(t, "POST", "/user2/repo1/upload-file", body)
req.Header.Add("Content-Type", uploadForm.FormDataContentType())
resp := session.MakeRequest(t, req, http.StatusOK)
respMap := map[string]string{}
DecodeJSON(t, resp, &respMap)
return respMap["uuid"]
} }
t.Run("EmailInactive", func(t *testing.T) { t.Run("EmailInactive", func(t *testing.T) {
defer tests.PrintCurrentTest(t)() defer tests.PrintCurrentTest(t)()
email := unittest.AssertExistsAndLoadBean(t, &user_model.EmailAddress{ID: 35, UID: user.ID}) email := unittest.AssertExistsAndLoadBean(t, &user_model.EmailAddress{ID: 35, UID: user.ID})
assert.False(t, email.IsActivated) require.False(t, email.IsActivated)
makeReq(t, "/user2/repo1/_edit/master/README.md", map[string]string{
req := newReq(t, session, email.Email, "test content") "tree_path": "README.md",
resp := session.MakeRequest(t, req, http.StatusOK) "content": "test content",
htmlDoc := NewHTMLParser(t, resp.Body) "commit_email": email.Email,
assert.Contains(t, }, "", "")
htmlDoc.doc.Find(".ui.negative.message").Text(),
translation.NewLocale("en-US").Tr("repo.editor.invalid_commit_email"),
)
}) })
t.Run("EmailInvalid", func(t *testing.T) { t.Run("EmailInvalid", func(t *testing.T) {
defer tests.PrintCurrentTest(t)() defer tests.PrintCurrentTest(t)()
email := unittest.AssertExistsAndLoadBean(t, &user_model.EmailAddress{ID: 1, IsActivated: true}) email := unittest.AssertExistsAndLoadBean(t, &user_model.EmailAddress{ID: 1, IsActivated: true})
assert.NotEqualValues(t, email.UID, user.ID) require.NotEqualValues(t, email.UID, user.ID)
makeReq(t, "/user2/repo1/_edit/master/README.md", map[string]string{
"tree_path": "README.md",
"content": "test content",
"commit_email": email.Email,
}, "", "")
})
req := newReq(t, session, email.Email, "test content") testWebGit := func(t *testing.T, linkForKeepPrivate string, paramsForKeepPrivate map[string]string, linkForChosenEmail string, paramsForChosenEmail map[string]string) (resp1, resp2 *httptest.ResponseRecorder) {
resp := session.MakeRequest(t, req, http.StatusOK) t.Run("DefaultEmailKeepPrivate", func(t *testing.T) {
htmlDoc := NewHTMLParser(t, resp.Body) defer tests.PrintCurrentTest(t)()
assert.Contains(t, paramsForKeepPrivate["commit_email"] = ""
htmlDoc.doc.Find(".ui.negative.message").Text(), resp1 = makeReq(t, linkForKeepPrivate, paramsForKeepPrivate, "User Two", "user2@noreply.example.org")
translation.NewLocale("en-US").Tr("repo.editor.invalid_commit_email"), })
t.Run("ChooseEmail", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
paramsForChosenEmail["commit_email"] = "user2@example.com"
resp2 = makeReq(t, linkForChosenEmail, paramsForChosenEmail, "User Two", "user2@example.com")
})
return resp1, resp2
}
t.Run("Edit", func(t *testing.T) {
testWebGit(t,
"/user2/repo1/_edit/master/README.md", map[string]string{"tree_path": "README.md", "content": "for keep private"},
"/user2/repo1/_edit/master/README.md", map[string]string{"tree_path": "README.md", "content": "for chosen email"},
) )
}) })
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) t.Run("UploadDelete", func(t *testing.T) {
gitRepo, _ := git.OpenRepository(git.DefaultContext, repo1.RepoPath()) file1UUID := uploadFile(t, "file1", "File 1")
defer gitRepo.Close() file2UUID := uploadFile(t, "file2", "File 2")
testWebGit(t,
t.Run("DefaultEmailKeepPrivate", func(t *testing.T) { "/user2/repo1/_upload/master", map[string]string{"files": file1UUID},
defer tests.PrintCurrentTest(t)() "/user2/repo1/_upload/master", map[string]string{"files": file2UUID},
req := newReq(t, session, "", "privacy email") )
session.MakeRequest(t, req, http.StatusSeeOther) testWebGit(t,
"/user2/repo1/_delete/master/file1", map[string]string{},
commit, err := gitRepo.GetCommitByPath("README.md") "/user2/repo1/_delete/master/file2", map[string]string{},
assert.NoError(t, err) )
fileContent, err := commit.GetFileContent("README.md", 64)
assert.NoError(t, err)
assert.EqualValues(t, "privacy email", fileContent)
assert.EqualValues(t, "User Two", commit.Author.Name)
assert.EqualValues(t, "user2@noreply.example.org", commit.Author.Email)
assert.EqualValues(t, "User Two", commit.Committer.Name)
assert.EqualValues(t, "user2@noreply.example.org", commit.Committer.Email)
}) })
t.Run("ChooseEmail", func(t *testing.T) { t.Run("ApplyPatchCherryPick", func(t *testing.T) {
defer tests.PrintCurrentTest(t)() testWebGit(t,
"/user2/repo1/_diffpatch/master", map[string]string{
"tree_path": "__dummy__",
"content": `diff --git a/patch-file-1.txt b/patch-file-1.txt
new file mode 100644
index 0000000000..aaaaaaaaaa
--- /dev/null
+++ b/patch-file-1.txt
@@ -0,0 +1 @@
+File 1
`,
},
"/user2/repo1/_diffpatch/master", map[string]string{
"tree_path": "__dummy__",
"content": `diff --git a/patch-file-2.txt b/patch-file-2.txt
new file mode 100644
index 0000000000..bbbbbbbbbb
--- /dev/null
+++ b/patch-file-2.txt
@@ -0,0 +1 @@
+File 2
`,
},
)
email := unittest.AssertExistsAndLoadBean(t, &user_model.EmailAddress{ID: 3, UID: user.ID, IsActivated: true}) commit1, err := gitRepo.GetCommitByPath("patch-file-1.txt")
req := newReq(t, session, email.Email, "chosen email") require.NoError(t, err)
session.MakeRequest(t, req, http.StatusSeeOther) commit2, err := gitRepo.GetCommitByPath("patch-file-2.txt")
require.NoError(t, err)
resp1, _ := testWebGit(t,
"/user2/repo1/_cherrypick/"+commit1.ID.String()+"/master", map[string]string{"revert": "true"},
"/user2/repo1/_cherrypick/"+commit2.ID.String()+"/master", map[string]string{"revert": "true"},
)
commit, err := gitRepo.GetCommitByPath("README.md") // By the way, test the "cherrypick" page: a successful revert redirects to the main branch
assert.NoError(t, err) assert.EqualValues(t, "/user2/repo1/src/branch/master", resp1.Header().Get("Location"))
fileContent, err := commit.GetFileContent("README.md", 64)
assert.NoError(t, err)
assert.EqualValues(t, "chosen email", fileContent)
assert.EqualValues(t, "User Two", commit.Author.Name)
assert.EqualValues(t, email.Email, commit.Author.Email)
assert.EqualValues(t, "User Two", commit.Committer.Name)
assert.EqualValues(t, email.Email, commit.Committer.Email)
}) })
}) })
} }

View File

@ -691,10 +691,6 @@ func TestOAuth_GrantScopesReadRepositoryFailOrganization(t *testing.T) {
FullRepoName: "user2/commitsonpr", FullRepoName: "user2/commitsonpr",
Private: false, Private: false,
}, },
{
FullRepoName: "user2/test_commit_revert",
Private: true,
},
} }
assert.Equal(t, reposExpected, reposCaptured) assert.Equal(t, reposExpected, reposCaptured)

View File

@ -1,37 +0,0 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package integration
import (
"net/http"
"testing"
"code.gitea.io/gitea/tests"
"github.com/stretchr/testify/assert"
)
func TestRepoMergeCommitRevert(t *testing.T) {
defer tests.PrepareTestEnv(t)()
session := loginUser(t, "user2")
req := NewRequest(t, "GET", "/user2/test_commit_revert/_cherrypick/deebcbc752e540bab4ce3ee713d3fc8fdc35b2f7/main?ref=main&refType=branch&cherry-pick-type=revert")
resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
req = NewRequestWithValues(t, "POST", "/user2/test_commit_revert/_cherrypick/deebcbc752e540bab4ce3ee713d3fc8fdc35b2f7/main", map[string]string{
"_csrf": htmlDoc.GetCSRF(),
"last_commit": "deebcbc752e540bab4ce3ee713d3fc8fdc35b2f7",
"page_has_posted": "true",
"revert": "true",
"commit_summary": "reverting test commit",
"commit_message": "test message",
"commit_choice": "direct",
"new_branch_name": "test-revert-branch-1",
})
resp = session.MakeRequest(t, req, http.StatusSeeOther)
// A successful revert redirects to the main branch
assert.EqualValues(t, "/user2/test_commit_revert/src/branch/main", resp.Header().Get("Location"))
}