Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-22 01:44:24 +01:00

Compare commits

...

5 commits

Author SHA1 Message Date
Earl Warren fea1aef146 Merge pull request 'Fix misleading comparisons when comparing branches' (#2194) from algernon/forgejo:b/diff-compare/avoid-404s into forgejo-dependency
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2194
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
2024-01-22 09:23:17 +00:00
Earl Warren ff93cc3b5c Merge pull request 'Don't consider orphan branches as recently pushed' (#2196) from algernon/forgejo:b/recently-pushed/branch-conditions into forgejo-dependency
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2196
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
2024-01-22 09:22:29 +00:00
Gergely Nagy e1fba517f4
Don't consider orphan branches as recently pushed
When displaying the recently pushed branches banner, don't display
branches that have no common history with the default branch. These
branches are usually not meant to be merged, so the banner is just noise
in this case.

Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
2024-01-22 09:46:55 +01:00
voltagex fecc14a16c
[GITEA] API comment update routers/api/v1/shared/runners.go
Refs: https://codeberg.org/forgejo/forgejo/pulls/2191

(cherry picked from commit 1e89dd95b9)
2024-01-22 08:36:50 +00:00
Gergely Nagy 022d0e0d71
Fix misleading comparisons when comparing branches
When comparing branches, only offer those branches to use as a base
where the repository allows pull requests. Those that do not allow pull
request would result in a 404, so offering them as an option would be
misleading.

Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
2024-01-21 23:31:45 +01:00
6 changed files with 166 additions and 8 deletions

View file

@ -12,7 +12,7 @@ import (
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
) )
// RegistrationToken is response related to registeration token // RegistrationToken is a string used to register a runner with a server
// swagger:response RegistrationToken // swagger:response RegistrationToken
type RegistrationToken struct { type RegistrationToken struct {
Token string `json:"token"` Token string `json:"token"`

View file

@ -1035,7 +1035,43 @@ func renderCode(ctx *context.Context) {
} }
} }
ctx.Data["RecentlyPushedNewBranches"] = branches // Filter out branches that have no relation to the default branch of
// the repository.
var filteredBranches []*git_model.Branch
for _, branch := range branches {
repo, err := branch.GetRepo(ctx)
if err != nil {
continue
}
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
if err != nil {
continue
}
defer gitRepo.Close()
head, err := gitRepo.GetCommit(branch.CommitID)
if err != nil {
continue
}
defaultBranch, err := gitRepo.GetDefaultBranch()
if err != nil {
continue
}
defaultBranchHead, err := gitRepo.GetCommit(defaultBranch)
if err != nil {
continue
}
hasMergeBase, err := head.HasPreviousCommit(defaultBranchHead.ID)
if err != nil {
continue
}
if hasMergeBase {
filteredBranches = append(filteredBranches, branch)
}
}
ctx.Data["RecentlyPushedNewBranches"] = filteredBranches
} }
PostRecentBranchCheck: PostRecentBranchCheck:

View file

