-
Notifications
You must be signed in to change notification settings - Fork 71
Github link validation - prep for user input #269
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
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6f64039
Add methods to assist github link validation to lib
courte bfca554
Add specs for github link validation methods
courte d63ae3a
Make GithubLinkValidation module available to its tests
courte 2f2dee1
Add tests for GithubLinkValidation#github_url?
courte a6adfbe
Prepare for testing all methods in the GithubLinkValidation module
courte 948f948
Account for urls entered without preprended 'http://'
courte e8e467c
Adjust link editing to switch to Github's preferred 'https://'
courte 097e69a
Add method to adjust urls to GitHub's non-'www.' format
courte 1cda642
Adjust url validation method to catch errors & be specific to Github …
courte 0956ebd
Add tests for GithubLinkValidator#valid_url?
courte 303af92
Add passing tests for GithubLinkValidator#parse_github_url
courte c7f1ae4
Adjust github detail fetcher for github link format
courte 87ad380
Add passing test for GithubLinkValidator#get_github_details
courte b43d939
Add github url outlier to existing specs as example accurate url
courte 9e05123
Adjust github validator regex for more accurate matches
courte 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 hidden or 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,50 @@ | ||
| require 'net/http' | ||
| require 'open-uri' | ||
|
|
||
| module GithubLinkValidator | ||
| GITHUB_URL_REGEX = /\A(http:\/\/|https:\/\/|www.)*github.com\/([\w\-]+)\/([\w\-]+)(\/[\w\-]*\/*)*(.git)*\z/ | ||
|
|
||
| def github_url?(url) | ||
| url =~ GITHUB_URL_REGEX | ||
| end | ||
|
|
||
| def append_https(url) | ||
| if url.start_with?('https') | ||
| url | ||
| elsif url.start_with?('http') | ||
| url.gsub!(/http:/, 'https:') | ||
| else | ||
| url = 'https://' + url | ||
| end | ||
| end | ||
|
|
||
| def remove_www(url) | ||
| return url.gsub!(/www./, '') if url.match(/www./) | ||
| url | ||
| end | ||
|
|
||
| def valid_url?(url) | ||
| github_preferred_url = remove_www(append_https(url)) | ||
|
|
||
| begin | ||
| response = Net::HTTP.get_response(URI(github_preferred_url)) | ||
| response.code == "200" | ||
| rescue | ||
| false | ||
| end | ||
| end | ||
|
|
||
| def parse_github_url(url) | ||
| url =~ GITHUB_URL_REGEX | ||
| github_org = $2 | ||
| github_repo = $3 | ||
|
|
||
| {github_org: github_org, github_repo: github_repo} | ||
| end | ||
|
|
||
| def get_github_details(url) | ||
| if github_url?(url) && valid_url?(url) | ||
| parse_github_url(url) | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,75 @@ | ||
| require 'spec_helper' | ||
| require "#{Rails.root}/lib/github_link_validator" | ||
|
|
||
| describe GithubLinkValidator do | ||
| include GithubLinkValidator | ||
|
|
||
| let (:valid_github_urls) { ['http://www.github.com/codemontagehq/codemontage', | ||
| 'https://www.github.com/courte/codemontage', | ||
| 'http://github.com/dbness/codemontage', | ||
| 'www.github.com/codemontagehq/codemontage', | ||
| 'https://github.com/courte/codemontage', | ||
| 'github.com/dbness/codemontage', | ||
| 'https://github.com/CodeMontageHQ/codemontage/pull/269'] } | ||
|
|
||
| let (:valid_github_url) { 'www.github.com/codemontagehq/codemontage' } | ||
|
|
||
| describe '#github_url?' do | ||
|
|
||
| it 'returns true for accurate github repo urls' do | ||
| good_github_urls = valid_github_urls + ['https://github.com/fake/repo', 'www.github.com/banana/pudding-cup'] | ||
|
|
||
| good_github_urls.each do |github_url| | ||
| expect(github_url?(github_url)).to be_true | ||
| end | ||
| end | ||
|
|
||
| it 'returns false for inaccurate github repo urls' do | ||
| urls = ['http://www.cnn.com', 'http://www.google.com/search?q=this+american+life', 'github.com/courte', 'https://commonplacebooks.com'] | ||
|
|
||
| urls.each do |url| | ||
| expect(github_url?(url)).to be_false | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe '#valid_url?' do | ||
|
|
||
| it 'returns true for urls that are ok or redirect' do | ||
|
|
||
| valid_github_urls.each do |valid_url| | ||
| expect(valid_url?(valid_url)).to be_true | ||
| end | ||
| end | ||
|
|
||
| it 'returns false for invalid urls' do | ||
| invalid_urls = ["http://www.ajiofj.io", "https://bananacrabcakepalace.net", "https://github.com/woinvewnus/cuxywwe", "www.github.com/courte/notarepo", "github.com/ospreys-2014/phase-1-guide"] | ||
|
|
||
| invalid_urls.each do |invalid_url| | ||
| expect(valid_url?(invalid_url)).to be_false | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe '#parse_github_url' do | ||
|
|
||
| it 'returns the github org, pulled from the input url' do | ||
| url = 'www.github.com/codemontagehq/codemontage' | ||
| github_details = parse_github_url(valid_github_url) | ||
| expect(github_details[:github_org]).to eq('codemontagehq') | ||
| end | ||
|
|
||
| it 'returns the github repo, pulled from the input url' do | ||
| github_details = parse_github_url(valid_github_url) | ||
| expect(github_details[:github_repo]).to eq('codemontage') | ||
| end | ||
| end | ||
|
|
||
| describe '#get_github_details' do | ||
|
|
||
| it "returns the github org and repo in a hash" do | ||
| github_details = get_github_details(valid_github_url) | ||
| expect(github_details).to eq({github_org: 'codemontagehq', github_repo: 'codemontage'}) | ||
| end | ||
| end | ||
| end | ||
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.
😋!