Skip to content

Commit

Permalink
fix: support gitlab partially
Browse files Browse the repository at this point in the history
danger doesn't support file diffs for gitlab yet, so using full file contents instead
  • Loading branch information
rohit-gohri committed Feb 13, 2020
1 parent aeae0d0 commit c117477
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type GenerateRepoUrl = (filepath: string) => string

export type RepoUrl = string | GenerateRepoUrl

export type IgnorePatterns = Array<string | RegExp>
export type IgnorePatterns = (string | RegExp)[]

interface PlainObject<T> {
[key: string]: T
Expand Down Expand Up @@ -44,7 +44,7 @@ export function getMatches(diffString: string, keyword: string) {

const regex = new RegExp(
`(?:\\/\\/|#|<!--|;|\\/\\*|^|\\/\\*\\*\\s*\\**)\\s*${escapedKeyword}(?::\\s*|\\s+)(.+)`,
"gi"
"gi",
)
const matches = diffString.match(regex)
if (!matches || !matches.length) {
Expand Down Expand Up @@ -74,7 +74,7 @@ export default async function todos({
}: {
repoUrl?: RepoUrl
ignore?: IgnorePatterns
keywords?: string[]
keywords?: string[],
} = {}) {
const keywordMatches: PlainObject<string[]> = {}

Expand All @@ -88,21 +88,35 @@ export default async function todos({
return
}

const diff = await danger.git.diffForFile(filepath)
let text: string = ''

if (!diff) {
try {
const diff = await danger.git.diffForFile(filepath)
if (diff) text = diff.added
}
catch (err) {
if (danger.gitlab) {
// Could not get diff, will be using full file
text = await danger.gitlab.utils.fileContents(filepath)
}
else {
throw err
}
}

if (!text) {
return
}

keywords.forEach(keyword => {
const matches = getMatches(diff.added, keyword)
const matches = getMatches(text, keyword)
const srcLink = getFormattedSrcLink(filepath, repoUrl)

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

const output: string[] = []
Expand Down

0 comments on commit c117477

Please sign in to comment.