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

Add support for base URL #24

Closed
wants to merge 11 commits into from
Closed

Add support for base URL #24

wants to merge 11 commits into from

Conversation

g12i
Copy link

@g12i g12i commented Aug 5, 2020

Solves #23

I used 2 url functions, both added in Node.js v0.1.25

  • url.resolve - safer way to resolve base + path
  • url.parse - see if hostname is present in whatever repostiory is set to

I had to update all the test cases that mentioned https://codeload.github.com/, by the new rules they resolve to https://codeload.github.com/

@g12i g12i mentioned this pull request Aug 5, 2020
var parsed = url.parse(repository)

if (parsed.host) {
return 'https://' + parsed.host + '/'
Copy link
Author

@g12i g12i Aug 5, 2020

Choose a reason for hiding this comment

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

I can't use parsed.protocol as it resolves to original protocol (e.g. git://), which is not what we need. But then if someone hosts GHE on http:// this will lead to errors :(

Copy link
Member

Choose a reason for hiding this comment

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

Are they always https?

Does d = new URL(url || 'https://github.com'); d.protocol = 'https'; return d.hostname work?

Copy link
Author

Choose a reason for hiding this comment

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

I'm afraid it has to be hardcoded in this fashion.

The WHATWG URL Standard considers a handful of URL protocol schemes to be special in terms of how they are parsed and serialized. When a URL is parsed using one of these special protocols, the url.protocol property may be changed to another special protocol but cannot be changed to a non-special protocol, and vice versa.

Likewise, changing from a non-special protocol to a special protocol is also not permitted:

const u = new URL('fish://example.org');
u.protocol = 'http';
console.log(u.href);
// fish://example.org

(source)

Copy link
Member

Choose a reason for hiding this comment

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

lol. That’s...

Can we work around it by doing something like: new URL('http:' + url.href.slice(url.href.indexOf(':')))?

I love url, but it’s a bit of a downside that it’s deprecated and the suggestion is to use URL

Copy link
Author

Choose a reason for hiding this comment

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

Switched it back to concatenation. Alternatively I can use:

 return new URL('/' + repo.user + '/' + repo.project, base) + '/'

which would be more foolproof, in case someone provide baseUrl with trailing slash /, leading to https://github.com//such/urls

package.json Outdated Show resolved Hide resolved
@codecov-commenter

This comment has been minimized.

Copy link
Member

@wooorm wooorm left a comment

Choose a reason for hiding this comment

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

Nice, sorry for the wait!

Some suggestions inline, and could use docs!

package.json Outdated Show resolved Hide resolved
test/index.js Outdated Show resolved Hide resolved
@@ -39,23 +40,30 @@ var repositoryExpression = new RegExp(
function github(options) {
var settings = options || {}
var repository = settings.repository
var baseUrl = settings.baseUrl
Copy link
Member

Choose a reason for hiding this comment

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

how about just base? (and githubBase too?)

Copy link
Author

Choose a reason for hiding this comment

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

Personally I think "base" is too generic, when providing plugin options. But if you insist I'm happy to change it.

lib/util/gh.js Outdated Show resolved Hide resolved
var parsed = url.parse(repository)

if (parsed.host) {
return 'https://' + parsed.host + '/'
Copy link
Member

Choose a reason for hiding this comment

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

Are they always https?

Does d = new URL(url || 'https://github.com'); d.protocol = 'https'; return d.hostname work?

@g12i
Copy link
Author

g12i commented Sep 8, 2020

Hey @wooorm! Sorry for wait, I was on holiday :). I switch to the modern API and removed unnecessary fixtures. Take a look, while I'll work on documentation.

Copy link
Member

@wooorm wooorm left a comment

Choose a reason for hiding this comment

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

Fancy! A couple code style suggestions.

And hope you had a lovely holiday!

@@ -2,18 +2,18 @@

module.exports = gh

const {URL} = require('url')
Copy link
Member

Choose a reason for hiding this comment

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

drop this one, it isn’t really needed in this file

if (project) {
repo = {user: repo, project: project}
}

if (repo) {
base += repo.user + '/' + repo.project + '/'
return new URL('/' + repo.user + '/' + repo.project, base) + '/'
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return new URL('/' + repo.user + '/' + repo.project, base) + '/'
return base + '/' + repo.user + '/' + repo.project + '/

Copy link
Author

Choose a reason for hiding this comment

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

You sure? Have you seen my comment about trailing slashes?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry I missed this one!

I dunno, I feel that that’s a failure by users and should result in an error / that output?

lib/util/resolve-base-url.js Outdated Show resolved Hide resolved
lib/util/resolve-base-url.js Outdated Show resolved Hide resolved
lib/util/resolve-base-url.js Outdated Show resolved Hide resolved
@wooorm
Copy link
Member

wooorm commented Oct 5, 2020

Oh gosh, I forgot about this WIP issue/PR when I rewrote the project. 😓 What do you think about my last comment?

@g12i
Copy link
Author

g12i commented Oct 6, 2020

Oh snap! I forgot about this as well :D. I'll try to find some time and open the new PR. It would be easier than resolving conflicts here.

@g12i g12i closed this Oct 6, 2020
@wooorm
Copy link
Member

wooorm commented Oct 6, 2020

The codebase is significantly easier now, and the changes here are roughly good to go, so applying them to the new code should be possible and easier than merge conflicts!

I don’t have access to enterprise so I don‘t feel comfortable doing most of the work, but I can help you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants