Skip to content

Commit

Permalink
feat: throw new CommitError if an API error occurs during commit proc…
Browse files Browse the repository at this point in the history
…ess (#434)
  • Loading branch information
chingor13 authored Jan 5, 2023
1 parent 06638c2 commit 5ee7f2a
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 59 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

export class CommitError extends Error {
cause: Error;
constructor(message: string, cause: Error) {
super(message);
this.cause = cause;
}
}
91 changes: 45 additions & 46 deletions src/github/commit-and-push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import {Octokit} from '@octokit/rest';
import {logger} from '../logger';
import {createCommit} from './create-commit';
import {CommitError} from '../errors';

const DEFAULT_FILES_PER_COMMIT = 100;

Expand Down Expand Up @@ -74,6 +75,7 @@ function* inGroupsOf<T>(
* @param {string} refHead the base of the new commit(s)
* @param {TreeObject[]} tree the set of GitHub changes to upload
* @returns {Promise<string>} the GitHub tree SHA
* @throws {CommitError}
*/
export async function createTree(
octokit: Octokit,
Expand All @@ -89,18 +91,22 @@ export async function createTree(
})
).data.tree.sha;
logger.info('Got the latest commit tree');
const treeSha = (
await octokit.git.createTree({
owner: origin.owner,
repo: origin.repo,
tree,
base_tree: oldTreeSha,
})
).data.sha;
logger.info(
`Successfully created a tree with the desired changes with SHA ${treeSha}`
);
return treeSha;
try {
const treeSha = (
await octokit.git.createTree({
owner: origin.owner,
repo: origin.repo,
tree,
base_tree: oldTreeSha,
})
).data.sha;
logger.info(
`Successfully created a tree with the desired changes with SHA ${treeSha}`
);
return treeSha;
} catch (e) {
throw new CommitError(`Error adding to tree: ${refHead}`, e as Error);
}
}

/**
Expand All @@ -119,14 +125,21 @@ export async function updateRef(
force: boolean
): Promise<void> {
logger.info(`Updating reference heads/${origin.branch} to ${newSha}`);
await octokit.git.updateRef({
owner: origin.owner,
repo: origin.repo,
ref: `heads/${origin.branch}`,
sha: newSha,
force,
});
logger.info(`Successfully updated reference ${origin.branch} to ${newSha}`);
try {
await octokit.git.updateRef({
owner: origin.owner,
repo: origin.repo,
ref: `heads/${origin.branch}`,
sha: newSha,
force,
});
logger.info(`Successfully updated reference ${origin.branch} to ${newSha}`);
} catch (e) {
throw new CommitError(
`Error updating ref heads/${origin.branch} to ${newSha}`,
e as Error
);
}
}

/**
Expand All @@ -140,6 +153,7 @@ export async function updateRef(
* @param {string} commitMessage the message of the new commit
* @param {boolean} force to force the commit changes given refHead
* @returns {Promise<void>}
* @throws {CommitError}
*/
export async function commitAndPush(
octokit: Octokit,
Expand All @@ -150,31 +164,16 @@ export async function commitAndPush(
force: boolean,
filesPerCommit: number = DEFAULT_FILES_PER_COMMIT
) {
try {
const tree = generateTreeObjects(changes);
for (const treeGroup of inGroupsOf(tree, filesPerCommit)) {
const treeSha = await createTree(
octokit,
originBranch,
refHead,
treeGroup
);
refHead = await createCommit(
octokit,
originBranch,
refHead,
treeSha,
commitMessage
);
}
await updateRef(octokit, originBranch, refHead, force);
} catch (err) {
(
err as Error
).message = `Error while creating a tree and updating the ref: ${
(err as Error).message
}`;
logger.error(err as Error);
throw err;
const tree = generateTreeObjects(changes);
for (const treeGroup of inGroupsOf(tree, filesPerCommit)) {
const treeSha = await createTree(octokit, originBranch, refHead, treeGroup);
refHead = await createCommit(
octokit,
originBranch,
refHead,
treeSha,
commitMessage
);
}
await updateRef(octokit, originBranch, refHead, force);
}
17 changes: 11 additions & 6 deletions src/github/create-commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import {Octokit} from '@octokit/rest';
import {RepoDomain} from '../types';
import {logger} from '../logger';
import {CommitError} from '../errors';

/**
* Create a commit with a repo snapshot SHA on top of the reference HEAD
Expand All @@ -34,15 +35,19 @@ export async function createCommit(
treeSha: string,
message: string
): Promise<string> {
const commitData = (
await octokit.git.createCommit({
try {
const {
data: {sha, url},
} = await octokit.git.createCommit({
owner: origin.owner,
repo: origin.repo,
message,
tree: treeSha,
parents: [refHead],
})
).data;
logger.info(`Successfully created commit. See commit at ${commitData.url}`);
return commitData.sha;
});
logger.info(`Successfully created commit. See commit at ${url}`);
return sha;
} catch (e) {
throw new CommitError(`Error creating commit for: ${treeSha}`, e as Error);
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {commitAndPush} from './github/commit-and-push';
import {openPullRequest} from './github/open-pull-request';
import {addLabels} from './github/labels';
export {getChanges, getDiffString} from './bin/handle-git-dir-change';
export {CommitError} from './errors';

/**
* Given a set of suggestions, make all the multiline inline review comments on a given pull request given
Expand Down Expand Up @@ -108,6 +109,7 @@ export async function reviewPullRequest(
* @param {Changes | null | undefined} changes A set of changes. The changes may be empty
* @param {CreatePullRequestUserOptions} options The configuration for interacting with GitHub provided by the user.
* @returns {Promise<number>} the pull request number. Returns 0 if unsuccessful.
* @throws {CommitError} on failure during commit process
*/
async function createPullRequest(
octokit: Octokit,
Expand Down
3 changes: 2 additions & 1 deletion test/commit-and-push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import * as handler from '../src/github/commit-and-push';
import * as createCommitModule from '../src/github/create-commit';
import {Changes, FileData, TreeObject, RepoDomain} from '../src/types';
import {createCommit} from '../src/github/create-commit';
import {CommitError} from '../src/errors';

type GetCommitResponse = GetResponseTypeFromEndpointMethod<
typeof octokit.git.getCommit
Expand Down Expand Up @@ -353,7 +354,7 @@ describe('Commit and push function', async () => {
handler.createTree(octokit, origin, '', [
{path: 'foo.txt', type: 'blob', mode: '100755'},
]),
error
e => e instanceof CommitError && e.cause === error
);
});
it('groups files into batches', async () => {
Expand Down

0 comments on commit 5ee7f2a

Please sign in to comment.