-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
/suggestSource
endpoint for external use (#300)
- Loading branch information
Showing
13 changed files
with
475 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,21 @@ | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
import { Octokit } from "@octokit/core"; | ||
|
||
import { auth } from "../auth/[...nextauth]"; | ||
import { getOctokit } from "@/utils/getOctokit"; | ||
import { withGithubErrorHandler } from "@/utils/githubApiErrorHandler"; | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
// Check if the user is authenticated | ||
const session = await auth(req, res); | ||
if (!session || !session.accessToken || !session.user?.githubUsername) { | ||
return res.status(401).json({ message: "Unauthorized" }); | ||
} | ||
async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
const { octokit, owner } = await getOctokit(req, res); | ||
const { repo } = req.body; | ||
|
||
const { owner, repo } = req.body; | ||
|
||
// Initialize Octokit with the user's access token | ||
const octokit = new Octokit({ auth: session.accessToken }); | ||
|
||
try { | ||
// Fork the repository | ||
const result = await octokit.request("POST /repos/{owner}/{repo}/forks", { | ||
owner, | ||
repo, | ||
}); | ||
res.status(200).json(result.data); | ||
} catch (error) { | ||
console.error("fork failed"); | ||
console.error(error); | ||
res.status(500).json({ message: "Error occurred while creating fork" }); | ||
} | ||
// Fork the repository | ||
const result = await octokit.request("POST /repos/{owner}/{repo}/forks", { | ||
owner, | ||
repo, | ||
}); | ||
res.status(200).json(result.data); | ||
} | ||
|
||
export default withGithubErrorHandler( | ||
handler, | ||
"Error occurred while creating fork" | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,26 @@ | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
import { Octokit } from "@octokit/core"; | ||
|
||
import { createPullRequest } from "@/utils/github"; | ||
import { auth } from "../auth/[...nextauth]"; | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
// Check if the user is authenticated | ||
const session = await auth(req, res); | ||
if (!session || !session.accessToken || !session.user?.githubUsername) { | ||
return res.status(401).json({ message: "Unauthorized" }); | ||
} | ||
import { getOctokit } from "@/utils/getOctokit"; | ||
import { withGithubErrorHandler } from "@/utils/githubApiErrorHandler"; | ||
|
||
// Initialize Octokit with the user's access token | ||
const octokit = new Octokit({ auth: session.accessToken }); | ||
async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
const { octokit, owner } = await getOctokit(req, res); | ||
const { repo, title, body, head, base } = req.body; | ||
|
||
const { owner, repo, title, body, head, base } = req.body; | ||
const prResult = await createPullRequest({ | ||
octokit, | ||
owner, | ||
repo, | ||
title, | ||
body, | ||
head, | ||
base, | ||
}); | ||
|
||
try { | ||
const prResult = await createPullRequest({ | ||
octokit, | ||
owner, | ||
repo, | ||
title, | ||
body, | ||
head, | ||
base, | ||
}); | ||
|
||
return res.status(200).json(prResult.data); | ||
} catch (error: any) { | ||
console.error(error); | ||
res.status(500).json({ | ||
message: | ||
error?.message ?? "Error occurred while creating the Pull Request", | ||
}); | ||
} | ||
return res.status(200).json(prResult.data); | ||
} | ||
|
||
export default withGithubErrorHandler( | ||
handler, | ||
"Error occurred while creating the Pull Request" | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
import yaml from "js-yaml"; | ||
import { getOctokit } from "@/utils/getOctokit"; | ||
import { upstreamOwner, upstreamRepo } from "@/config/default"; | ||
import { deriveFileSlug } from "@/utils"; | ||
import { createNewBranch } from "./newBranch"; | ||
import { withGithubErrorHandler } from "@/utils/githubApiErrorHandler"; | ||
|
||
async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
if (req.method !== "POST") { | ||
return res.status(405).json({ message: "Method not allowed" }); | ||
} | ||
|
||
const { octokit, owner } = await getOctokit(req, res, { | ||
allowAppFallback: true, | ||
}); | ||
const { title, media, targetRepository } = req.body; | ||
|
||
if (owner !== upstreamOwner) { | ||
// Fork the main repository | ||
await octokit.request("POST /repos/{owner}/{repo}/forks", { | ||
owner: upstreamOwner, | ||
repo: upstreamRepo, | ||
}); | ||
} | ||
|
||
// Create new branch | ||
const timeInSeconds = Math.floor(Date.now() / 1000); | ||
const fileName = deriveFileSlug(title); | ||
const branchName = `${timeInSeconds}-${fileName}`; | ||
await createNewBranch({ | ||
octokit, | ||
upstreamRepo, | ||
baseBranch: | ||
process.env.NEXT_PUBLIC_VERCEL_ENV === "production" | ||
? "master" | ||
: "staging", | ||
branchName, | ||
owner, | ||
}); | ||
|
||
// Save file | ||
const transcriptMarkdown = | ||
`---\n` + | ||
yaml.dump( | ||
{ | ||
title, | ||
media, | ||
needs: "transcript", | ||
}, | ||
{ | ||
forceQuotes: true, | ||
} | ||
) + | ||
"---\n"; | ||
|
||
await octokit.request("PUT /repos/{owner}/{repo}/contents/{path}", { | ||
owner, | ||
repo: upstreamRepo, | ||
path: `misc/${fileName}.md`, | ||
message: `curate(transcript): "${title}"`, | ||
content: Buffer.from(transcriptMarkdown).toString("base64"), | ||
branch: branchName, | ||
}); | ||
|
||
// Open PR with user's suggestion | ||
const prResult = await octokit.request("POST /repos/{owner}/{repo}/pulls", { | ||
owner: targetRepository === "user" ? owner : upstreamOwner, | ||
repo: upstreamRepo, | ||
title: `curate(transcript): "${title}"`, | ||
body: `This PR is a suggestion for the transcription of [${title}](${media}).`, | ||
head: `${owner}:${branchName}`, | ||
base: | ||
process.env.NEXT_PUBLIC_VERCEL_ENV === "production" | ||
? "master" | ||
: "staging", | ||
}); | ||
|
||
res.status(200).json({ | ||
message: "Suggestion submitted successfully", | ||
pr_url: prResult.data.html_url, | ||
}); | ||
} | ||
|
||
export default withGithubErrorHandler( | ||
handler, | ||
"Error occurred while submitting suggestion" | ||
); |
Oops, something went wrong.