1#!/usr/bin/env python3
2#
3# Script to mark bunch of PRs as spam
4#
5# This file is part of GCC.
6#
7# GCC is free software; you can redistribute it and/or modify it under
8# the terms of the GNU General Public License as published by the Free
9# Software Foundation; either version 3, or (at your option) any later
10# version.
11#
12# GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13# WARRANTY; without even the implied warranty of MERCHANTABILITY or
14# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15# for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with GCC; see the file COPYING3.  If not see
19# <http://www.gnu.org/licenses/>.  */
20#
21#
22#
23
24import requests
25import json
26import argparse
27
28base_url = 'https://gcc.gnu.org/bugzilla/rest.cgi/'
29
30def mark_as_spam(id, api_key, verbose):
31    print('Marking as spam: PR%d' % id)
32    # 1) get bug info to find 'cc'
33    u = base_url + 'bug/' + str(id)
34    r = requests.get(u)
35    response = json.loads(r.text)
36
37    if 'error' in response and response['error']:
38        print(response['message'])
39        return
40
41    # 2) mark the bug as spam
42    bug = response['bugs'][0]
43    creator = bug['creator']
44    cc_list = bug['cc']
45    data = {
46        'status': 'RESOLVED',
47        'resolution': 'INVALID',
48        'summary': 'spam',
49        'ids': [id],
50        'api_key': api_key,
51        'comment': { 'comment': 'spam'},
52        'product': 'gcc',
53        'component': 'spam',
54        'version': 'unknown',
55        'cc': {'remove': cc_list},
56        'priority': 'P5',
57        'severity': 'trivial',
58        'url': '',
59        'assigned_to': 'unassigned@gcc.gnu.org' }
60
61    r = requests.put(u, json = data)
62    if verbose:
63        print(r)
64        print(r.text)
65
66    # 3) mark the first comment as spam
67    r = requests.get(u + '/comment')
68    response = json.loads(r.text)
69    for c in response['bugs'][str(id)]['comments']:
70        if c['creator'] == creator:
71            comment_id = c['id']
72            u2 = '%sbug/comment/%d/tags' % (base_url, comment_id)
73            print(u2)
74            r = requests.put(u2, json = {'comment_id': comment_id, 'add': ['spam'], 'api_key': api_key})
75            if verbose:
76                print(r)
77                print(r.text)
78
79    # 4) mark all attachments as spam
80    r = requests.get(u + '/attachment')
81    response = json.loads(r.text)
82    attachments = response['bugs'][str(id)]
83    for a in attachments:
84        attachment_id = a['id']
85        url = '%sbug/attachment/%d' % (base_url, attachment_id)
86        r = requests.put(url, json = {'ids': [attachment_id],
87            'summary': 'spam',
88            'file_name': 'spam',
89            'content_type': 'application/x-spam',
90            'is_obsolete': True,
91            'api_key': api_key})
92        if verbose:
93            print(r)
94            print(r.text)
95
96parser = argparse.ArgumentParser(description='Mark Bugzilla issues as spam.')
97parser.add_argument('api_key', help = 'API key')
98parser.add_argument('range', help = 'Range of IDs, e.g. 10-23,24,25,27')
99parser.add_argument('--verbose', action = 'store_true', help = 'Verbose logging')
100
101args = parser.parse_args()
102
103chunks = args.range.split(',')
104for c in chunks:
105    parts = list(map(lambda x: int(x), c.split('-')))
106    if len(parts) == 1:
107        r = [parts[0]]
108    else:
109        r = range(parts[0], parts[1] + 1)
110
111    for id in r:
112        mark_as_spam(id, args.api_key, args.verbose)
113