Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:deleted branch from repository #49

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app/dashboard/[projectId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default async function Page({
<DetailItem
label="Last Commit"
value={
repository.lastCommit.commit.author?.date
repository.lastCommit && repository.lastCommit.commit.author?.date
? new Date(
repository.lastCommit.commit.author.date,
).toLocaleDateString()
Expand Down
32 changes: 26 additions & 6 deletions src/lib/github/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { unstable_cache } from "next/cache"
import { Octokit } from "octokit"
import { Octokit, RequestError } from "octokit"
import { checkSpecificErrorGithubApi } from "../handle-error"

export const getRepositoryDetails = unstable_cache(
async (owner: string, repo: string, octokit: Octokit) => {
Expand All @@ -8,15 +9,15 @@ export const getRepositoryDetails = unstable_cache(
repo,
})

const { data: lastActivity } = await octokit.rest.repos.getBranch({
const lastActivity = await getBranchRepository(
owner,
repo,
branch: repository.default_branch,
})

repository.default_branch,
octokit,
)
return {
...repository,
lastCommit: lastActivity.commit,
lastCommit: lastActivity?.commit,
}
},
undefined,
Expand All @@ -25,6 +26,25 @@ export const getRepositoryDetails = unstable_cache(
},
)

export const getBranchRepository = unstable_cache(
async (owner: string, repo: string, branch: string, octokit: Octokit) => {
try {
const { data: lastActivity } = await octokit.rest.repos.getBranch({
owner,
repo,
branch: branch,
})
return lastActivity
} catch (error) {
if (error instanceof RequestError) {
checkSpecificErrorGithubApi(error)
} else {
throw error
}
}
},
)

export const getMyntenanceRepository = unstable_cache(
async (octokit: Octokit) => {
return getRepositoryDetails("Balastrong", "myntenance", octokit)
Expand Down
10 changes: 10 additions & 0 deletions src/lib/handle-error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isRedirectError } from "next/dist/client/components/redirect"
import { RequestError } from "octokit"
import { toast } from "sonner"
import { z } from "zod"

Expand All @@ -23,3 +24,12 @@ export function showErrorToast(err: unknown) {
const errorMessage = getErrorMessage(err)
return toast.error(errorMessage)
}

export function checkSpecificErrorGithubApi(error: RequestError) {
const message = error.message.split("-")[0].trim()
switch (message) {
case "Branch not found":
default:
return null
}
}