filtering dependency-track vulnerabilities notification - pebble

I have a dependency-track server that sends notifications to email for all new vulnerabilities.
I would like to send notifications only for critical vulnerabilities.
In the pebble templates, I would like to have something like this:
{% if subject.vulnerability.severity == "CRITICAL" %}
// send notifications
{% else %}
// do not send any notification
{% endif %}
Any idea?
I tried to use the template below, but it continues to send empty notifications also for those vulnerabilities with severity High, Medium and Minor.
In the else statement I should have something that invalidates the template.
{% if notification.group == "NEW_VULNERABILITY" %}
{% if subject.vulnerability.severity == "CRITICAL" %}
{{ notification.title }}
--------------------------------------------------------------------------------
Vulnerability ID: {{ subject.vulnerability.vulnId }}
Severity: {{ subject.vulnerability.severity }}
Source: {{ subject.vulnerability.source }}
Component: {{ subject.component.toString }}
Component URL: {{ baseUrl }}/component/?uuid={{ subject.component.uuid }}
Project: {{ subject.component.project.name }}
Version: {{ subject.component.project.version }}
Description: {{ subject.component.project.description }}
Project URL: {{ baseUrl }}/projects/{{ subject.component.project.uuid }}
{% if notification.subject.affectedProjects|length > 1%}
--------------------------------------------------------------------------------
Other affected projects:
{% for affectedProject in notification.subject.affectedProjects %}
{% if not (affectedProject.uuid == subject.component.project.uuid) %}
Project:[{{affectedProject.name}} : {{ affectedProject.version }}]
Project URL:{{ baseUrl }}/project/{{ affectedProject.uuid }}
{% endif %}
{% endfor %}
{% endif %}
--------------------------------------------------------------------------------
{{ notification.content }}
--------------------------------------------------------------------------------
{{ timestamp }}
{% endif %}
{% endif %}

Related

Why did the lower built in filter not work in Django template 2.2?

Using Django v2.2 built-in filters for templates.
my template looks like this:
{% load i18n %}{% blocktrans with site_name=current_site.organization.name site_domain=current_site.name %}Hello from {{ site_name }}!
You're receiving this e-mail because your email is subscribed to receiving notification when a Quotation is Pending PO Release.
{{ quote_type }}
{{ quote_type|lower }}
- Quotation: {{ display_quotation_number }}
- PO Date: {{ po_date_display }} (New)
{% endblocktrans %}
{% blocktrans with site_name=current_site.organization.name site_domain=current_site.name %}Thank you for using {{ site_name }}!
{{ site_domain }}{% endblocktrans %}
I can say with certainty that {{ quote_type }} definitely prints something out. {{ quote_type|lower }} prints nothing.
What did I do wrong?
I was certain that the filter is builtin; see https://docs.djangoproject.com/en/2.2/ref/templates/builtins/#lower
UPdate
This is my current workard and it works
extra_context = self.lower_quote_type(extra_context)
quotation.email_people(
recipient_list=recipient_list,
extra_context=extra_context,
)
def lower_quote_type(self, extra_context):
# #makeAnIssue temp workard for #1334 but try to permanently fix it elegantly
if "quote_type" in extra_context:
extra_context["quote_type"] = extra_context["quote_type"].lower()
return extra_context
Then this works
{% load i18n %}{% blocktrans with site_name=current_site.organization.name site_domain=current_site.name %}Hello from {{ site_name }}!
You're receiving this e-mail because your email is subscribed to receiving notification when a Quotation is Pending PO Release.
{{ quote_type }} # this correctly shows up as lower case
- Quotation: {{ display_quotation_number }}
- PO Date: {{ po_date_display }} (New)
{% endblocktrans %}
{% blocktrans with site_name=current_site.organization.name site_domain=current_site.name %}Thank you for using {{ site_name }}!
{{ site_domain }}{% endblocktrans %}

Django/DRY: Avoid repetition in template

