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
feat: allow update function to trigger file deletion #130
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const DELETE_FILE: unique symbol = Symbol("DELETE_FILE"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ export type Options = { | |
|
||
export type Changes = { | ||
files?: { | ||
[path: string]: string | File | UpdateFunction | null; | ||
[path: string]: string | File | UpdateFunction | null | Symbol; | ||
}; | ||
emptyCommit?: boolean | string; | ||
commit: string; | ||
|
@@ -54,7 +54,7 @@ export type UpdateFunctionFile = | |
|
||
export type UpdateFunction = ( | ||
file: UpdateFunctionFile | ||
) => string | File | null | Promise<string | File | null>; | ||
) => string | File | null | Symbol | Promise<string | File | null | Symbol>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the update function returns There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to type the Symbol to ensure it is the actual There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know, I actually never worked with types for Symbols |
||
|
||
export type State = { | ||
octokit: Octokit; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Octokit as Core } from "@octokit/core"; | ||
import { RequestError } from "@octokit/request-error"; | ||
|
||
import { createPullRequest, DELETE_FILE } from "../src"; | ||
const Octokit = Core.plugin(createPullRequest); | ||
|
||
test("delete files function", async () => { | ||
const fixtures = require("./fixtures/delete-files-function"); | ||
const fixturePr = fixtures[fixtures.length - 1].response; | ||
const octokit = new Octokit(); | ||
|
||
octokit.hook.wrap("request", (_, options) => { | ||
const currentFixtures = fixtures.shift(); | ||
const { | ||
baseUrl, | ||
method, | ||
url, | ||
request, | ||
headers, | ||
mediaType, | ||
draft, | ||
...params | ||
} = options; | ||
|
||
expect( | ||
`${currentFixtures.request.method} ${currentFixtures.request.url}` | ||
).toEqual(`${options.method} ${options.url}`); | ||
|
||
Object.keys(params).forEach((paramName) => { | ||
expect(currentFixtures.request[paramName]).toStrictEqual( | ||
params[paramName] | ||
); | ||
}); | ||
|
||
if (currentFixtures.response.status >= 400) { | ||
throw new RequestError("Error", currentFixtures.response.status, { | ||
request: currentFixtures.request, | ||
headers: currentFixtures.response.headers, | ||
}); | ||
} | ||
return currentFixtures.response; | ||
}); | ||
|
||
const pr = await octokit.createPullRequest({ | ||
owner: "gr2m", | ||
repo: "pull-request-test", | ||
title: "One comes, one goes", | ||
body: "because", | ||
head: "patch", | ||
changes: { | ||
files: { | ||
"path/to/file1.txt": "Content for file1", | ||
"path/to/file2.txt": () => DELETE_FILE, | ||
"path/to/file-does-not-exist.txt": () => DELETE_FILE, | ||
}, | ||
commit: "why", | ||
}, | ||
}); | ||
|
||
expect(pr).toStrictEqual(fixturePr); | ||
expect(fixtures.length).toEqual(0); | ||
}); |
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.
I thought this is fine since I saw basically the same for other catch blocks, but please let me know if you would prefer a test case for this.