Skip to content
This repository has been archived by the owner on May 25, 2023. It is now read-only.

fix: able to add repo name with "."(dot) in it issue#1451 #1453

Merged
merged 6 commits into from
Jun 25, 2022
Merged

fix: able to add repo name with "."(dot) in it issue#1451 #1453

merged 6 commits into from
Jun 25, 2022

Conversation

us3r64bit
Copy link
Contributor

@us3r64bit us3r64bit commented Jun 19, 2022

What type of PR is this? (check all applicable)

  • πŸ• Feature
  • πŸ› Bug Fix
  • πŸ“ Documentation Update
  • 🎨 Style
  • πŸ§‘β€πŸ’» Code Refactor
  • πŸ”₯ Performance Improvements
  • βœ… Test
  • πŸ€– Build
  • πŸ” CI
  • πŸ“¦ Chore (Release)
  • ⏩ Revert

Description

This PR fix #1451

Related Tickets & Documents

Mobile & Desktop Screensh

Screenshot from 2022-06-19 16-41-26
ots/Recordings

Added tests?

  • πŸ‘ yes
  • πŸ™… no, because they aren't needed
  • πŸ™‹ no, because I need help

Added to documentation?

  • πŸ“œ README.md
  • πŸ““ docs.opensauced.pizza
  • πŸ• dev.to/opensauced
  • πŸ“• storybook
  • πŸ™… no documentation needed

[optional] Are there any post-deployment tasks we need to perform?

[optional] What gif best describes this PR or how it makes you feel?

@mtfoley
Copy link
Contributor

mtfoley commented Jun 19, 2022

@arth2002 thanks for getting this PR started.

@0-vortex with that failing test I guess if what we really care about are the ones that start with github.com, we could make that a third case. WDYT?

@0-vortex
Copy link
Contributor

@arth2002 thanks for getting this PR started.

@0-vortex with that failing test I guess if what we really care about are the ones that start with github.com, we could make that a third case. WDYT?

We should support all repos, this should fix things not become a breaking change. Changing from includes->substr will do the trick - it's a "relative" URL only if it starts with those matchers, not if it's included in repoName

@mtfoley
Copy link
Contributor

mtfoley commented Jun 21, 2022

I checked out this PR locally, and I actually think the utils.js would work well like this:

export function isValidRepoUrl(url) {
  
  return fullnameValidator(urlToFullName(url));
}

