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

Made contributing.md file name check case insensitive in the cookbook source_url repo; Fixed github url regex; #2180

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ def evaluate(cookbook_json)
return true if repo.blank?

begin
octokit_client.contents(repo, path: "CONTRIBUTING.md")
# if found, does not fail the metric
false
rescue Octokit::NotFound
# if not found, does fail the metric
repo_contents = octokit_client.contents(repo)
# if found then returns false, and does not fail the metric
return !repo_contents.any? {|file| file.with_indifferent_access["name"] =~ /^contributing\.md$/i}
rescue Octokit::InvalidRepository, Octokit::NotFound
# if repository not found, does fail the metric
true
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class SourceRepoWorker

def source_repo(cookbook_json)
url = source_repo_url(cookbook_json)

url.match(%r{(?<=github.com\/)[\w-]+\/[\w-]+}).to_s
# https://rubular.com/r/uTLH7q6rJES5xW
url.match(%r{^(https?\://)?(github\.com/)(\w+/\w+)}).try(:[], 3).to_s
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we include rubular permalink as a comment here? e.g. from https://rubular.com

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great suggestion @dheerajd-msys . Thanks for the link. It's a great tool. Should I add the permalink in code?

tas50 marked this conversation as resolved.
Show resolved Hide resolved
end

def source_repo_url(cookbook_json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

context "when a source repo is present" do
let(:cookbook_json_response) { File.read("spec/support/cookbook_source_url_fixture.json") }
let(:github_file_present_response) { File.read("spec/support/github_contributing_file_positive.json") }
let(:github_file_present_response) { JSON.parse(File.read("spec/support/github_contributing_file_positive.json")) }
let(:octokit) { Octokit::Client.new(access_token: ENV["FIERI_SUPERMARKET_ENDPOINT"]) }

before do
Expand All @@ -29,8 +29,8 @@
let(:parsed_response) { JSON.parse(cookbook_json_response) }

before do
stub_request(:get, "https://github.com/gitapi/repos/johndoe/example_repo/contents/CONTRIBUTING.md")
.to_return(status: 200, body: "", headers: {})
stub_request(:get, "https://github.com/gitapi/repos/johndoe/example_repo/contents/")
.to_return(status: 200, body: [], headers: {})
end

it "parses the cookbook_json" do
Expand All @@ -41,20 +41,20 @@
it "attempts to find a match for a github url" do
sample_source_url = "https://github.com/johndoe/example_repo"
allow_any_instance_of(SourceRepoWorker).to receive(:source_repo_url).and_return(sample_source_url)
expect(sample_source_url).to receive(:match).with(%r{(?<=github.com\/)[\w-]+\/[\w-]+}).and_return("johndoe/example_repo")
expect(sample_source_url).to receive(:match).with(%r{^(https?\://)?(github\.com/)(\w+/\w+)}).and_return("johndoe/example_repo")
cfw.perform(cookbook_json_response, cookbook_name)
end
end

context "when a source url is valid" do
# to be valid, must be a github url

it "checks the contents of the repo for a CONTRIBUTING.md file" do
expect(octokit).to receive(:contents).with("johndoe/example_repo", path: "CONTRIBUTING.md").and_return(github_file_present_response)
it "checks the contents of the repo" do
expect(octokit).to receive(:contents).with("johndoe/example_repo").and_return(github_file_present_response)
cfw.perform(cookbook_json_response, cookbook_name)
end

context "and a CONTRIBUTING.md file is present" do
context "and a CONTRIBUTING.md file is present in the repo" do
before do
allow(octokit).to receive(:contents).and_return(github_file_present_response)
end
Expand All @@ -70,7 +70,7 @@
end
end

context "and a CONTRIBUTING.md file is not present" do
context "and a CONTRIBUTING.md file is not present in the repo" do
# The Github API returns a 404 when a file is not present
before do
allow(octokit).to receive(:contents).and_raise(Octokit::NotFound)
Expand All @@ -88,7 +88,7 @@
end
end

context "when a source url is not valid" do
context "when a source url is null" do
let(:invalid_source_url_json_response) { File.read("spec/support/cookbook_null_source_url_fixture.json") }

it "does not attempt to contact the Github API" do
Expand All @@ -106,5 +106,24 @@
end
end
end

context "when a source url is not a github repo URL" do
let(:invalid_source_url_json_response) { File.read("spec/support/cookbook_non_github_source_url_fixture.json") }

it "does not attempt to contact the Github API" do
expect(Octokit::Client).to_not receive(:new)
cfw.perform(invalid_source_url_json_response, cookbook_name)
end

it "posts a failing metric" do
cfw.perform(invalid_source_url_json_response, cookbook_name)

assert_requested(:post, "#{ENV["FIERI_SUPERMARKET_ENDPOINT"]}/api/v1/quality_metrics/contributing_file_evaluation", times: 1) do |req|
expect(req.body).to include("cookbook_name=#{cookbook_name}")
expect(req.body).to include("contributing_file_failure=true")
expect(req.body).to include("contributing_file_feedback=Failure")
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

context "parsing the source url" do
let(:parsed_response) { JSON.parse(cookbook_json_response) }
let(:github_repo_url_regex) { %r{^(https?\://)?(github\.com/)(\w+/\w+)} }

it "parses the cookbook_json" do
expect(JSON).to receive(:parse).with(cookbook_json_response).and_return(parsed_response)
Expand All @@ -38,7 +39,8 @@
it "attempts to find a match for a github url" do
sample_source_url = "https://github.com/johndoe/example_repo"
allow_any_instance_of(SourceRepoWorker).to receive(:source_repo_url).and_return(sample_source_url)
expect(sample_source_url).to receive(:match).with(%r{(?<=github.com\/)[\w-]+\/[\w-]+}).and_return("johndoe/example_repo")
expect(github_repo_url_regex).to match(sample_source_url)
expect(sample_source_url.match(github_repo_url_regex)[3]).to eq("johndoe/example_repo")
vfw.perform(cookbook_json_response, cookbook_name, cookbook_version)
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{:name=>"CONTRIBUTING.md",
:path=>"CONTRIBUTING.md",
:sha=>"253074c7703892156822bf4b21c05e538f531bdb",
:size=>5417,
:url=>
[{"name": "CONTRIBUTING.md",
"path": "CONTRIBUTING.md",
"sha": "253074c7703892156822bf4b21c05e538f531bdb",
"size": 5417,
"url":
"https://github.com/gitapi/repos/johndoe/example_repo/contents/CONTRIBUTING.md?ref=master",
:html_url=>"https://github.com/johndoe/example_repo/blob/master/CONTRIBUTING.md",
:git_url=>
"html_url": "https://github.com/johndoe/example_repo/blob/master/CONTRIBUTING.md",
"git_url":
"https://github.com/gitapi/repos/johndoe/example_repo/git/blobs/253074c7703892156822bf4b21c05e538f531bdb",
:download_url=>
"download_url":
"https://github.com/raw/johndoe/example_repo/master/CONTRIBUTING.md",
:type=>"file",
:content=> "xyz",
:encoding=>"base64",
:_links=>
{:self=>
"type": "file",
"content": "xyz",
"encoding": "base64",
"_links":
{"self":
"https://github.com/gitapi/repos/johndoe/example_repo/contents/CONTRIBUTING.md?ref=master",
:git=>
"git":
"https://github.com/gitapi/repos/johndoe/example_repo/git/blobs/253074c7703892156822bf4b21c05e538f531bdb",
:html=>"https://github.com/johndoe/example_repo/blob/master/CONTRIBUTING.md"}}
"html": "https://github.com/johndoe/example_repo/blob/master/CONTRIBUTING.md"}}]