[API] Get a single commit via Ref (#10915)
* GET /repos/:owner/:repo/commits/:ref * add Validation Checks * Fix & Extend TEST * add two new tast cases
This commit is contained in:
parent
71979d9663
commit
3d63caa542
7 changed files with 172 additions and 33 deletions
|
@ -386,7 +386,6 @@ func ToCommitUser(sig *git.Signature) *api.CommitUser {
|
|||
func ToCommitMeta(repo *models.Repository, tag *git.Tag) *api.CommitMeta {
|
||||
return &api.CommitMeta{
|
||||
SHA: tag.Object.String(),
|
||||
// TODO: Add the /commits API endpoint and use it here (https://developer.github.com/v3/repos/commits/#get-a-single-commit)
|
||||
URL: util.URLJoin(repo.APIURL(), "git/commits", tag.ID.String()),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ package git
|
|||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/go-git/go-git/v5/plumbing"
|
||||
|
@ -16,6 +17,9 @@ import (
|
|||
// EmptySHA defines empty git SHA
|
||||
const EmptySHA = "0000000000000000000000000000000000000000"
|
||||
|
||||
// SHAPattern can be used to determine if a string is an valid sha
|
||||
var SHAPattern = regexp.MustCompile(`^[0-9a-f]{4,40}$`)
|
||||
|
||||
// SHA1 a git commit name
|
||||
type SHA1 = plumbing.Hash
|
||||
|
||||
|
|
|
@ -22,12 +22,32 @@ const (
|
|||
)
|
||||
|
||||
var (
|
||||
// GitRefNamePattern is regular expression with unallowed characters in git reference name
|
||||
// GitRefNamePatternInvalid is regular expression with unallowed characters in git reference name
|
||||
// They cannot have ASCII control characters (i.e. bytes whose values are lower than \040, or \177 DEL), space, tilde ~, caret ^, or colon : anywhere.
|
||||
// They cannot have question-mark ?, asterisk *, or open bracket [ anywhere
|
||||
GitRefNamePattern = regexp.MustCompile(`[\000-\037\177 \\~^:?*[]+`)
|
||||
GitRefNamePatternInvalid = regexp.MustCompile(`[\000-\037\177 \\~^:?*[]+`)
|
||||
)
|
||||
|
||||
// CheckGitRefAdditionalRulesValid check name is valid on additional rules
|
||||
func CheckGitRefAdditionalRulesValid(name string) bool {
|
||||
|
||||
// Additional rules as described at https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
|
||||
if strings.HasPrefix(name, "/") || strings.HasSuffix(name, "/") ||
|
||||
strings.HasSuffix(name, ".") || strings.Contains(name, "..") ||
|
||||
strings.Contains(name, "//") || strings.Contains(name, "@{") ||
|
||||
name == "@" {
|
||||
return false
|
||||
}
|
||||
parts := strings.Split(name, "/")
|
||||
for _, part := range parts {
|
||||
if strings.HasSuffix(part, ".lock") || strings.HasPrefix(part, ".") {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// AddBindingRules adds additional binding rules
|
||||
func AddBindingRules() {
|
||||
addGitRefNameBindingRule()
|
||||
|
@ -44,25 +64,15 @@ func addGitRefNameBindingRule() {
|
|||
IsValid: func(errs binding.Errors, name string, val interface{}) (bool, binding.Errors) {
|
||||
str := fmt.Sprintf("%v", val)
|
||||
|
||||
if GitRefNamePattern.MatchString(str) {
|
||||
if GitRefNamePatternInvalid.MatchString(str) {
|
||||
errs.Add([]string{name}, ErrGitRefName, "GitRefName")
|
||||
return false, errs
|
||||
}
|
||||
// Additional rules as described at https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
|
||||
if strings.HasPrefix(str, "/") || strings.HasSuffix(str, "/") ||
|
||||
strings.HasSuffix(str, ".") || strings.Contains(str, "..") ||
|
||||
strings.Contains(str, "//") || strings.Contains(str, "@{") ||
|
||||
str == "@" {
|
||||
|
||||
if !CheckGitRefAdditionalRulesValid(str) {
|
||||
errs.Add([]string{name}, ErrGitRefName, "GitRefName")
|
||||
return false, errs
|
||||
}
|
||||
parts := strings.Split(str, "/")
|
||||
for _, part := range parts {
|
||||
if strings.HasSuffix(part, ".lock") || strings.HasPrefix(part, ".") {
|
||||
errs.Add([]string{name}, ErrGitRefName, "GitRefName")
|
||||
return false, errs
|
||||
}
|
||||
}
|
||||
|
||||
return true, errs
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue