Skip to content

Commit e0a723e

Browse files
Copilotmheap
andcommitted
Replace 'any' types with proper Octokit types
Co-authored-by: mheap <[email protected]>
1 parent 0f5d9c5 commit e0a723e

File tree

2 files changed

+103
-52
lines changed

2 files changed

+103
-52
lines changed

create-or-update-files.ts

Lines changed: 100 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,65 @@
1-
function isBase64(str: any): boolean {
1+
import { Octokit } from "@octokit/rest";
2+
import { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";
3+
4+
interface FileChange {
5+
contents?: string | Buffer;
6+
mode?: "100644" | "100755" | "040000" | "160000" | "120000";
7+
type?: "blob" | "tree" | "commit";
8+
}
9+
10+
interface Change {
11+
message: string;
12+
files?: Record<string, string | Buffer | FileChange>;
13+
filesToDelete?: string[];
14+
ignoreDeletionFailures?: boolean;
15+
}
16+
17+
interface Options {
18+
owner: string;
19+
repo: string;
20+
branch: string;
21+
base?: string;
22+
createBranch?: boolean;
23+
committer?: RestEndpointMethodTypes["git"]["createCommit"]["parameters"]["committer"];
24+
author?: RestEndpointMethodTypes["git"]["createCommit"]["parameters"]["author"];
25+
changes: Change[];
26+
batchSize?: number;
27+
forkFromBaseBranch?: boolean;
28+
}
29+
30+
interface CommitResult {
31+
commits: Array<
32+
RestEndpointMethodTypes["git"]["createCommit"]["response"]["data"]
33+
>;
34+
}
35+
36+
function isBase64(str: string | Buffer): boolean {
237
// Handle buffer inputs
38+
let strValue: string;
339
if (Buffer.isBuffer(str)) {
4-
str = str.toString("utf8");
40+
strValue = str.toString("utf8");
41+
} else {
42+
strValue = str;
543
}
644

745
var notBase64 = /[^A-Z0-9+\/=]/i;
8-
const isString = typeof str === "string" || str instanceof String;
946

10-
if (!isString) {
11-
let invalidType;
12-
if (str === null) {
13-
invalidType = "null";
14-
} else {
15-
invalidType = typeof str;
16-
if (
17-
invalidType === "object" &&
18-
str.constructor &&
19-
str.constructor.hasOwnProperty("name")
20-
) {
21-
invalidType = str.constructor.name;
22-
} else {
23-
invalidType = `a ${invalidType}`;
24-
}
25-
}
26-
throw new TypeError(`Expected string but received ${invalidType}.`);
27-
}
28-
29-
const len = str.length;
30-
if (!len || len % 4 !== 0 || notBase64.test(str)) {
47+
const len = strValue.length;
48+
if (!len || len % 4 !== 0 || notBase64.test(strValue)) {
3149
return false;
3250
}
33-
const firstPaddingChar = str.indexOf("=");
51+
const firstPaddingChar = strValue.indexOf("=");
3452
return (
3553
firstPaddingChar === -1 ||
3654
firstPaddingChar === len - 1 ||
37-
(firstPaddingChar === len - 2 && str[len - 1] === "=")
55+
(firstPaddingChar === len - 2 && strValue[len - 1] === "=")
3856
);
3957
}
4058

41-
module.exports = function (octokit: any, opts: any) {
59+
module.exports = function (
60+
octokit: Octokit,
61+
opts: Options,
62+
): Promise<CommitResult> {
4263
return new Promise(async (resolve, reject) => {
4364
// Up front validation
4465
try {
@@ -110,7 +131,9 @@ module.exports = function (octokit: any, opts: any) {
110131
}
111132

112133
// Create blobs
113-
const commits = [];
134+
const commits: Array<
135+
RestEndpointMethodTypes["git"]["createCommit"]["response"]["data"]
136+
> = [];
114137
for (const change of changes) {
115138
const message = change.message;
116139
if (!message) {
@@ -129,7 +152,9 @@ module.exports = function (octokit: any, opts: any) {
129152
);
130153
}
131154

132-
const treeItems = [];
155+
const treeItems: Array<
156+
RestEndpointMethodTypes["git"]["createTree"]["parameters"]["tree"][number]
157+
> = [];
133158
// Handle file deletions
134159
if (hasFilesToDelete) {
135160
for (const batch of chunk(change.filesToDelete, batchSize)) {
@@ -171,9 +196,22 @@ module.exports = function (octokit: any, opts: any) {
171196
batch.map(async (fileName: string) => {
172197
const properties = change.files[fileName] || "";
173198

174-
const contents = properties.contents || properties;
175-
const mode = properties.mode || "100644";
176-
const type = properties.type || "blob";
199+
let contents: string | Buffer;
200+
let mode: "100644" | "100755" | "040000" | "160000" | "120000";
201+
let type: "blob" | "tree" | "commit";
202+
203+
if (
204+
typeof properties === "string" ||
205+
Buffer.isBuffer(properties)
206+
) {
207+
contents = properties;
208+
mode = "100644";
209+
type = "blob";
210+
} else {
211+
contents = properties.contents || "";
212+
mode = properties.mode || "100644";
213+
type = properties.type || "blob";
214+
}
177215

178216
if (!contents) {
179217
return reject(`No file contents provided for ${fileName}`);
@@ -257,12 +295,12 @@ module.exports = function (octokit: any, opts: any) {
257295
};
258296

259297
async function fileExistsInRepo(
260-
octokit: any,
298+
octokit: Octokit,
261299
owner: string,
262300
repo: string,
263301
path: string,
264302
branch: string,
265-
) {
303+
): Promise<boolean> {
266304
try {
267305
await octokit.rest.repos.getContent({
268306
method: "HEAD",
@@ -278,15 +316,15 @@ async function fileExistsInRepo(
278316
}
279317

280318
async function createCommit(
281-
octokit: any,
319+
octokit: Octokit,
282320
owner: string,
283321
repo: string,
284-
committer: any,
285-
author: any,
322+
committer: RestEndpointMethodTypes["git"]["createCommit"]["parameters"]["committer"],
323+
author: RestEndpointMethodTypes["git"]["createCommit"]["parameters"]["author"],
286324
message: string,
287-
tree: any,
325+
tree: RestEndpointMethodTypes["git"]["createTree"]["response"]["data"],
288326
baseTree: string,
289-
) {
327+
): Promise<RestEndpointMethodTypes["git"]["createCommit"]["response"]["data"]> {
290328
return (
291329
await octokit.rest.git.createCommit({
292330
owner,
@@ -301,12 +339,14 @@ async function createCommit(
301339
}
302340

303341
async function createTree(
304-
octokit: any,
342+
octokit: Octokit,
305343
owner: string,
306344
repo: string,
307-
treeItems: any[],
345+
treeItems: Array<
346+
RestEndpointMethodTypes["git"]["createTree"]["parameters"]["tree"][number]
347+
>,
308348
baseTree: string,
309-
) {
349+
): Promise<RestEndpointMethodTypes["git"]["createTree"]["response"]["data"]> {
310350
return (
311351
await octokit.rest.git.createTree({
312352
owner,
@@ -318,19 +358,23 @@ async function createTree(
318358
}
319359

320360
async function createBlob(
321-
octokit: any,
361+
octokit: Octokit,
322362
owner: string,
323363
repo: string,
324-
contents: any,
364+
contents: string | Buffer,
325365
type: string,
326-
) {
366+
): Promise<string> {
327367
if (type === "commit") {
328-
return contents;
368+
// For submodules, contents is the commit SHA
369+
return typeof contents === "string" ? contents : contents.toString();
329370
} else {
330-
let content = contents;
371+
let content: string;
331372

332-
if (!isBase64(content)) {
373+
if (!isBase64(contents)) {
333374
content = Buffer.from(contents).toString("base64");
375+
} else {
376+
content =
377+
typeof contents === "string" ? contents : contents.toString("base64");
334378
}
335379

336380
const file = (
@@ -345,7 +389,12 @@ async function createBlob(
345389
}
346390
}
347391

348-
async function loadRef(octokit: any, owner: string, repo: string, ref: string) {
392+
async function loadRef(
393+
octokit: Octokit,
394+
owner: string,
395+
repo: string,
396+
ref: string,
397+
): Promise<string | undefined> {
349398
try {
350399
const x = await octokit.rest.git.getRef({
351400
owner,
@@ -358,8 +407,8 @@ async function loadRef(octokit: any, owner: string, repo: string, ref: string) {
358407
}
359408
}
360409

361-
const chunk = (input: any[], size: number) => {
362-
return input.reduce((arr, item, idx) => {
410+
const chunk = <T>(input: T[], size: number): T[][] => {
411+
return input.reduce((arr: T[][], item: T, idx: number) => {
363412
return idx % size === 0
364413
? [...arr, [item]]
365414
: [...arr.slice(0, -1), [...arr.slice(-1)[0], item]];

index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { Octokit } from "@octokit/rest";
2+
13
const createOrUpdateFilesPlugin = require("./create-or-update-files");
24

3-
module.exports = function (octokit: any) {
5+
module.exports = function (octokit: Octokit) {
46
return {
57
createOrUpdateFiles: createOrUpdateFilesPlugin.bind(null, octokit),
68
};

0 commit comments

Comments
 (0)