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

Load all project drops and parse liquid async #1262

Merged
merged 22 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 1 addition & 2 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[v#.#.#] ([month] [YYYY])
- [entity]:
- [future tense verb] [feature]
- Liquid: Make project-level drops available for Liquid syntax
aapomm marked this conversation as resolved.
Show resolved Hide resolved
- Upgraded gems: nokogiri, rexml
- Bugs fixes:
- [entity]:
Expand Down
25 changes: 24 additions & 1 deletion app/services/liquid_assigns_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def initialize(project)
end

def assigns
result = { 'project' => ProjectDrop.new(project) }
result = project_drops
aapomm marked this conversation as resolved.
Show resolved Hide resolved
result.merge!(assigns_pro) if defined?(Dradis::Pro)
result
end
Expand All @@ -15,4 +15,27 @@ def assigns

def assigns_pro
end

def project_drops
{
'evidence' => project_records(type: :evidence),
aapomm marked this conversation as resolved.
Show resolved Hide resolved
'issues' => project_records(type: :issue),
'nodes' => project_records(type: :node),
'notes' => project_records(type: :note),
'project' => ProjectDrop.new(project),
'tags' => project_records(type: :tag)
}
end

def project_records(type:)
aapomm marked this conversation as resolved.
Show resolved Hide resolved
records = project.send(type.to_s.pluralize)
records = records.user_nodes if type == :node
aapomm marked this conversation as resolved.
Show resolved Hide resolved

cache_key = "liquid-project-#{project.id}-#{type.to_s.pluralize}:#{records.maximum(:updated_at).to_i}-#{records.count}"
drop_class = "#{type.to_s.camelize}Drop".constantize

Rails.cache.fetch(cache_key) do
records.map { |record| drop_class.new(record) }
end
end
end
16 changes: 16 additions & 0 deletions spec/services/liquid_assigns_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,21 @@

let(:liquid_assigns) { described_class.new(project).assigns }

before do
node = create(:node, project: project)
issue = create(:issue, node: project.issue_library)
create(:evidence, issue: issue, node: node)
create(:note, node: node)
create(:tag)
end

it 'builds a hash of liquid assigns' do
expect(liquid_assigns['project'].name).to eq(project.name)
expect(liquid_assigns['issues'].map(&:title)).to eq(project.issues.map(&:title))
expect(liquid_assigns['evidence'].map(&:title)).to eq(project.evidence.map(&:title))
expect(liquid_assigns['nodes'].map(&:label)).to eq(project.nodes.user_nodes.map(&:label))
expect(liquid_assigns['notes'].map(&:title)).to eq(project.notes.map(&:title))
expect(liquid_assigns['tags'].map(&:display_name)).to eq(project.tags.map(&:display_name))
end

context 'with pro records', skip: !defined?(Dradis::Pro) do
Expand All @@ -16,11 +29,14 @@
report_content = project.content_library
report_content.properties = { 'dradis.project' => project.name }
report_content.save

create(:content_block, project: project)
end

it 'builds a hash with Dradis::Pro assigns' do
expect(liquid_assigns['document_properties'].available_properties).to eq({ 'dradis.project' => project.name })
expect(liquid_assigns['team'].name).to eq(project.team.name)
expect(liquid_assigns['content_blocks'].map(&:content)).to eq(project.content_blocks.map(&:content))
end
end
end
Loading