Skip to content

Commit

Permalink
chore: undo key location change
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhamPalriwala committed Feb 6, 2024
1 parent 1352ae8 commit 92d185b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 61 deletions.
106 changes: 51 additions & 55 deletions github/services/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import { App } from "octokit"
import { env } from "@/env.mjs"
import { db } from "@/lib/db"

const privateKeyPath =
"/home/shubham/Downloads/ossgg-test.2024-01-18.private-key.pem"
// const resolvedPath = path.resolve(__dirname, privateKeyPath)
const resolvedPath = path.resolve(privateKeyPath)
const privateKeyPath = "../../../../key.pem"
const resolvedPath = path.resolve(__dirname, privateKeyPath)
const privateKey = readFileSync(resolvedPath, "utf8")

export const sendInstallationDetails = async (
Expand All @@ -26,72 +24,69 @@ export const sendInstallationDetails = async (
installation: any
): Promise<void> => {
try {
const config = {
appId: appId,
privateKey: privateKey,
const app = new App({
appId,
privateKey,
webhooks: {
secret: env.GITHUB_WEBHOOK_SECRET!,
},
}
const app = new App(config)
})
const octokit = await app.getInstallationOctokit(installationId)

const result = await db.$transaction(async (tx) => {
await db.$transaction(async (tx) => {
const installationPrisma = await tx.installation.upsert({
where: { githubId: installationId },
update: {
githubId: installationId,
type: installation?.account?.type.toLowerCase(),
},
update: { type: installation?.account?.type.toLowerCase() },
create: {
githubId: installationId,
type: installation?.account?.type.toLowerCase(),
},
})

if (installation?.account?.type.toLowerCase() === "organization") {
const userType = installation?.account?.type.toLowerCase()
if (userType === "organization") {
const membersOfOrg = await octokit.rest.orgs.listMembers({
org: installation?.account?.login,
role: "all",
})

for (const member of membersOfOrg.data) {
const newUser = await tx.user.upsert({
where: { githubId: member.id },
update: {},
create: {
githubId: member.id,
login: member.login,
},
})
await Promise.all(
membersOfOrg.data.map(async (member) => {
const newUser = await tx.user.upsert({
where: { githubId: member.id },
update: {},
create: {
githubId: member.id,
login: member.login,
},
})

await tx.membership.upsert({
where: {
userId_installationId: {
await tx.membership.upsert({
where: {
userId_installationId: {
userId: newUser.id,
installationId: installationPrisma.id,
},
},
update: {},
create: {
userId: newUser.id,
installationId: installationPrisma.id,
role: "member",
},
},
update: {},
create: {
userId: newUser.id,
installationId: installationPrisma.id,
role: "member",
},
})
})
}
)
} else {
const user = installation.account
const newUser = await tx.user.upsert({
where: {
githubId: user.id,
},
where: { githubId: user.id },
update: {},
create: {
githubId: user.id,
login: user.login,
name: user.name,
email: user.email,
name: user.name || "",
email: user.email || "",
},
})

Expand All @@ -110,24 +105,25 @@ export const sendInstallationDetails = async (
},
})
}
if (!repos) {
return
}
for (const repo of repos) {
await tx.repository.upsert({
where: { githubId: repo.id },
update: {},
create: {
githubId: repo.id,
name: repo.name,
installationId: installationPrisma.id,
},
})

if (repos) {
await Promise.all(
repos.map(async (repo) => {
await tx.repository.upsert({
where: { githubId: repo.id },
update: {},
create: {
githubId: repo.id,
name: repo.name,
installationId: installationPrisma.id,
},
})
})
)
}
})

return result
} catch (error) {
console.error(`Failed to post installation details: ${error}`)
throw new Error(`Failed to post installation details: ${error}`)
}
}
8 changes: 2 additions & 6 deletions github/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ import { Octokit } from "@octokit/rest"

import { GITHUB_APP_APP_ID } from "./constants"

// const resolvedPath = path.resolve(__dirname, "../../../../key.pem")
const privateKeyPath =
"/home/shubham/Downloads/ossgg-test.2024-01-18.private-key.pem"
// const resolvedPath = path.resolve(__dirname, privateKeyPath)
const resolvedPath = path.resolve(privateKeyPath)

const privateKeyPath = "../../../../key.pem"
const resolvedPath = path.resolve(__dirname, privateKeyPath)
const privateKey = readFileSync(resolvedPath, "utf8")

export const getOctokitInstance = (installationId: number) => {
Expand Down

0 comments on commit 92d185b

Please sign in to comment.