Anyone know how to avoid this repetition my template? I tried it with a for loop, but it didn't work out.
<p>
Current:<br>
{% for event in states.current %}
{{ event.name }}
{% endfor %}
</p>
<p>
Past:<br>
{% for event in states.past %}
{{ event.name }}
{% endfor %}
</p>
Simple solution: set the proper context data structure in your view:
def yourview(request, ...):
# code here
context = {
# other stuff here
"states_data": [
# (label, events)
("Current", states.current()),
("Past", states.past())),
]
}
Then in your template:
{% for label, events in states_data %}
<p>
{{ label }}
{% for event in events %}
{{ event.name }}
{% endfor %}
</p>
{% endfor %}

Get the first element in the for loops in the Django template

Template:
{% for code in group_codes %}
*_{{ code.build }}_*<br />
{% if test_info.test_type = 0 %}
{{ code.pre_testing_fail }}/{{ code.pre_testing_total }} failed pre-test<br />
{% else %}
{% for shelf in final_shelf_info %}
{{ shelf.build }} <br/>
{% if shelf.build = code.build %}
{{ mr_script_count_func }}/{{ code.script_total }}
<span>MR</span> failed during script<br />
{{gw_script_count_func}}/{{ code.script_total }}
<span>GW</span> failed during script<br />
{{ mr_post_count_func }}/{{ code.post_testing_total }}
MR failed during post-test<br/>
{{ gw_post_count_func }}/{{ code.post_testing_total }}
GW failed during post-test<br/>
{% endif %}
{% endfor %}
<br/>
<br/>
{% endif %}
{% endfor %}
View
def final_shelf_info(self):
shelves = self.bugs_stbs()
shelfList = list()
for shelf in shelves:
shelfList.append(shelf.results_stb_id)
final_info = ResultsStbs.objects.select_related(
'build',
'pre_testing_result',
'script_result',
'post_result',
).filter(
results_stb_id__in=shelfList,
tr_test_case_id=self.kwargs['trTestCaseID'],
).order_by(
'pair_no','shelf_no',
)
for info in final_info:
if info.stb_hw_info_ids:
info.stb_type = info.stb_hw_info_ids.stb_hw_info.stb_type
else:
info.stb_type = None
return final_info
I would like to get the first element in the for loop
{% for shelf in final_shelf_info %}
and compare with another data.
How can I get the first element in the first loop.
First element : Q004.01.55.01.55.19_9423
{{ shelf[0].build }} I tried like that, it did not work.
The output of the for loop:
1234.xx.xx.xx.xx.xx
Any helps would be appreciated.
{% for shelf in final_shelf_info %}
{% if forloop.first %}
Do something with {{ shelf }} since its the first item iterated
{% endif %}
{% endfor %}
More on the {% for %} template loop in the docs.
You could do something like this:
{% for t in things %}
{% if forloop.first %}
// do something
{% endif %}
// do stuff
{% if forloop.last or things.count == 1 %}
// do something
{% endif %}
{% endfor %}
More documentation is available at Django documentation
{% if final_shelf_info.0 == shelf %}
or
{% if final_shelf_info.first == shelf %}

Why doesn't django like my dictionary?

