Skip to content

Commit

Permalink
Add support for getting the blob url for a check
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed May 16, 2018
1 parent 0b90375 commit 6a113f2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
64 changes: 48 additions & 16 deletions source/platforms/github/comms/checks/resultsToCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
template as githubResultsTemplate,
inlineTemplate as githubResultsInlineTemplate,
} from "../../../../runner/templates/githubIssueTemplate"
import * as GitHubNodeAPI from "@octokit/rest"

export interface CheckImages {
alt: string
Expand Down Expand Up @@ -53,7 +54,12 @@ export interface CheckOptions {
}
}

export const resultsToCheck = (results: DangerResults, options: ExecutorOptions, pr: GitHubPRDSL): CheckOptions => {
export const resultsToCheck = async (
results: DangerResults,
options: ExecutorOptions,
pr: GitHubPRDSL,
api: GitHubNodeAPI
): Promise<CheckOptions> => {
const repo = pr.base.repo
const hasFails = results.fails.length > 0
const hasWarnings = results.warnings.length > 0
Expand All @@ -63,6 +69,18 @@ export const resultsToCheck = (results: DangerResults, options: ExecutorOptions,

const mainBody = githubResultsTemplate(options.dangerID, mainResults)

const getBlobUrlForPath = async (path: string) => {
try {
const { data } = await api.repos.getContent({ repo: pr.head.repo.name, owner: pr.head.repo.owner.login, path })
// https://developer.github.com/v3/checks/runs/#example-of-completed-conclusion
// e.g. "blob_href": "http://github.com/octocat/Hello-World/blob/837db83be4137ca555d9a5598d0a1ea2987ecfee/README.md",
return `${pr.head.repo.html_url}/blob/${data.sha}/${data.path}`
} catch (error) {
console.error(`An error was raised in getting the blob path for ${path} - ${error}`)
return ""
}
}

return {
name: "Danger",
status: "completed",
Expand All @@ -82,28 +100,42 @@ export const resultsToCheck = (results: DangerResults, options: ExecutorOptions,
output: {
title: "Title, figure out what to put here",
summary: mainBody,
annotations: inlineResultsToAnnotations(annotationResults, options),
annotations: await inlineResultsToAnnotations(annotationResults, options, getBlobUrlForPath),
},
}
}

const inlineResultsToAnnotations = (results: DangerResults, options: ExecutorOptions): CheckAnnotation[] => {
const inlineResultsToAnnotations = async (
results: DangerResults,
options: ExecutorOptions,
getBlobUrlForPath: any
): Promise<CheckAnnotation[]> => {
// Basically coalesces violations based on file and line
const inlineResults = resultsIntoInlineResults(results)

return inlineResults.map(perFileResults => ({
filename: perFileResults.file,
blob_href: "", //TODO, this will be trick, today we should do the dumbest thing, but maybe in the future this can use the tree API to get the blobs for many files at once
warning_level: warningLevelForInlineResults(perFileResults),
message: githubResultsInlineTemplate(
options.dangerID,
inlineResultsIntoResults(perFileResults),
perFileResults.file,
perFileResults.line
),
start_line: perFileResults.line || 0,
end_line: perFileResults.line || 0,
}))
// Make a list of annotations, because we use async it's
// a bit of faffing
const annotations: CheckAnnotation[] = []

for (const perFileResults of inlineResults) {
const annotation: CheckAnnotation = {
filename: perFileResults.file,
blob_href: await getBlobUrlForPath(perFileResults.file),
warning_level: warningLevelForInlineResults(perFileResults),
message: githubResultsInlineTemplate(
options.dangerID,
inlineResultsIntoResults(perFileResults),
perFileResults.file,
perFileResults.line
),
start_line: perFileResults.line || 0,
end_line: perFileResults.line || 0,
}

annotations.push(annotation)
}

return annotations
}

const warningLevelForInlineResults = (results: DangerInlineResults): "notice" | "warning" | "failure" => {
Expand Down
2 changes: 1 addition & 1 deletion source/platforms/github/comms/checksCommenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const GitHubChecksCommenter = (api: GitHubAPI): PlatformCommunicator | un
return
}

const checkData = resultsToCheck(results, options, pr)
const checkData = await resultsToCheck(results, options, pr, octokit)
await octokit.checks.create(checkData)
// const existingReport = octokit.issues
},
Expand Down

0 comments on commit 6a113f2

Please sign in to comment.