Skip to content

Commit 5514532

Browse files
committed
ContributionChecker::InvalidCommitUrlError to the rescue
1 parent f090f88 commit 5514532

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

lib/contribution-checker.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
require "contribution-checker/version"
2+
require "contribution-checker/error"
23
require "contribution-checker/checker"

lib/contribution-checker/checker.rb

+21-5
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ def initialize(options = {})
4646
# }
4747
# }
4848
def check
49-
parts = URI.parse(@commit_url).path.split("/")
50-
@nwo = "#{parts[1]}/#{parts[2]}"
51-
@sha = parts[4]
52-
@user = @client.user
49+
@nwo, @sha = parse_commit_url @commit_url
50+
begin
51+
@commit = @client.commit @nwo, @sha
52+
rescue Octokit::NotFound
53+
raise ContributionChecker::InvalidCommitUrlError
54+
end
5355
@repo = @client.repository @nwo
54-
@commit = @client.commit @nwo, @sha
56+
@user = @client.user
5557

5658
@commit_in_valid_branch = commit_in_valid_branch?
5759
@commit_in_last_year = commit_in_last_year?
@@ -79,6 +81,20 @@ def check
7981
}
8082
end
8183

84+
# Parses the commit URL provided.
85+
#
86+
# @return [Array] URL parts: nwo, sha
87+
def parse_commit_url(url)
88+
begin
89+
parts = URI.parse(@commit_url).path.split("/")
90+
nwo = "#{parts[1]}/#{parts[2]}"
91+
sha = parts[4]
92+
return nwo, sha
93+
rescue
94+
raise ContributionChecker::InvalidCommitUrlError
95+
end
96+
end
97+
8298
# Checks whether the commit is in a valid branch. A valid branch is defined
8399
# as either the default branch of the repository, or the gh-pages branch.
84100
#

lib/contribution-checker/error.rb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module ContributionChecker
2+
3+
# Error class to help us rescue from invalid commit URL input.
4+
class InvalidCommitUrlError < StandardError
5+
def initialize
6+
super "Invalid commit URL provided"
7+
end
8+
end
9+
10+
end

0 commit comments

Comments
 (0)