function urlToFullName(url){
  const fullname = url.replace(/^(http|https):\/\//i, "") // take off URL protocol
    .replace(/^(www\.)*github\.com\//i, "") // take off github.com variant
    .replace(/^\/*/ ,"")// take off leading slash(es) if there
    .replace(/ /g, ""); // take out spaces
  return fullname;
}

function fullnameValidator(fullname){
  // take only first two elements
  const [owner, repo] = fullname.split("/");
  if(!owner || !repo) return [false, null];
  // alphanumerics and hyphens are allowed in owner names
  const invalidOwnerRegex = /[^a-z0-9-]+/i;
  // alphanumerics, hyphens, underscores, and dots are allowed in repo names
  const invalidRepoRegex = /[^a-z0-9-_\.]+/i;
  const valid = !invalidOwnerRegex.test(owner) && !invalidRepoRegex.test(repo);
  // if invalid, return [false, null]
  // if valid, return [true, fullname]
  return [valid, valid ? fullname : null];
}

@arth2002 I think the code above needs some more tweaking but I wanted to bounce the idea off of you and @0-vortex.

The test file actually has some issues as well so I'll follow up with a later comment.

@0-vortex
Copy link
Contributor

I checked out this PR locally, and I actually think the utils.js would work well like this:

export function isValidRepoUrl(url) {
  
  return fullnameValidator(urlToFullName(url));
}

function urlToFullName(url){
  const fullname = url.replace(/^(http|https):\/\//i, "") // take off URL protocol
    .replace(/^(www\.)*github\.com\//i, "") // take off github.com variant
    .replace(/^\/*/ ,"")// take off leading slash(es) if there
    .replace(/ /g, ""); // take out spaces
  return fullname;
}

function fullnameValidator(fullname){
  // take only first two elements
  const [owner, repo] = fullname.split("/");
  if(!owner || !repo) return [false, null];
  // alphanumerics and hyphens are allowed in owner names
  const invalidOwnerRegex = /[^a-z0-9-]+/i;
  // alphanumerics, hyphens, underscores, and dots are allowed in repo names
  const invalidRepoRegex = /[^a-z0-9-_\.]+/i;
  const valid = !invalidOwnerRegex.test(owner) && !invalidRepoRegex.test(repo);
  // if invalid, return [false, null]
  // if valid, return [true, fullname]
  return [valid, valid ? fullname : null];
}

@arth2002 I think the code above needs some more tweaking but I wanted to bounce the idea off of you and @0-vortex.

The test file actually has some issues as well so I'll follow up with a later comment.

Hard no on this - you have taken a function that checks whether a subset of the URL RFC is relative or not and turned it into a URL validator, that's potentially the opposite of what we are trying to do here; the regexp approach sounds good in theory but if you take a look at RFC 1738 you will understand why this approach is so bad. You can spend weeks perfecting regular expressions like this.

To make a better example, I mentioned in the previous review that the third includes should be changed into a substr, that is, essentially, to check for that condition at the beginning of the string. If you use regular expressions that can be simplified into ^(https?:\/\/)*(www\.?)*([a-z]+\.[a-z]+\/)*, checking for all the parts of a URL and excluding them as they appear you get the problem that it has to be a perfect regex, all the parts have to work in all cases, which makes the difficulty of adjusting to all those use cases, terrible. The above regex would cover the following use cases:

brob/plug11ty.com
github.com/brob/plug11ty.com
www.github.com/brob/plug11ty.com
https://github.com/brob/plug11ty.com
https://www.github.com/brob/plug11ty.com

However, that is going full-on into scope creep. The existing code is meant to catch 3 things at the beginning of the string:

  • http (done with substr)
  • https (done with substr)
  • www (done with regex without ^)

Add the ^ to the third check or transform the regex into a substr

@mtfoley
Copy link
Contributor

mtfoley commented Jun 21, 2022

Fair enough. @arth2002 is the instruction clear from what @0-vortex was saying?

@us3r64bit
Copy link
Contributor Author

Fair enough. @arth2002 is the instruction clear from what @0-vortex was saying?

Hey @mtfoley sorry to bother you sir
I just want to check we're on the same page, as per I understood do I need to replace
this code

const isRelativeUrl = !(url.substr(0, 4) === "http" || url.includes(".com") || url.includes("www."));

with this one

 const isRelativeUrl = !(url.substr(0,7) === 'http://' || url.substr(0,8) === 'https://' || url.substr(0,4) === 'www.');

am I correct? if not can please correct me
Thank You :)

@0-vortex
Copy link
Contributor

Fair enough. @arth2002 is the instruction clear from what @0-vortex was saying?

Hey @mtfoley sorry to bother you sir I just want to check we're on the same page, as per I understood do I need to replace this code

const isRelativeUrl = !(url.substr(0, 4) === "http" || url.includes(".com") || url.includes("www."));

with this one

 const isRelativeUrl = !(url.substr(0,7) === 'http://' || url.substr(0,8) === 'https://' || url.substr(0,4) === 'www.');

am I correct? if not can please correct me Thank You :)

In case you missed the πŸ‘ emoji, it's good! :D

@us3r64bit
Copy link
Contributor Author

Please review it

@mtfoley mtfoley self-requested a review June 24, 2022 00:38
Copy link
Contributor

@mtfoley mtfoley left a comment

Choose a reason for hiding this comment

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

@arth2002, I think it would also be helpful to add a couple of tests. The two cases I'd suggest would both be "valid", and would be:

  1. the repo of the original issue posting ("brob/plug11ty.com")
  2. the repo "httpie/httpie", just to verify that use case works also.

src/lib/util.js Outdated Show resolved Hide resolved
src/lib/util.js Outdated
@@ -1,8 +1,8 @@
export function isValidRepoUrl(url) {
url = url.trim();
url = url.toLowerCase().trim();
url = url.substr(0, 1) === "/" ? url.substr(1) : url;
Copy link
Contributor

Choose a reason for hiding this comment

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

@arth2002 I just want you to know you're being very patient with me! That said, I'd suggest we take a moment to change url.substr() to be url.substring(). It looks like substr() is deprecated (though widely supported). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr#browser_compatibility

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done πŸ‘

@mtfoley
Copy link
Contributor

mtfoley commented Jun 25, 2022

Looks good to me!

@mtfoley mtfoley merged commit fcca807 into open-sauced:main Jun 25, 2022
github-actions bot pushed a commit that referenced this pull request Jun 25, 2022
### [0.52.1](v0.52.0...v0.52.1) (2022-06-25)

### πŸ› Bug Fixes

* able to add repo name with "."(dot) in it issue[#1451](#1451) ([#1453](#1453)) ([fcca807](fcca807))
@github-actions
Copy link
Contributor

πŸŽ‰ This PR is included in version 0.52.1 πŸŽ‰

The release is available on:

Your semantic-release bot πŸ“¦πŸš€

@us3r64bit us3r64bit deleted the fix-repo-name-issue branch June 26, 2022 05:33
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: When adding brob/plug11ty.com "not valid" error
3 participants