Skip to content

Commit

Permalink
feat: 收集贡献者信息用于问题修复者的判断和指定
Browse files Browse the repository at this point in the history
  • Loading branch information
murphysec-osc committed May 25, 2023
1 parent 9ca7612 commit 0a77c64
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
10 changes: 10 additions & 0 deletions api/collect_contributor.go
Original file line number Diff line number Diff line change
@@ -0,0 1,10 @@
package api

import (
"context"
"github.com/murphysecurity/murphysec/collect"
)

func ReportCollectedContributors(ctx context.Context, client *Client, data *collect.ContributorUpload) {
_ = client.DoJson(client.PostJson(joinURL(client.baseUrl, "/committer/save"), data), nil)
}
11 changes: 11 additions & 0 deletions cmd/murphy/internal/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 6,14 @@ import (
"github.com/murphysecurity/murphysec/api"
"github.com/murphysecurity/murphysec/chunkupload"
"github.com/murphysecurity/murphysec/cmd/murphy/internal/cv"
"github.com/murphysecurity/murphysec/collect"
"github.com/murphysecurity/murphysec/gitinfo"
"github.com/murphysecurity/murphysec/infra/logctx"
"github.com/murphysecurity/murphysec/infra/ref"
"github.com/murphysecurity/murphysec/inspector"
"github.com/murphysecurity/murphysec/model"
"github.com/murphysecurity/murphysec/utils/must"
"go.uber.org/zap"
"path/filepath"
)

Expand Down Expand Up @@ -105,6 107,15 @@ func scan(ctx context.Context, dir string, accessType model.AccessType, mode mod

cv.DisplayWaitingResponse(ctx)
defer cv.DisplayStatusClear(ctx)
// 收集贡献者信息
cu, e := collect.CollectDir(ctx, task.ProjectPath)
if e != nil {
logger.Warn("收集贡献者信息失败", zap.Error(e))
} else {
cu.RepoInfo.SubtaskId = createTaskResp.SubtaskID
api.ReportCollectedContributors(ctx, api.DefaultClient(), cu)
logger.Info("报送贡献者信息成功")
}
// query result
var result *model.ScanResultResponse
result, e = api.QueryResult(ctx, api.DefaultClient(), task.SubtaskId)
Expand Down
84 changes: 84 additions & 0 deletions collect/collect.go
Original file line number Diff line number Diff line change
@@ -0,0 1,84 @@
package collect

import (
"context"
"errors"
"fmt"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/murphysecurity/murphysec/infra/logctx"
"time"
)

type ContributorUpload struct {
RepoInfo struct {
SubtaskId string `json:"subtask_id"`
} `json:"repo_info"`
LastCommitter *Contributor `json:"last_committer"`
Committers []Contributor `json:"committers"`
}

type Contributor struct {
Name string `json:"name"`
Email string `json:"email"`
LastCommitDate time.Time `json:"last_commit_date"`
CommitCount int `json:"commit_count"`
}

func CollectDir(ctx context.Context, dir string) (*ContributorUpload, error) {
logger := logctx.Use(ctx).Sugar().Named("collector")
logger.Debugf("plain open %s", dir)
repo, e := git.PlainOpen(dir)
if errors.Is(e, git.ErrRepositoryNotExists) {
logger.Debugf("no repository found")
return nil, nil
}
if e != nil {
logger.Debugf("open repository failed: %s", e.Error())
return nil, fmt.Errorf("collector open repository: %w", e)
}
head, e := repo.Head()
if e != nil {
logger.Debugf("get head failed: %s", e.Error())
return nil, fmt.Errorf("collector get head failed: %w", e)
}

set := make(map[[2]string]*Contributor)
var last *Contributor

commit, e := repo.CommitObject(head.Hash())
for counter := 0; counter < 2000; counter {
if errors.Is(e, plumbing.ErrObjectNotFound) || errors.Is(e, object.ErrParentNotFound) {
break
}
if e != nil {
logger.Warnf("errors during iterate commits, %s", e.Error())
break
}
key := [2]string{commit.Author.Name, commit.Author.Email}
if _, ok := set[key]; !ok {
contributor := &Contributor{
Name: commit.Author.Name,
Email: commit.Author.Email,
LastCommitDate: commit.Author.When,
CommitCount: 0,
}
set[key] = contributor
if last == nil {
last = contributor
}
}
set[key].CommitCount
commit, e = commit.Parent(0)
}

var r = &ContributorUpload{
LastCommitter: last,
Committers: nil,
}
for _, contributor := range set {
r.Committers = append(r.Committers, *contributor)
}
return r, nil
}
10 changes: 10 additions & 0 deletions collect/collect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 1,10 @@
package collect

import (
"context"
"testing"
)

func TestName(t *testing.T) {
CollectDir(context.TODO(), "..")
}
2 changes: 2 additions & 0 deletions collect/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 1,2 @@
// Package collect 收集信息,一键修复功能中指派修复成员的逻辑
package collect

0 comments on commit 0a77c64

Please sign in to comment.