<commit_before># -*- coding: utf-8 -*-
from git import *
from datetime import datetime
import subprocess
import re
from sys import exit, argv
from shutil import copyfile
from os import remove, stat
import codecs
import requests


def get_tag_name(version):
        return "v{maj}.{min}.{hf}".format(maj=version[0], min=version[1], hf=version[2])


class ReleaseManager:

    def __init__(self, release_type, remote_name="canalTP"):
        self.directory = "."
        self.release_type = release_type
        self.repo = Repo(self.directory)
        self.git = self.repo.git

        #we fetch latest version from remote
        self.remote_name = remote_name

        print "fetching from {}".format(remote_name)
        self.repo.remote(remote_name).fetch("--tags")

        #and we update dev and release branches
        print "rebase dev and release"

        #TODO quit on error
        self.git.rebase(remote_name + "/dev", "dev")
        self.git.rebase(remote_name + "/release", "release")

        print "current branch {}".format(self.repo.active_branch)

        self.version = None
        self.str_version = ""
        self.latest_tag = ""

    def get_new_version_number(self):
        latest_version = None
        last_tag = self.git.describe('--tags', abbrev=0)

        version = re.search('.*(\d+\.\d+\.\d+).*', last_tag)
        if version:
            latest_version = version.group(1)

        if not latest_version:
            print "no latest version found"
            exit(1)

        version_n = latest_version.split('.')
        print "latest version is {}".format(version_n)

        self.version = [int(i) for i in version_n]

        self.latest_tag = get_tag_name(self.version)
        print "last tag is " + self.latest_tag

        if self.release_type == "major":
            self.version[0] += 1
            self.version[1] = version_n[2] = 0
        elif self.release_type == "minor":
            self.version[1] += 1
            self.version[2] = 0
        elif self.release_type == "hotfix":
            self.version[2] += 1
        else:
            exit(5)

        self.str_version = "{maj}.{min}.{hf}".format(maj=self.version[0],
                                                     min=self.version[1],
                                                     hf=self.version[2])

        print "current version is {}".format(self.str_version)
        return self.str_version

    def checkout_parent_branch(self):
        parent=""
        if self.release_type == "major" or self.release_type == "minor":
            parent = "dev"
        else:
            parent = "release"

        self.git.checkout(parent)

        print "current branch {}".format(self.repo.active_branch)

    def get_merged_pullrequest(self):

        #we get all closed PR since latest tag
        #WARNING: it is not the list of merged PR since github does not have that
        query = "https://api.github.com/repos/CanalTP/navitia/" \
                "pulls?state=closed&head={latest_tag}".format(latest_tag=self.latest_tag)

        print "query github api: " + query

        github_response = requests.get(query)

        closed_pr = github_response.json()

        lines = []
        for pr in closed_pr:
            title = pr['title']
            url = pr['html_url']
            lines.append(u'  * {title}  <{url}>\n'.format(title=title, url=url))
<commit_msg>Script_release: manage  github's API errors<commit_after># -*- coding: utf-8 -*-
from git import *
from datetime import datetime
import subprocess
import re
from sys import exit, argv
from shutil import copyfile
from os import remove, stat
import codecs
import requests


def get_tag_name(version):
        return "v{maj}.{min}.{hf}".format(maj=version[0], min=version[1], hf=version[2])


class ReleaseManager:

    def __init__(self, release_type, remote_name="canalTP"):
        self.directory = "."
        self.release_type = release_type
        self.repo = Repo(self.directory)
        self.git = self.repo.git

        #we fetch latest version from remote
        self.remote_name = remote_name

        print "fetching from {}".format(remote_name)
        self.repo.remote(remote_name).fetch("--tags")

        #and we update dev and release branches
        print "rebase dev and release"

        #TODO quit on error
        self.git.rebase(remote_name + "/dev", "dev")
        self.git.rebase(remote_name + "/release", "release")

        print "current branch {}".format(self.repo.active_branch)

        self.version = None
        self.str_version = ""
        self.latest_tag = ""

    def get_new_version_number(self):
        latest_version = None
        last_tag = self.git.describe('--tags', abbrev=0)

        version = re.search('.*(\d+\.\d+\.\d+).*', last_tag)
        if version:
            latest_version = version.group(1)

        if not latest_version:
            print "no latest version found"
            exit(1)

        version_n = latest_version.split('.')
        print "latest version is {}".format(version_n)

        self.version = [int(i) for i in version_n]

        self.latest_tag = get_tag_name(self.version)
        print "last tag is " + self.latest_tag

        if self.release_type == "major":
            self.version[0] += 1
            self.version[1] = version_n[2] = 0
        elif self.release_type == "minor":
            self.version[1] += 1
            self.version[2] = 0
        elif self.release_type == "hotfix":
            self.version[2] += 1
        else:
            exit(5)

        self.str_version = "{maj}.{min}.{hf}".format(maj=self.version[0],
                                                     min=self.version[1],
                                                     hf=self.version[2])

        print "current version is {}".format(self.str_version)
        return self.str_version

    def checkout_parent_branch(self):
        parent=""
        if self.release_type == "major" or self.release_type == "minor":
            parent = "dev"
        else:
            parent = "release"

        self.git.checkout(parent)

        print "current branch {}".format(self.repo.active_branch)

    def get_merged_pullrequest(self):

        #we get all closed PR since latest tag
        #WARNING: it is not the list of merged PR since github does not have that
        query = "https://api.github.com/repos/CanalTP/navitia/" \
                "pulls?state=closed&head={latest_tag}".format(latest_tag=self.latest_tag)

        print "query github api: " + query

        github_response = requests.get(query)

        if github_response.status_code != 200:
            message = github_response.json()['message']
            return u'  * Impossible de récupérer les PR\n  * '+ message

        closed_pr = github_response.json()

        lines = []
        for pr in closed_pr:
            title = pr['title']
            url = pr['html_url']
            lines.append(u'  * {title}  <{url}>\n'.format(title=title, url=url))

{'fraction_non_alphanumeric': 0.1199541941024907, 'fraction_numerical': 0.006727741196679072, 'mean_word_length': 2.0671641791044775, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1953512', 'n_tokens_mistral': 2109, 'n_tokens_neox': 2015, 'n_words': 495}