Back to packages list

Vals using @octokit/rest

Description from the NPM package:
GitHub REST API client for Node.js

valToGH

A utility function for programmatically updating a GitHub repository with code retrieved from a Val.

NOTE: This function currently does not change the contents of a file if it is already present. I will however be adding that functionality.

Usage

Create valimport { valToGH } from 'https://esm.town/v/nbbaier/valToGH'; const repo = "yourGitHubUser/yourRepo"; const val = "valUser/valName"; // or vals = ["valUser/valName1","valUser/valName2", ...] const ghToken = Deno.env.get("yourGitHubToken"); const result = await valToGH(repo, val, ghToken); console.log(result);

Parameters

  • repo: The GitHub repository in the format {user}/{repo}.
  • val: A single repository in the format {user}/{val}.
  • ghToken: Your GitHub token for authentication (must have write permission to the target repo)

Options

  • branch: Optional target branch. Default is main.
  • prefix: Optional directory path prefix for each file.
  • message: Optional commit message. Default is the current date and time in the format yyyy-MM-dd T HH:mm:ss UTC.
  • ts: Optional flag to use a .ts extension for the committed file instead of the default .tsx.
Readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { API_URL } from "https://esm.town/v/std/API_URL?v=5";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=42";
import { Octokit } from "npm:@octokit/rest";
import { DateTime } from "npm:luxon";
async function getLatestCommit(ghUser: string, ghRepo: string, branch: string, client: Octokit)
{
const { data: refData } = await client.rest.git.getRef({
owner: ghUser,
repo: ghRepo,
ref: `heads/${branch}`,
});
return refData.object.sha;
}
async function createNewTree(
ghUser: string,
ghRepo: string,
tree: {
path?: string;
mode?: "100644" | "100755" | "040000" | "160000" | "120000";
type?: "tree" | "commit" | "blob";
sha?: string;
content?: string;
}[],
commitSHA: string,
client: Octokit,
) {
const {
data: { sha: currentTreeSHA },
} = await client.rest.git.createTree({
owner: ghUser,
repo: ghRepo,
tree,
base_tree: commitSHA,
parents: [commitSHA],
});
return currentTreeSHA;
}
async function createNewCommit(
ghUser: string,
ghRepo: string,
commitSHA: string,
currentTreeSHA: string,
message: string,
client: Octokit,
) {
const {
data: { sha: newCommitSHA },
} = await client.rest.git.createCommit({
owner: ghUser,
repo: ghRepo,
tree: currentTreeSHA,
message: message,
parents: [commitSHA],
});
return newCommitSHA;
}
async function updateBranchRef(
owner: string,
repo: string,
newCommitSHA: string,
branch: string = "main",
client: Octokit,
) {
const result = await client.rest.git.updateRef({
owner,
repo,
ref: `heads/${branch}`,
sha: newCommitSHA,
});
return result;
}
type Options = { branch?: string; prefix?: string; message?: string; ts?: boolean };
/**
* Commits a value or an array of values to a GitHub repository.
*
* @param {string} repo - The GitHub repository in the format of {owner}/{repo}.
* @param {string | string[]} val - A single value or array of values in the format of {user}/{val}.
* @param {string} ghToken - The GitHub token for authentication.
* @param {Options} [options] - Options to configure the commit.
* @param {string} [options.branch="main"] - The branch to commit to.
* @param {string} [options.prefix=""] - Prefix to be added to the file path.
* @param {string} [options.message="<current date and time> UTC"] - The commit message.
* @param {boolean} [options.ts=true] - Whether to use TypeScript or JavaScript.
* @returns {Promise<any>} - The result of the commit.
*/
export async function valToGH(
repo: string, // owner/repo
val: string | string[], // user/val
ghToken: string,
options?: Options,
) {
const client = new Octokit({ auth: ghToken });

valToGH

A utility function for programmatically updating a GitHub repository with code retrieved from a Val.

NOTE: This function currently does not change the contents of a file if it is already present. I will however be adding that functionality.

Usage

Create valimport { valToGH } from 'https://esm.town/v/nbbaier/valToGH'; const repo = "yourGitHubUser/yourRepo"; const val = "valUser/valName"; // or vals = ["valUser/valName1","valUser/valName2", ...] const ghToken = Deno.env.get("yourGitHubToken"); const result = await valToGH(repo, val, ghToken); console.log(result);

Parameters

  • repo: The GitHub repository in the format {user}/{repo}.
  • val: A single repository in the format {user}/{val}.
  • ghToken: Your GitHub token for authentication (must have write permission to the target repo)

Options

  • branch: Optional target branch. Default is main.
  • prefix: Optional directory path prefix for each file.
  • message: Optional commit message. Default is the current date and time in the format yyyy-MM-dd T HH:mm:ss UTC.
  • ts: Optional flag to use a .ts extension for the committed file instead of the default .tsx.
Readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { API_URL } from "https://esm.town/v/std/API_URL?v=5";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=42";
import { Octokit } from "npm:@octokit/rest";
import { DateTime } from "npm:luxon";
async function getLatestCommit(ghUser: string, ghRepo: string, branch: string, client: Octokit)
{
const { data: refData } = await client.rest.git.getRef({
owner: ghUser,
repo: ghRepo,
ref: `heads/${branch}`,
});
return refData.object.sha;
}
async function createNewTree(
ghUser: string,
ghRepo: string,
tree: {
path?: string;
mode?: "100644" | "100755" | "040000" | "160000" | "120000";
type?: "tree" | "commit" | "blob";
sha?: string;
content?: string;
}[],
commitSHA: string,
client: Octokit,
) {
const {
data: { sha: currentTreeSHA },
} = await client.rest.git.createTree({
owner: ghUser,
repo: ghRepo,
tree,
base_tree: commitSHA,
parents: [commitSHA],
});
return currentTreeSHA;
}
async function createNewCommit(
ghUser: string,
ghRepo: string,
commitSHA: string,
currentTreeSHA: string,
message: string,
client: Octokit,
) {
const {
data: { sha: newCommitSHA },
} = await client.rest.git.createCommit({
owner: ghUser,
repo: ghRepo,
tree: currentTreeSHA,
message: message,
parents: [commitSHA],
});
return newCommitSHA;
}
async function updateBranchRef(
owner: string,
repo: string,
newCommitSHA: string,
branch: string = "main",
client: Octokit,
) {
const result = await client.rest.git.updateRef({
owner,
repo,
ref: `heads/${branch}`,
sha: newCommitSHA,
});
return result;
}
type Options = { branch?: string; prefix?: string; message?: string; ts?: boolean };
export async function valToGH(
repo: string, // owner/repo
val: string | string[], // user/val
ghToken: string,
options?: Options,
) {
const client = new Octokit({ auth: ghToken });
// validate the repo name
const [ghUser, ghRepo] = repo.split("/");
if (repo.split("/").length !== 2) {
throw new Error("Repo name must be of the form {user}/{repo}");
}
// validate the val names
let valsToCommit = typeof val === "string" ? [val] : val;
if (valsToCommit.every(val => val.split("/").length !== 2)) {
throw new Error("All val names must be of the form {user}/{val}");
}
// Assigning reasonable defaults if options are not provided

Submit a PR from Val Town

This val provides a (very) thin wrapper around the GH rest API methods for creating a pull request. It handles the creation of a Octokit client for you.

Usage

Create valimport { submitPR } from "https://esm.town/v/nbbaier/submitPR"; await submitPR(Deno.env.get("GH_REPO_TOKEN"), { owner: "nbbaier", repo: "test-ground", head: "branch_2", base: "main", title: "trying another PR", body: "cool stuff, take a look", });

Parameters

The function takes two parameters: your gh access token and an object that's identical to the object submitted to the gh API. See GH's documentation for more info!

Readme
1
2
3
4
5
6
import { Octokit } from "npm:@octokit/rest";
type PR = Parameters<typeof Octokit["rest"]["pulls"]["create"]>;
export async function submitPR(ghToken: string, ...pr: PR) {
return new Octokit({ auth: ghToken }).rest.pulls.create(pr);
}

githubRestClient

Does what it says! Defaults to using GITHUB_TOKEN env var if none is passed to the function.

Readme
1
2
3
4
5
import { Octokit } from "npm:@octokit/rest";
export async function githubRestClient(ghToken: string | undefined) {
return new Octokit({ auth: ghToken || Deno.env.get("GITHUB_TOKEN") }).rest;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { truncateVal } from "https://esm.town/v/stevekrouse/truncateVal";
import process from "node:process";
export const untitled_qAbtMd3a = (async () => {
const { Octokit } = await import("npm:@octokit/rest");
const octokit = new Octokit({
auth: process.env.githubGists,
});
return truncateVal(octokit.request("POST /gists", {
description: "How big 5?",
"public": false,
files: {
"README.md": {
content: JSON.stringify(new Array(10000000).fill(1)),
},
},
headers: {
"X-GitHub-Api-Version": "2022-11-28",
},
}));
})();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
export const Github_SearchTopRepos = async (
start: string | Date,
days: number = 1,
) => {
const { Octokit } = await import("npm:@octokit/rest");
const octokit = new Octokit();
if (typeof start == "string")
start = new Date(start);
const end = new Date(start);
end.setDate(start.getDate() + days);
return await octokit.rest.search.repos({
q: `created:${start.toISOString()}..${end.toISOString()}`,
sort: "stars",
order: "desc",
per_page: 100,
});
};
1
Next