|
14 | 14 | # limitations under the License. |
15 | 15 | """Compliance Github service helper.""" |
16 | 16 |
|
17 | | -import base64 |
18 | 17 | import json |
19 | 18 | import random |
20 | | -import re |
21 | | -import sys |
22 | 19 | from collections import OrderedDict |
23 | 20 | from urllib.parse import parse_qs, urlparse |
24 | 21 |
|
25 | 22 | from compliance.utils.credentials import Config |
26 | 23 | from compliance.utils.data_parse import deep_merge |
27 | 24 | from compliance.utils.http import BaseSession |
28 | 25 |
|
29 | | -import yaml |
30 | | - |
31 | 26 |
|
32 | 27 | class Github(object): |
33 | 28 | """Github service helper class.""" |
@@ -286,103 +281,6 @@ def get_all_issues(self, owner, repo, **kwargs): |
286 | 281 | ) |
287 | 282 | return all_issues |
288 | 283 |
|
289 | | - def get_issue_template( |
290 | | - self, |
291 | | - owner, |
292 | | - repo, |
293 | | - template_name, |
294 | | - strip_annotations=False, |
295 | | - strip_header=True, |
296 | | - annotations=None, |
297 | | - render=False |
298 | | - ): |
299 | | - """ |
300 | | - Retrieve the contents of an issue template based on template name. |
301 | | -
|
302 | | - The .md extension and any numeric prefix) from the owner and repo |
303 | | - are ignored. Both the header (the YAML section at the top), and any |
304 | | - defined annotations (the JSON code block inside the issue body) are |
305 | | - returned as dictionaries along with the body of the template. You can |
306 | | - pass ``strip_annotations`` or ``strip_header`` to remove these sections |
307 | | - from the body that's returned. You can pass a dictionary as |
308 | | - ``annotations``, and the values will be merged with those extracted |
309 | | - from the template. |
310 | | -
|
311 | | - If ``render`` is ``True``, then all annotations will be substituted on |
312 | | - the body and header. |
313 | | - """ |
314 | | - # get a list of all templates in this repo |
315 | | - path_elements = [ |
316 | | - 'repos', owner, repo, 'contents', '.github', 'ISSUE_TEMPLATE' |
317 | | - ] |
318 | | - response = self._make_request('get', '/'.join(path_elements)) |
319 | | - templates = [t['name'] for t in response] |
320 | | - |
321 | | - # find the required template, ignoring any numeric prefix |
322 | | - template_file = next( |
323 | | - ( |
324 | | - t for t in templates |
325 | | - if re.sub(r'(^[0-9]+\.)?(.*)\.md$', r'\2', t) == template_name |
326 | | - ), |
327 | | - None |
328 | | - ) |
329 | | - if not template_file: |
330 | | - raise ValueError(f'Template {template_name} not found') |
331 | | - |
332 | | - # get the template from .github/ISSUE_TEMPLATE in the |
333 | | - # repo (master branch) |
334 | | - path_elements = [ |
335 | | - 'repos', |
336 | | - owner, |
337 | | - repo, |
338 | | - 'contents', |
339 | | - '.github', |
340 | | - 'ISSUE_TEMPLATE', |
341 | | - template_file |
342 | | - ] |
343 | | - response = self._make_request('get', '/'.join(path_elements)) |
344 | | - |
345 | | - # result is base64 encoded (and bytes in py3) |
346 | | - assert response['encoding'] == 'base64' |
347 | | - template = base64.b64decode(response['content']) |
348 | | - template = template.decode(sys.stdout.encoding) |
349 | | - |
350 | | - # extract header data structure (YAML) |
351 | | - body_no_header, extracted_header = extract_header(template) |
352 | | - |
353 | | - # optionally strip off the leading header section |
354 | | - if strip_header: |
355 | | - template = body_no_header |
356 | | - |
357 | | - # extract annotations (JSON) |
358 | | - body_no_annotations, extracted_annotations = extract_annotations( |
359 | | - template |
360 | | - ) |
361 | | - |
362 | | - # if we were passed some annotations, |
363 | | - # merge them into what's in the template |
364 | | - if annotations: |
365 | | - deep_merge(extracted_annotations, annotations) |
366 | | - |
367 | | - if render: |
368 | | - body_no_annotations = body_no_annotations.format( |
369 | | - **extracted_annotations |
370 | | - ) |
371 | | - extracted_header = { |
372 | | - k: v.format(**extracted_annotations) |
373 | | - for k, |
374 | | - v in extracted_header.items() |
375 | | - } |
376 | | - # optionally strip off the annotations section |
377 | | - if strip_annotations: |
378 | | - template = body_no_annotations |
379 | | - else: |
380 | | - template = self._annotate_body( |
381 | | - body_no_annotations, extracted_annotations |
382 | | - ) |
383 | | - |
384 | | - return template, extracted_annotations, extracted_header |
385 | | - |
386 | 284 | def search_issues( |
387 | 285 | self, query, sort=None, order=None, owner=None, repo=None |
388 | 286 | ): |
@@ -605,31 +503,3 @@ def extract_annotations(content): |
605 | 503 | annotations = OrderedDict({}) |
606 | 504 |
|
607 | 505 | return content_no_annotations, annotations |
608 | | - |
609 | | - |
610 | | -def extract_header(content): |
611 | | - """ |
612 | | - Retrieve header (YAML) from a string (issue body). |
613 | | -
|
614 | | - Returns the remaining content without the header, |
615 | | - and then the header fields themselves as a dictionary. |
616 | | - """ |
617 | | - header_yaml = [] |
618 | | - lines = content.splitlines(True) |
619 | | - if lines[0] == '---\n': |
620 | | - header_yaml.append(lines.pop(0)) |
621 | | - line = '' |
622 | | - while line != '---\n': |
623 | | - line = lines.pop(0) |
624 | | - if line != '---\n': |
625 | | - header_yaml.append(line) |
626 | | - if lines[0] == '\n': |
627 | | - lines.pop(0) |
628 | | - |
629 | | - content_no_header = ''.join(lines) |
630 | | - if header_yaml: |
631 | | - header_fields = yaml.safe_load(''.join(header_yaml)) |
632 | | - else: |
633 | | - header_fields = {} |
634 | | - |
635 | | - return content_no_header, header_fields |
0 commit comments