Skip to content

Commit 92796dc

Browse files
wolfogredelvhlafrikslunny
authored
Use complete SHA to create and query commit status (#22244) (#22258)
Backport #22244. Fix #13485. Co-authored-by: delvh <[email protected]> Co-authored-by: Lauris BH <[email protected]> Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: delvh <[email protected]> Co-authored-by: Lauris BH <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 4845093 commit 92796dc

File tree

19 files changed

+67
-24
lines changed

19 files changed

+67
-24
lines changed

integrations/repo_commits_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) {
6666
reqOne := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/"+path.Base(commitURL)+"/status")
6767
testRepoCommitsWithStatus(t, session.MakeRequest(t, req, http.StatusOK), session.MakeRequest(t, reqOne, http.StatusOK), state)
6868

69+
// By short SHA
70+
req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/"+path.Base(commitURL)[:10]+"/statuses")
71+
reqOne = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/"+path.Base(commitURL)[:10]+"/status")
72+
testRepoCommitsWithStatus(t, session.MakeRequest(t, req, http.StatusOK), session.MakeRequest(t, reqOne, http.StatusOK), state)
73+
6974
// By Ref
7075
req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/master/statuses")
7176
reqOne = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/master/status")

models/action.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ func (a *Action) GetRefLink() string {
287287
return a.GetRepoLink() + "/src/branch/" + util.PathEscapeSegments(strings.TrimPrefix(a.RefName, git.BranchPrefix))
288288
case strings.HasPrefix(a.RefName, git.TagPrefix):
289289
return a.GetRepoLink() + "/src/tag/" + util.PathEscapeSegments(strings.TrimPrefix(a.RefName, git.TagPrefix))
290-
case len(a.RefName) == 40 && git.IsValidSHAPattern(a.RefName):
290+
case len(a.RefName) == git.SHAFullLength && git.IsValidSHAPattern(a.RefName):
291291
return a.GetRepoLink() + "/src/commit/" + a.RefName
292292
default:
293293
// FIXME: we will just assume it's a branch - this was the old way - at some point we may want to enforce that there is always a ref here.

models/git/commit_status.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ func NewCommitStatus(opts NewCommitStatusOptions) error {
291291
return fmt.Errorf("NewCommitStatus[%s, %s]: no user specified", repoPath, opts.SHA)
292292
}
293293

294+
if _, err := git.NewIDFromString(opts.SHA); err != nil {
295+
return fmt.Errorf("NewCommitStatus[%s, %s]: invalid sha: %w", repoPath, opts.SHA, err)
296+
}
297+
294298
// Get the next Status Index
295299
idx, err := GetNextCommitStatusIndex(opts.Repo.ID, opts.SHA)
296300
if err != nil {

modules/context/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ func RepoRefForAPI(next http.Handler) http.Handler {
388388
return
389389
}
390390
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
391-
} else if len(refName) == 40 {
391+
} else if len(refName) == git.SHAFullLength {
392392
ctx.Repo.CommitID = refName
393393
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName)
394394
if err != nil {

modules/context/repo.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
807807
}
808808
// For legacy and API support only full commit sha
809809
parts := strings.Split(path, "/")
810-
if len(parts) > 0 && len(parts[0]) == 40 {
810+
if len(parts) > 0 && len(parts[0]) == git.SHAFullLength {
811811
ctx.Repo.TreePath = strings.Join(parts[1:], "/")
812812
return parts[0]
813813
}
@@ -843,7 +843,7 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
843843
return getRefNameFromPath(ctx, path, ctx.Repo.GitRepo.IsTagExist)
844844
case RepoRefCommit:
845845
parts := strings.Split(path, "/")
846-
if len(parts) > 0 && len(parts[0]) >= 7 && len(parts[0]) <= 40 {
846+
if len(parts) > 0 && len(parts[0]) >= 7 && len(parts[0]) <= git.SHAFullLength {
847847
ctx.Repo.TreePath = strings.Join(parts[1:], "/")
848848
return parts[0]
849849
}
@@ -952,7 +952,7 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
952952
return
953953
}
954954
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
955-
} else if len(refName) >= 7 && len(refName) <= 40 {
955+
} else if len(refName) >= 7 && len(refName) <= git.SHAFullLength {
956956
ctx.Repo.IsViewCommit = true
957957
ctx.Repo.CommitID = refName
958958

@@ -962,7 +962,7 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
962962
return
963963
}
964964
// If short commit ID add canonical link header
965-
if len(refName) < 40 {
965+
if len(refName) < git.SHAFullLength {
966966
ctx.RespHeader().Set("Link", fmt.Sprintf("<%s>; rel=\"canonical\"",
967967
util.URLJoin(setting.AppURL, strings.Replace(ctx.Req.URL.RequestURI(), util.PathEscapeSegments(refName), url.PathEscape(ctx.Repo.Commit.ID.String()), 1))))
968968
}

modules/git/repo_commit_gogit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (repo *Repository) RemoveReference(name string) error {
4242

4343
// ConvertToSHA1 returns a Hash object from a potential ID string
4444
func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
45-
if len(commitID) == 40 {
45+
if len(commitID) == SHAFullLength {
4646
sha1, err := NewIDFromString(commitID)
4747
if err == nil {
4848
return sha1, nil

modules/git/repo_commit_nogogit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (repo *Repository) getCommitFromBatchReader(rd *bufio.Reader, id SHA1) (*Co
138138

139139
// ConvertToSHA1 returns a Hash object from a potential ID string
140140
func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
141-
if len(commitID) == 40 && IsValidSHAPattern(commitID) {
141+
if len(commitID) == SHAFullLength && IsValidSHAPattern(commitID) {
142142
sha1, err := NewIDFromString(commitID)
143143
if err == nil {
144144
return sha1, nil

modules/git/repo_index.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
// ReadTreeToIndex reads a treeish to the index
1919
func (repo *Repository) ReadTreeToIndex(treeish string, indexFilename ...string) error {
20-
if len(treeish) != 40 {
20+
if len(treeish) != SHAFullLength {
2121
res, _, err := NewCommand(repo.Ctx, "rev-parse", "--verify", treeish).RunStdString(&RunOpts{Dir: repo.Path})
2222
if err != nil {
2323
return err

modules/git/repo_tree_gogit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (repo *Repository) getTree(id SHA1) (*Tree, error) {
2020

2121
// GetTree find the tree object in the repository.
2222
func (repo *Repository) GetTree(idStr string) (*Tree, error) {
23-
if len(idStr) != 40 {
23+
if len(idStr) != SHAFullLength {
2424
res, _, err := NewCommand(repo.Ctx, "rev-parse", "--verify", idStr).RunStdString(&RunOpts{Dir: repo.Path})
2525
if err != nil {
2626
return nil, err

modules/git/repo_tree_nogogit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (repo *Repository) getTree(id SHA1) (*Tree, error) {
6767

6868
// GetTree find the tree object in the repository.
6969
func (repo *Repository) GetTree(idStr string) (*Tree, error) {
70-
if len(idStr) != 40 {
70+
if len(idStr) != SHAFullLength {
7171
res, err := repo.GetRefCommitID(idStr)
7272
if err != nil {
7373
return nil, err

0 commit comments

Comments
 (0)