forked from Gitlink/gitea-1120-rc1
modify accesslog, record git push event
This commit is contained in:
parent
9ee9bc6945
commit
19545f4cee
|
|
@ -1,13 +1,154 @@
|
|||
// package userlog
|
||||
package userlog
|
||||
|
||||
// import (
|
||||
// "fmt"
|
||||
// "path"
|
||||
// "code.gitea.io/gitea/modules/log"
|
||||
// "code.gitea.io/gitea/modules/setting"
|
||||
// )
|
||||
import (
|
||||
// "fmt"
|
||||
// "path"
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/gitgraph"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
repo_module "code.gitea.io/gitea/modules/repository"
|
||||
"fmt"
|
||||
"time"
|
||||
//"net/http"
|
||||
)
|
||||
|
||||
// func init(){
|
||||
// filename := path.Join(setting.LogRootPath, "userlog.log")
|
||||
// log.NewLogger(1000, "userlog", "file", fmt.Sprintf(`{"filename":"%s"}`, filename))
|
||||
// }
|
||||
// }
|
||||
type GraphItem = gitgraph.GraphItem
|
||||
type GraphItems = gitgraph.GraphItems
|
||||
|
||||
type LogTemplate struct {
|
||||
login string
|
||||
time string
|
||||
method string
|
||||
uri string
|
||||
proto string
|
||||
response_code string
|
||||
agent string
|
||||
request_body string
|
||||
}
|
||||
|
||||
func GetGraphAndBranch(ctx *context.Context, repo *models.Repository) ([]GraphItem, []*git.Branch) {
|
||||
if repo.IsEmpty { // 项目是否为空
|
||||
//ctx.JSON(409, api.APIError{
|
||||
// Message: "Git Repository is empty.",
|
||||
// URL: setting.API.SwaggerURL,
|
||||
//})
|
||||
return []GraphItem{}, nil
|
||||
}
|
||||
|
||||
gitRepo, err := git.OpenRepository(repo.RepoPath())
|
||||
if err != nil {
|
||||
//ctx.ServerError("OpenRepository", err)
|
||||
return []GraphItem{}, nil
|
||||
}
|
||||
graph, err := gitgraph.GetCommitGraphNoPage(gitRepo)
|
||||
if err != nil {
|
||||
//ctx.ServerError("GetCommitGraph", err)
|
||||
return []GraphItem{}, nil
|
||||
}
|
||||
branches, err := repo_module.GetBranches(repo)
|
||||
if err != nil {
|
||||
//ctx.Error(http.StatusInternalServerError, "GetBranches", err)
|
||||
return []GraphItem{}, nil
|
||||
}
|
||||
return graph, branches
|
||||
}
|
||||
|
||||
func CompareCommitAndBranch(ctx *context.Context, repo *models.Repository, oldGraph []GraphItem, oldBranches []*git.Branch) {
|
||||
newGraph, newBranches := GetGraphAndBranch(ctx, repo)
|
||||
oldGraphLen := len(oldGraph)
|
||||
oldBranchLen := len(oldBranches)
|
||||
newGraphLen := len(newGraph)
|
||||
newBranchLen := len(newBranches)
|
||||
|
||||
identity := "-"
|
||||
if val, ok := ctx.Data["SignedUserName"]; ok {
|
||||
if stringVal, ok := val.(string); ok && stringVal != "" {
|
||||
identity = stringVal
|
||||
}
|
||||
}
|
||||
|
||||
if newGraphLen > oldGraphLen {
|
||||
str := StringfyGraph(CompareGraph(newGraph, oldGraph))
|
||||
logTemplate := LogTemplate{
|
||||
login: " "+identity,
|
||||
time: " "+time.Now().Format("2006-01-02 15:04:05 -0700"),
|
||||
method: " "+ctx.Req.Method,
|
||||
uri: " "+ctx.Req.URL.RequestURI(),
|
||||
proto: " "+ctx.Req.Proto,
|
||||
response_code: " -",
|
||||
agent: " "+ctx.Req.UserAgent(),
|
||||
request_body: " "+str,
|
||||
}
|
||||
accessLogger := log.GetLogger("access")
|
||||
accessLogger.SendLog(log.INFO, "", "", 0, fmt.Sprintf("%+v\n", logTemplate), "")
|
||||
}
|
||||
|
||||
if newBranchLen != oldBranchLen{
|
||||
var str string
|
||||
if newBranchLen > oldBranchLen {
|
||||
branch := FindBranch(newBranches, oldBranches)
|
||||
str = fmt.Sprintf("{action: createBranch, branch_name: %v}", branch.Name)
|
||||
}
|
||||
|
||||
if newBranchLen < oldBranchLen {
|
||||
branch := FindBranch(oldBranches, newBranches)
|
||||
str = fmt.Sprintf("{action: createBranch, branch_name: %v}", branch.Name)
|
||||
}
|
||||
logTemplate := LogTemplate{
|
||||
login: " "+identity,
|
||||
time: " "+time.Now().Format("2006-01-02 15:04:05 -0700"),
|
||||
method: " "+ctx.Req.Method,
|
||||
uri: " "+ctx.Req.URL.RequestURI(),
|
||||
proto: " "+ctx.Req.Proto,
|
||||
response_code: " -",
|
||||
agent: " "+ctx.Req.UserAgent(),
|
||||
request_body: " "+str,
|
||||
}
|
||||
accessLogger := log.GetLogger("access")
|
||||
accessLogger.SendLog(log.INFO, "", "", 0, fmt.Sprintf("%+v\n", logTemplate), "")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func CompareGraph(newGraph []GraphItem, oldGraph []GraphItem)([]GraphItem) {
|
||||
resultLen := len(newGraph) - len(oldGraph)
|
||||
result := make([]GraphItem, resultLen)
|
||||
ptr1 := 0
|
||||
ptr2 := 0
|
||||
count := 0
|
||||
for ; count < resultLen; {
|
||||
for ; (ptr1<len(newGraph))&&(ptr2 >= len(oldGraph) || newGraph[ptr1].Rev != oldGraph[ptr2].Rev); {
|
||||
result[count] = newGraph[ptr1]
|
||||
count += 1
|
||||
ptr1 += 1
|
||||
}
|
||||
ptr1 += 1
|
||||
ptr2 += 1
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func StringfyGraph(Graph []GraphItem)(string){
|
||||
str := "{action: commit, payload: ["
|
||||
for i := 0; i < len(Graph); i++{
|
||||
str += fmt.Sprintf("%+v, ", Graph[i])
|
||||
}
|
||||
str += "]}"
|
||||
return str
|
||||
}
|
||||
|
||||
func FindBranch(longerBranches []*git.Branch, shorterBranches []*git.Branch) (*git.Branch) {
|
||||
for i := 0; i < len(shorterBranches); i++{
|
||||
if longerBranches[i].Name != shorterBranches[i].Name{
|
||||
return longerBranches[i]
|
||||
}
|
||||
}
|
||||
return longerBranches[len(shorterBranches)]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,8 +10,8 @@ import (
|
|||
"compress/gzip"
|
||||
gocontext "context"
|
||||
"fmt"
|
||||
//"io"
|
||||
"io/ioutil"
|
||||
// "io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
|
@ -33,6 +33,7 @@ import (
|
|||
"code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
repo_service "code.gitea.io/gitea/services/repository"
|
||||
"code.gitea.io/gitea/modules/userlog"
|
||||
)
|
||||
|
||||
// HTTP implmentation git smart HTTP protocol
|
||||
|
|
@ -369,8 +370,9 @@ func HTTP(ctx *context.Context) {
|
|||
ctx.NotFound("Smart Git HTTP", err)
|
||||
return
|
||||
}
|
||||
|
||||
graph, branches := userlog.GetGraphAndBranch(ctx, repo)
|
||||
route.handler(serviceHandler{cfg, w, r, dir, file, cfg.Env})
|
||||
userlog.CompareCommitAndBranch(ctx, repo, graph, branches)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -571,10 +573,13 @@ func serviceRPC(h serviceHandler, service string) {
|
|||
cmd.Stdin = reqBody
|
||||
cmd.Stderr = &stderr
|
||||
|
||||
// var gitcontent bytes.Buffer
|
||||
// _ = io.MultiWriter(h.w, &gitcontent)
|
||||
// accessLogger := log.GetLogger("access")
|
||||
// accessLogger.SendLog(log.INFO, "", "", 0, gitcontent.String(), "")
|
||||
//var gitcontent bytes.Buffer
|
||||
//rsp := io.MultiWriter(h.w, &gitcontent)
|
||||
//h.w = rsp
|
||||
//buf := new(bytes.Buffer)
|
||||
//buf.ReadFrom(reqBody)
|
||||
//accessLogger := log.GetLogger("access")
|
||||
//accessLogger.SendLog(log.INFO, "", "", 0, "-------------\n"+buf.String()+"\n-------------\n", "")
|
||||
|
||||
pid := process.GetManager().Add(fmt.Sprintf("%s %s %s [repo_path: %s]", git.GitExecutable, service, "--stateless-rpc", h.dir), cancel)
|
||||
defer process.GetManager().Remove(pid)
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
Subproject commit d4c86e31027a1e099df108bf1c5ec1754c4578ed
|
||||
|
|
@ -0,0 +1 @@
|
|||
Subproject commit 0db5d458480499a7dcd1aa1040f3e77936400c06
|
||||
Loading…
Reference in New Issue