mark_spam.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. import requests
  24. import json
  25. import argparse
  26. base_url = 'https://gcc.gnu.org/bugzilla/rest.cgi/'
  27. def mark_as_spam(id, api_key, verbose):
  28. print('Marking as spam: PR%d' % id)
  29. # 1) get bug info to find 'cc'
  30. u = base_url + 'bug/' + str(id)
  31. r = requests.get(u)
  32. response = json.loads(r.text)
  33. if 'error' in response and response['error']:
  34. print(response['message'])
  35. return
  36. # 2) mark the bug as spam
  37. bug = response['bugs'][0]
  38. creator = bug['creator']
  39. cc_list = bug['cc']
  40. data = {
  41. 'status': 'RESOLVED',
  42. 'resolution': 'INVALID',
  43. 'summary': 'spam',
  44. 'ids': [id],
  45. 'api_key': api_key,
  46. 'comment': { 'comment': 'spam'},
  47. 'product': 'gcc',
  48. 'component': 'spam',
  49. 'version': 'unknown',
  50. 'cc': {'remove': cc_list},
  51. 'priority': 'P5',
  52. 'severity': 'trivial',
  53. 'url': '',
  54. 'assigned_to': 'unassigned@gcc.gnu.org' }
  55. r = requests.put(u, json = data)
  56. if verbose:
  57. print(r)
  58. print(r.text)
  59. # 3) mark the first comment as spam
  60. r = requests.get(u + '/comment')
  61. response = json.loads(r.text)
  62. for c in response['bugs'][str(id)]['comments']:
  63. if c['creator'] == creator:
  64. comment_id = c['id']
  65. u2 = '%sbug/comment/%d/tags' % (base_url, comment_id)
  66. print(u2)
  67. r = requests.put(u2, json = {'comment_id': comment_id, 'add': ['spam'], 'api_key': api_key})
  68. if verbose:
  69. print(r)
  70. print(r.text)
  71. # 4) mark all attachments as spam
  72. r = requests.get(u + '/attachment')
  73. response = json.loads(r.text)
  74. attachments = response['bugs'][str(id)]
  75. for a in attachments:
  76. attachment_id = a['id']
  77. url = '%sbug/attachment/%d' % (base_url, attachment_id)
  78. r = requests.put(url, json = {'ids': [attachment_id],
  79. 'summary': 'spam',
  80. 'file_name': 'spam',
  81. 'content_type': 'application/x-spam',
  82. 'is_obsolete': True,
  83. 'api_key': api_key})
  84. if verbose:
  85. print(r)
  86. print(r.text)
  87. parser = argparse.ArgumentParser(description='Mark Bugzilla issues as spam.')
  88. parser.add_argument('api_key', help = 'API key')
  89. parser.add_argument('range', help = 'Range of IDs, e.g. 10-23,24,25,27')
  90. parser.add_argument('--verbose', action = 'store_true', help = 'Verbose logging')
  91. args = parser.parse_args()
  92. chunks = args.range.split(',')
  93. for c in chunks:
  94. parts = list(map(lambda x: int(x), c.split('-')))
  95. if len(parts) == 1:
  96. r = [parts[0]]
  97. else:
  98. r = range(parts[0], parts[1] + 1)
  99. for id in r:
  100. mark_as_spam(id, args.api_key, args.verbose)