Skip to content

Commit 6650f2c

Browse files
authored
Merge branch 'main' into fix/23139-system-webhooks
2 parents 1b086b5 + 348b707 commit 6650f2c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+570
-700
lines changed

models/actions/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (run *ActionRun) RefLink() string {
8888
if refName.IsPull() {
8989
return run.Repo.Link() + "/pulls/" + refName.ShortName()
9090
}
91-
return git.RefURL(run.Repo.Link(), run.Ref)
91+
return run.Repo.Link() + "/src/" + refName.RefWebLinkPath()
9292
}
9393

9494
// PrettyRef return #id for pull ref or ShortName for others

models/activities/action.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func (a *Action) GetBranch() string {
355355

356356
// GetRefLink returns the action's ref link.
357357
func (a *Action) GetRefLink(ctx context.Context) string {
358-
return git.RefURL(a.GetRepoLink(ctx), a.RefName)
358+
return a.GetRepoLink(ctx) + "/src/" + git.RefName(a.RefName).RefWebLinkPath()
359359
}
360360

361361
// GetTag returns the action's repository tag.

models/issues/comment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ const (
112112
CommentTypePRScheduledToAutoMerge // 34 pr was scheduled to auto merge when checks succeed
113113
CommentTypePRUnScheduledToAutoMerge // 35 pr was un scheduled to auto merge when checks succeed
114114

115-
CommentTypePin // 36 pin Issue
116-
CommentTypeUnpin // 37 unpin Issue
115+
CommentTypePin // 36 pin Issue/PullRequest
116+
CommentTypeUnpin // 37 unpin Issue/PullRequest
117117

118118
CommentTypeChangeTimeEstimate // 38 Change time estimate
119119
)

models/repo/archiver.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,11 @@ func repoArchiverForRelativePath(relativePath string) (*RepoArchiver, error) {
5656
if err != nil {
5757
return nil, util.SilentWrap{Message: fmt.Sprintf("invalid storage path: %s", relativePath), Err: util.ErrInvalidArgument}
5858
}
59-
nameExts := strings.SplitN(parts[2], ".", 2)
60-
if len(nameExts) != 2 {
59+
commitID, archiveType := git.SplitArchiveNameType(parts[2])
60+
if archiveType == git.ArchiveUnknown {
6161
return nil, util.SilentWrap{Message: fmt.Sprintf("invalid storage path: %s", relativePath), Err: util.ErrInvalidArgument}
6262
}
63-
64-
return &RepoArchiver{
65-
RepoID: repoID,
66-
CommitID: parts[1] + nameExts[0],
67-
Type: git.ToArchiveType(nameExts[1]),
68-
}, nil
63+
return &RepoArchiver{RepoID: repoID, CommitID: commitID, Type: archiveType}, nil
6964
}
7065

7166
// GetRepoArchiver get an archiver

modules/cache/cache.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ func Init() error {
3737
}
3838

3939
const (
40-
testCacheKey = "DefaultCache.TestKey"
41-
SlowCacheThreshold = 100 * time.Microsecond
40+
testCacheKey = "DefaultCache.TestKey"
41+
// SlowCacheThreshold marks cache tests as slow
42+
// set to 30ms per discussion: https://github.com/go-gitea/gitea/issues/33190
43+
// TODO: Replace with metrics histogram
44+
SlowCacheThreshold = 30 * time.Millisecond
4245
)
4346

47+
// Test performs delete, put and get operations on a predefined key
48+
// returns
4449
func Test() (time.Duration, error) {
4550
if defaultCache == nil {
4651
return 0, fmt.Errorf("default cache not initialized")

modules/cache/cache_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ func TestTest(t *testing.T) {
4343
elapsed, err := Test()
4444
assert.NoError(t, err)
4545
// mem cache should take from 300ns up to 1ms on modern hardware ...
46-
assert.Less(t, elapsed, time.Millisecond)
46+
assert.Positive(t, elapsed)
47+
assert.Less(t, elapsed, SlowCacheThreshold)
4748
}
4849

4950
func TestGetCache(t *testing.T) {

modules/git/commit.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,12 @@ func (c *Commit) GetRepositoryDefaultPublicGPGKey(forceUpdate bool) (*GPGSetting
476476
}
477477

478478
func IsStringLikelyCommitID(objFmt ObjectFormat, s string, minLength ...int) bool {
479-
minLen := util.OptionalArg(minLength, objFmt.FullLength())
480-
if len(s) < minLen || len(s) > objFmt.FullLength() {
479+
maxLen := 64 // sha256
480+
if objFmt != nil {
481+
maxLen = objFmt.FullLength()
482+
}
483+
minLen := util.OptionalArg(minLength, maxLen)
484+
if len(s) < minLen || len(s) > maxLen {
481485
return false
482486
}
483487
for _, c := range s {

modules/git/ref.go

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ func RefNameFromTag(shortName string) RefName {
8080
return RefName(TagPrefix + shortName)
8181
}
8282

83+
func RefNameFromCommit(shortName string) RefName {
84+
return RefName(shortName)
85+
}
86+
8387
func (ref RefName) String() string {
8488
return string(ref)
8589
}
@@ -181,32 +185,38 @@ func (ref RefName) RefGroup() string {
181185
return ""
182186
}
183187

188+
// RefType is a simple ref type of the reference, it is used for UI and webhooks
189+
type RefType string
190+
191+
const (
192+
RefTypeBranch RefType = "branch"
193+
RefTypeTag RefType = "tag"
194+
RefTypeCommit RefType = "commit"
195+
)
196+
184197
// RefType returns the simple ref type of the reference, e.g. branch, tag
185198
// It's different from RefGroup, which is using the name of the directory under .git/refs
186-
// Here we using branch but not heads, using tag but not tags
187-
func (ref RefName) RefType() string {
188-
var refType string
189-
if ref.IsBranch() {
190-
refType = "branch"
191-
} else if ref.IsTag() {
192-
refType = "tag"
199+
func (ref RefName) RefType() RefType {
200+
switch {
201+
case ref.IsBranch():
202+
return RefTypeBranch
203+
case ref.IsTag():
204+
return RefTypeTag
205+
case IsStringLikelyCommitID(nil, string(ref), 6):
206+
return RefTypeCommit
193207
}
194-
return refType
208+
return ""
195209
}
196210

197-
// RefURL returns the absolute URL for a ref in a repository
198-
func RefURL(repoURL, ref string) string {
199-
refFullName := RefName(ref)
200-
refName := util.PathEscapeSegments(refFullName.ShortName())
201-
switch {
202-
case refFullName.IsBranch():
203-
return repoURL + "/src/branch/" + refName
204-
case refFullName.IsTag():
205-
return repoURL + "/src/tag/" + refName
206-
case !Sha1ObjectFormat.IsValid(ref):
207-
// assume they mean a branch
208-
return repoURL + "/src/branch/" + refName
209-
default:
210-
return repoURL + "/src/commit/" + refName
211+
// RefWebLinkPath returns a path for the reference that can be used in a web link:
212+
// * "branch/<branch_name>"
213+
// * "tag/<tag_name>"
214+
// * "commit/<commit_id>"
215+
// It returns an empty string if the reference is not a branch, tag or commit.
216+
func (ref RefName) RefWebLinkPath() string {
217+
refType := ref.RefType()
218+
if refType == "" {
219+
return ""
211220
}
221+
return string(refType) + "/" + util.PathEscapeSegments(ref.ShortName())
212222
}

modules/git/ref_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ func TestRefName(t *testing.T) {
2020

2121
// Test pull names
2222
assert.Equal(t, "1", RefName("refs/pull/1/head").PullName())
23+
assert.True(t, RefName("refs/pull/1/head").IsPull())
24+
assert.True(t, RefName("refs/pull/1/merge").IsPull())
2325
assert.Equal(t, "my/pull", RefName("refs/pull/my/pull/head").PullName())
2426

2527
// Test for branch names
@@ -30,9 +32,8 @@ func TestRefName(t *testing.T) {
3032
assert.Equal(t, "c0ffee", RefName("c0ffee").ShortName())
3133
}
3234

33-
func TestRefURL(t *testing.T) {
34-
repoURL := "/user/repo"
35-
assert.Equal(t, repoURL+"/src/branch/foo", RefURL(repoURL, "refs/heads/foo"))
36-
assert.Equal(t, repoURL+"/src/tag/foo", RefURL(repoURL, "refs/tags/foo"))
37-
assert.Equal(t, repoURL+"/src/commit/c0ffee", RefURL(repoURL, "c0ffee"))
35+
func TestRefWebLinkPath(t *testing.T) {
36+
assert.Equal(t, "branch/foo", RefName("refs/heads/foo").RefWebLinkPath())
37+
assert.Equal(t, "tag/foo", RefName("refs/tags/foo").RefWebLinkPath())
38+
assert.Equal(t, "commit/c0ffee", RefName("c0ffee").RefWebLinkPath())
3839
}

modules/git/repo_archive.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,35 @@ import (
1616
type ArchiveType int
1717

1818
const (
19-
// ZIP zip archive type
20-
ZIP ArchiveType = iota + 1
21-
// TARGZ tar gz archive type
22-
TARGZ
23-
// BUNDLE bundle archive type
24-
BUNDLE
19+
ArchiveUnknown ArchiveType = iota
20+
ArchiveZip // 1
21+
ArchiveTarGz // 2
22+
ArchiveBundle // 3
2523
)
2624

27-
// String converts an ArchiveType to string
25+
// String converts an ArchiveType to string: the extension of the archive file without prefix dot
2826
func (a ArchiveType) String() string {
2927
switch a {
30-
case ZIP:
28+
case ArchiveZip:
3129
return "zip"
32-
case TARGZ:
30+
case ArchiveTarGz:
3331
return "tar.gz"
34-
case BUNDLE:
32+
case ArchiveBundle:
3533
return "bundle"
3634
}
3735
return "unknown"
3836
}
3937

40-
func ToArchiveType(s string) ArchiveType {
41-
switch s {
42-
case "zip":
43-
return ZIP
44-
case "tar.gz":
45-
return TARGZ
46-
case "bundle":
47-
return BUNDLE
38+
func SplitArchiveNameType(s string) (string, ArchiveType) {
39+
switch {
40+
case strings.HasSuffix(s, ".zip"):
41+
return strings.TrimSuffix(s, ".zip"), ArchiveZip
42+
case strings.HasSuffix(s, ".tar.gz"):
43+
return strings.TrimSuffix(s, ".tar.gz"), ArchiveTarGz
44+
case strings.HasSuffix(s, ".bundle"):
45+
return strings.TrimSuffix(s, ".bundle"), ArchiveBundle
4846
}
49-
return 0
47+
return s, ArchiveUnknown
5048
}
5149

5250
// CreateArchive create archive content to the target path

0 commit comments

Comments
 (0)