markup: render SHA links without branch prefix (#6350)

Co-authored-by: Zhukov Roman <zhukov.roman@gmail.com>
Co-authored-by: ᴜɴᴋɴᴡᴏɴ <u@gogs.io>
pull/6378/head
Jeff 2020-09-29 23:07:16 +08:00 committed by ᴜɴᴋɴᴡᴏɴ
parent 6b6bfe8bb0
commit e6b4c467e8
No known key found for this signature in database
GPG Key ID: B43718D76E30A238
5 changed files with 66 additions and 16 deletions

View File

@ -21,6 +21,7 @@ All notable changes to Gogs are documented in this file.
- Backup can be processed when `--target` is specified on Windows. [#6339](https://github.com/gogs/gogs/issues/6339) - Backup can be processed when `--target` is specified on Windows. [#6339](https://github.com/gogs/gogs/issues/6339)
- Commit message contains keywords look like an issue reference no longer fails the push entirely. [#6289](https://github.com/gogs/gogs/issues/6289) - Commit message contains keywords look like an issue reference no longer fails the push entirely. [#6289](https://github.com/gogs/gogs/issues/6289)
- _Regression:_ When running Gogs on Windows, push commits no longer fail on a daily basis with the error "pre-receive hook declined". [#6316](https://github.com/gogs/gogs/issues/6316) - _Regression:_ When running Gogs on Windows, push commits no longer fail on a daily basis with the error "pre-receive hook declined". [#6316](https://github.com/gogs/gogs/issues/6316)
- Auto-linked commit SHAs now have correct links. [#6300](https://github.com/gogs/gogs/issues/6300)
### Removed ### Removed

View File

@ -426,24 +426,29 @@ func (repo *Repository) UpdateSize() error {
return nil return nil
} }
// ComposeMetas composes a map of metas for rendering external issue tracker URL. // ComposeMetas composes a map of metas for rendering SHA1 URL and external issue tracker URL.
func (repo *Repository) ComposeMetas() map[string]string { func (repo *Repository) ComposeMetas() map[string]string {
if !repo.EnableExternalTracker { if repo.ExternalMetas != nil {
return nil return repo.ExternalMetas
} else if repo.ExternalMetas == nil { }
repo.ExternalMetas = map[string]string{
"format": repo.ExternalTrackerFormat, repo.ExternalMetas = map[string]string{
"user": repo.MustOwner().Name, "repoLink": repo.Link(),
"repo": repo.Name, }
}
if repo.EnableExternalTracker {
repo.ExternalMetas["user"] = repo.MustOwner().Name
repo.ExternalMetas["repo"] = repo.Name
repo.ExternalMetas["format"] = repo.ExternalTrackerFormat
switch repo.ExternalTrackerStyle { switch repo.ExternalTrackerStyle {
case markup.ISSUE_NAME_STYLE_ALPHANUMERIC: case markup.ISSUE_NAME_STYLE_ALPHANUMERIC:
repo.ExternalMetas["style"] = markup.ISSUE_NAME_STYLE_ALPHANUMERIC repo.ExternalMetas["style"] = markup.ISSUE_NAME_STYLE_ALPHANUMERIC
default: default:
repo.ExternalMetas["style"] = markup.ISSUE_NAME_STYLE_NUMERIC repo.ExternalMetas["style"] = markup.ISSUE_NAME_STYLE_NUMERIC
} }
} }
return repo.ExternalMetas return repo.ExternalMetas
} }

View File

@ -19,14 +19,19 @@ func TestRepository_ComposeMetas(t *testing.T) {
t.Run("no external tracker is configured", func(t *testing.T) { t.Run("no external tracker is configured", func(t *testing.T) {
repo.EnableExternalTracker = false repo.EnableExternalTracker = false
assert.Equal(t, map[string]string(nil), repo.ComposeMetas())
// Should be nil even if other settings are present metas := repo.ComposeMetas()
repo.ExternalTrackerStyle = markup.ISSUE_NAME_STYLE_NUMERIC assert.Equal(t, metas["repoLink"], repo.Link())
assert.Equal(t, map[string]string(nil), repo.ComposeMetas())
// Should no format and style if no external tracker is configured
_, ok := metas["format"]
assert.False(t, ok)
_, ok = metas["style"]
assert.False(t, ok)
}) })
t.Run("an external issue tracker is configured", func(t *testing.T) { t.Run("an external issue tracker is configured", func(t *testing.T) {
repo.ExternalMetas = nil
repo.EnableExternalTracker = true repo.EnableExternalTracker = true
// Default to numeric issue style // Default to numeric issue style

View File

@ -145,7 +145,8 @@ func RenderSha1CurrentPattern(rawBytes []byte, urlPrefix string) []byte {
if com.StrTo(m).MustInt() > 0 { if com.StrTo(m).MustInt() > 0 {
return m return m
} }
return fmt.Sprintf(`<a href="%s/commit/%s"><code>%s</code></a>`, urlPrefix, m, tool.ShortSHA1(string(m)))
return fmt.Sprintf(`<a href="%s/commit/%s"><code>%s</code></a>`, urlPrefix, m, tool.ShortSHA1(m))
})) }))
} }
@ -160,7 +161,7 @@ func RenderSpecialLink(rawBytes []byte, urlPrefix string, metas map[string]strin
rawBytes = RenderIssueIndexPattern(rawBytes, urlPrefix, metas) rawBytes = RenderIssueIndexPattern(rawBytes, urlPrefix, metas)
rawBytes = RenderCrossReferenceIssueIndexPattern(rawBytes, urlPrefix, metas) rawBytes = RenderCrossReferenceIssueIndexPattern(rawBytes, urlPrefix, metas)
rawBytes = RenderSha1CurrentPattern(rawBytes, urlPrefix) rawBytes = RenderSha1CurrentPattern(rawBytes, metas["repoLink"])
return rawBytes return rawBytes
} }

View File

@ -215,3 +215,41 @@ func Test_RenderIssueIndexPattern(t *testing.T) {
}) })
}) })
} }
func TestRenderSha1CurrentPattern(t *testing.T) {
metas := map[string]string{
"repoLink": "/someuser/somerepo",
}
tests := []struct {
desc string
input string
prefix string
expVal string
}{
{
desc: "Full SHA (40 symbols)",
input: "ad8ced4f57d9068cb2874557245be3c7f341149d",
prefix: metas["repoLink"],
expVal: `<a href="/someuser/somerepo/commit/ad8ced4f57d9068cb2874557245be3c7f341149d"><code>ad8ced4f57</code></a>`,
},
{
desc: "Short SHA (8 symbols)",
input: "ad8ced4f",
prefix: metas["repoLink"],
expVal: `<a href="/someuser/somerepo/commit/ad8ced4f"><code>ad8ced4f</code></a>`,
},
{
desc: "9 digits",
input: "123456789",
prefix: metas["repoLink"],
expVal: "123456789",
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
assert.Equal(t, test.expVal, string(RenderSha1CurrentPattern([]byte(test.input), test.prefix)))
})
}
}