This repository was archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalyzer.go
More file actions
55 lines (47 loc) · 1.34 KB
/
analyzer.go
File metadata and controls
55 lines (47 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"fmt"
"path/filepath"
"strings"
"github.com/bradleyjkemp/sigma-go"
"github.com/go-git/go-git/v5/plumbing/object"
)
type ChangeEnum int8
const (
New ChangeEnum = iota
Changed
)
type FileChange struct {
ChangeType ChangeEnum `json:"changetype"`
BaseName string `json:"basename"`
RemoteUrl string `json:"remoteurl"`
RuleDesc string `json:"ruledesc"`
RuleTitle string `json:"ruletitle"`
Path string `json:"path"`
}
func (f FileChange) String() string {
return fmt.Sprintf("%#v", f)
}
func Compare(hCommit *object.Commit, sCommit *object.Commit, repo *Repository) ([]FileChange, error) {
result := []FileChange{}
patch, err := sCommit.Patch(hCommit)
if err != nil {
return nil, err
}
for _, el := range patch.FilePatches() {
oldfile, newfile := el.Files()
if oldfile == nil && strings.HasPrefix(newfile.Path(), repo.RulesPath) {
path := newfile.Path()
remoteurl := fmt.Sprintf("%s/blob/%s/%s", repo.Addr, repo.Branch, path)
result = append(result, FileChange{ChangeType: New, Path: path, BaseName: filepath.Base(path), RemoteUrl: remoteurl})
}
}
return result, nil
}
func EnrichFileChange(filechange FileChange, d []byte) FileChange {
rule, err := sigma.ParseRule(d)
checkerr(err)
filechange.RuleDesc = rule.Description
filechange.RuleTitle = rule.Title
return filechange
}