I'm new to django, and desperately trying to figure out why I can't get a set of dictionary objects to render. Here is a snippet of the template--with some pprints for debugging:
<ul>
{% with req.requirement_id as reqid %}
req.requirement_id: {{ req.requirement_id|pprint }}<br />
reqid: {{ reqid|pprint }}<br />
e_quals: {{ e_quals|pprint }}<br />
e_quals.reqid: {{ e_quals.reqid|pprint }}<br />
{% for qual in e_quals.reqid %}
qual.qual_type: {{ qual.qual_type }}
{% if qual.qual_type == "self" %}
<li>Only self-endorsements.</li>
{% endif %}
{% if qual.qual_type == "other" %}
<li>No self-endoresements.</li>
{% endif %}
{% if qual.qual_type == "hasa" %}
<li>Endorser must hold an active {{ qual.qual_data }} badge.</li>
{% endif %}
{% endfor %}
{% endwith %}
</ul>
And here is what I get as an output:
req.requirement_id: u'man_keephead'
reqid: u'man_keephead'
e_quals: {u'man_keephead': [<EndorsementQual: man_keephead_others>, <EndorsementQual: man_keephead_man>], u'man_trustself': [<EndorsementQual: man_trustself_self>], u'man_waiting': [<EndorsementQual: man_waiting_other>]}
e_quals.reqid: ''
I really seems like--given that reqid and that e_quals dictionary, e_quals.reqid should produce that list of objects. I'm not sure what I'm missing.
You can't do this sort of indirect variable resolution in Django's template language. It will always interpret e_quals.req_id as e_quals["req_id"] - ie as a literal key.
You'll need to create a simple template filter:
#register.filter
def dict_get(my_dict, key):
return my_dict.get(key)
{{ e_quals|dict_get:req_id }}

How to escape liquid template tags?

This sounds very easy, however I couldn't find it anywhere in the docs. How can I write {% this %} in a liquid template, without it being processed by the engine?
it is possible to disable liquid processing engine using the raw tag:
{% raw %}
{% this %}
{% endraw %}
will display
{% this %}
For future searchers, there is a way to escape without plugins, use the code below:
{{ "{% this " }}%}
and for tags, to escape {{ this }} use:
{{ "{{ this " }}}}
There is also a jekyll plugin for this which makes it a whole lot easier: https://gist.github.com/1020852
Raw tag for jekyll. Keeps liquid from parsing text betweeen {% raw %}
and {% endraw %}
Reference
BTW:
If you want to display {{ "{% this " }}%}in Jekyll, you can code like this:
{{ "{{ " }}"{{ "{% this" }} " }}{{ "}}%}
To escape {{ "{{ this " }}}}use:
{{ "{{ " }}"{{ "{{ this" }} " }}{{ "}}}}
You can escape liquid tags in Jekyll posts using {% raw %} {% endraw %} i.e
{% raw %}
{% for post in site.posts %}
{{ post.content }}
{% endfor %}
{% endraw %}
will produce
{% for post in site.posts %}
{{ post.content }}
{% endfor %}
There is another option: to use HTML special characters codes for replacing the curly braces with its matching codes:
replace each { with {
replace each } with }
For more details about this solution see:
http://www.tikalk.com/devops/curly_brances_workaround/
I found a omnipotent way to display any text with curly braces. You can assign plain text to a variable, and display it.
{% assign var = "{{ sth }}" %}
{{ var }}
As mentioned here also, plain {% raw %} and {% endraw %} are only the second best solution since those are shown if you look up the Markdown on normal github.com.
The best way is to put {% raw %} and {% endraw %} in HTML comments:
<!-- {% raw %} -->
something with curlky brackets like { this } and { that }
<!-- {% endraw %} -->
Due to the HTML comments it is seen by Github as a comment. In Github pages the raw tags will prevent the parsing of the curly brackets in between the tags.
I tried {% raw %} something {% endraw %} ,
and {{ "{% this " }}%}. But they both don't work.
finally, my working answer is
{{ "{%" xxx }} something }}.
My code:
{{ "{%" }} extends 'xadmin/base_site.html' %}
{{ "{%" }} block nav_form %}
<h3>{{ "{{" }} title }}</h3>
{{ "{%" }} for i in context1 %}
<p>{{ "{{" }} i }}</p>
{{ "{%" }} endfor %}
{{ "{%" }} endblock %}
The result:
{% extends 'xadmin/base_site.html' %}
{% block nav_form %}
<h3>{{ title }}</h3>
{% for i in context1 %}
<p>{{ i }}</p>
{% endfor %}
{% endblock %}
Allows output of Liquid code on a page without being parsed.
{% raw %}{{ 5 | plus: 6 }}{% endraw %} equals 11.
{{ 5 | plus: 6 }} equals 11.
For more details about this solution see: https://www.shoplazza.dev/docs/theme-tags