diff --git a/lib/licensee/projects/github_project.rb b/lib/licensee/projects/github_project.rb index 6c15bee..1785a51 100644 --- a/lib/licensee/projects/github_project.rb +++ b/lib/licensee/projects/github_project.rb @@ -28,10 +28,10 @@ module Licensee private def files - @files ||= contents.map { |data| { name: data[:name], dir: '/' } } + @files ||= dir_files rescue Octokit::NotFound - raise RepoNotFound, - "Could not load GitHub repo #{repo}, it may be private or deleted" + msg = "Could not load GitHub repo #{repo}, it may be private or deleted" + raise RepoNotFound, msg end def load_file(file) @@ -39,8 +39,11 @@ module Licensee accept: 'application/vnd.github.v3.raw') end - def contents - Octokit.contents(@repo).select { |data| data[:type] == 'file' } + def dir_files(path = nil) + files = Octokit.contents(@repo, path: path) + files = files.select { |data| data[:type] == 'file' } + files.each { |data| data[:dir] = File.dirname(data[:path]) } + files.map(&:to_h) end end end diff --git a/spec/fixtures/gemspec/project.gemspec b/spec/fixtures/gemspec/project.gemspec deleted file mode 100644 index 95e14b0..0000000 --- a/spec/fixtures/gemspec/project.gemspec +++ /dev/null @@ -1,5 +0,0 @@ -# Using a `.gemspec` extension here breaks the `gem release` command -Gem::Specification.new do |gem| - gem.name = "licensee-fixture-project" - gem.license = 'mit' -end diff --git a/spec/licensee/project_spec.rb b/spec/licensee/project_spec.rb index 86132dc..b520998 100644 --- a/spec/licensee/project_spec.rb +++ b/spec/licensee/project_spec.rb @@ -27,7 +27,7 @@ FileUtils.rm_rf File.expand_path '.git', path end elsif described_class == Licensee::Projects::GitHubProject - let(:path) do + before do stub_request( :get, "#{api_base}/#{stubbed_org}/#{fixture}/contents/" ).to_return( @@ -43,9 +43,8 @@ .with(headers: { 'accept' => 'application/vnd.github.v3.raw' }) .to_return(status: 200, body: File.read(file)) end - - "https://github.com/#{stubbed_org}/#{fixture}" end + let(:path) { "https://github.com/#{stubbed_org}/#{fixture}" } end if described_class == Licensee::Projects::GitProject @@ -178,6 +177,25 @@ end end + if described_class == Licensee::Projects::GitHubProject + before do + stub_request( + :get, "#{api_base}/#{stubbed_org}/#{fixture}/contents/" + ).to_return( + status: 200, + body: fixture_root_contents_from_api(fixture), + headers: { 'Content-Type' => 'application/json' } + ) + + file = fixture_path "#{fixture}/project.gemspec" + relative_path = File.basename(file) + parts = [api_base, stubbed_org, fixture, 'contents', relative_path] + stub_request(:get, parts.join('/')) + .with(headers: { 'accept' => 'application/vnd.github.v3.raw' }) + .to_return(status: 200, body: File.read(file)) + end + end + after do FileUtils.rm("#{fixture_path(fixture)}/project.gemspec") end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e8cfa20..6a67177 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -43,7 +43,8 @@ def fixture_root_contents_from_api(fixture) fixture_root_files(fixture).map do |file| { name: File.basename(file), - type: 'file' + type: 'file', + path: File.basename(file) } end.to_json end