Browse Source

Project code in URL for the "Add PagerDuty (Connect)" page. cc: #336

pull/340/head
Pēteris Caune 5 years ago
parent
commit
ea333f7ac1
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
4 changed files with 22 additions and 16 deletions
  1. +6
    -4
      hc/front/tests/test_add_pdc.py
  2. +2
    -2
      hc/front/urls.py
  3. +13
    -9
      hc/front/views.py
  4. +1
    -1
      templates/front/channels.html

+ 6
- 4
hc/front/tests/test_add_pdc.py View File

@ -5,7 +5,9 @@ from hc.test import BaseTestCase
@override_settings(PD_VENDOR_KEY="foo")
class AddPdConnectTestCase(BaseTestCase):
url = "/integrations/add_pdc/"
def setUp(self):
super(AddPdConnectTestCase, self).setUp()
self.url = "/projects/%s/add_pdc/" % self.project.code
def test_instructions_work(self):
self.client.login(username="[email protected]", password="password")
@ -19,8 +21,8 @@ class AddPdConnectTestCase(BaseTestCase):
self.client.login(username="[email protected]", password="password")
url = self.url + "1234567890AB/?service_key=123"
r = self.client.get(url)
self.assertEqual(r.status_code, 302)
r = self.client.get(url, follow=True)
self.assertRedirects(r, self.channels_url)
c = Channel.objects.get()
self.assertEqual(c.kind, "pd")
@ -29,7 +31,7 @@ class AddPdConnectTestCase(BaseTestCase):
def test_it_validates_code(self):
session = self.client.session
session["pd"] = "1234567890AB" # 12 characters
session["pd"] = "1234567890AB"
session.save()
self.client.login(username="[email protected]", password="password")


+ 2
- 2
hc/front/urls.py View File

@ -24,8 +24,6 @@ check_urls = [
channel_urls = [
path("", views.channels, name="hc-channels"),
path("add_pdc/", views.add_pdc, name="hc-add-pdc"),
path("add_pdc/<str:state>/", views.add_pdc, name="hc-add-pdc-state"),
path("add_slack/", views.add_slack, name="hc-add-slack"),
path("add_slack_btn/", views.add_slack_btn, name="hc-add-slack-btn"),
path(
@ -64,6 +62,8 @@ project_urls = [
path("add_pagerteam/", views.add_pagerteam, name="hc-add-pagerteam"),
path("add_pagertree/", views.add_pagertree, name="hc-add-pagertree"),
path("add_pd/", views.add_pd, name="hc-add-pd"),
path("add_pdc/", views.add_pdc, name="hc-add-pdc"),
path("add_pdc/<str:state>/", views.add_pdc, name="hc-add-pdc-state"),
path("add_prometheus/", views.add_prometheus, name="hc-add-prometheus"),
path("add_pushbullet/", views.add_pushbullet, name="hc-add-pushbullet"),
path("add_shell/", views.add_shell, name="hc-add-shell"),


+ 13
- 9
hc/front/views.py View File

@ -941,10 +941,12 @@ def add_pd(request, code):
return render(request, "integrations/add_pd.html", ctx)
def add_pdc(request, state=None):
def add_pdc(request, code, state=None):
if settings.PD_VENDOR_KEY is None:
raise Http404("pagerduty integration is not available")
project = _get_project_for_user(request, code)
if state and request.user.is_authenticated:
if "pd" not in request.session:
return HttpResponseBadRequest()
@ -954,11 +956,10 @@ def add_pdc(request, state=None):
return HttpResponseBadRequest()
if request.GET.get("error") == "cancelled":
messages.warning(request, "PagerDuty setup was cancelled")
return redirect("hc-channels")
messages.warning(request, "PagerDuty setup was cancelled.")
return redirect("hc-p-channels", project.code)
channel = Channel(kind="pd", project=request.project)
channel.user = request.project.owner
channel = Channel(kind="pd", project=project)
channel.value = json.dumps(
{
"service_key": request.GET.get("service_key"),
@ -968,15 +969,18 @@ def add_pdc(request, state=None):
channel.save()
channel.assign_all_checks()
messages.success(request, "The PagerDuty integration has been added!")
return redirect("hc-channels")
return redirect("hc-p-channels", project.code)
state = _prepare_state(request, "pd")
callback = settings.SITE_ROOT + reverse("hc-add-pdc-state", args=[state])
state = token_urlsafe()
callback = settings.SITE_ROOT + reverse(
"hc-add-pdc-state", args=[project.code, state]
)
connect_url = "https://connect.pagerduty.com/connect?" + urlencode(
{"vendor": settings.PD_VENDOR_KEY, "callback": callback}
)
ctx = {"page": "channels", "project": request.project, "connect_url": connect_url}
ctx = {"page": "channels", "project": project, "connect_url": connect_url}
request.session["pd"] = state
return render(request, "integrations/add_pdc.html", ctx)


+ 1
- 1
templates/front/channels.html View File

@ -257,7 +257,7 @@
<p>On-call scheduling, alerting, and incident tracking.</p>
{% if enable_pdc %}
<a href="{% url 'hc-add-pdc' %}" class="btn btn-primary">Add Integration</a>
<a href="{% url 'hc-add-pdc' project.code %}" class="btn btn-primary">Add Integration</a>
{% else %}
<a href="{% url 'hc-add-pd' project.code %}" class="btn btn-primary">Add Integration</a>
{% endif %}


Loading…
Cancel
Save