From 6f64039a03ed9664892c47e0e03024eab1a60669 Mon Sep 17 00:00:00 2001 From: Courteney Ervin Date: Sun, 2 Nov 2014 07:24:23 -0500 Subject: [PATCH 01/15] Add methods to assist github link validation to lib --- lib/github_link_validator.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 lib/github_link_validator.rb diff --git a/lib/github_link_validator.rb b/lib/github_link_validator.rb new file mode 100644 index 00000000..b79fbd41 --- /dev/null +++ b/lib/github_link_validator.rb @@ -0,0 +1,31 @@ +require 'net/http' +require 'open-uri' + +module GithubLinkValidator + GITHUB_URL_REGEX = /\A(http:\/\/|https:\/\/|www.)*github.com\/(\S+)\/(\S+)(.git)*\z/ + + def github_url?(url) + url =~ GITHUB_URL_REGEX + end + + def valid_url?(url) + response = Net::HTTP.get_response(URI(url)) + response.code == 200 || 301 + 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 valid_url?(url) && github_url?(url) + parse_github_url(url) + else + raise "The submitted url is not valid." + end + end +end From bfca5543f41215a790057e17d9992ea8abe104f9 Mon Sep 17 00:00:00 2001 From: Courteney Ervin Date: Sun, 2 Nov 2014 07:24:54 -0500 Subject: [PATCH 02/15] Add specs for github link validation methods --- spec/lib/github_link_validator_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 spec/lib/github_link_validator_spec.rb diff --git a/spec/lib/github_link_validator_spec.rb b/spec/lib/github_link_validator_spec.rb new file mode 100644 index 00000000..8849a1cc --- /dev/null +++ b/spec/lib/github_link_validator_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe GithubLinkValidator do + +end From d63ae3af6310e22e8d292212121e97db715f8321 Mon Sep 17 00:00:00 2001 From: Courteney Ervin Date: Sun, 2 Nov 2014 08:15:01 -0500 Subject: [PATCH 03/15] Make GithubLinkValidation module available to its tests --- spec/lib/github_link_validator_spec.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/lib/github_link_validator_spec.rb b/spec/lib/github_link_validator_spec.rb index 8849a1cc..12e2de1e 100644 --- a/spec/lib/github_link_validator_spec.rb +++ b/spec/lib/github_link_validator_spec.rb @@ -1,5 +1,7 @@ require 'spec_helper' +require "#{Rails.root}/lib/github_link_validator" describe GithubLinkValidator do + include GithubLinkValidator end From 2f2dee1a8beec01ea1ee2afa08607354af32e48b Mon Sep 17 00:00:00 2001 From: Courteney Ervin Date: Sun, 2 Nov 2014 08:16:30 -0500 Subject: [PATCH 04/15] Add tests for GithubLinkValidation#github_url? --- spec/lib/github_link_validator_spec.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/spec/lib/github_link_validator_spec.rb b/spec/lib/github_link_validator_spec.rb index 12e2de1e..70eb60e3 100644 --- a/spec/lib/github_link_validator_spec.rb +++ b/spec/lib/github_link_validator_spec.rb @@ -4,4 +4,29 @@ 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'] } + + 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 end From a6adfbe7c309fd8af343038ae1d9ee8278f789d9 Mon Sep 17 00:00:00 2001 From: Courteney Ervin Date: Sun, 2 Nov 2014 08:17:01 -0500 Subject: [PATCH 05/15] Prepare for testing all methods in the GithubLinkValidation module --- spec/lib/github_link_validator_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/lib/github_link_validator_spec.rb b/spec/lib/github_link_validator_spec.rb index 70eb60e3..66ff2e48 100644 --- a/spec/lib/github_link_validator_spec.rb +++ b/spec/lib/github_link_validator_spec.rb @@ -29,4 +29,13 @@ end end end + + describe '#valid_url?' do + end + + describe '#parse_github_url' do + end + + describe '#get_github_details' do + end end From 948f948a9b96480b23c88b63e4272c23d0e2b963 Mon Sep 17 00:00:00 2001 From: Courteney Ervin Date: Sun, 2 Nov 2014 08:33:04 -0500 Subject: [PATCH 06/15] Account for urls entered without preprended 'http://' --- lib/github_link_validator.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/github_link_validator.rb b/lib/github_link_validator.rb index b79fbd41..bd92d417 100644 --- a/lib/github_link_validator.rb +++ b/lib/github_link_validator.rb @@ -8,7 +8,16 @@ def github_url?(url) url =~ GITHUB_URL_REGEX end + def append_http(url) + if url.start_with?('http') + url + else + url = 'https://' + url + end + end + def valid_url?(url) + url = append_http(url) response = Net::HTTP.get_response(URI(url)) response.code == 200 || 301 end From e8e467c0149971efb445801b9a1cf3fb56044dd4 Mon Sep 17 00:00:00 2001 From: Courteney Ervin Date: Mon, 3 Nov 2014 14:44:46 -0500 Subject: [PATCH 07/15] Adjust link editing to switch to Github's preferred 'https://' --- lib/github_link_validator.rb | 6 ++++-- spec/lib/github_link_validator_spec.rb | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/github_link_validator.rb b/lib/github_link_validator.rb index bd92d417..4efdeab6 100644 --- a/lib/github_link_validator.rb +++ b/lib/github_link_validator.rb @@ -8,9 +8,11 @@ def github_url?(url) url =~ GITHUB_URL_REGEX end - def append_http(url) - if url.start_with?('http') + def append_https(url) + if url.start_with?('https') url + elsif url.start_with?('http') + url.gsub!(/http:/, 'https:') else url = 'https://' + url end diff --git a/spec/lib/github_link_validator_spec.rb b/spec/lib/github_link_validator_spec.rb index 66ff2e48..a1cc01b5 100644 --- a/spec/lib/github_link_validator_spec.rb +++ b/spec/lib/github_link_validator_spec.rb @@ -31,6 +31,13 @@ end describe '#valid_url?' do + it 'returns true for urls that are ok or redirect' do + valid_urls = valid_github_urls + ['http://www.cnn.com', 'http://www.google.com/search?q=this+american+life', 'www.twitter.com'] + + valid_urls.each do |valid_url| + expect(valid_url?(valid_url)).to be_true + end + end end describe '#parse_github_url' do From 097e69abb4b667ff87ca07604b3f62ceed743791 Mon Sep 17 00:00:00 2001 From: Courteney Ervin Date: Mon, 3 Nov 2014 15:00:51 -0500 Subject: [PATCH 08/15] Add method to adjust urls to GitHub's non-'www.' format --- lib/github_link_validator.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/github_link_validator.rb b/lib/github_link_validator.rb index 4efdeab6..bef417cf 100644 --- a/lib/github_link_validator.rb +++ b/lib/github_link_validator.rb @@ -18,6 +18,11 @@ def append_https(url) end end + def remove_www(url) + return url.gsub!(/www./, '') if url.match(/www./) + url + end + def valid_url?(url) url = append_http(url) response = Net::HTTP.get_response(URI(url)) From 1cda642e1ec47d43b30c4ac1a3436549c8561b03 Mon Sep 17 00:00:00 2001 From: Courteney Ervin Date: Mon, 3 Nov 2014 15:07:32 -0500 Subject: [PATCH 09/15] Adjust url validation method to catch errors & be specific to Github urls --- lib/github_link_validator.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/github_link_validator.rb b/lib/github_link_validator.rb index bef417cf..7667710f 100644 --- a/lib/github_link_validator.rb +++ b/lib/github_link_validator.rb @@ -24,9 +24,14 @@ def remove_www(url) end def valid_url?(url) - url = append_http(url) - response = Net::HTTP.get_response(URI(url)) - response.code == 200 || 301 + 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) From 0956ebd026f1be2381896d3ddc245446beac1390 Mon Sep 17 00:00:00 2001 From: Courteney Ervin Date: Mon, 3 Nov 2014 15:08:37 -0500 Subject: [PATCH 10/15] Add tests for GithubLinkValidator#valid_url? --- spec/lib/github_link_validator_spec.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/spec/lib/github_link_validator_spec.rb b/spec/lib/github_link_validator_spec.rb index a1cc01b5..33fc715c 100644 --- a/spec/lib/github_link_validator_spec.rb +++ b/spec/lib/github_link_validator_spec.rb @@ -31,13 +31,21 @@ end describe '#valid_url?' do + it 'returns true for urls that are ok or redirect' do - valid_urls = valid_github_urls + ['http://www.cnn.com', 'http://www.google.com/search?q=this+american+life', 'www.twitter.com'] - valid_urls.each do |valid_url| + 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 From 303af92cf356211c940b41a885cbd2f3411dd06f Mon Sep 17 00:00:00 2001 From: Courteney Ervin Date: Mon, 3 Nov 2014 15:12:25 -0500 Subject: [PATCH 11/15] Add passing tests for GithubLinkValidator#parse_github_url --- spec/lib/github_link_validator_spec.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/spec/lib/github_link_validator_spec.rb b/spec/lib/github_link_validator_spec.rb index 33fc715c..e23f7c69 100644 --- a/spec/lib/github_link_validator_spec.rb +++ b/spec/lib/github_link_validator_spec.rb @@ -11,6 +11,8 @@ 'https://github.com/courte/codemontage', 'github.com/dbness/codemontage'] } + let (:valid_github_url) { 'www.github.com/codemontagehq/codemontage' } + describe '#github_url?' do it 'returns true for accurate github repo urls' do @@ -49,6 +51,17 @@ 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 From c7f1ae4d33cf0531aa43eedb9dfb1d5ec60c2d39 Mon Sep 17 00:00:00 2001 From: Courteney Ervin Date: Mon, 3 Nov 2014 15:19:41 -0500 Subject: [PATCH 12/15] Adjust github detail fetcher for github link format --- lib/github_link_validator.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/github_link_validator.rb b/lib/github_link_validator.rb index 7667710f..a5576de4 100644 --- a/lib/github_link_validator.rb +++ b/lib/github_link_validator.rb @@ -43,10 +43,8 @@ def parse_github_url(url) end def get_github_details(url) - if valid_url?(url) && github_url?(url) + if github_url?(url) && valid_url?(url) parse_github_url(url) - else - raise "The submitted url is not valid." end end end From 87ad380562693e5fff39451ea45832613f25e803 Mon Sep 17 00:00:00 2001 From: Courteney Ervin Date: Mon, 3 Nov 2014 15:20:06 -0500 Subject: [PATCH 13/15] Add passing test for GithubLinkValidator#get_github_details --- spec/lib/github_link_validator_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/lib/github_link_validator_spec.rb b/spec/lib/github_link_validator_spec.rb index e23f7c69..a8fe0ae4 100644 --- a/spec/lib/github_link_validator_spec.rb +++ b/spec/lib/github_link_validator_spec.rb @@ -65,5 +65,10 @@ 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 From b43d93952a01772752d4019cdb77eba9fd6e4f1a Mon Sep 17 00:00:00 2001 From: Courteney Ervin Date: Mon, 29 Dec 2014 19:43:11 -0500 Subject: [PATCH 14/15] Add github url outlier to existing specs as example accurate url --- spec/lib/github_link_validator_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/lib/github_link_validator_spec.rb b/spec/lib/github_link_validator_spec.rb index a8fe0ae4..1807f9ca 100644 --- a/spec/lib/github_link_validator_spec.rb +++ b/spec/lib/github_link_validator_spec.rb @@ -9,7 +9,8 @@ 'http://github.com/dbness/codemontage', 'www.github.com/codemontagehq/codemontage', 'https://github.com/courte/codemontage', - 'github.com/dbness/codemontage'] } + 'github.com/dbness/codemontage', + 'https://github.com/CodeMontageHQ/codemontage/pull/269'] } let (:valid_github_url) { 'www.github.com/codemontagehq/codemontage' } From 9e051238326b697c0aeb7800d90cf30f6e289c49 Mon Sep 17 00:00:00 2001 From: Courteney Ervin Date: Mon, 29 Dec 2014 19:45:53 -0500 Subject: [PATCH 15/15] Adjust github validator regex for more accurate matches --- lib/github_link_validator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/github_link_validator.rb b/lib/github_link_validator.rb index a5576de4..948138ac 100644 --- a/lib/github_link_validator.rb +++ b/lib/github_link_validator.rb @@ -2,7 +2,7 @@ require 'open-uri' module GithubLinkValidator - GITHUB_URL_REGEX = /\A(http:\/\/|https:\/\/|www.)*github.com\/(\S+)\/(\S+)(.git)*\z/ + GITHUB_URL_REGEX = /\A(http:\/\/|https:\/\/|www.)*github.com\/([\w\-]+)\/([\w\-]+)(\/[\w\-]*\/*)*(.git)*\z/ def github_url?(url) url =~ GITHUB_URL_REGEX