Enable a way to checkout Pull Requests from remote refs

This commit is contained in:
Martin Hartkorn 2016-01-30 23:56:38 +01:00
parent 6e03f61617
commit 20403f75fb
4 changed files with 159 additions and 0 deletions

View file

@ -5,6 +5,7 @@
package models
import (
"errors"
"fmt"
"os"
"path"
@ -395,6 +396,44 @@ func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequ
Join("INNER", "issue", "issue.id=pull_request.issue_id").Find(&prs)
}
// Gets a Pull Request by the path of the forked repo and the branch from where the PR
// got submitted.
func GetUnmergedPullRequestByRepoPathAndHeadBranch(user, repo, branch string) (*PullRequest, error) {
userLower := strings.ToLower(user)
repoLower := strings.ToLower(repo)
pr := new(PullRequest)
if x == nil {
return nil, errors.New("Fail")
}
has, err := x.
Where("head_user_name=? AND head_branch=? AND has_merged=? AND issue.is_closed=? AND repository.lower_name=?", userLower, branch, 0, 0, repoLower).
Join("INNER", "repository", "repository.id=pull_request.head_repo_id").
Join("INNER", "issue", "issue.id=pull_request.issue_id").
Get(pr)
if err != nil {
return nil, err
} else if !has {
return nil, ErrPullRequestNotExist{0, 0, 0, 0, branch, ""}
}
baseRepo := new(Repository)
has, err = x.Where("repository.id=?", pr.BaseRepoID).
Join("LEFT", "user", "user.id=repository.owner_id").
Get(baseRepo)
if err != nil {
return nil, err
} else if !has {
return nil, ErrRepoNotExist{pr.BaseRepoID, 0, ""}
}
pr.BaseRepo = baseRepo
return pr, nil
}
// GetPullRequestByID returns a pull request by given ID.
func GetPullRequestByID(id int64) (*PullRequest, error) {
pr := new(PullRequest)

View file

@ -39,6 +39,7 @@ import (
const (
_TPL_UPDATE_HOOK = "#!/usr/bin/env %s\n%s update $1 $2 $3 --config='%s'\n"
_TPL_POST_RECEIVE_HOOK = "#!/usr/bin/env %s\n%s pull --path \"$(pwd)\"\n"
)
var (
@ -596,6 +597,11 @@ func createUpdateHook(repoPath string) error {
fmt.Sprintf(_TPL_UPDATE_HOOK, setting.ScriptType, "\""+setting.AppPath+"\"", setting.CustomConf))
}
func createPostReceiveHook(repoPath string) error {
return git.SetPostReceiveHook(repoPath,
fmt.Sprintf(_TPL_POST_RECEIVE_HOOK, setting.ScriptType, "\""+setting.AppPath+"\""))
}
type MigrateRepoOptions struct {
Name string
Description string
@ -818,6 +824,8 @@ func initRepository(e Engine, repoPath string, u *User, repo *Repository, opts C
return fmt.Errorf("InitRepository: %v", err)
} else if err = createUpdateHook(repoPath); err != nil {
return fmt.Errorf("createUpdateHook: %v", err)
} else if err = createPostReceiveHook(repoPath); err != nil {
return fmt.Errorf("createPostReceiveHook: %v", err)
}
tmpDir := filepath.Join(os.TempDir(), "gogs-"+repo.Name+"-"+com.ToStr(time.Now().Nanosecond()))
@ -1498,6 +1506,15 @@ func RewriteRepositoryUpdateHook() error {
})
}
// RewriteRepositoryPostReceiveHook rewrites all repositories' update hook.
func RewriteRepositoryPostReceiveHook() error {
return x.Where("id > 0").Iterate(new(Repository),
func(idx int, bean interface{}) error {
repo := bean.(*Repository)
return createPostReceiveHook(repo.RepoPath())
})
}
// statusPool represents a pool of status with true/false.
type statusPool struct {
lock sync.RWMutex
@ -2045,6 +2062,8 @@ func ForkRepository(u *User, oldRepo *Repository, name, desc string) (_ *Reposit
if err = createUpdateHook(repoPath); err != nil {
return nil, fmt.Errorf("createUpdateHook: %v", err)
} else if err = createPostReceiveHook(repoPath); err != nil {
return nil, fmt.Errorf("createPostReceiveHook: %v", err)
}
return repo, sess.Commit()