@ -67,12 +67,12 @@
{{range .Branches}} {{range .Branches}}
<div class="item {{if eq $.BaseBranch .}}selected{{end}}" data-url="{{$.RepoLink}}/compare/{{PathEscapeSegments .}}{{$.CompareSeparator}}{{if not $.PullRequestCtx.SameRepo}}{{PathEscape $.HeadUser.Name}}/{{PathEscape $.HeadRepo.Name}}:{{end}}{{PathEscapeSegments $.HeadBranch}}">{{$BaseCompareName}}:{{.}}</div> <div class="item {{if eq $.BaseBranch .}}selected{{end}}" data-url="{{$.RepoLink}}/compare/{{PathEscapeSegments .}}{{$.CompareSeparator}}{{if not $.PullRequestCtx.SameRepo}}{{PathEscape $.HeadUser.Name}}/{{PathEscape $.HeadRepo.Name}}:{{end}}{{PathEscapeSegments $.HeadBranch}}">{{$BaseCompareName}}:{{.}}</div>
{{end}} {{end}}
{{if not .PullRequestCtx.SameRepo}} {{if and (not .PullRequestCtx.SameRepo) ($.HeadRepo.AllowsPulls ctx)}}
{{range .HeadBranches}} {{range .HeadBranches}}
<div class="item" data-url="{{$.HeadRepo.Link}}/compare/{{PathEscapeSegments .}}{{$.CompareSeparator}}{{PathEscape $.HeadUser.Name}}/{{PathEscape $.HeadRepo.Name}}:{{PathEscapeSegments $.HeadBranch}}">{{$HeadCompareName}}:{{.}}</div> <div class="item" data-url="{{$.HeadRepo.Link}}/compare/{{PathEscapeSegments .}}{{$.CompareSeparator}}{{PathEscape $.HeadUser.Name}}/{{PathEscape $.HeadRepo.Name}}:{{PathEscapeSegments $.HeadBranch}}">{{$HeadCompareName}}:{{.}}</div>
{{end}} {{end}}
{{end}} {{end}}
{{if .OwnForkRepo}} {{if and .OwnForkRepo (.OwnForkRepo.AllowsPulls ctx)}}
{{range .OwnForkRepoBranches}} {{range .OwnForkRepoBranches}}
<div class="item" data-url="{{$.OwnForkRepo.Link}}/compare/{{PathEscapeSegments .}}{{$.CompareSeparator}}{{PathEscape $.HeadUser.Name}}/{{PathEscape $.HeadRepo.Name}}:{{PathEscapeSegments $.HeadBranch}}">{{$OwnForkCompareName}}:{{.}}</div> <div class="item" data-url="{{$.OwnForkRepo.Link}}/compare/{{PathEscapeSegments .}}{{$.CompareSeparator}}{{PathEscape $.HeadUser.Name}}/{{PathEscape $.HeadRepo.Name}}:{{PathEscapeSegments $.HeadBranch}}">{{$OwnForkCompareName}}:{{.}}</div>
{{end}} {{end}}
@ -87,17 +87,17 @@
{{range .Tags}} {{range .Tags}}
<div class="item {{if eq $.BaseBranch .}}selected{{end}}" data-url="{{$.RepoLink}}/compare/{{PathEscapeSegments .}}{{$.CompareSeparator}}{{if not $.PullRequestCtx.SameRepo}}{{PathEscape $.HeadUser.Name}}/{{PathEscape $.HeadRepo.Name}}:{{end}}{{PathEscapeSegments $.HeadBranch}}">{{$BaseCompareName}}:{{.}}</div> <div class="item {{if eq $.BaseBranch .}}selected{{end}}" data-url="{{$.RepoLink}}/compare/{{PathEscapeSegments .}}{{$.CompareSeparator}}{{if not $.PullRequestCtx.SameRepo}}{{PathEscape $.HeadUser.Name}}/{{PathEscape $.HeadRepo.Name}}:{{end}}{{PathEscapeSegments $.HeadBranch}}">{{$BaseCompareName}}:{{.}}</div>
{{end}} {{end}}
{{if not .PullRequestCtx.SameRepo}} {{if and (not .PullRequestCtx.SameRepo) ($.HeadRepo.AllowsPulls ctx)}}
{{range .HeadTags}} {{range .HeadTags}}
<div class="item" data-url="{{$.HeadRepo.Link}}/compare/{{PathEscapeSegments .}}{{$.CompareSeparator}}{{PathEscape $.HeadUser.Name}}/{{PathEscape $.HeadRepo.Name}}:{{PathEscapeSegments $.HeadBranch}}">{{$HeadCompareName}}:{{.}}</div> <div class="item" data-url="{{$.HeadRepo.Link}}/compare/{{PathEscapeSegments .}}{{$.CompareSeparator}}{{PathEscape $.HeadUser.Name}}/{{PathEscape $.HeadRepo.Name}}:{{PathEscapeSegments $.HeadBranch}}">{{$HeadCompareName}}:{{.}}</div>
{{end}} {{end}}
{{end}} {{end}}
{{if .OwnForkRepo}} {{if and .OwnForkRepo (.OwnForkRepo.AllowsPulls ctx)}}
{{range .OwnForkRepoTags}} {{range .OwnForkRepoTags}}
<div class="item" data-url="{{$.OwnForkRepo.Link}}/compare/{{PathEscapeSegments .}}{{$.CompareSeparator}}{{PathEscape $.HeadUser.Name}}/{{PathEscape $.HeadRepo.Name}}:{{PathEscapeSegments $.HeadBranch}}">{{$OwnForkCompareName}}:{{.}}</div> <div class="item" data-url="{{$.OwnForkRepo.Link}}/compare/{{PathEscapeSegments .}}{{$.CompareSeparator}}{{PathEscape $.HeadUser.Name}}/{{PathEscape $.HeadRepo.Name}}:{{PathEscapeSegments $.HeadBranch}}">{{$OwnForkCompareName}}:{{.}}</div>
{{end}} {{end}}
{{end}} {{end}}
{{if .RootRepo}} {{if and .RootRepo (.RootRepo.AllowsPulls ctx)}}
{{range .RootRepoTags}} {{range .RootRepoTags}}
<div class="item" data-url="{{$.RootRepo.Link}}/compare/{{PathEscapeSegments .}}{{$.CompareSeparator}}{{PathEscape $.HeadUser.Name}}/{{PathEscape $.HeadRepo.Name}}:{{PathEscapeSegments $.HeadBranch}}">{{$RootRepoCompareName}}:{{.}}</div> <div class="item" data-url="{{$.RootRepo.Link}}/compare/{{PathEscapeSegments .}}{{$.CompareSeparator}}{{PathEscape $.HeadUser.Name}}/{{PathEscape $.HeadRepo.Name}}:{{PathEscapeSegments $.HeadBranch}}">{{$RootRepoCompareName}}:{{.}}</div>
{{end}} {{end}}

