Skip to content

Commit 10c6287

Browse files
authored
Merge branch 'main' into fix/ui/release-post-with-tag-only
2 parents 7a5595c + 348b707 commit 10c6287

File tree

14 files changed

+68
-74
lines changed

14 files changed

+68
-74
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.

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: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -185,32 +185,38 @@ func (ref RefName) RefGroup() string {
185185
return ""
186186
}
187187

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+
188197
// RefType returns the simple ref type of the reference, e.g. branch, tag
189198
// It's different from RefGroup, which is using the name of the directory under .git/refs
190-
// Here we using branch but not heads, using tag but not tags
191-
func (ref RefName) RefType() string {
192-
var refType string
193-
if ref.IsBranch() {
194-
refType = "branch"
195-
} else if ref.IsTag() {
196-
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
197207
}
198-
return refType
208+
return ""
199209
}
200210

201-
// RefURL returns the absolute URL for a ref in a repository
202-
func RefURL(repoURL, ref string) string {
203-
refFullName := RefName(ref)
204-
refName := util.PathEscapeSegments(refFullName.ShortName())
205-
switch {
206-
case refFullName.IsBranch():
207-
return repoURL + "/src/branch/" + refName
208-
case refFullName.IsTag():
209-
return repoURL + "/src/tag/" + refName
210-
case !Sha1ObjectFormat.IsValid(ref):
211-
// assume they mean a branch
212-
return repoURL + "/src/branch/" + refName
213-
default:
214-
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 ""
215220
}
221+
return string(refType) + "/" + util.PathEscapeSegments(ref.ShortName())
216222
}

modules/git/ref_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ func TestRefName(t *testing.T) {
3232
assert.Equal(t, "c0ffee", RefName("c0ffee").ShortName())
3333
}
3434

35-
func TestRefURL(t *testing.T) {
36-
repoURL := "/user/repo"
37-
assert.Equal(t, repoURL+"/src/branch/foo", RefURL(repoURL, "refs/heads/foo"))
38-
assert.Equal(t, repoURL+"/src/tag/foo", RefURL(repoURL, "refs/tags/foo"))
39-
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())
4039
}

routers/api/actions/runner/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func generateTaskContext(t *actions_model.ActionTask) *structpb.Struct {
120120
"ref": ref, // string, The fully-formed ref of the branch or tag that triggered the workflow run. For workflows triggered by push, this is the branch or tag ref that was pushed. For workflows triggered by pull_request, this is the pull request merge branch. For workflows triggered by release, this is the release tag created. For other triggers, this is the branch or tag ref that triggered the workflow run. This is only set if a branch or tag is available for the event type. The ref given is fully-formed, meaning that for branches the format is refs/heads/<branch_name>, for pull requests it is refs/pull/<pr_number>/merge, and for tags it is refs/tags/<tag_name>. For example, refs/heads/feature-branch-1.
121121
"ref_name": refName.ShortName(), // string, The short ref name of the branch or tag that triggered the workflow run. This value matches the branch or tag name shown on GitHub. For example, feature-branch-1.
122122
"ref_protected": false, // boolean, true if branch protections are configured for the ref that triggered the workflow run.
123-
"ref_type": refName.RefType(), // string, The type of ref that triggered the workflow run. Valid values are branch or tag.
123+
"ref_type": string(refName.RefType()), // string, The type of ref that triggered the workflow run. Valid values are branch or tag.
124124
"path": "", // string, Path on the runner to the file that sets system PATH variables from workflow commands. This file is unique to the current step and is a different file for each step in a job. For more information, see "Workflow commands for GitHub Actions."
125125
"repository": t.Job.Run.Repo.OwnerName + "/" + t.Job.Run.Repo.Name, // string, The owner and repository name. For example, Codertocat/Hello-World.
126126
"repository_owner": t.Job.Run.Repo.OwnerName, // string, The repository owner's name. For example, Codertocat.

routers/web/repo/render.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ import (
2121

2222
// RenderFile renders a file by repos path
2323
func RenderFile(ctx *context.Context) {
24-
blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
24+
var blob *git.Blob
25+
var err error
26+
if ctx.Repo.TreePath != "" {
27+
blob, err = ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
28+
} else {
29+
blob, err = ctx.Repo.GitRepo.GetBlob(ctx.PathParam("sha"))
30+
}
2531
if err != nil {
2632
if git.IsErrNotExist(err) {
2733
ctx.NotFound("GetBlobByPath", err)

routers/web/web.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,7 @@ func registerRoutes(m *web.Router) {
15241524
m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.SingleDownloadOrLFS)
15251525
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.SingleDownloadOrLFS)
15261526
m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.SingleDownloadOrLFS)
1527-
m.Get("/blob/{sha}", context.RepoRefByType(context.RepoRefBlob), repo.DownloadByIDOrLFS)
1527+
m.Get("/blob/{sha}", repo.DownloadByIDOrLFS)
15281528
// "/*" route is deprecated, and kept for backward compatibility
15291529
m.Get("/*", context.RepoRefByType(context.RepoRefUnknown), repo.SingleDownloadOrLFS)
15301530
}, repo.MustBeNotEmpty)
@@ -1533,7 +1533,7 @@ func registerRoutes(m *web.Router) {
15331533
m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.SingleDownload)
15341534
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.SingleDownload)
15351535
m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.SingleDownload)
1536-
m.Get("/blob/{sha}", context.RepoRefByType(context.RepoRefBlob), repo.DownloadByID)
1536+
m.Get("/blob/{sha}", repo.DownloadByID)
15371537
// "/*" route is deprecated, and kept for backward compatibility
15381538
m.Get("/*", context.RepoRefByType(context.RepoRefUnknown), repo.SingleDownload)
15391539
}, repo.MustBeNotEmpty)
@@ -1542,7 +1542,7 @@ func registerRoutes(m *web.Router) {
15421542
m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.RenderFile)
15431543
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.RenderFile)
15441544
m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.RenderFile)
1545-
m.Get("/blob/{sha}", context.RepoRefByType(context.RepoRefBlob), repo.RenderFile)
1545+
m.Get("/blob/{sha}", repo.RenderFile)
15461546
}, repo.MustBeNotEmpty)
15471547

