xref: /web-bugs/templates/addghpull.php (revision 9d47b05f)
1<?php
2response_header('Add Pull Request :: ' . clean($package_name));
3?>
4<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
5<script src="js/Markdown.Converter.js"></script>
6<script src="js/Markdown.Sanitizer.js"></script>
7<h2>Add a Pull Request to <a href="bug.php?id=<?php echo $bug_id; ?>">Bug #<?php echo $bug_id; ?></a></h2>
8<ul>
9 <li>One problem per pull request, please</li>
10 <li>The pull request must be opened against a PHP project on GitHub</li>
11 <li>Choose a meaningful request name (i.e. include bug id and title)</li>
12</ul>
13<form name="patchform" method="post" action="gh-pull-add.php">
14<input type="hidden" name="bug" value="<?php echo $bug_id; ?>">
15<?php
16if (!empty($errors)) {
17    foreach ($errors as $err) {
18        echo '<div class="errors">' . htmlspecialchars($err) . '</div>';
19    }
20}
21?>
22<table>
23<?php
24if (!$logged_in) {
25    $_SESSION['answer'] = $captcha->getAnswer();
26?>
27 <tr>
28  <th class="form-label_left">
29   Email Address (MUST BE VALID)
30  </th>
31  <td class="form-input">
32   <input type="text" name="email" value="<?php echo clean($email); ?>">
33  </td>
34 </tr>
35 <tr>
36  <th>Solve the problem:<br><?php echo $captcha->getQuestion(); ?></th>
37  <td class="form-input"><input type="text" name="captcha"></td>
38 </tr>
39<?php } ?>
40 <tr>
41  <th class="form-label_left">
42   Repository:
43  </th>
44  <td class="form-input">
45   <select name="repository" id="repository_field"><option value=""></option></select>
46   <small>The repository must belong to https://github.com/php</small>
47  </td>
48 </tr>
49 <tr>
50  <th class="form-label_left">
51   Pull Request:
52  </th>
53  <td class="form-input">
54   <img src="images/loading-blue.gif" id="loading">
55   <select name="pull_id" id="pull_id_field"></select>
56   <div id="pull_details"></div>
57  </td>
58 </tr>
59</table>
60<br>
61<input type="submit" name="addpull" value="Save">
62</form>
63<script>
64var gh_pulls = false;
65var converter;
66if (typeof($) != "function") {
67  window.alert("Failed to load jQuery!");
68}
69
70$(document).ready(function() {
71  var MAX_PER_PAGE = 100;
72  var org = "php";
73  var baseurl = "https://api.github.com/";
74  var url = baseurl+'orgs/'+org+'/repos?per_page=' + MAX_PER_PAGE;
75
76  converter = new Markdown.getSanitizingConverter();
77
78  $("#pull_id_field").empty().hide();
79  $('#pull_details').empty();
80
81
82  // using https://gist.github.com/niallo/3109252
83  function parse_link_header(header) {
84    if (header.length === 0) {
85      throw new Error("input must not be of zero length");
86    }
87
88    // Split parts by comma
89    var parts = header.split(',');
90    var links = {};
91    // Parse each part into a named link
92    for(var i=0; i<parts.length; i++) {
93      var section = parts[i].split(';');
94      if (section.length !== 2) {
95        throw new Error("section could not be split on ';'");
96      }
97      var url = section[0].replace(/<(.*)>/, '$1').trim();
98      var name = section[1].replace(/rel="(.*)"/, '$1').trim();
99      links[name] = url;
100    }
101    return links;
102  }
103
104
105  function recursiveFetch(url, items, finalCallback) {
106    var hasNext = false;
107    $.ajax({ dataType: 'json', url: url, success: function(data, textStatus, request) {
108      items = items.concat(data);
109      if (request.getResponseHeader('Link')) {
110        var links = parse_link_header(request.getResponseHeader('Link'));
111        if (links['next']) {
112          hasNext = true;
113          recursiveFetch(links['next'], items, finalCallback);
114        }
115      }
116      if (hasNext === false) {
117        finalCallback(items);
118      }
119    }
120    });
121  }
122
123  var repos = [], gh_pulls = [];
124  recursiveFetch(url, [], function(items) {
125    items.map(function(repo) {
126      repos.push(repo.name);
127    });
128    repos.sort();
129    repos.map(function(repo) {
130      $("#repository_field").append("<option>"+repo+"</option>");
131    });
132    $("#loading").hide();
133  });
134
135  $("#repository_field").change(function() {
136    $("#pull_id_field").empty().hide();
137    $('#pull_details').empty();
138    $('#loading').show();
139    gh_pulls = [];
140    $("#pull_id_field").append("<option value=''></option>");
141    var repo = $("#repository_field").val();
142    if (repo == "") {
143      $('#loading').hide();
144      return;
145    }
146
147    var url = baseurl + 'repos/' + org + '/' + repo + '/pulls?per_page=' + MAX_PER_PAGE;
148    recursiveFetch(url, [], function(items) {
149      items.map(function(item) {
150        $("#pull_id_field").append("<option value=" + (item.number + 0) + ">" + item.number + " - " + item.title + "</option>");
151      });
152      gh_pulls = items;
153      $("#pull_id_field").show();
154      $("#loading").hide();
155    });
156  });
157
158
159  $("#pull_id_field").change(function() {
160    var val = $("#pull_id_field").val();
161    $('#pull_details').empty();
162    if (val == "" || gh_pulls.length === 0) {
163      return;
164    }
165    var pr = false;
166    for (var i in gh_pulls) {
167      if (gh_pulls[i].number == val) {
168        pr = gh_pulls[i];
169        break;
170      }
171    }
172    if (pr) {
173      $('#pull_details').append('<b>'+pr.title+'</b><br>'+converter.makeHtml(pr.body)+'<p><a href="'+pr.html_url+'">View on GitHub</a></p>');
174    }
175  });
176
177}); // document.load
178
179</script>
180<br>
181<?php
182
183$canpatch = false;
184require "{$ROOT_DIR}/templates/listpulls.php";
185response_footer();
186