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

Updated regex to ignore backslash #559

Merged
merged 2 commits into from
Apr 5, 2024
Merged
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
12 changes: 6 additions & 6 deletions functions/lib/rewriters/github-rewriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export class GitHubRewriter extends DefaultRewriter {

async rewriteUrl(url: URL) {
// Blob URLs - https://github.com/<owner>/<repo>/blob/<branch>/<path>
if (url.origin === "https://github.com" && /\/(.*)\/blob\/(.*)$/.test(url.pathname)) {
// https://github.com/<owner>/<repo>/blob/<branch>/<path> -> https://raw.githubusercontent.com<owner>/<repo>/<branch>/<path>
if (url.origin === "https://github.com" && /\/(.*)\/blob\/(.*)\/?$/.test(url.pathname)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# Works
/import https://github.com/tarasglek/chatcraft.org/blob/main/.eslintignore

# Fails
/import https://github.com/tarasglek/chatcraft.org/blob/main/.eslintignore/

The latter returns a 400 from the server

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just verified that this is something we have in production as well

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to fix it while you're working on this or split it off into a future issue?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to work on debugging this, but as this PR fixes the problem for PRs and gist, i would like to get this merged and create another issue and PR for that one.

// https://github.com/<owner>/<repo>/blob/<branch>/<path> ->
return new URL(
url.href.replace(
/^https:\/\/github\.com\/(.*)\/blob\/(.*)$/,
/^https:\/\/github\.com\/(.*)\/blob\/(.*)\/?$/,
"https://raw.githubusercontent.com/$1/$2"
)
);
Expand All @@ -23,14 +23,14 @@ export class GitHubRewriter extends DefaultRewriter {
// https://gist.github.com/<owner>/<gist-id> -> https://gist.githubusercontent.com/<owner>/<gist-id>/raw
return new URL(
url.href.replace(
/^https:\/\/gist\.github\.com\/([a-zA-Z0-9_-]+)\/([a-f0-9]+)$/,
/^https:\/\/gist\.github\.com\/([a-zA-Z0-9_-]+)\/([a-f0-9]+)\/?$/,
Rachit1313 marked this conversation as resolved.
Show resolved Hide resolved
"https://gist.githubusercontent.com/$1/$2/raw"
)
);
}

// PR URLs - https://github.com/<owner>/<repo>/pull/<number>
if (url.origin === "https://github.com" && /\/(.*)\/pull\/(.*)$/.test(url.pathname)) {
if (url.origin === "https://github.com" && /\/(.*)\/pull\/(.*)\/?$/.test(url.pathname)) {
Rachit1313 marked this conversation as resolved.
Show resolved Hide resolved
// TODO: we need to add more logic to deal with a specific comment in a PR via the GitHub API
// Example: https://github.com/tarasglek/chatcraft.org/pull/370#issuecomment-1916037856
if (url.hash.startsWith("#issuecomment-")) {
Expand All @@ -40,7 +40,7 @@ export class GitHubRewriter extends DefaultRewriter {
// https://github.com/<owner>/<repo>/pull/<number> -> https://patch-diff.githubusercontent.com/raw/<owner>/<repo>/pull/<number>.patch
return new URL(
url.href.replace(
/^https:\/\/github\.com\/([^/]+)\/([^/]+)\/pull\/(\d+)$/,
/^https:\/\/github\.com\/([^/]+)\/([^/]+)\/pull\/(\d+)\/?$/,
"https://patch-diff.githubusercontent.com/raw/$1/$2/pull/$3.patch"
)
);
Expand Down
Loading