Skip to content

Commit

Permalink
Merge branch 'master' into feat/547-delete-new-comment
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Jan 4, 2021
2 parents f73d0b8 + cf39ac0 commit 7157ea1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

<!-- Your comment below this -->

- Adds options `newComment` and `removePreviousComments` - [@davidhouweling]
- Adds options `--newComment` and `--removePreviousComments` - [@davidhouweling]
- Add support for a file path filter when calculation lines of code - [@melvinvermeer]

<!-- Your comment above this -->

Expand Down
4 changes: 3 additions & 1 deletion source/dsl/GitDSL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ export interface GitDSL extends GitJSONDSL {

/**
* Offers the overall lines of code added/removed in the diff
*
* @param {string} pattern an option glob pattern to filer files that will considered for lines of code.
*/
linesOfCode(): Promise<number | null>
linesOfCode(pattern?: string): Promise<number | null>
}
12 changes: 8 additions & 4 deletions source/platforms/git/gitJSONToGitDSL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import * as jsonDiff from "fast-json-patch"
import jsonpointer from "jsonpointer"
import JSON5 from "json5"

import micromatch from "micromatch"

import { GitDSL, JSONPatchOperation, GitJSONDSL, StructuredDiff } from "../../dsl/GitDSL"
import chainsmoker from "../../commands/utils/chainsmoker"

Expand Down Expand Up @@ -155,11 +157,13 @@ export const gitJSONToGitDSL = (gitJSONRep: GitJSONDSL, config: GitJSONToGitDSLC
}, Object.create(null))
}

const linesOfCode = async () => {
const linesOfCode = async (pattern?: string) => {
const isPatternMatch = (path: string) => pattern === undefined || micromatch.isMatch(path, pattern)

const [createdFilesDiffs, modifiedFilesDiffs, deletedFilesDiffs] = await Promise.all([
Promise.all(gitJSONRep.created_files.map(path => diffForFile(path))),
Promise.all(gitJSONRep.modified_files.map(path => diffForFile(path))),
Promise.all(gitJSONRep.deleted_files.map(path => diffForFile(path))),
Promise.all(gitJSONRep.created_files.filter(isPatternMatch).map(path => diffForFile(path))),
Promise.all(gitJSONRep.modified_files.filter(isPatternMatch).map(path => diffForFile(path))),
Promise.all(gitJSONRep.deleted_files.filter(isPatternMatch).map(path => diffForFile(path))),
])

let additions = createdFilesDiffs
Expand Down
10 changes: 10 additions & 0 deletions source/platforms/github/_tests/_github_git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ describe("the dangerfile gitDSL", () => {
])
})

it("counts the lines of code", async () => {
const loc = await gitDSL.linesOfCode()
expect(loc).toBe(1151)
})

it("allows the lines of code to be filtered by file path", async () => {
const loc = await gitDSL.linesOfCode("lib/**")
expect(loc).toBe(720)
})

it("show diff chunks for a specific file", async () => {
const { chunks } = (await gitDSL.structuredDiffForFile("tsconfig.json"))!

Expand Down

0 comments on commit 7157ea1

Please sign in to comment.