Convenience method to create/edit/delete a text file based on its current content
Table of contents
Browsers |
Load <script type="module">
import { Octokit } from "https://esm.sh/@octokit/core";
import {
createOrUpdateTextFile,
composeCreateOrUpdateTextFile,
} from "https://esm.sh/@octokit/plugin-create-or-update-text-file";
</script> |
---|---|
Node |
Install with const { Octokit } = require("@octokit/core");
const {
createOrUpdateTextFile,
composeCreateOrUpdateTextFile,
} = require("@octokit/plugin-create-or-update-text-file"); |
const MyOctokit = Octokit.plugin(createOrUpdateTextFile);
const octokit = new MyOctokit({ auth: "secret123" });
const {
updated,
data: { commit },
} = await octokit.createOrUpdateTextFile({
owner: "octocat",
repo: "hello-world",
path: "test.txt",
content: "content here",
message: "update test.txt",
});
if (updated) {
console.log("test.txt updated via %s", data.commit.html_url);
} else {
console.log("test.txt already up to date");
}
const { deleted } = await octokit.createOrUpdateTextFile({
owner: "octocat",
repo: "hello-world",
path: "test.txt",
content: null,
message: "delete test.txt",
});
if (deleted) {
console.log("test.txt deleted via %s", data.commit.html_url);
} else {
console.log("test.txt does not exist");
}
const { updated, deleted, data } = await octokit.createOrUpdateTextFile({
owner: "octocat",
repo: "hello-world",
path: "test.txt",
content({ exists, content }) {
// do not create file
if (!exists) return null;
return content.toUpperCase();
},
message: "update test.txt",
});
const octokit = new Octokit({ auth: "secret123" });
await { updated, deleted, data } = await createOrUpdateTextFile(octokit, {
owner: "octocat",
repo: "hello-world",
path: "test.txt",
content: "content here",
message: "update test.txt",
});
name | type | description |
---|---|---|
owner
|
string
|
Required. Repository owner login |
repo
|
string
|
Required. Repository repository name |
path
|
string
|
Required. Path to repository file within the repository |
path
|
string
|
Required. Path to repository file within the repository |
message
|
string
|
Required. Commit message in case an update is necessary |
content
|
string | null | function
|
Required. Set to a Set to Set to a function that either returns
|
branch | string |
The repository branch on which to update the file. Defaults to the repository's default branch |
committer | object |
Same as the committer object from the PUT /repos/{owner}/{repo}/contents/{path} REST API endpoint |
author | object |
Same as the author object from the PUT /repos/{owner}/{repo}/contents/{path} REST API endpoint |
You can import the method options and response types as well as the type of the content
update function
export {
Options,
ContentUpdateFunction,
Response,
} from "@octokit/plugin-create-or-update-text-file";
See CONTRIBUTING.md