xref: /web-bugs/www/gh-pull-add.php (revision 353abb3d)
1<?php
2
3use App\Repository\BugRepository;
4use App\Repository\PullRequestRepository;
5use App\Utils\Captcha;
6use App\Utils\GitHub;
7
8// Obtain common includes
9require_once '../include/prepend.php';
10
11session_start();
12$canpatch = true;
13
14// Authenticate
15bugs_authenticate($user, $pw, $logged_in, $user_flags);
16
17/// Input vars
18$bug_id = !empty($_REQUEST['bug']) ? (int) $_REQUEST['bug'] : 0;
19if (empty($bug_id)) {
20    $bug_id = !empty($_REQUEST['bug_id']) ? (int) $_REQUEST['bug_id'] : 0;
21}
22
23if (empty($bug_id)) {
24    response_header('Error :: no bug selected');
25    display_bug_error('No bug selected to add a patch to (no bug or bug_id!)');
26    response_footer();
27    exit;
28}
29
30$bugRepository = $container->get(BugRepository::class);
31
32if (!($buginfo = $bugRepository->findOneById($bug_id))) {
33    response_header('Error :: invalid bug selected');
34    display_bug_error("Invalid bug #{$bug_id} selected");
35    response_footer();
36    exit;
37}
38
39$package_name = $buginfo['package_name'];
40
41// captcha is not necessary if the user is logged in
42if (!$logged_in) {
43    $captcha = $container->get(Captcha::class);
44}
45
46$show_bug_info = bugs_has_access($bug_id, $buginfo, $pw, $user_flags);
47
48if (!$show_bug_info) {
49    response_header('Private report');
50    display_bug_error("The bug #{$bug_id} is not available to public");
51    response_footer();
52    exit;
53}
54
55$pullinfo = $container->get(GitHub::class);
56$pullRequestRepository = $container->get(PullRequestRepository::class);
57
58if (isset($_POST['addpull'])) {
59    $errors = [];
60    if (empty($_POST['repository'])) {
61        $errors[] = 'No repository selected';
62    }
63    if (empty($_POST['pull_id'])) {
64        $errors[] = 'No Pull request selected';
65    }
66
67    if (!$logged_in) {
68        try {
69            $email = isset($_POST['email']) ? $_POST['email'] : '';
70
71            if (!is_valid_email($email, $logged_in)) {
72                $errors[] = 'Email address must be valid!';
73            }
74
75            /**
76             * Check if session answer is set, then compare
77             * it with the post captcha value. If it's not
78             * the same, then it's an incorrect password.
79             */
80            if (!isset($_SESSION['answer']) || $_POST['captcha'] != $_SESSION['answer']) {
81                $errors[] = 'Incorrect Captcha';
82            }
83
84            if (count($errors)) {
85                throw new Exception('');
86            }
87
88        } catch (Exception $e) {
89            $pulls = $pullRequestRepository->findAllByBugId($bug_id);
90            include "{$ROOT_DIR}/templates/addghpull.php";
91            exit;
92        }
93    } else {
94        $email = $auth_user->email;
95    }
96
97    if (!count($errors)) {
98        try {
99            $newpr = $pullinfo->attach($bug_id, $_POST['repository'], $_POST['pull_id'], $email);
100        } catch(\Exception $e) {
101            $errors = ['Could not attach pull request to Bug #'.$bug_id];
102
103            if ($e->errorInfo[1] === 1062) {
104                $errors[] = 'This pull request is already added.';
105            }
106
107            if ('dev' === $container->get('env')) {
108                $errors[] = $e->getMessage();
109            }
110        }
111    }
112
113    if (count($errors)) {
114        $pulls = $pullRequestRepository->findAllByBugId($bug_id);
115        include "{$ROOT_DIR}/templates/addghpull.php";
116        exit;
117    }
118
119    // Add a comment to the bug report.
120    $text = <<<TXT
121The following pull request has been associated:
122
123Patch Name: {$newpr->title}
124On GitHub:  {$newpr->html_url}
125Patch:      {$newpr->patch_url}
126TXT;
127
128    $res = bugs_add_comment($bug_id, $email, $auth_user->name, $text, 'patch');
129
130    // Send emails
131    mail_bug_updates($buginfo, $buginfo, $email, $text, 4, $bug_id);
132
133    $pulls = $pullRequestRepository->findAllByBugId($bug_id);
134    $errors = [];
135    include "{$ROOT_DIR}/templates/addghpull.php";
136    exit;
137}
138
139$email = isset($_GET['email']) ? $_GET['email'] : '';
140$pulls = $pullRequestRepository->findAllByBugId($bug_id);
141
142include "{$ROOT_DIR}/templates/addghpull.php";
143