<commit_before>#!/usr/bin/env python2.7

# example
# An example of helper utility to do stuff on multiple incidents based on filter
#
# Stuff can be closing incidents, running a command, changing type or changing playbook
#
# Author:       <NAME>
# Version:      1.0
#

import json
import argparse
from datetime import date, timedelta
import demisto


def format_dt(dt):
    return dt.strftime('%Y-%m-%dT%H:%M:%S')


def options_handler():
    parser = argparse.ArgumentParser(
        description='Utility for batch action on incidents')
    parser.add_argument(
        '-k', '--key', help='The API key to access the server', required=True)
    parser.add_argument(
        '-s', '--server', help='The server URL to connect to', required=True)
    monthAgo = date.today() - timedelta(days=30)
    parser.add_argument('-f', '--filter', help='The filter query to chose the alerts, default is open incidents created in last month',
                        default='(status:=0 or status:=1) and created:>%s' % format_dt(monthAgo))
    parser.add_argument(
        '-m', '--page', help='The page we are working on', default=0, type=int)
    parser.add_argument(
        '-n', '--size', help='The size per page', default=100, type=int)
    parser.add_argument('-a', '--action', help='The action to perform',
                        default='close', choices=['close'])
    parser.add_argument('--closeReason', help='The close reason')
    parser.add_argument('--closeNotes', help='The close notes')
    parser.add_argument(
        '--customFields', help='The json that includes the values for the custom fields')
    parser.add_argument('--playbook', help='The new playbook name')
    parser.add_argument('--type', help='The new type')
    parser.add_argument('--entry', help='The new entry data to create')
    parser.add_argument('-q', '--quiet', action='store_false',
                        dest='verbose', default=True, help="don't use voice")
    options = parser.parse_args()
    global verbose
    verbose = options.verbose
    return options


def main():
    options = options_handler()
    c = demisto.DemistoClient(options.key, options.server)
    incidents = c.SearchIncidents(options.page, 0, options.filter)
    print(('using filter %s' % options.filter))
    print(('Total #incidents: %d, incidents going to be updated' %
           (incidents['total'])))
    proceed = input('OK to proceed (type y, yes or leave empty)? ')
    proceed = proceed.lower()
    if proceed == 'y' or proceed == 'yes' or proceed == '':
        if options.action == 'close':
            data = {'closeReason': options.closeReason, 'closeNotes': options.closeNotes, 'filter': {
                'page': options.page, 'size': options.size, 'query': options.filter}}
            data['all'] = True
            if options.customFields:
                data['CustomFields'] = json.loads(options.customFields)
            r = c.req('POST', 'incident/batchClose', data)
            if r.status_code != 200:
                raise RuntimeError(
                    'Error updating incidents - %d (%s)' % (r.status_code, r.reason))
            rj = json.loads(r.content)
            if rj['notUpdated'] > 0:
                print(('Updated %d and could not update %d' %
                       (rj['total']), rj['notUpdated']))
            else:
                print(('Updated %d incidents' % rj['total']))


if __name__ == '__main__':
    main()<commit_msg>remove extra parentheses<commit_after>#!/usr/bin/env python2.7

# example
# An example of helper utility to do stuff on multiple incidents based on filter
#
# Stuff can be closing incidents, running a command, changing type or changing playbook
#
# Author:       <NAME>
# Version:      1.0
#

import json
import argparse
from datetime import date, timedelta
import demisto


def format_dt(dt):
    return dt.strftime('%Y-%m-%dT%H:%M:%S')


def options_handler():
    parser = argparse.ArgumentParser(
        description='Utility for batch action on incidents')
    parser.add_argument(
        '-k', '--key', help='The API key to access the server', required=True)
    parser.add_argument(
        '-s', '--server', help='The server URL to connect to', required=True)
    monthAgo = date.today() - timedelta(days=30)
    parser.add_argument('-f', '--filter', help='The filter query to chose the alerts, default is open incidents created in last month',
                        default='(status:=0 or status:=1) and created:>%s' % format_dt(monthAgo))
    parser.add_argument(
        '-m', '--page', help='The page we are working on', default=0, type=int)
    parser.add_argument(
        '-n', '--size', help='The size per page', default=100, type=int)
    parser.add_argument('-a', '--action', help='The action to perform',
                        default='close', choices=['close'])
    parser.add_argument('--closeReason', help='The close reason')
    parser.add_argument('--closeNotes', help='The close notes')
    parser.add_argument(
        '--customFields', help='The json that includes the values for the custom fields')
    parser.add_argument('--playbook', help='The new playbook name')
    parser.add_argument('--type', help='The new type')
    parser.add_argument('--entry', help='The new entry data to create')
    parser.add_argument('-q', '--quiet', action='store_false',
                        dest='verbose', default=True, help="don't use voice")
    options = parser.parse_args()
    global verbose
    verbose = options.verbose
    return options


def main():
    options = options_handler()
    c = demisto.DemistoClient(options.key, options.server)
    incidents = c.SearchIncidents(options.page, 0, options.filter)
    print('using filter %s' % options.filter)
    print('Total #incidents: %d, incidents going to be updated' %
          (incidents['total']))
    proceed = input('OK to proceed (type y, yes or leave empty)? ')
    proceed = proceed.lower()
    if proceed == 'y' or proceed == 'yes' or proceed == '':
        if options.action == 'close':
            data = {'closeReason': options.closeReason, 'closeNotes': options.closeNotes, 'filter': {
                'page': options.page, 'size': options.size, 'query': options.filter}}
            data['all'] = True
            if options.customFields:
                data['CustomFields'] = json.loads(options.customFields)
            r = c.req('POST', 'incident/batchClose', data)
            if r.status_code != 200:
                raise RuntimeError(
                    'Error updating incidents - %d (%s)' % (r.status_code, r.reason))
            rj = json.loads(r.content)
            if rj['notUpdated'] > 0:
                print('Updated %d and could not update %d' %
                       (rj['total'], rj['notUpdated']))
            else:
                print('Updated %d incidents' % rj['total'])


if __name__ == '__main__':
    main()
{'fraction_non_alphanumeric': 0.1431480942682572, 'fraction_numerical': 0.004946173988943846, 'mean_word_length': 2.781628162816282, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5861211', 'n_tokens_mistral': 2013, 'n_tokens_neox': 1948, 'n_words': 581}