From eec8ee056ce6e208d5e4d11320dcb81721ed6765 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 10 Jan 2026 02:58:21 +0800 Subject: [PATCH] Fix some trivial problems (#36336) 1. correctly parse git protocol's "OldCommit NewCommit RefName" line, it should be explicitly split by space 2. add missing "return" in CreatePullRequest 3. add comments for "/user.keys" and "/user.gpg" outputs 4. trim space for the "commit status context name" to follow the same behavior of git_model.NewCommitStatus --- cmd/hook.go | 24 +++++++++++++----------- cmd/hook_test.go | 14 ++++++++++++++ routers/api/v1/repo/pull.go | 1 + routers/web/user/home.go | 4 ++++ services/actions/commit_status.go | 2 ++ tests/integration/user_test.go | 25 ++++++++++++++++--------- 6 files changed, 50 insertions(+), 20 deletions(-) diff --git a/cmd/hook.go b/cmd/hook.go index 1845ade625..b65d5fbbd2 100644 --- a/cmd/hook.go +++ b/cmd/hook.go @@ -163,6 +163,14 @@ func (n *nilWriter) WriteString(s string) (int, error) { return len(s), nil } +func parseGitHookCommitRefLine(line string) (oldCommitID, newCommitID string, refFullName git.RefName, ok bool) { + fields := strings.Split(line, " ") + if len(fields) != 3 { + return "", "", "", false + } + return fields[0], fields[1], git.RefName(fields[2]), true +} + func runHookPreReceive(ctx context.Context, c *cli.Command) error { if isInternal, _ := strconv.ParseBool(os.Getenv(repo_module.EnvIsInternal)); isInternal { return nil @@ -228,14 +236,11 @@ Gitea or set your environment appropriately.`, "") continue } - fields := bytes.Fields(scanner.Bytes()) - if len(fields) != 3 { + oldCommitID, newCommitID, refFullName, ok := parseGitHookCommitRefLine(scanner.Text()) + if !ok { continue } - oldCommitID := string(fields[0]) - newCommitID := string(fields[1]) - refFullName := git.RefName(fields[2]) total++ lastline++ @@ -378,16 +383,13 @@ Gitea or set your environment appropriately.`, "") continue } - fields := bytes.Fields(scanner.Bytes()) - if len(fields) != 3 { + var ok bool + oldCommitIDs[count], newCommitIDs[count], refFullNames[count], ok = parseGitHookCommitRefLine(scanner.Text()) + if !ok { continue } fmt.Fprintf(out, ".") - oldCommitIDs[count] = string(fields[0]) - newCommitIDs[count] = string(fields[1]) - refFullNames[count] = git.RefName(fields[2]) - commitID, _ := git.NewIDFromString(newCommitIDs[count]) if refFullNames[count] == git.BranchPrefix+"master" && !commitID.IsZero() && count == total { masterPushed = true diff --git a/cmd/hook_test.go b/cmd/hook_test.go index 86cd4834f2..fefc33c01c 100644 --- a/cmd/hook_test.go +++ b/cmd/hook_test.go @@ -39,3 +39,17 @@ func TestPktLine(t *testing.T) { assert.NoError(t, err) assert.Equal(t, []byte("0007a\nb"), w.Bytes()) } + +func TestParseGitHookCommitRefLine(t *testing.T) { + oldCommitID, newCommitID, refName, ok := parseGitHookCommitRefLine("a b c") + assert.True(t, ok) + assert.Equal(t, "a", oldCommitID) + assert.Equal(t, "b", newCommitID) + assert.Equal(t, "c", string(refName)) + + _, _, _, ok = parseGitHookCommitRefLine("a\tb\tc") + assert.False(t, ok) + + _, _, _, ok = parseGitHookCommitRefLine("a b") + assert.False(t, ok) +} diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 09eeefd2c7..b5bacd9669 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -500,6 +500,7 @@ func CreatePullRequest(ctx *context.APIContext) { unitPullRequest, err := ctx.Repo.Repository.GetUnit(ctx, unit.TypePullRequests) if err != nil { ctx.APIErrorInternal(err) + return } prIssue := &issues_model.Issue{ diff --git a/routers/web/user/home.go b/routers/web/user/home.go index b53a3daedb..9e77c51d12 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -660,6 +660,8 @@ func ShowSSHKeys(ctx *context.Context) { } var buf bytes.Buffer + // "authorized_keys" file format: "#" followed by comment line per key + buf.WriteString("# Gitea isn't a key server. The keys are exported as the user uploaded and might not have been fully verified.\n") for i := range keys { buf.WriteString(keys[i].OmitEmail()) buf.WriteString("\n") @@ -695,6 +697,8 @@ func ShowGPGKeys(ctx *context.Context) { var buf bytes.Buffer headers := make(map[string]string) + // https://www.rfc-editor.org/rfc/rfc4880 + headers["Comment"] = "Gitea isn't a key server. The keys are exported as the user uploaded and might not have been fully verified." if len(failedEntitiesID) > 0 { // If some key need re-import to be exported headers["Note"] = "The keys with the following IDs couldn't be exported and need to be reuploaded " + strings.Join(failedEntitiesID, ", ") } else if len(entities) == 0 { diff --git a/services/actions/commit_status.go b/services/actions/commit_status.go index 089dfeb634..7271f58091 100644 --- a/services/actions/commit_status.go +++ b/services/actions/commit_status.go @@ -9,6 +9,7 @@ import ( "fmt" "path" "strconv" + "strings" actions_model "code.gitea.io/gitea/models/actions" "code.gitea.io/gitea/models/db" @@ -129,6 +130,7 @@ func createCommitStatus(ctx context.Context, repo *repo_model.Repository, event, runName = wfs[0].Name } ctxName := fmt.Sprintf("%s / %s (%s)", runName, job.Name, event) + ctxName = strings.TrimSpace(ctxName) // git_model.NewCommitStatus also trims spaces state := toCommitStatus(job.Status) if statuses, err := git_model.GetLatestCommitStatus(ctx, repo.ID, commitID, db.ListOptionsAll); err == nil { for _, v := range statuses { diff --git a/tests/integration/user_test.go b/tests/integration/user_test.go index faaae1103d..8981b6b319 100644 --- a/tests/integration/user_test.go +++ b/tests/integration/user_test.go @@ -27,6 +27,12 @@ func TestViewUser(t *testing.T) { req := NewRequest(t, "GET", "/user2") MakeRequest(t, req, http.StatusOK) + + req = NewRequest(t, "GET", "/user2.keys") + resp := MakeRequest(t, req, http.StatusOK) + assert.Equal(t, `# Gitea isn't a key server. The keys are exported as the user uploaded and might not have been fully verified. +ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDWVj0fQ5N8wNc0LVNA41wDLYJ89ZIbejrPfg/avyj3u/ZohAKsQclxG4Ju0VirduBFF9EOiuxoiFBRr3xRpqzpsZtnMPkWVWb+akZwBFAx8p+jKdy4QXR/SZqbVobrGwip2UjSrri1CtBxpJikojRIZfCnDaMOyd9Jp6KkujvniFzUWdLmCPxUE9zhTaPu0JsEP7MW0m6yx7ZUhHyfss+NtqmFTaDO+QlMR7L2QkDliN2Jl3Xa3PhuWnKJfWhdAq1Cw4oraKUOmIgXLkuiuxVQ6mD3AiFupkmfqdHq6h+uHHmyQqv3gU+/sD8GbGAhf6ftqhTsXjnv1Aj4R8NoDf9BS6KRkzkeun5UisSzgtfQzjOMEiJtmrep2ZQrMGahrXa+q4VKr0aKJfm+KlLfwm/JztfsBcqQWNcTURiCFqz+fgZw0Ey/de0eyMzldYTdXXNRYCKjs9bvBK+6SSXRM7AhftfQ0ZuoW5+gtinPrnmoOaSCEJbAiEiTO/BzOHgowiM= +`, resp.Body.String()) } func TestRenameUsername(t *testing.T) { @@ -194,8 +200,17 @@ func TestRenameReservedUsername(t *testing.T) { func TestExportUserGPGKeys(t *testing.T) { defer tests.PrepareTestEnv(t)() + testExportUserGPGKeys := func(t *testing.T, user, expected string) { + session := loginUser(t, user) + t.Logf("Testing username %s export gpg keys", user) + req := NewRequest(t, "GET", "/"+user+".gpg") + resp := session.MakeRequest(t, req, http.StatusOK) + assert.Equal(t, expected, resp.Body.String()) + } + // Export empty key list testExportUserGPGKeys(t, "user1", `-----BEGIN PGP PUBLIC KEY BLOCK----- +Comment: Gitea isn't a key server. The keys are exported as the user uploaded and might not have been fully verified. Note: This user hasn't uploaded any GPG keys. @@ -237,6 +252,7 @@ GrE0MHOxUbc9tbtyk0F1SuzREUBH -----END PGP PUBLIC KEY BLOCK-----`) // Export new key testExportUserGPGKeys(t, "user1", `-----BEGIN PGP PUBLIC KEY BLOCK----- +Comment: Gitea isn't a key server. The keys are exported as the user uploaded and might not have been fully verified. xsBNBFyy/VUBCADJ7zbM20Z1RWmFoVgp5WkQfI2rU1Vj9cQHes9i42wVLLtcbPeo QzubgzvMPITDy7nfWxgSf83E23DoHQ1ACFbQh/6eFSRrjsusp3YQ/08NSfPPbcu8 @@ -268,15 +284,6 @@ GrE0MHOxUbc9tbtyk0F1SuzREUBH -----END PGP PUBLIC KEY BLOCK-----`) } -func testExportUserGPGKeys(t *testing.T, user, expected string) { - session := loginUser(t, user) - t.Logf("Testing username %s export gpg keys", user) - req := NewRequest(t, "GET", "/"+user+".gpg") - resp := session.MakeRequest(t, req, http.StatusOK) - // t.Log(resp.Body.String()) - assert.Equal(t, expected, resp.Body.String()) -} - func TestGetUserRss(t *testing.T) { defer tests.PrepareTestEnv(t)()