<commit_before># -*- coding: utf-8 -*-
u"""run github backups and restores

:copyright: Copyright (c) 2013-2018 Bivio Software, Inc.  All Rights Reserved.
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""
from __future__ import absolute_import, division, print_function
from pykern import pkconfig
from pykern import pkio
from pykern import pkjson
from pykern.pkcollections import PKDict
from pykern.pkdebug import pkdlog, pkdp, pkdc, pkdexc
import datetime
import github3
import github3.exceptions
import glob
import json
import os
import os.path
import re
import subprocess
import sys
import time


_GITHUB_HOST = 'github.com'
_GITHUB_URI = 'https://' + _GITHUB_HOST
_GITHUB_API = 'https://api.' + _GITHUB_HOST
_WIKI_ERROR_OK = r'fatal: remote error: access denied or repository not exported: .*wiki.git'
_RE_TYPE = type(re.compile(''))
_MAX_TRIES = 3
_TEST_REPO = 'test-pykern-github'
_TXZ = '.txz'
_LIST_ARG_SEP_RE = re.compile(r'[\s,:;]+')

def backup():
    """Backs up all github repositories associated with user into pwd

    Creates timestamped directory, and purges directories older than cfg.keep_days
    """
    try:
        _Backup()
    except subprocess.CalledProcessError as e:
        if hasattr(e, 'output'):
            pkdlog('ERROR: Backup {}', e.output)
    pkdlog('DONE')


def collaborators(org, filename, affiliation='outside', private=True):
    """Lists direct repos to which user has access and is not a team member

    Configured user must be an owner

    Args:
        org (str): GitHub organization
        affiliation (str): all, direct, outside
    """
    from pykern import pkyaml

    g = _GitHub().login()
    o = g.organization(org)
    res = dict()
    for r in o.repositories():
        if r.private == private:
            x = [str(c.login) for c in r.collaborators(affiliation=affiliation)]
            if x:
                res[str(r.name)] = x
    pkyaml.dump_pretty(res, filename)


def create_issue(repo, title, body='', assignees=None, labels=None, milestone=None):
    r = _repo_arg(repo)
    a = PKDict()
    if milestone:
        try:
            m = int(milestone)
            assert m > 0
            a.milestone = str(m)
        except Exception:
            a.milestone = get_milestone(r, title=milestone)
    if labels:
        a.labels = _LIST_ARG_SEP_RE.split(labels)
    if assignees:
        a.assignees = _LIST_ARG_SEP_RE.split(assignees)
    return r.create_issue(
        title=title,
        body=body,
        **a,
    ).number


def create_milestone(repo, title, description='', due_on=None):
    a = PKDict()
    if due_on:
        # GitHub seems to always create at 08:00:00Z in any zone
        # so just have the user put in the time.
        a.due_on = due_on + 'T08:00:00Z'
    if description:
        a.description = description
    return _repo_arg(repo).create_milestone(
        title=title,
        **a,
    ).number


def get_milestone(repo, title):
    t = title.lower()
    for m in _repo_arg(repo).milestones(state='open'):
        if m.title.lower() == t:
            return m.number
    raise KeyError(f'milestone={title} not found')
<commit_msg>allow labels and assignees to be lists/tuples<commit_after># -*- coding: utf-8 -*-
u"""run github backups and restores

:copyright: Copyright (c) 2013-2018 Bivio Software, Inc.  All Rights Reserved.
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""
from __future__ import absolute_import, division, print_function
from pykern import pkconfig
from pykern import pkio
from pykern import pkjson
from pykern.pkcollections import PKDict
from pykern.pkdebug import pkdlog, pkdp, pkdc, pkdexc
import datetime
import github3
import github3.exceptions
import glob
import json
import os
import os.path
import re
import subprocess
import sys
import time


_GITHUB_HOST = 'github.com'
_GITHUB_URI = 'https://' + _GITHUB_HOST
_GITHUB_API = 'https://api.' + _GITHUB_HOST
_WIKI_ERROR_OK = r'fatal: remote error: access denied or repository not exported: .*wiki.git'
_RE_TYPE = type(re.compile(''))
_MAX_TRIES = 3
_TEST_REPO = 'test-pykern-github'
_TXZ = '.txz'
_LIST_ARG_SEP_RE = re.compile(r'[\s,:;]+')

def backup():
    """Backs up all github repositories associated with user into pwd

    Creates timestamped directory, and purges directories older than cfg.keep_days
    """
    try:
        _Backup()
    except subprocess.CalledProcessError as e:
        if hasattr(e, 'output'):
            pkdlog('ERROR: Backup {}', e.output)
    pkdlog('DONE')


def collaborators(org, filename, affiliation='outside', private=True):
    """Lists direct repos to which user has access and is not a team member

    Configured user must be an owner

    Args:
        org (str): GitHub organization
        affiliation (str): all, direct, outside
    """
    from pykern import pkyaml

    g = _GitHub().login()
    o = g.organization(org)
    res = dict()
    for r in o.repositories():
        if r.private == private:
            x = [str(c.login) for c in r.collaborators(affiliation=affiliation)]
            if x:
                res[str(r.name)] = x
    pkyaml.dump_pretty(res, filename)


def create_issue(repo, title, body='', assignees=None, labels=None, milestone=None):

    r = _repo_arg(repo)
    a = PKDict()
    if milestone:
        try:
            m = int(milestone)
            assert m > 0
            a.milestone = str(m)
        except Exception:
            a.milestone = get_milestone(r, title=milestone)
    def _list_arg(arg):
        if isinstance(arg, str):
            return _LIST_ARG_SEP_RE.split(arg)
        return arg

    if labels:
        a.labels = _list_arg(labels)
    if assignees:
        a.assignees = _list_arg(assignees)
    return r.create_issue(
        title=title,
        body=body,
        **a,
    ).number


def create_milestone(repo, title, description='', due_on=None):
    a = PKDict()
    if due_on:
        # GitHub seems to always create at 08:00:00Z in any zone
        # so just have the user put in the time.
        a.due_on = due_on + 'T08:00:00Z'
    if description:
        a.description = description
    return _repo_arg(repo).create_milestone(
        title=title,
        **a,
    ).number


def get_milestone(repo, title):
    t = title.lower()
    for m in _repo_arg(repo).milestones(state='open'):
        if m.title.lower() == t:
            return m.number
    raise KeyError(f'milestone={title} not found')

{'fraction_non_alphanumeric': 0.10733759152515968, 'fraction_numerical': 0.0084125253154697, 'mean_word_length': 2.8721351025331723, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '2812528', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2062, 'n_words': 607}