Skip to content

Commit

Permalink
feat: get author from git history
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimonemms committed Apr 4, 2024
1 parent 30f33f5 commit 3ac760f
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Required for Toodaloo hook to work

- name: Set up Go
uses: actions/setup-go@v4
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ repos:
This will generate a [Markdown](https://www.markdownguide.org) formatted file
at `toodaloo.md`. It also only scans files in the git tree.
> If using in CI, you will need to clone the whole Git history for the "get author"
> functionality to work
## Contributing
### Open in a container
Expand Down
3 changes: 3 additions & 0 deletions docs/pre-commit.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ This makes some changes to the default options passed in to the `scan` command.
names the file `toodaloo.md`. This can be overridden if desired.

See the [scan command](./commands#scan) for more details on the available flag.

> If using in CI, you will need to clone the whole Git history for the "get author"
> functionality to work
2 changes: 1 addition & 1 deletion pkg/output/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (o MarkdownOutput) generate(report []scanner.Report) ([]byte, error) {
res = append(res, []string{
fmt.Sprintf("[%s](%s#L%d)", item.File, item.File, item.LineNumber),
strconv.Itoa(item.LineNumber),
item.Author,
fmt.Sprintf("%s <%s>", item.Author, item.AuthorEmail),
item.Msg,
})
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/scanner/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
package scanner

type Report struct {
File string `json:"file"`
LineNumber int `json:"lineNumber"`
Author string `json:"author,omitempty"`
Msg string `json:"message,omitempty"`
File string `json:"file"`
LineNumber int `json:"lineNumber"`
Author string `json:"author,omitempty"`
AuthorEmail string `json:"authorEmail,omitempty"`
Msg string `json:"message,omitempty"`
}
54 changes: 48 additions & 6 deletions pkg/scanner/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,41 @@ type Scan struct {
config *config.Config
}

func (s *Scan) getAuthorForLine(filename string, lineNumber int, defaultAuthor string) (author, authorEmail string, err error) {
repo, err := git.PlainOpen(s.config.WorkingDirectory)
if err != nil {
if err == git.ErrRepositoryNotExists {
// Line not under Git control - return the default author
err = nil
author = defaultAuthor
return
}
return
}

l, err := repo.Log(&git.LogOptions{
FileName: &filename,
})
if err != nil {
return
}

commit, err := l.Next()
if err != nil {
return
}

blame, err := git.Blame(commit, filename)
if err != nil {
return
}

author = blame.Lines[lineNumber].AuthorName
authorEmail = blame.Lines[lineNumber].Author

return
}

func (s *Scan) FindFilesByGit() ([]string, error) {
files := make([]string, 0)

Expand Down Expand Up @@ -186,8 +221,14 @@ func (s *Scan) scanFileForTodo(filename string) ([]Report, error) {

l1.WithField("matches", matches).Debug("Found matches")

// @todo(sje): get the author from the Git history
author := strings.TrimSpace(matches[4])
repoFilename := strings.Replace(filename, s.config.WorkingDirectory, "", 1)
repoFilename = strings.TrimLeft(repoFilename, "/")

author, authorEmail, err := s.getAuthorForLine(repoFilename, lineNumber, strings.TrimSpace(matches[4]))
if err != nil {
l1.WithError(err).Error("Error getting author")
return nil, err
}
msg := strings.TrimSpace(matches[6])

// Remove working directory
Expand All @@ -196,10 +237,11 @@ func (s *Scan) scanFileForTodo(filename string) ([]Report, error) {
cleanFilename = strings.TrimPrefix(cleanFilename, "/")

res = append(res, Report{
File: cleanFilename,
LineNumber: lineNumber,
Author: author,
Msg: msg,
File: cleanFilename,
LineNumber: lineNumber,
Author: author,
AuthorEmail: authorEmail,
Msg: msg,
})
}
}
Expand Down
3 changes: 1 addition & 2 deletions toodaloo.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
| File | Line Number | Author | Message |
| --- | --- | --- | --- |
| [pkg/output/markdown.go](pkg/output/markdown.go#L30) | 30 | sje | implement report |
| [pkg/scanner/scan.go](pkg/scanner/scan.go#L189) | 189 | sje | get the author from the Git history |
| [pkg/output/markdown.go](pkg/output/markdown.go#L30) | 30 | Simon Emms <[email protected]> | implement report |

0 comments on commit 3ac760f

Please sign in to comment.