View file

@ -24354,7 +24354,7 @@
} }
}, },
"RegistrationToken": { "RegistrationToken": {
"description": "RegistrationToken is response related to registeration token", "description": "RegistrationToken is a string used to register a runner with a server",
"headers": { "headers": {
"token": { "token": {
"type": "string" "type": "string"

View file

@ -1,4 +1,5 @@
// Copyright 2021 The Gitea Authors. All rights reserved. // Copyright 2021 The Gitea Authors. All rights reserved.
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
package integration package integration
@ -6,9 +7,14 @@ package integration
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"net/url"
"strings" "strings"
"testing" "testing"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
unit_model "code.gitea.io/gitea/models/unit"
repo_service "code.gitea.io/gitea/services/repository"
"code.gitea.io/gitea/tests" "code.gitea.io/gitea/tests"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -118,3 +124,61 @@ func TestCompareBranches(t *testing.T) {
inspectCompare(t, htmlDoc, diffCount, diffChanges) inspectCompare(t, htmlDoc, diffCount, diffChanges)
} }
func TestCompareWithPRsDisabled(t *testing.T) {
onGiteaRun(t, func(t *testing.T, u *url.URL) {
session := loginUser(t, "user1")
testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
testCreateBranch(t, session, "user1", "repo1", "branch/master", "recent-push", http.StatusSeeOther)
testEditFile(t, session, "user1", "repo1", "recent-push", "README.md", "Hello recently!\n")
repo, err := repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user1", "repo1")
assert.NoError(t, err)
defer func() {
// Reenable PRs on the repo
err := repo_service.UpdateRepositoryUnits(db.DefaultContext, repo,
[]repo_model.RepoUnit{{
RepoID: repo.ID,
Type: unit_model.TypePullRequests,
}},
nil)
assert.NoError(t, err)
}()
// Disable PRs on the repo
err = repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, nil,
[]unit_model.Type{unit_model.TypePullRequests})
assert.NoError(t, err)
t.Run("branch view doesn't offer creating PRs", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := NewRequest(t, "GET", "/user1/repo1/branches")
resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
htmlDoc.AssertElement(t, "a[href='/user1/repo1/compare/master...recent-push']", false)
})
t.Run("compare doesn't offer local branches", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := NewRequest(t, "GET", "/user2/repo1/compare/master...user1/repo1:recent-push")
resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
branches := htmlDoc.Find(".choose.branch .menu .reference-list-menu.base-branch-list .item, .choose.branch .menu .reference-list-menu.base-tag-list .item")
expectedPrefix := "user2:"
for i := 0; i < len(branches.Nodes); i++ {
assert.True(t, strings.HasPrefix(branches.Eq(i).Text(), expectedPrefix))
}
})
t.Run("comparing against a disabled-PR repo is 404", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := NewRequest(t, "GET", "/user1/repo1/compare/master...recent-push")
session.MakeRequest(t, req, http.StatusNotFound)
})
})
}

View file

@ -16,8 +16,13 @@ import (
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
unit_model "code.gitea.io/gitea/models/unit" unit_model "code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/modules/test"
repo_service "code.gitea.io/gitea/services/repository" repo_service "code.gitea.io/gitea/services/repository"
files_service "code.gitea.io/gitea/services/repository/files"
"code.gitea.io/gitea/tests" "code.gitea.io/gitea/tests"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -331,5 +336,58 @@ func TestRecentlyPushed(t *testing.T) {
// PR link compares against the correct rep, and qualified branch name // PR link compares against the correct rep, and qualified branch name
assert.Equal(t, "/user2/repo1/compare/master...user1/repo1:recent-push", forkPRLink) assert.Equal(t, "/user2/repo1/compare/master...user1/repo1:recent-push", forkPRLink)
}) })
t.Run("unrelated branches are not shown", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
adminUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{IsAdmin: true})
// Create a new branch with no relation to the default branch.
// 1. Create a new Tree object
cmd := git.NewCommand(db.DefaultContext, "write-tree")
treeID, _, gitErr := cmd.RunStdString(&git.RunOpts{Dir: repo.RepoPath()})
assert.NoError(t, gitErr)
treeID = strings.TrimSpace(treeID)
// 2. Create a new (empty) commit
cmd = git.NewCommand(db.DefaultContext, "commit-tree", "-m", "Initial orphan commit").AddDynamicArguments(treeID)
commitID, _, gitErr := cmd.RunStdString(&git.RunOpts{Dir: repo.RepoPath()})
assert.NoError(t, gitErr)
commitID = strings.TrimSpace(commitID)
// 3. Create a new ref pointing to the orphaned commit
cmd = git.NewCommand(db.DefaultContext, "update-ref", "refs/heads/orphan1").AddDynamicArguments(commitID)
_, _, gitErr = cmd.RunStdString(&git.RunOpts{Dir: repo.RepoPath()})
assert.NoError(t, gitErr)
// 4. Sync the git repo to the database
syncErr := repo_service.AddAllRepoBranchesToSyncQueue(graceful.GetManager().ShutdownContext(), adminUser.ID)
assert.NoError(t, syncErr)
// 5. Add a fresh commit, so that FindRecentlyPushedBranches has
// something to find.
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user1"})
changeResp, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, owner,
&files_service.ChangeRepoFilesOptions{
Files: []*files_service.ChangeRepoFile{
{
Operation: "create",
TreePath: "README.md",
ContentReader: strings.NewReader("a readme file"),
},
},
Message: "Add README.md",
OldBranch: "orphan1",
NewBranch: "orphan1",
})
assert.NoError(t, err)
assert.NotEmpty(t, changeResp)
// Check that we only have 1 message on the main repo, the orphaned
// one is not shown.
req := NewRequest(t, "GET", "/user1/repo1")
resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
htmlDoc.AssertElement(t, ".ui.message", true)
link, _ := htmlDoc.Find(".ui.message a[href*='/src/branch/']").Attr("href")
assert.Equal(t, "/user1/repo1/src/branch/recent-push", link)
})
}) })
} }