Skip to content

Changelog #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"author": "Ash Furrow <ash@ashfurrow.com>",
"license": "MIT",
"scripts": {
"precommit": "lint-staged"
},
"dependencies": {},
"devDependencies": {
Expand Down Expand Up @@ -37,7 +38,7 @@
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json"]
},
"lint-staged": {
"*.@(ts|tsx)": ["yarn prettier --write", "git add"],
"*.@(ts|tsx)": ["yarn prettier --write", "jest", "git add"],
"*.json": ["yarn prettier --write", "git add"]
}
}
48 changes: 46 additions & 2 deletions pr.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
import { markdown, warn } from "danger"
import { schedule, danger, warn, fail, markdown } from "danger"

// TODO: Add a check to see if a changelog exists, then warn if it isn't in modified etc
// Hey there!
//
// When a PR is opened, this file gets run. It's not very straighforward, but
// follow the changelog example and ignore the next four const lines.
// The inspiration for this is https://github.com/artsy/artsy-danger/blob/f019ee1a3abffabad65014afabe07cb9a12274e7/org/all-prs.ts
const isJest = typeof jest !== "undefined"
// Stores the parameter in a closure that can be invoked in tests.
const _test = (reason: string, closure: () => void | Promise<any>) =>
// We return a closure here so that the (promise is resolved|closure is invoked)
// during test time and not when we call rfc().
() => (closure instanceof Promise ? closure : Promise.resolve(closure()))
// Either schedules the promise for execution via Danger, or invokes closure.
const _run = (reason: string, closure: () => void | Promise<any>) =>
closure instanceof Promise ? schedule(closure) : closure()

const wrap: any = isJest ? _test : _run

// See: https://github.com/artsy/artsy-danger/blob/f019ee1a3abffabad65014afabe07cb9a12274e7/org/all-prs.ts#L67-L85
export const changelog = wrap("Require changelog entries on PRs with code changes", async () => {
// First we check if there is a changelog in the repository.
const pr = danger.github.pr
const changelogs = ["CHANGELOG.md", "changelog.md", "Changelog.md", "CHANGELOG.yml"]

const getContentParams = { path: "", owner: pr.head.user.login, repo: pr.head.repo.name }
const rootContents: any = await danger.github.api.repos.getContent(getContentParams)

const hasChangelog = rootContents.data.find((file: any) => changelogs.includes(file.name))
const markedTrivial = (pr.title + pr.body).includes("#trivial")
if (hasChangelog) {
const files = [...danger.git.modified_files, ...danger.git.created_files]

// Look for Swift files that aren't in a unit test directory.
const hasCodeChanges = files.find((file: any) => file.match(/.*\.swift/) && !file.match(/(test|spec)/i))
const hasChangelogChanges = files.find(file => changelogs.includes(file))

if (hasCodeChanges && !hasChangelogChanges) {
const baseMessage = "It looks like code was changed without adding anything to the Changelog. "
if (markedTrivial) {
markdown(baseMessage)
} else {
warn(baseMessage + "If this is a trivial PR that doesn't need a changelog, add #trivial to the PR title or body.")
}
}
}
})
157 changes: 157 additions & 0 deletions tests/changelog.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
jest.mock("danger", () => jest.fn())
import * as danger from "danger"
const dm = danger as any

import { changelog } from "../pr"

beforeEach(() => {
dm.danger = {}
dm.warn = jest.fn()
dm.markdown = jest.fn()
})

const pr = {
head: {
user: {
login: "danger",
},
repo: {
name: "danger-js",
},
},
title: "This is the pull request title.",
body: "",
}

it("warns when code has changed but no changelog entry was made", () => {
dm.danger.github = {
api: {
repos: {
getContent: () => Promise.resolve({ data: [{ name: "code.swift" }, { name: "CHANGELOG.md" }] }),
},
},
pr,
}
dm.danger.git = {
modified_files: ["code.swift"],
created_files: [],
}
return changelog().then(() => {
expect(dm.warn).toBeCalled()
})
})

it("does nothing when there is no changelog file", () => {
dm.danger.github = {
api: {
repos: {
getContent: () => Promise.resolve({ data: [{ name: "code.js" }] }),
},
},
pr,
}
dm.danger.git = {
modified_files: [],
created_files: [],
}
return changelog().then(() => {
expect(dm.warn).not.toBeCalled()
})
})

it("does nothing when only non-Swift files were changed", () => {
dm.danger.github = {
api: {
repos: {
getContent: () => Promise.resolve({ data: [{ name: "CHANGELOG.md" }] }),
},
},
pr,
}
dm.danger.git = {
modified_files: ["Podfile"],
created_files: [],
}
return changelog().then(() => {
expect(dm.warn).not.toBeCalled()
})
})

it("does nothing when only `test` files were changed", () => {
dm.danger.github = {
api: {
repos: {
getContent: () => Promise.resolve({ data: [{ name: "CHANGELOG.md" }] }),
},
},
pr,
}
dm.danger.git = {
modified_files: ["Tests/CalculatorSpec.swift"],
created_files: [],
}
return changelog().then(() => {
expect(dm.warn).not.toBeCalled()
})
})

it("does nothing when the changelog was changed", () => {
dm.danger.github = {
api: {
repos: {
getContent: () => Promise.resolve({ data: [{ name: "code.js" }, { name: "CHANGELOG.md" }] }),
},
},
pr,
}
dm.danger.git = {
modified_files: ["src/index.html", "CHANGELOG.md"],
created_files: [],
}
return changelog().then(() => {
expect(dm.warn).not.toBeCalled()
})
})

it("sends a message if the PR title includes #trivial", () => {
dm.danger.github = {
api: {
repos: {
getContent: () => Promise.resolve({ data: [{ name: "code.swift" }, { name: "CHANGELOG.md" }] }),
},
},
pr: {
...pr,
title: "Just fixing some typos #trivial",
},
}
dm.danger.git = {
modified_files: ["code.swift"],
created_files: [],
}
return changelog().then(() => {
expect(dm.markdown).toBeCalled()
})
})

it("sends a message if the PR body includes #trivial", () => {
dm.danger.github = {
api: {
repos: {
getContent: () => Promise.resolve({ data: [{ name: "code.swift" }, { name: "CHANGELOG.md" }] }),
},
},
pr: {
...pr,
title: "Just fixing some typos",
body: "Nothing fancy, pretty #trivial",
},
}
dm.danger.git = {
modified_files: ["code.swift"],
created_files: [],
}
return changelog().then(() => {
expect(dm.markdown).toBeCalled()
})
})
8 changes: 8 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["es2017"],
"strict": true
}
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3630,7 +3630,7 @@ verror@1.10.0:
core-util-is "1.0.2"
extsprintf "^1.2.0"

"vm2@github:patriksimek/vm2#custom_files":
vm2@patriksimek/vm2#custom_files:
version "3.5.0"
resolved "https://codeload.github.com/patriksimek/vm2/tar.gz/7e82f90ac705fc44fad044147cb0df09b4c79a57"

Expand Down