forked from murphysecurity/murphysec
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
murphysec-osc
committed
May 25, 2023
1 parent
9ca7612
commit 0a77c64
Showing
5 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), "..") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,2 @@ | ||
// Package collect 收集信息,一键修复功能中指派修复成员的逻辑 | ||
package collect |