-
Notifications
You must be signed in to change notification settings - Fork 99
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
render readme html scrapped via the server #781
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
This file was deleted.
Oops, something went wrong.
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,133 @@ | ||
import axios from "axios"; | ||
import type { Contributor, GUIPackage, Package } from "$libs/types"; | ||
import yaml from "js-yaml"; | ||
import { isDev } from "@native"; | ||
import log from "./logger"; | ||
export async function getPackageYaml(pkgYamlUrl: string) { | ||
const url = pkgYamlUrl.replace("/github.com", "/raw.githubusercontent.com").replace("/blob", ""); | ||
|
||
const { data: rawYaml } = await axios.get(url); | ||
|
||
const data = await yaml.load(rawYaml); | ||
|
||
return data; | ||
} | ||
|
||
export async function getReadme( | ||
pkg: Partial<GUIPackage> | ||
): Promise<{ data: string; type: "md" | "rst" | "html" }> { | ||
let type: "md" | "rst" | "html" = "md"; | ||
let data = ""; | ||
try { | ||
const stage = (await isDev()) ? "dev" : "prod"; | ||
const reqHTML = await axios.get(`https://gui.tea.xyz/${stage}/${pkg.full_name!}/readme.html`); | ||
if (reqHTML.status === 200 && reqHTML.data) { | ||
return { | ||
data: reqHTML.data, | ||
type: "html" | ||
}; | ||
} | ||
|
||
const repo = repoCloudProvider(pkg.github_url!); | ||
const { namespace, projectName } = getNamespaceProject(pkg.github_url!); // TODO: replace with repo_url | ||
|
||
if (repo.isGithub) { | ||
const reqGithub = await axios.get( | ||
`https://github.com/repos/${namespace}/${projectName}/readme` | ||
); | ||
type = reqGithub.data.name.endsWith(".rst") ? "rst" : "md"; | ||
const reqDl = await axios.get(reqGithub.data.download_url); | ||
data = reqDl.data; | ||
} else if (repo.isGitlab) { | ||
const gitlabApiUrl = `https://gitlab.com/api/v4/projects/${encodeURIComponent( | ||
namespace + "/" + projectName | ||
)}`; | ||
const reqForId = await axios.get(gitlabApiUrl); | ||
const projectId = reqForId.data.id; | ||
const reqGitlab = await axios.get( | ||
`https://gitlab.com/api/v4/projects/${projectId}/repository/files/README.md/raw` | ||
); | ||
data = reqGitlab.data; | ||
} | ||
} catch (error) { | ||
log.error(error); | ||
} | ||
return { data, type }; | ||
} | ||
|
||
export async function getContributors(pkg: Pick<GUIPackage, "github_url">): Promise<Contributor[]> { | ||
// TODO: replace with repo_url | ||
// TODO: work with gitlab api GET /projects/:id/repository/contributors or index this in the db too | ||
const { namespace, projectName } = getNamespaceProject(pkg.github_url!); | ||
// maintainer/repo | ||
let contributors: Contributor[] = []; | ||
try { | ||
const repo = repoCloudProvider(pkg.github_url!); | ||
if (repo.isGithub) { | ||
const req = await axios.get( | ||
`https://github.com/repos/${namespace}/${projectName}/contributors` | ||
); | ||
if (req.data) { | ||
contributors = req.data.map((c: Contributor & { id: number }) => ({ | ||
login: c.login, | ||
avatar_url: c.avatar_url, | ||
name: c.name || "", | ||
github_id: c.id, | ||
contributions: c.contributions | ||
})); | ||
} | ||
} | ||
} catch (error) { | ||
log.error(error); | ||
} | ||
return contributors; | ||
} | ||
|
||
export async function getRepoAsPackage( | ||
pkg: Pick<GUIPackage, "github_url"> | ||
): Promise<Partial<Package>> { | ||
const { namespace, projectName } = getNamespaceProject(pkg.github_url!); | ||
|
||
const newPkg: Partial<Package> = {}; | ||
try { | ||
const repo = repoCloudProvider(pkg.github_url!); | ||
if (repo.isGithub) { | ||
const req = await axios.get(`https://github.com/repos/${namespace}/${projectName}`); | ||
if (req.data) { | ||
newPkg.license = req.data?.license?.name || ""; | ||
} | ||
} | ||
} catch (error) { | ||
log.error(error); | ||
} | ||
return newPkg; | ||
} | ||
|
||
export const trimGithubSlug = (slug: string): string => { | ||
const gh = slug.replace("https://github.com/", ""); | ||
const [owner, repo] = gh.split("/"); | ||
return [owner, repo].join("/"); | ||
}; | ||
|
||
export const getNamespaceProject = (repoURL: string) => { | ||
const parsedUrl = new URL(repoURL); | ||
const pathParts = parsedUrl.pathname.split("/").filter((part) => part); | ||
const namespace = pathParts[0]; | ||
const projectName = pathParts[1]; | ||
return { | ||
namespace, | ||
projectName | ||
}; | ||
}; | ||
|
||
export const getRepoLabel = (repoURL: string) => { | ||
const { namespace, projectName } = getNamespaceProject(repoURL); | ||
return `${namespace}/${projectName}`; | ||
}; | ||
|
||
export const repoCloudProvider = (repoURL: string) => { | ||
return { | ||
isGithub: repoURL.includes("github.com"), | ||
isGitlab: repoURL.includes("gitlab") | ||
}; | ||
}; |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this single condition will cover all the bases for relative URLs. I had found most examples of a relative path in the Openssl, lefthook, and xcodegen libraries. Among those three you get srcs like:
I had begun working on a remedy to this issue yesterday before the s3 solution was mentioned/implemented, and a regular expression covered the bases for relative paths we'd run in to in READMEs. Something like:
const isRelativePath = src?.match(/^[^\/]+\/[^\/].*$|^\/[^\/].*$/i)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: This regex could probably be simplified, as it covers conditions we won't likely run in to as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ryanml ill add that thank you, i missed out on that hehehe