Skip to content

Commit

Permalink
Merge pull request #1280 from danger/step_summs
Browse files Browse the repository at this point in the history
Adds support for posting to the markdown summary
  • Loading branch information
orta committed May 18, 2022
2 parents 83aa9b5 + 88a6fe6 commit 25dd589
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 42 deletions.
24 changes: 0 additions & 24 deletions .circleci/config.yml

This file was deleted.

8 changes: 6 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v1
- uses: actions/setup-node@v2
with:
node-version: '14'
node-version: "14"

# Get local dependencies & test
- run: yarn install
Expand All @@ -27,7 +27,11 @@ jobs:
# if it becomes an issue.
- run: echo "Testing Flow definition file"
- run: yarn build:flow-types
# - run: yarn flow check

- run: echo "Running built Danger"
- run: node distribution/commands/danger-ci.js
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- run: 'echo "This is only for Integration tests on two blank projects"'

Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@

<!-- Your comment below this -->

- Adds support for the new [GitHub Job summaries](https://github.blog/2022-05-09-supercharging-github-actions-with-job-summaries/) API via:
- `danger.github.setSummaryMarkdown("[markdown]")` for the JavaScript DSL
- The results DSL for sub-processes now accepts `{ github: { stepSummary: "[markdown]" } }` from projects the Swift and Kotlin implementations of Danger.

This gives you the chance to leave feedback on the overview page for a PR, it won't ping people
in the PR thread or trigger emails which is why it's a separate attribute in the DSL. Potentially open to having the main
comment in the job summary if someone can make a good case for it in the Danger JS issues. [@orta]

<!-- Your comment above this -->


Expand Down
6 changes: 4 additions & 2 deletions dangerfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import yarn from "danger-plugin-yarn"
import jest from "danger-plugin-jest"

import { DangerDSLType } from "./source/dsl/DangerDSL"
declare var danger: DangerDSLType
declare const danger: DangerDSLType
// declare var results: any
declare function warn(message: string, file?: string, line?: number): void
// declare function fail(params: string): void
declare function fail(params: string): void
// declare function message(params: string): void
// declare function markdown(params: string): void
// declare function schedule(promise: Promise<any | void>): void
Expand Down Expand Up @@ -44,6 +44,8 @@ export default async () => {
if (packageDiff.version && danger.github.pr.user.login !== "orta") {
fail("Please don't make package version changes")
}

danger.github.setSummaryMarkdown("Looking good")
}

// Re-run the git push hooks
Expand Down
38 changes: 25 additions & 13 deletions source/dsl/DangerResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export interface DangerResults {
*/
markdowns: Violation[]

github?: {
/**
* Markdown text which gets added as a summary in the first
* page which you see when you click through to the PR results.
*
* https://github.blog/2022-05-09-supercharging-github-actions-with-job-summaries/ */
stepSummary?: string
}

/** Meta information about the runtime evaluation */
meta?: {
/** E.g. "dangerJS", or "Danger Swift" */
Expand Down Expand Up @@ -89,15 +98,15 @@ export function validateResults(results: DangerResults) {
// consumers, let's take the time to ensure the data is how we think it is.
const { fails, warnings, messages, markdowns } = results
const props = { fails, warnings, messages, markdowns }
Object.keys(props).forEach(name => {
Object.keys(props).forEach((name) => {
//
// Must include the key 4 types
if (!props[name]) {
throw new Error(`Results passed to Danger JS did not include ${name}.\n\n${JSON.stringify(results, null, " ")}`)
}

const violations: Violation[] = props[name]
violations.forEach(v => {
violations.forEach((v) => {
// They should always have a message
if (!v.message) {
throw new Error(
Expand All @@ -106,7 +115,7 @@ export function validateResults(results: DangerResults) {
}
// Warn if anything other than the initial API is on a violation
const officialAPI = ["message", "line", "file", "icon"]
const keys = Object.keys(v).filter(f => !officialAPI.includes(f))
const keys = Object.keys(v).filter((f) => !officialAPI.includes(f))
if (keys.length) {
console.warn(`Received unexpected key in Violation, expected only ${officialAPI} but got ${Object.keys(v)}`)
}
Expand All @@ -117,21 +126,22 @@ export function validateResults(results: DangerResults) {
/** Returns only the inline violations from Danger results */
export function inlineResults(results: DangerResults): DangerResults {
return {
fails: results.fails.filter(m => isInline(m)),
warnings: results.warnings.filter(m => isInline(m)),
messages: results.messages.filter(m => isInline(m)),
markdowns: results.markdowns.filter(m => isInline(m)),
fails: results.fails.filter((m) => isInline(m)),
warnings: results.warnings.filter((m) => isInline(m)),
messages: results.messages.filter((m) => isInline(m)),
markdowns: results.markdowns.filter((m) => isInline(m)),
}
}

/** Returns only the main-comment comments violations from Danger results */
export function regularResults(results: DangerResults): DangerResults {
return {
fails: results.fails.filter(m => !isInline(m)),
warnings: results.warnings.filter(m => !isInline(m)),
messages: results.messages.filter(m => !isInline(m)),
markdowns: results.markdowns.filter(m => !isInline(m)),
fails: results.fails.filter((m) => !isInline(m)),
warnings: results.warnings.filter((m) => !isInline(m)),
messages: results.messages.filter((m) => !isInline(m)),
markdowns: results.markdowns.filter((m) => !isInline(m)),
meta: results.meta,
github: results.github,
}
}

Expand All @@ -143,13 +153,14 @@ export function mergeResults(results1: DangerResults, results2: DangerResults):
messages: results1.messages.concat(results2.messages),
markdowns: results1.markdowns.concat(results2.markdowns),
meta: results1.meta || results2.meta,
github: results1.github || results2.github,
}
}

/** Sorts all of the results according to their files and lines */
export function sortInlineResults(inlineResults: DangerInlineResults[]): DangerInlineResults[] {
// First sort messages in every inline result
const sortedInlineResults = inlineResults.map(i => {
const sortedInlineResults = inlineResults.map((i) => {
return {
file: i.file,
line: i.line,
Expand Down Expand Up @@ -216,6 +227,7 @@ export function sortResults(results: DangerResults): DangerResults {
messages: results.messages.sort(sortByFile),
markdowns: results.markdowns.sort(sortByFile),
meta: results.meta,
github: results.github,
}
}

Expand All @@ -237,7 +249,7 @@ export function resultsIntoInlineResults(results: DangerResults): DangerInlineRe
const violationsIntoInlineResults = (kind: string) => {
for (let violation of results[kind]) {
if (violation.file && violation.line) {
const findInlineResult = dangerInlineResults.find(r => r.file == violation.file && r.line == violation.line)
const findInlineResult = dangerInlineResults.find((r) => r.file == violation.file && r.line == violation.line)
if (findInlineResult) {
findInlineResult[kind].push(violation.message)
} else {
Expand Down
7 changes: 7 additions & 0 deletions source/dsl/GitHubDSL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ export interface GitHubDSL extends GitHubJSONDSL {
api: GitHub
/** A scope for useful functions related to GitHub */
utils: GitHubUtilsDSL
/**
* Sets a markdown summary which shows on the overview page for the
* results of all steps in your CI job.
*
* See: https://github.blog/2022-05-09-supercharging-github-actions-with-job-summaries/
*/
setSummaryMarkdown: (markdown: string) => void
}

/** Useful functions for GitHub related work */
Expand Down
2 changes: 2 additions & 0 deletions source/dsl/_tests/__snapshots__/DangerResults.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ Object {
"message": "Fail message3",
},
],
"github": undefined,
"markdowns": Array [],
"messages": Array [
Object {
Expand Down Expand Up @@ -289,6 +290,7 @@ Object {
"message": "Fail message3",
},
],
"github": undefined,
"markdowns": Array [
Object {
"file": "Test.swift",
Expand Down
9 changes: 8 additions & 1 deletion source/platforms/GitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ export const githubJSONToGitHubDSL = (gh: GitHubJSONDSL, api: NodeGitHub): GitHu
...gh,
api,
utils: GitHubUtils(gh.pr, api),
setSummaryMarkdown: (markdown: string) => {
const filePath = process.env.GITHUB_STEP_SUMMARY
if (!filePath) {
throw new Error("process.env.GITHUB_STEP_SUMMARY was not set, which is needed for setSummaryMarkdown")
}
writeFileSync(filePath, markdown, "utf8")
},
}
}

Expand All @@ -97,7 +104,7 @@ import {
dangerRepresentationForPath,
} from "./github/customGitHubRequire"
import { DangerRunner } from "../runner/runners/runner"
import { existsSync, readFileSync } from "fs"
import { existsSync, readFileSync, writeFileSync } from "fs"
import cleanDangerfile from "../runner/runners/utils/cleanDangerfile"
import transpiler from "../runner/runners/utils/transpiler"

Expand Down
10 changes: 10 additions & 0 deletions source/runner/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { DangerRunner } from "./runners/runner"
import { GitDSL } from "../dsl/GitDSL"
import { DangerDSL } from "../dsl/DangerDSL"
import { emptyGitJSON } from "../platforms/github/GitHubGit"
import { writeFileSync } from "fs"

export interface ExecutorOptions {
/** Should we do a text-only run? E.g. skipping comments */
Expand Down Expand Up @@ -331,6 +332,15 @@ export class Executor {
if (this.options.verbose) {
await this.handleResultsPostingToSTDOUT(results)
}

// Write to the GitHub Env if a sub-process has included a reference to github's job summary
if (results.github?.stepSummary) {
const filePath = process.env.GITHUB_STEP_SUMMARY
if (!filePath) {
throw new Error("process.env.GITHUB_STEP_SUMMARY was not set, which is needed for setSummaryMarkdown")
}
writeFileSync(filePath, results.github.stepSummary, "utf8")
}
}

async updatePrStatus(
Expand Down

0 comments on commit 25dd589

Please sign in to comment.