Skip to content

Commit

Permalink
feat(plugin): Add basic logic for todo matching
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit-gohri committed Oct 18, 2019
1 parent 0854d7e commit 99eb4e2
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 168 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"lint-staged": "^3.4.1",
"prettier": "^1.3.1",
"semantic-release": "^6.3.6",
"ts-jest": "^20.0.0",
"ts-jest": "^24.0.0",
"tslint": "^5.4.3",
"typescript": "^3.6.4",
"validate-commit-msg": "^2.12.1"
Expand Down
75 changes: 70 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,81 @@
// Provides dev-time type structures for `danger` - doesn't affect runtime.
import {DangerDSLType} from "../node_modules/danger/distribution/dsl/DangerDSL"
import { DangerDSLType } from "../node_modules/danger/distribution/dsl/DangerDSL"
declare var danger: DangerDSLType
export declare function message(message: string): void
export declare function warn(message: string): void
export declare function fail(message: string): void
export declare function markdown(message: string): void

function getCreatedOrModifiedFiles() {
return [...danger.git.created_files, ...danger.git.modified_files]
}

/**
* A danger-js plugin to list all todos/fixmes/etc added/changed in a PR
*/
export default function todos() {
// Replace this with the code from your Dangerfile
const title = danger.github.pr.title
message(`PR Title: ${title}`)
export default async function todos({
repoUrl = "",
ignore = [],
keywords = ["TODO", "FIXME"],
}: {
repoUrl?: string
ignore?: Array<string | RegExp>
keywords?: string[]
} = {}) {
const keywordMatches: { [key: string]: string[] } = {}

keywords.forEach(keyword => {
keywordMatches[keyword] = []
})

await Promise.all(
getCreatedOrModifiedFiles().map(async filepath => {
let ignoreFile = false
ignore.forEach(ignorePath => {
if (typeof ignorePath === "string" && filepath.includes(ignorePath)) {
ignoreFile = true
} else if (ignorePath instanceof RegExp && ignorePath.test(filepath)) {
ignoreFile = true
}
})

if (ignoreFile) {
return
}

const diff = await danger.git.diffForFile(filepath)

if (!diff) {
return
}

keywords.forEach(keyword => {
const regex = new RegExp(`(?:\/\/|#|<!--|;|\/\*|^|\/\*\*\s*\**)\s*${keyword.trim()}(?::\s*|\s+)(.+)`, "gi")
const matches = diff.added.match(regex)
const srcLink = repoUrl
? `[\`${filepath}\`](${repoUrl}/blob/${danger.git.commits[0].sha}/${filepath})`
: `\`${filepath}\``

if (matches) {
matches.forEach(match => {
keywordMatches[keyword].push(`\`\`${match}\`\` : ${srcLink}`)
})
}
})
})
)

const output: string[] = []
Object.values(keywordMatches).forEach(matches => {
if (matches.length) {
output.push(...matches)
}
})

if (!output.length) {
return
}

markdown(`### ${keywords.join("s/")}s:
- ${output.join("\n- ")}`)
}
Loading

0 comments on commit 99eb4e2

Please sign in to comment.