Skip to content

Commit 7221ced

Browse files
authored
feat: return repo fullname in fork script (#29)
This PR is a follow-up to a missing step in the `fork` script initial implementation at #3; the fork script failed to return a `repo` which can be used to in the next step of computation. This was because of a weird assumption I had during the initial implementation. 😆See my assumption below... > I assume that the call to the `"POST /repos/{owner}/{repo}/forks"` endpoint only assures of initiating a fork process without assuring us of a response at all. Meaning we might not exactly get a `response.data` following the call ...but that wasn't true, I found out that a `response.data` actually comes, but it might just take some time and only in cases where the repo being forked is huge.... and at the moment forking the project repo happens in less than 5secs. ### Changes Made - Returned `fork` repo - this is a repo fullname value returned from the `isRepositoryForked` helper function; I hereby return it as main returned value from the `forkRepository` function execution in the condition where the repo is already forked on a executing user's account - Returned `response.data.full_name` - this is a newly created fork repo fullname; Its a value from the response to the `"POST /repos/{owner}/{repo}/forks"` endpoint call; I hereby return it as main retuned value from the `forkRepository` function execution in cases where there was no fork already already found on the executing user's account - Cherry picked some changes from #25 to use on here - f12f25f - 436ceea 📖
1 parent d3550d8 commit 7221ced

3 files changed

Lines changed: 8 additions & 9 deletions

File tree

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ GITHUB_APP_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nMII...und==\n-----END PRIVA
88
CRYPTO_SECRET_KEY="secret"
99

1010
PROJECT_REPO="babblebey/jargons.dev"
11-
PROJECT_REPO_BRANCH_REF="heads/main"
11+
PROJECT_REPO_BRANCH_REF="refs/heads/main"

src/lib/branch.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@ export async function createBranch(userOctokit, repoDetails, newBranchName) {
3939
*/
4040
export async function getBranch(userOctokit, repo, ref) {
4141
const { repoOwner, repoName } = getRepoParts(repo);
42+
const formattedRef = ref.split("/")[0] === "refs"
43+
? ref.split("/").slice(1).join("/")
44+
: ref;
4245

4346
const response = await userOctokit.request("GET /repos/{owner}/{repo}/git/ref/{ref}", {
4447
owner: repoOwner,
4548
repo: repoName,
46-
ref: ref,
49+
ref: formattedRef,
4750
});
4851

4952
return response.data;

src/lib/fork.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export async function forkRepository(userOctokit, repoDetails) {
1515

1616
const fork = await isRepositoryForked(userOctokit, repoFullname, user.login );
1717

18-
if (!!fork) {
18+
if (fork) {
1919
console.log("Repo is already forked!");
2020

2121
const { isUpdated, updateSHA } = await isRepositoryForkUpdated(userOctokit, repoDetails, fork)
@@ -29,19 +29,15 @@ export async function forkRepository(userOctokit, repoDetails) {
2929
});
3030
}
3131

32-
return;
32+
return fork;
3333
}
3434

3535
const response = await userOctokit.request("POST /repos/{owner}/{repo}/forks", {
3636
owner: repoOwner,
3737
repo: repoName,
3838
});
3939

40-
if (response.status === 202) {
41-
console.log("Forking process initiated successfully!");
42-
} else {
43-
console.log("Error occurred while forking repository.");
44-
}
40+
return response.data.full_name;
4541
} catch (error) {
4642
console.log("Error occurred while forking repository:", error);
4743
}

0 commit comments

Comments
 (0)