Skip to content

Commit

Permalink
cp
Browse files Browse the repository at this point in the history
  • Loading branch information
slavingia committed Sep 25, 2024
1 parent 5337501 commit 4a097ad
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
11 changes: 9 additions & 2 deletions app/(dashboard)/dashboard/pull-request.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export function PullRequestItem({ pullRequest }: PullRequestItemProps) {
content: file.content,
}));

await commitChangesToPullRequest(
const newCommitUrl = await commitChangesToPullRequest(
pullRequest.repository.owner.login,
pullRequest.repository.name,
pullRequest.number,
Expand All @@ -132,7 +132,14 @@ export function PullRequestItem({ pullRequest }: PullRequestItemProps) {

toast({
title: "Changes committed successfully",
description: "The test files have been added to the pull request.",
description: (
<>
The test files have been added to the pull request.{" "}
<Link href={newCommitUrl} className="underline">
View commit
</Link>
</>
),
});

setTestFiles([]);
Expand Down
55 changes: 42 additions & 13 deletions lib/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export async function commitChangesToPullRequest(
repo: string,
pullNumber: number,
filesToCommit: TestFile[]
) {
): Promise<string> {
const octokit = await getOctokit();

try {
Expand All @@ -191,28 +191,53 @@ export async function commitChangesToPullRequest(
commit_sha: ref.object.sha,
});

const tree = await Promise.all(
filesToCommit.map(async (file) => {
const { data: blob } = await octokit.git.createBlob({
owner,
repo,
content: file.content,
encoding: "utf-8",
});
const { data: currentTree } = await octokit.git.getTree({
owner,
repo,
tree_sha: commit.tree.sha,
recursive: "true",
});

return {
const updatedTree = currentTree.tree.map((item) => ({
path: item.path,
mode: item.mode,
type: item.type,
sha: item.sha,
}));

for (const file of filesToCommit) {
const { data: blob } = await octokit.git.createBlob({
owner,
repo,
content: file.content,
encoding: "utf-8",
});

const existingFileIndex = updatedTree.findIndex(
(item) => item.path === file.name
);
if (existingFileIndex !== -1) {
updatedTree[existingFileIndex] = {
path: file.name,
mode: "100644",
type: "blob",
sha: blob.sha,
};
})
);
} else {
updatedTree.push({
path: file.name,
mode: "100644",
type: "blob",
sha: blob.sha,
});
}
}

const { data: newTree } = await octokit.git.createTree({
owner,
repo,
tree: tree as any,
tree: updatedTree as any,
base_tree: commit.tree.sha,
});

const { data: newCommit } = await octokit.git.createCommit({
Expand All @@ -229,11 +254,15 @@ export async function commitChangesToPullRequest(
ref: `heads/${pr.head.ref}`,
sha: newCommit.sha,
});

// Return the commit URL
return `https://github.com/${owner}/${repo}/commit/${newCommit.sha}`;
} catch (error) {
console.error("Error committing changes to pull request:", error);
throw error;
}
}

export async function getPullRequestInfo(
owner: string,
repo: string,
Expand Down

0 comments on commit 4a097ad

Please sign in to comment.