-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow update function to trigger file deletion (#130)
BREAKING CHANGE: The meaning of `null` has changed! Previously the following snippet was causing a file deletion. ```js files: { "file/to/delete.txt": null } ``` If you want to retain this behavior you must use the DELETE_FILE Symbol import { createPullRequest, DELETE_FILE } from 'octokit-plugin-create-pull-request'; ```js files: { "file/to/delete.txt": DELETE_FILE } ``` If you want to trigger a file deletion from an update function, you can now do so by returning the deleteFile Symbol. ```js import { createPullRequest, DELETE_FILE } from 'octokit-plugin-create-pull-request'; files: { "file/to/delete.txt": ({ exists, encoding, content }) => { const fileContent = Buffer.from(content, encoding).toString("utf-8") if (fileContent.includes('abc')) { // trigger file deletion return DELETE_FILE } // do not alter file content return null; } }
- Loading branch information
1 parent
f961a6d
commit ac571dc
Showing
8 changed files
with
1,769 additions
and
9 deletions.
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export const DELETE_FILE: unique symbol = Symbol("DELETE_FILE"); |
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
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 |
---|---|---|
@@ -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); | ||
}); |
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
Oops, something went wrong.