15481548
m.Group("/commits", func() {
@@ -1572,6 +1572,7 @@ func registerRoutes(m *web.Router) {
15721572
m.Get("/atom/branch/*", context.RepoRefByType(context.RepoRefBranch), feedEnabled, feed.RenderBranchFeed)
15731573

15741574
m.Group("/src", func() {
1575+
m.Get("", func(ctx *context.Context) { ctx.Redirect(ctx.Repo.RepoLink) }) // there is no "{owner}/{repo}/src" page, so redirect to "{owner}/{repo}" to avoid 404
15751576
m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.Home)
15761577
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.Home)
15771578
m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.Home)

services/actions/notifier.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -563,9 +563,9 @@ func (n *actionsNotifier) CreateRef(ctx context.Context, pusher *user_model.User
563563
newNotifyInput(repo, pusher, webhook_module.HookEventCreate).
564564
WithRef(refFullName.String()).
565565
WithPayload(&api.CreatePayload{
566-
Ref: refFullName.String(),
566+
Ref: refFullName.String(), // HINT: here is inconsistent with the Webhook's payload: webhook uses ShortName
567567
Sha: refID,
568-
RefType: refFullName.RefType(),
568+
RefType: string(refFullName.RefType()),
569569
Repo: apiRepo,
570570
Sender: apiPusher,
571571
}).
@@ -580,8 +580,8 @@ func (n *actionsNotifier) DeleteRef(ctx context.Context, pusher *user_model.User
580580

581581
newNotifyInput(repo, pusher, webhook_module.HookEventDelete).
582582
WithPayload(&api.DeletePayload{
583-
Ref: refFullName.String(),
584-
RefType: refFullName.RefType(),
583+
Ref: refFullName.String(), // HINT: here is inconsistent with the Webhook's payload: webhook uses ShortName
584+
RefType: string(refFullName.RefType()),
585585
PusherType: api.PusherTypeUser,
586586
Repo: apiRepo,
587587
Sender: apiPusher,

services/context/repo.go

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,7 @@ func (r *Repository) GetCommitGraphsCount(ctx context.Context, hidePRRefs bool,
210210
// * "commit/123456"
211211
// It is usually used to construct a link like ".../src/{{RefTypeNameSubURL}}/{{PathEscapeSegments TreePath}}"
212212
func (r *Repository) RefTypeNameSubURL() string {
213-
switch {
214-
case r.IsViewBranch:
215-
return "branch/" + util.PathEscapeSegments(r.BranchName)
216-
case r.IsViewTag:
217-
return "tag/" + util.PathEscapeSegments(r.TagName)
218-
case r.IsViewCommit:
219-
return "commit/" + util.PathEscapeSegments(r.CommitID)
220-
}
221-
log.Error("Unknown view type for repo: %v", r)
222-
return ""
213+
return r.RefFullName.RefWebLinkPath()
223214
}
224215

225216
// GetEditorconfig returns the .editorconfig definition if found in the
@@ -695,7 +686,6 @@ const (
695686
RepoRefBranch
696687
RepoRefTag
697688
RepoRefCommit
698-
RepoRefBlob
699689
)
700690

701691
const headRefName = "HEAD"
@@ -734,9 +724,6 @@ func getRefNameLegacy(ctx *Base, repo *Repository, reqPath, extraRef string) (st
734724
repo.TreePath = strings.Join(reqRefPathParts[1:], "/")
735725
return reqRefPathParts[0], RepoRefCommit
736726
}
737-
if refName := getRefName(ctx, repo, reqPath, RepoRefBlob); refName != "" {
738-
return refName, RepoRefBlob
739-
}
740727
// FIXME: the old code falls back to default branch if "ref" doesn't exist, there could be an edge case:
741728
// "README?ref=no-such" would read the README file from the default branch, but the user might expect a 404
742729
repo.TreePath = reqPath
@@ -794,12 +781,6 @@ func getRefName(ctx *Base, repo *Repository, path string, pathType RepoRefType)
794781
repo.TreePath = strings.Join(parts[1:], "/")
795782
return commit.ID.String()
796783
}
797-
case RepoRefBlob:
798-
_, err := repo.GitRepo.GetBlob(path)
799-
if err != nil {
800-
return ""
801-
}
802-
return path
803784
default:
804785
panic(fmt.Sprintf("Unrecognized path type: %v", pathType))
805786
}

0 commit comments

Comments
 (0)