Skip to content

Commit

Permalink
(puppetlabsGH-256) Add puppetfile-resolver endpoint
Browse files Browse the repository at this point in the history
Adds a new endpoint for parsing a Puppetfile and returning a list of modules. This only returns forge modules right now because those are the only ones that are resolved by puppetfile-resolver currently.
  • Loading branch information
jpogran authored and glennsarti committed Jun 14, 2020
1 parent 33d871a commit b5dd3ba
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how

## Unreleased

### Added

- ([GH-256](https://github.com/puppetlabs/puppet-editor-services/issues/256)) Add Puppetfile dependency endpoint

## 0.26.0 - 2020-05-01

### Added
Expand Down
21 changes: 21 additions & 0 deletions lib/lsp/lsp_custom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,27 @@ def from_h!(value)
end
end

# export interface PuppetfileDependencyResponse {
# dotContent: string;
# data: string;
# }
class PuppetfileDependencyResponse < LSPBase
attr_accessor :dependencies # type: string[]
attr_accessor :error # type: string

def initialize(initial_hash = nil)
super
@optional_method_names = %i[error]
end

def from_h!(value)
value = {} if value.nil?
self.dependencies = value['dependencies']
self.error = value['error']
self
end
end

# export interface CompileNodeGraphResponse {
# dotContent: string;
# data: string;
Expand Down
17 changes: 17 additions & 0 deletions lib/puppet-languageserver/message_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ def request_puppet_compilenodegraph(_, json_rpc_message)
end
end

def request_puppetfile_getdependencies(_, json_rpc_message)
file_uri = json_rpc_message.params['uri']
return LSP::PuppetfileDependencyResponse.new('error' => 'Must be a puppetfile in order to find dependencies.') unless documents.document_type(file_uri) == :puppetfile

content = documents.document(file_uri)

result = []
begin
result = PuppetLanguageServer::Puppetfile::ValidationProvider.find_dependencies(content)
rescue StandardError => e
PuppetLanguageServer.log_message(:error, "(puppetfile/getdependencies) Error parsing puppetfile. #{e}")
return LSP::PuppetfileDependencyResponse.new('error' => 'An internal error occured while parsing the puppetfile. Please see the debug log files for more information.')
end

LSP::PuppetfileDependencyResponse.new('dependencies' => result)
end

def request_puppet_fixdiagnosticerrors(_, json_rpc_message)
formatted_request = LSP::PuppetFixDiagnosticErrorsRequest.new(json_rpc_message.params)
file_uri = formatted_request.documentUri
Expand Down
26 changes: 26 additions & 0 deletions lib/puppet-languageserver/puppetfile/validation_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,32 @@ def self.validate_resolution(puppetfile_document, document_uri, cache, module_pa
end
end

def self.find_dependencies(content)
require 'puppetfile-resolver'
require 'puppetfile-resolver/puppetfile/parser/r10k_eval'
parser = PuppetfileResolver::Puppetfile::Parser::R10KEval

result = []
puppetfile = parser.parse(content)

return result if puppetfile.nil?

raise 'Puppetfile is not valid' unless puppetfile.valid?

puppetfile.modules.select { |d| d.module_type == :forge }.each do |dep|
result << {
name: dep.name,
title: dep.title,
owner: dep.owner,
version: dep.version.to_s,
start_line: dep.location.start_line,
end_line: dep.location.end_line
}
end

result
end

def self.resolver_cache
return @resolver_cache unless @resolver_cache.nil?
require 'puppetfile-resolver/cache/base'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,4 +430,46 @@ def load(name)
end
end
end

describe "#find_dependencies" do
context 'with a valid Puppetfile' do
let(:content) do <<-EOT
forge 'https://forge.puppetlabs.com/'
# Modules from the Puppet Forge
mod 'puppetlabs-somemodule', '1.0.0'
# Git style modules
mod 'gitcommitmodule',
:git => 'https://github.com/username/repo',
:commit => 'abc123'
mod 'gittagmodule',
:git => 'https://github.com/username/repo',
:tag => '0.1'
# Svn style modules
mod 'svnmodule',
:svn => 'svn://host/repo',
:rev => 'abc123'
# local style modules
mod 'localmodule',
:local => 'true'
EOT
end

it 'should return no validation errors' do
result = subject.find_dependencies(content)

expect(result).to eq([{:end_line=>3,
:name=>"somemodule",
:owner=>"puppetlabs",
:start_line=>3,
:title=>"puppetlabs-somemodule",
:version=>"1.0.0"}])
end
end

end

end

0 comments on commit b5dd3ba

Please sign in to comment.