-
-
Notifications
You must be signed in to change notification settings - Fork 369
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
Use full unformatted subject message #1127
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,64 @@ | ||
import { formatJSON } from "../localGetCommits" | ||
import gitlog from "gitlog" | ||
|
||
import { localGetCommits } from "../localGetCommits" | ||
|
||
const hash = "hash" | ||
const abbrevParentHashes = "abbrevParentHashes" | ||
const treeHash = "treeHash" | ||
const authorName = "authorName" | ||
const authorEmail = "authorEmail" | ||
const authorDate = "authorDate" | ||
const committerName = "committerName" | ||
const committerEmail = "committerEmail" | ||
const committerDate = "committerDate" | ||
const subject = "subject" | ||
|
||
const gitLogCommitMock = { | ||
hash, | ||
abbrevParentHashes, | ||
treeHash, | ||
authorName, | ||
authorEmail, | ||
authorDate, | ||
committerName, | ||
committerEmail, | ||
committerDate, | ||
subject, | ||
} | ||
|
||
jest.mock("gitlog", () => ({ | ||
__esModule: true, | ||
default: jest.fn(() => [gitLogCommitMock]), | ||
})) | ||
|
||
it("generates a JSON-like commit message", () => { | ||
expect(formatJSON).toEqual( | ||
'{ "sha": "%H", "parents": "%p", "author": {"name": "%an", "email": "%ae", "date": "%ai" }, "committer": {"name": "%cn", "email": "%ce", "date": "%ci" }, "message": "%f"},' | ||
) | ||
const base = "base-branch" | ||
const head = "head-branch" | ||
|
||
const result = localGetCommits(base, head) | ||
|
||
expect(gitlog).toHaveBeenCalledWith({ | ||
repo: expect.any(String), | ||
branch: `${base}...${head}`, | ||
fields: expect.any(Array), | ||
}) | ||
|
||
const withoutComma = formatJSON.substring(0, formatJSON.length - 1) | ||
expect(() => JSON.parse(withoutComma)).not.toThrow() | ||
expect(result).toEqual([ | ||
{ | ||
sha: hash, | ||
author: { | ||
name: authorName, | ||
email: authorEmail, | ||
date: authorDate, | ||
}, | ||
committer: { | ||
name: committerName, | ||
email: committerEmail, | ||
date: committerDate, | ||
}, | ||
message: subject, | ||
tree: treeHash, | ||
url: expect.stringContaining(hash), | ||
}, | ||
]) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,52 @@ | ||
import { debug } from "../../debug" | ||
import JSON5 from "json5" | ||
import gitlog, { GitlogOptions } from "gitlog" | ||
|
||
import { spawn } from "child_process" | ||
import { GitCommit } from "../../dsl/Commit" | ||
|
||
const d = debug("localGetDiff") | ||
|
||
const sha = "%H" | ||
const parents = "%p" | ||
const authorName = "%an" | ||
const authorEmail = "%ae" | ||
const authorDate = "%ai" | ||
const committerName = "%cn" | ||
const committerEmail = "%ce" | ||
const committerDate = "%ci" | ||
const message = "%f" // this is subject, not message, so it'll only be one line | ||
|
||
const author = `"author": {"name": "${authorName}", "email": "${authorEmail}", "date": "${authorDate}" }` | ||
const committer = `"committer": {"name": "${committerName}", "email": "${committerEmail}", "date": "${committerDate}" }` | ||
export const formatJSON = `{ "sha": "${sha}", "parents": "${parents}", ${author}, ${committer}, "message": "${message}"},` | ||
|
||
export const localGetCommits = (base: string, head: string) => | ||
new Promise<GitCommit[]>((resolve, reject) => { | ||
const args = ["log", `${base}...${head}`, `--pretty=format:${formatJSON}`] | ||
const child = spawn("git", args, { env: process.env }) | ||
d("> git", args.join(" ")) | ||
const commits: GitCommit[] = [] | ||
|
||
child.stdout.on("data", async (chunk: Buffer) => { | ||
const data = chunk.toString() | ||
// remove trailing comma, and wrap into an array | ||
const asJSONString = `[${data.substring(0, data.length - 1)}]` | ||
const parsedCommits = JSON5.parse(asJSONString) | ||
const realCommits = parsedCommits.map((c: any) => ({ | ||
...c, | ||
parents: c.parents.split(" "), | ||
url: "fake.danger.systems/" + c.sha, | ||
})) | ||
|
||
commits.push(...realCommits) | ||
}) | ||
|
||
child.stderr.on("end", () => resolve(commits)) | ||
|
||
const errorParts: string[] = [] | ||
|
||
child.stderr.on("data", (chunk: Buffer) => errorParts.push(chunk.toString())) | ||
|
||
child.on("close", code => { | ||
if (code !== 0) { | ||
console.error(`Could not get commits from git between ${base} and ${head}`) | ||
reject(new Error(errorParts.join(""))) | ||
} else { | ||
resolve(commits) | ||
} | ||
}) | ||
}) | ||
export const localGetCommits = (base: string, head: string) => { | ||
const options: GitlogOptions< | ||
| "hash" | ||
| "abbrevParentHashes" | ||
| "treeHash" | ||
| "authorName" | ||
| "authorEmail" | ||
| "authorDate" | ||
| "committerName" | ||
| "committerEmail" | ||
| "committerDate" | ||
| "subject" | ||
> = { | ||
repo: process.cwd(), | ||
branch: `${base}...${head}`, | ||
fields: [ | ||
"hash", | ||
"abbrevParentHashes", | ||
"treeHash", | ||
"authorName", | ||
"authorEmail", | ||
"authorDate", | ||
"committerName", | ||
"committerEmail", | ||
"committerDate", | ||
"subject", | ||
], | ||
} | ||
|
||
const commits: GitCommit[] = gitlog(options).map(commit => ({ | ||
sha: commit.hash, | ||
author: { | ||
name: commit.authorName, | ||
email: commit.authorEmail, | ||
date: commit.authorDate, | ||
}, | ||
committer: { | ||
name: commit.committerName, | ||
email: commit.committerEmail, | ||
date: commit.committerDate, | ||
}, | ||
message: commit.subject, | ||
tree: commit.treeHash, | ||
url: "fake.danger.systems/" + commit.hash, | ||
})) | ||
|
||
return commits | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because TS was updated it started to complain about
So had to fix this type to fix it