Skip to content

Commit

Permalink
Upgrade to latest @octokit/rest
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Feb 4, 2020
1 parent 6535ca0 commit 883b382
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 96 deletions.
12 changes: 6 additions & 6 deletions build/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as GitHub from "@octokit/rest";
import { Octokit } from "@octokit/rest";
interface MemFSVolume {
toJSON(): any;
}
Expand Down Expand Up @@ -27,11 +27,11 @@ interface FileMap {
/**
* Creates a bunch of blobs, wraps them in a tree, updates a reference from a memfs volume
*/
export declare const memFSToGitHubCommits: (api: GitHub, volume: MemFSVolume, settings: BranchCreationConfig) => Promise<void>;
export declare const memFSToGitHubCommits: (api: Octokit, volume: MemFSVolume, settings: BranchCreationConfig) => Promise<void>;
/**
* Creates a bunch of blobs, wraps them in a tree, updates a reference from a map of files to contents
*/
export declare const filepathContentsMapToUpdateGitHubBranch: (api: GitHub, fileMap: FileMap, settings: BranchCreationConfig) => Promise<void>;
export declare const filepathContentsMapToUpdateGitHubBranch: (api: Octokit, fileMap: FileMap, settings: BranchCreationConfig) => Promise<void>;
/**
* A Git tree object creates the hierarchy between files in a Git repository. To create a tree
* we need to make a list of blobs (which represent changes to the FS)
Expand All @@ -40,19 +40,19 @@ export declare const filepathContentsMapToUpdateGitHubBranch: (api: GitHub, file
*
* https://developer.github.com/v3/git/trees/
*/
export declare const createTree: (api: GitHub, settings: BranchCreationConfig) => (fileMap: FileMap, baseSha: string) => Promise<any>;
export declare const createTree: (api: Octokit, settings: BranchCreationConfig) => (fileMap: FileMap, baseSha: string) => Promise<any>;
/**
* A Git commit is a snapshot of the hierarchy (Git tree) and the contents of the files (Git blob) in a Git repository
*
* https://developer.github.com/v3/git/commits/
*/
export declare const createACommit: (api: GitHub, settings: BranchCreationConfig) => (treeSha: string, parentSha: string) => Promise<any>;
export declare const createACommit: (api: Octokit, settings: BranchCreationConfig) => (treeSha: string, parentSha: string) => Promise<any>;
/**
* A Git reference (git ref) is just a file that contains a Git commit SHA-1 hash. When referring
* to a Git commit, you can use the Git reference, which is an easy-to-remember name, rather than
* the hash. The Git reference can be rewritten to point to a new commit.
*
* https://developer.github.com/v3/git/refs/#git-references
*/
export declare const updateReference: (api: GitHub, settings: BranchCreationConfig) => (newSha: string) => Promise<any>;
export declare const updateReference: (api: Octokit, settings: BranchCreationConfig) => (newSha: string) => Promise<any>;
export {};
14 changes: 7 additions & 7 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ exports.filepathContentsMapToUpdateGitHubBranch = (api, fileMap, settings) => __
});
/** If we want to make a commit, or update a reference, we'll need the original commit */
const shaForBranch = (api, settings) => __awaiter(this, void 0, void 0, function* () {
return api.gitdata.getReference({
return api.git.getRef({
owner: settings.owner,
repo: settings.repo,
ref: settings.fullBaseBranch || "heads/master"
Expand All @@ -43,22 +43,22 @@ const shaForBranch = (api, settings) => __awaiter(this, void 0, void 0, function
*/
exports.createTree = (api, settings) => (fileMap, baseSha) => __awaiter(this, void 0, void 0, function* () {
const blobSettings = { owner: settings.owner, repo: settings.repo };
const createBlobs = Object.keys(fileMap).map(filename => api.gitdata.createBlob(Object.assign({}, blobSettings, { content: fileMap[filename] })).then((blob) => ({
const createBlobs = Object.keys(fileMap).map(filename => api.git.createBlob(Object.assign({}, blobSettings, { content: fileMap[filename] })).then((blob) => ({
sha: blob.data.sha,
path: filename,
mode: "100644",
type: "blob"
})));
const blobs = yield Promise.all(createBlobs);
const tree = yield api.gitdata.createTree(Object.assign({}, blobSettings, { tree: blobs, base_tree: baseSha }));
const tree = yield api.git.createTree(Object.assign({}, blobSettings, { tree: blobs, base_tree: baseSha }));
return tree.data;
});
/**
* A Git commit is a snapshot of the hierarchy (Git tree) and the contents of the files (Git blob) in a Git repository
*
* https://developer.github.com/v3/git/commits/
*/
exports.createACommit = (api, settings) => (treeSha, parentSha) => api.gitdata.createCommit({
exports.createACommit = (api, settings) => (treeSha, parentSha) => api.git.createCommit({
owner: settings.owner,
repo: settings.repo,
message: settings.message,
Expand All @@ -79,12 +79,12 @@ exports.updateReference = (api, settings) => (newSha) => __awaiter(this, void 0,
ref: `refs/${settings.fullBranchReference}`
};
try {
yield api.gitdata.getReference(refSettings);
yield api.git.getRef(refSettings);
// It must exist, so we should update it
return api.gitdata.createReference(Object.assign({}, refSettings, { sha: newSha }));
return api.git.createRef(Object.assign({}, refSettings, { sha: newSha }));
}
catch (error) {
// We have to create the reference because it doesn't exist yet
return api.gitdata.createReference(Object.assign({}, refSettings, { sha: newSha }));
return api.git.createRef(Object.assign({}, refSettings, { sha: newSha }));
}
});
28 changes: 14 additions & 14 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as GitHub from "@octokit/rest"
import { Octokit } from "@octokit/rest"

interface MemFSVolume {
toJSON(): any
Expand Down Expand Up @@ -31,7 +31,7 @@ interface FileMap {
/**
* Creates a bunch of blobs, wraps them in a tree, updates a reference from a memfs volume
*/
export const memFSToGitHubCommits = async (api: GitHub, volume: MemFSVolume, settings: BranchCreationConfig) => {
export const memFSToGitHubCommits = async (api: Octokit, volume: MemFSVolume, settings: BranchCreationConfig) => {
const fileMap: FileMap = volume.toJSON()
return filepathContentsMapToUpdateGitHubBranch(api, fileMap, settings)
}
Expand All @@ -40,7 +40,7 @@ export const memFSToGitHubCommits = async (api: GitHub, volume: MemFSVolume, set
* Creates a bunch of blobs, wraps them in a tree, updates a reference from a map of files to contents
*/
export const filepathContentsMapToUpdateGitHubBranch = async (
api: GitHub,
api: Octokit,
fileMap: FileMap,
settings: BranchCreationConfig
) => {
Expand All @@ -52,8 +52,8 @@ export const filepathContentsMapToUpdateGitHubBranch = async (
}

/** If we want to make a commit, or update a reference, we'll need the original commit */
const shaForBranch = async (api: GitHub, settings: BranchCreationConfig) =>
api.gitdata.getReference({
const shaForBranch = async (api: Octokit, settings: BranchCreationConfig) =>
api.git.getRef({
owner: settings.owner,
repo: settings.repo,
ref: settings.fullBaseBranch || "heads/master"
Expand All @@ -67,13 +67,13 @@ const shaForBranch = async (api: GitHub, settings: BranchCreationConfig) =>
*
* https://developer.github.com/v3/git/trees/
*/
export const createTree = (api: GitHub, settings: BranchCreationConfig) => async (
export const createTree = (api: Octokit, settings: BranchCreationConfig) => async (
fileMap: FileMap,
baseSha: string
): Promise<any> => {
const blobSettings = { owner: settings.owner, repo: settings.repo }
const createBlobs = Object.keys(fileMap).map(filename =>
api.gitdata.createBlob({ ...blobSettings, content: fileMap[filename] }).then((blob: any) => ({
api.git.createBlob({ ...blobSettings, content: fileMap[filename] }).then((blob: any) => ({
sha: blob.data.sha,
path: filename,
mode: "100644",
Expand All @@ -82,7 +82,7 @@ export const createTree = (api: GitHub, settings: BranchCreationConfig) => async
)

const blobs = await Promise.all(createBlobs)
const tree = await api.gitdata.createTree({ ...blobSettings, tree: blobs as any, base_tree: baseSha })
const tree = await api.git.createTree({ ...blobSettings, tree: blobs as any, base_tree: baseSha })
return tree.data
}

Expand All @@ -91,11 +91,11 @@ export const createTree = (api: GitHub, settings: BranchCreationConfig) => async
*
* https://developer.github.com/v3/git/commits/
*/
export const createACommit = (api: GitHub, settings: BranchCreationConfig) => (
export const createACommit = (api: Octokit, settings: BranchCreationConfig) => (
treeSha: string,
parentSha: string
): Promise<any> =>
api.gitdata.createCommit({
api.git.createCommit({
owner: settings.owner,
repo: settings.repo,
message: settings.message,
Expand All @@ -110,7 +110,7 @@ export const createACommit = (api: GitHub, settings: BranchCreationConfig) => (
*
* https://developer.github.com/v3/git/refs/#git-references
*/
export const updateReference = (api: GitHub, settings: BranchCreationConfig) => async (
export const updateReference = (api: Octokit, settings: BranchCreationConfig) => async (
newSha: string
): Promise<any> => {
const refSettings = {
Expand All @@ -119,16 +119,16 @@ export const updateReference = (api: GitHub, settings: BranchCreationConfig) =>
ref: `refs/${settings.fullBranchReference}`
}
try {
await api.gitdata.getReference(refSettings)
await api.git.getRef(refSettings)

// It must exist, so we should update it
return api.gitdata.createReference({
return api.git.createRef({
...refSettings,
sha: newSha
})
} catch (error) {
// We have to create the reference because it doesn't exist yet
return api.gitdata.createReference({
return api.git.createRef({
...refSettings,
sha: newSha
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"typescript": "^3.1.1"
},
"dependencies": {
"@octokit/rest": "15.12.1"
"@octokit/rest": "^16.43.1"
}
}
Loading

0 comments on commit 883b382

Please sign in to comment.