<commit_before># -*- coding: utf-8 -*-

# Copyright(C) 2013      <NAME>
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.


from weboob.tools.browser import BaseBrowser
from weboob.tools.json import json as json_module
from base64 import b64encode
import datetime
import re
import os
from urllib import quote_plus


__all__ = ['GithubBrowser']



class GithubBrowser(BaseBrowser):
    PROTOCOL = 'https'
    DOMAIN = 'api.github.com'
    ENCODING = 'utf-8'

    def __init__(self, *a, **kw):
        kw['parser'] = 'json'
        BaseBrowser.__init__(self, *a, **kw)
        self.fewer_requests = not bool(self.username)

    def home(self):
        pass

    def get_project(self, project_id):
        json = self.do_get('https://api.github.com/repos/%s' % project_id)
        return {'name': json['name'], 'id': project_id}

    def get_issue(self, project_id, issue_number):
        json = self.do_get('https://api.github.com/repos/%s/issues/%s' % (project_id, issue_number))
        return self._make_issue(project_id, issue_number, json)

    def iter_project_issues(self, project_id):
        base_url = 'https://api.github.com/repos/%s/issues' % project_id
        for json in self._paginated(base_url):
            for jissue in json:
                issue_number = jissue['number']
                yield self._make_issue(project_id, issue_number, jissue)
            if len(json) < 100:
                break

    def iter_issues(self, query):
        qsparts = ['repo:%s' % query.project]
        if query.assignee:
            qsparts.append('assignee:%s' % query.assignee)
        if query.author:
            qsparts.append('author:%s' % query.author)
        if query.status:
            qsparts.append('state:%s' % query.status)
        if query.title:
            qsparts.append('%s in:title' % query.title)

        qs = quote_plus(' '.join(qsparts))

        base_url = 'https://api.github.com/search/issues?q=%s' % qs
        for json in self._paginated(base_url):
            for jissue in json['items']:
                issue_number = jissue['number']
                yield self._make_issue(query.project, issue_number, jissue)
            if not len(json['items']):
                break

    def post_issue(self, issue):
        base_data = self._issue_post_body(issue)
        url = 'https://api.github.com/repos/%s/issues' % issue.project.id
        json = self.do_post(url, base_data)
        issue_number = json['id']
        return self._make_issue(issue.project.id, issue_number, json)

    def edit_issue(self, issue, issue_number):
        base_data = self._issue_post_body(issue)
        url = 'https://api.github.com/repos/%s/issues/%s' % (issue.project.id, issue_number)
        self.do_patch(url, base_data)
        return issue

    def _issue_post_body(self, issue):
        data = {'title': issue.title, 'body': issue.body}
        if issue.assignee:
            data['assignee'] = issue.assignee.id
        if issue.version:
            data['milestone'] = issue.version.id
        return json_module.dumps(data)

    def post_comment(self, issue_id, comment):
        project_id, issue_number = issue_id.rsplit('/', 1)
        url = 'https://api.github.com/repos/%s/issues/%s/comments' % (project_id, issue_number)
        data = json_module.dumps({'body': comment})
        self.do_post(url, data)

    # helpers
    def _make_issue(self, project_id, issue_number, json):
        d = {'number': issue_number, 'title': json['title'], 'body': json['body'], 'creation': parse_date(json['created_at']), 'updated': parse_date(json['updated_at']), 'author': json['user']['login'], 'status': json['state']}

        if json['assignee']:
            d['assignee'] = json['assignee']['login']
        else:
            d['assignee'] = None<commit_msg>github: implement edit/set issue status<commit_after># -*- coding: utf-8 -*-

# Copyright(C) 2013      <NAME>
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.


from weboob.tools.browser import BaseBrowser
from weboob.tools.json import json as json_module
from base64 import b64encode
import datetime
import re
import os
from urllib import quote_plus


__all__ = ['GithubBrowser']



class GithubBrowser(BaseBrowser):
    PROTOCOL = 'https'
    DOMAIN = 'api.github.com'
    ENCODING = 'utf-8'

    def __init__(self, *a, **kw):
        kw['parser'] = 'json'
        BaseBrowser.__init__(self, *a, **kw)
        self.fewer_requests = not bool(self.username)

    def home(self):
        pass

    def get_project(self, project_id):
        json = self.do_get('https://api.github.com/repos/%s' % project_id)
        return {'name': json['name'], 'id': project_id}

    def get_issue(self, project_id, issue_number):
        json = self.do_get('https://api.github.com/repos/%s/issues/%s' % (project_id, issue_number))
        return self._make_issue(project_id, issue_number, json)

    def iter_project_issues(self, project_id):
        base_url = 'https://api.github.com/repos/%s/issues' % project_id
        for json in self._paginated(base_url):
            for jissue in json:
                issue_number = jissue['number']
                yield self._make_issue(project_id, issue_number, jissue)
            if len(json) < 100:
                break

    def iter_issues(self, query):
        qsparts = ['repo:%s' % query.project]
        if query.assignee:
            qsparts.append('assignee:%s' % query.assignee)
        if query.author:
            qsparts.append('author:%s' % query.author)
        if query.status:
            qsparts.append('state:%s' % query.status)
        if query.title:
            qsparts.append('%s in:title' % query.title)

        qs = quote_plus(' '.join(qsparts))

        base_url = 'https://api.github.com/search/issues?q=%s' % qs
        for json in self._paginated(base_url):
            for jissue in json['items']:
                issue_number = jissue['number']
                yield self._make_issue(query.project, issue_number, jissue)
            if not len(json['items']):
                break

    def post_issue(self, issue):
        base_data = self._issue_post_body(issue)
        url = 'https://api.github.com/repos/%s/issues' % issue.project.id
        json = self.do_post(url, base_data)
        issue_number = json['id']
        return self._make_issue(issue.project.id, issue_number, json)

    def edit_issue(self, issue, issue_number):
        base_data = self._issue_post_body(issue)
        url = 'https://api.github.com/repos/%s/issues/%s' % (issue.project.id, issue_number)
        self.do_patch(url, base_data)
        return issue

    def _issue_post_body(self, issue):
        data = {'title': issue.title, 'body': issue.body}
        if issue.assignee:
            data['assignee'] = issue.assignee.id
        if issue.version:
            data['milestone'] = issue.version.id
        if issue.status:
            data['state'] = issue.status.name # TODO improve if more statuses are implemented
        return json_module.dumps(data)

    def post_comment(self, issue_id, comment):
        project_id, issue_number = issue_id.rsplit('/', 1)
        url = 'https://api.github.com/repos/%s/issues/%s/comments' % (project_id, issue_number)
        data = json_module.dumps({'body': comment})
        self.do_post(url, data)

    # helpers
    def _make_issue(self, project_id, issue_number, json):
        d = {'number': issue_number, 'title': json['title'], 'body': json['body'], 'creation': parse_date(json['created_at']), 'updated': parse_date(json['updated_at']), 'author': json['user']['login'], 'status': json['state']}

        if json['assignee']:
            d['assignee'] = json['assignee']['login']
        else:
            d['assignee'] = None
{'fraction_non_alphanumeric': 0.12758351022232153, 'fraction_numerical': 0.0033515808289576584, 'mean_word_length': 2.777215189873418, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 14, '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': '5254296', 'n_tokens_mistral': 2828, 'n_tokens_neox': 2713, 'n_words': 710}