forked from DataDog/gello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
106 lines (86 loc) · 3.23 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-
#
# Unless explicitly stated otherwise all files in this repository are licensed
# under the Apache 2 License.
#
# This product includes software developed at Datadog
# (https://www.datadoghq.com/).
#
# Copyright 2018 Datadog, Inc.
#
"""subscriptions/views.py
subscriptions-related routes and view-specific logic.
"""
import re
import textwrap
from flask import redirect, url_for, flash, request
from flask_login import login_required
from . import subscription
from .forms import NewSubscriptionForm, UpdateForm
from ...services import TrelloSubscriptionService
from ...tasks import CreateGitHubWebhook, DeleteGitHubWebhook
from ...models import Repo
from ...models import Subscription
subscription_service = TrelloSubscriptionService()
@subscription.route('/create', methods=['POST'])
@login_required
def create():
create_form = NewSubscriptionForm(request.form)
if create_form.validate():
ids = create_form.list_ids.data
list_ids = re.split("\s*,\s*", ids.strip()) if ids else []
repo_id = create_form.get_repo_id()
# check if there already exists a webhook with this repository
repo = Repo.query.filter_by(github_repo_id=repo_id).first()
if repo.github_webhook_id is None:
# Enqueue a task to create a webhook for the repo (and persist webhook_id)
CreateGitHubWebhook.delay(
url_root=request.url_root,
repo_id=create_form.get_repo_id()
)
subscription_service.create(
board_id=create_form.get_board_id(),
repo_id=repo_id,
issue_autocard=create_form.issue_autocard.data,
pull_request_autocard=create_form.pull_request_autocard.data,
list_ids=list_ids
)
flash('Created subscription')
return redirect(url_for('main.index'))
else:
flash(
textwrap.dedent(
f"""
Could not create subscription because an error occurred:
{create_form.get_error_message()}
"""
)
)
return redirect(url_for('main.index'))
@subscription.route('/<string:board_id>/<int:repo_id>/update', methods=['POST'])
@login_required
def update(board_id, repo_id):
form = UpdateForm(request.form)
subscription_service.update(
board_id=board_id,
repo_id=repo_id,
issue_autocard=form.issue_autocard.data,
pull_request_autocard=form.pull_request_autocard.data
)
flash('Updated subscription')
return redirect(url_for('main.index'))
@subscription.route('/<string:board_id>/<int:repo_id>/delete', methods=['POST'])
@login_required
def delete(board_id, repo_id):
subscription_service.delete(board_id, repo_id)
# check if no other subscriptions are listening to this repo
subscriptions = Subscription.query.filter_by(repo_id=repo_id).all()
if not subscriptions:
# Enqueue a task to delete the webhook from the repo (and remove webhook_id)
repo = Repo.query.filter_by(github_repo_id=repo_id).first()
DeleteGitHubWebhook.delay(
webhook_id=repo.github_webhook_id,
repo_id=repo_id
)
flash('Deleted subscription')
return redirect(url_for('main.index'))