Sfoglia il codice sorgente

[GinS] Indexing should also start on web changes

cgars 7 anni fa
parent
commit
34fa3483d9
2 ha cambiato i file con 65 aggiunte e 22 eliminazioni
  1. 1 22
      cmd/serv.go
  2. 64 0
      models/repo_editor.go

+ 1 - 22
cmd/serv.go

@@ -21,9 +21,6 @@ import (
 	"github.com/G-Node/gogs/pkg/setting"
 	http "github.com/G-Node/gogs/routes/repo"
 	"syscall"
-	"encoding/json"
-	http2 "net/http"
-	"bytes"
 )
 
 const (
@@ -277,30 +274,12 @@ func runServ(c *cli.Context) error {
 	}
 	runGit(cmd, requestMode, user, owner, repo)
 	if setting.Search.Do && (requestMode == models.ACCESS_MODE_WRITE) {
-		startIndexing(user, owner, repo)
+		models.StartIndexing(user, owner, repo)
 	}
 	return nil
 
 }
 
-func startIndexing(user, owner *models.User, repo *models.Repository) {
-	var ireq struct{ RepoID, RepoPath string }
-	ireq.RepoID = fmt.Sprintf("%d", repo.ID)
-	ireq.RepoPath = repo.RepoPath()
-	data, err := json.Marshal(ireq)
-	if err != nil {
-		log.Trace("could not marshal index request :%+v", err)
-		return
-	}
-	req, _ := http2.NewRequest(http2.MethodPost, setting.Search.IndexUrl, bytes.NewReader(data))
-	client := http2.Client{}
-	resp, err := client.Do(req)
-	if err != nil || resp.StatusCode != http2.StatusOK {
-		log.Trace("Error doing index request:%+v,%d", err, resp.StatusCode)
-		return
-	}
-}
-
 func runGit(cmd []string, requestMode models.AccessMode, user *models.User, owner *models.User,
 	repo *models.Repository) error {
 	log.Info("will exectute:%s", cmd)

+ 64 - 0
models/repo_editor.go

@@ -24,6 +24,9 @@ import (
 	"github.com/G-Node/gogs/pkg/process"
 	"github.com/G-Node/gogs/pkg/setting"
 	"github.com/G-Node/go-annex"
+	"encoding/json"
+	"bytes"
+	"net/http"
 )
 
 const (
@@ -192,6 +195,44 @@ func (repo *Repository) UpdateRepoFile(doer *User, opts UpdateRepoFileOptions) (
 		})); err != nil {
 		return fmt.Errorf("git push origin %s: %v", opts.NewBranch, err)
 	}
+
+	gitRepo, err := git.OpenRepository(repo.RepoPath())
+	if err != nil {
+		log.Error(2, "OpenRepository: %v", err)
+		return nil
+	}
+	commit, err := gitRepo.GetBranchCommit(opts.NewBranch)
+	if err != nil {
+		log.Error(2, "GetBranchCommit [branch: %s]: %v", opts.NewBranch, err)
+		return nil
+	}
+
+	// Simulate push event.
+	pushCommits := &PushCommits{
+		Len:     1,
+		Commits: []*PushCommit{CommitToPushCommit(commit)},
+	}
+	oldCommitID := opts.LastCommitID
+	if opts.NewBranch != opts.OldBranch {
+		oldCommitID = git.EMPTY_SHA
+	}
+	if err := CommitRepoAction(CommitRepoActionOptions{
+		PusherName:  doer.Name,
+		RepoOwnerID: repo.MustOwner().ID,
+		RepoName:    repo.Name,
+		RefFullName: git.BRANCH_PREFIX + opts.NewBranch,
+		OldCommitID: oldCommitID,
+		NewCommitID: commit.ID.String(),
+		Commits:     pushCommits,
+	}); err != nil {
+		log.Error(2, "CommitRepoAction: %v", err)
+		return nil
+	}
+
+	go AddTestPullRequestTask(doer, repo.ID, opts.NewBranch, true)
+	if setting.Search.Do {
+		StartIndexing(doer, repo.MustOwner(), repo)
+	}
 	return nil
 }
 
@@ -575,6 +616,29 @@ func (repo *Repository) UploadRepoFiles(doer *User, opts UploadRepoFileOptions)
 		log.Error(1, "Annex uninit failed with error: %v,%s, at: %s/ This repository might fail at "+
 			"subsequent uploads!", err, msg, localPath)
 	}
+	// Indexing support
+	if setting.Search.Do {
+		StartIndexing(doer, repo.MustOwner(), repo)
+	}
+
 	RemoveAllWithNotice("Cleaning out after upload", localPath)
 	return DeleteUploads(uploads...)
 }
+
+func StartIndexing(user, owner *User, repo *Repository) {
+	var ireq struct{ RepoID, RepoPath string }
+	ireq.RepoID = fmt.Sprintf("%d", repo.ID)
+	ireq.RepoPath = repo.RepoPath()
+	data, err := json.Marshal(ireq)
+	if err != nil {
+		log.Trace("could not marshal index request :%+v", err)
+		return
+	}
+	req, _ := http.NewRequest(http.MethodPost, setting.Search.IndexUrl, bytes.NewReader(data))
+	client := http.Client{}
+	resp, err := client.Do(req)
+	if err != nil || resp.StatusCode != http.StatusOK {
+		log.Trace("Error doing index request:%+v,%d", err, resp.StatusCode)
+		return
+	}
+}