浏览代码

[GinS] Indexing should also start on web changes

cgars 7 年之前
父节点
当前提交
e04420723c
共有 2 个文件被更改,包括 30 次插入22 次删除
  1. 1 22
      cmd/serv.go
  2. 29 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)

+ 29 - 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"
 )
 
 // ___________    .___.__  __    ___________.__.__
@@ -168,6 +171,9 @@ func (repo *Repository) UpdateRepoFile(doer *User, opts UpdateRepoFileOptions) (
 	}
 
 	go AddTestPullRequestTask(doer, repo.ID, opts.NewBranch, true)
+	if setting.Search.Do {
+		StartIndexing(doer, repo.MustOwner(), repo)
+	}
 	return nil
 }
 
@@ -557,6 +563,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
+	}
+}