gitee.py 5.5 KB
Newer Older
1 2 3 4 5 6 7
#!/usr/bin/python3
"""
This is a simple script to query that contact person for specific package
"""

import urllib
import urllib.request
8 9
import urllib.parse
import urllib.error
10 11 12 13 14
import argparse
import yaml
import re
import os.path
import json
15
import pprint
16 17 18 19


class Gitee:
    def __init__(self):
20
        self.secret = open(os.path.expanduser("~/.gitee_personal_token.json"), "r")
21 22
        self.token = json.load(self.secret)

23
        self.headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW 64; rv:50.0) Gecko/20100101 Firefox/50.0'}
24 25 26 27 28
        self.specfile_url_template = "https://gitee.com/src-openeuler/{package}/raw/master/{specfile}"
        self.yamlfile_url_template = "https://gitee.com/src-openeuler/{package}/raw/master/{package}.yaml"
        self.advisor_url_template = "https://gitee.com/openeuler/openEuler-Advisor/raw/master/upstream-info/{package}.yaml"
        self.specfile_exception_url = "https://gitee.com/openeuler/openEuler-Advisor/raw/master/helper/specfile_exceptions.yaml"

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    def post_gitee(self, url, values, headers = None):
        if headers == None:
            headers = self.headers.copy()
        data = urllib.parse.urlencode(values).encode('utf-8')
        req = urllib.request.Request(url = url, data = data, headers=headers, method="POST")
        try:
            u = urllib.request.urlopen(req)
        except urllib.error.HTTPError as err:
            print("WARNING:"+str(err.code))
            print("WARNING:"+str(err.headers))
        return u.read().decode("utf-8")

    def fork_repo(self, repo):
        url = "https://gitee.com/api/v5/repos/src-openeuler/{repo}/forks".format(repo=repo)
        values = {}
        values["access_token"] = self.token["access_token"]
       # headers["User-Agent"] = "curl/7.66.0"
        #headers["Content-Type"] = "application/json;charset=UTF-8"
        #headers["HOST"] = "gitee.com"
        #headers["Accept"] = "*/*"
        return self.post_gitee(url, values)

    def create_pr(self, head, repo):
        url = "https://gitee.com/api/v5/repos/src-openeuler/{repo}/pulls".format(repo=repo)
        values = {}
        values["access_token"] = self.token["access_token"]
        values["title"] = "Upgrade to latest version of {repo}".format(repo=repo)
        values["head"] = "{head}:master".format(head=head)
        values["base"] = "master"
S
Shinwell Hu 已提交
58
        values["body"] = "This is a (mostly) automatically created PR by openEuler-Advisor.\nPlease be noted that it's not throughly tested.\nReview carefully before accept this PR.\nThanks.\nYours openEuler-Advisor.\n"
59 60
        return self.post_gitee(url, values)

61 62 63 64 65 66 67 68 69
    def get_gitee(self, url, headers=None):
        if headers == None:
            req = urllib.request.Request(url=url, headers=self.headers)
        else:
            req = urllib.request.Request(url=url, headers=headers)
        u = urllib.request.urlopen(req)
        return u.read().decode("utf-8")

    def get_gitee_json(self, url):
70 71 72 73
        #headers = self.headers.copy()
        headers = {}
        headers["Content-Type"] = "application/json;charset=UTF-8"
        resp = self.get_gitee(url, headers)
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        return json.loads(resp)

    def get_spec_exception(self):
        resp = self.get_gitee(self.specfile_exception_url)
        exps = yaml.load(resp, Loader=yaml.Loader)
        return exps

    def get_spec(self, pkg):
        exp = self.get_spec_exception()
        if pkg in exp:
            dir_name = exp[pkg]["dir"]
            file_name = exp[pkg]["file"]
            specurl = self.specfile_url_template.format(package=pkg, specfile=dir_name + "/" + file_name)
        else:
            specurl = self.specfile_url_template.format(package=pkg, specfile=pkg+".spec")

        return self.get_gitee(specurl)

    def get_yaml(self, pkg):
        yamlurl = self.advisor_url_template.format(package=pkg)
        resp = self.get_gitee(yamlurl)
        if re.match("Not found", resp):
            yamlurl = self.yamlfile_url_template.format(package=pkg)
            resp = self.get_gitee(yamlurl)
            if re.match("Not found", resp):
                print("Cannot find upstream metadata")
                return False
            else:
                return resp
        else:
            return False

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
class Advisor:
    def __init__(self):
        self.gitee = Gitee()

    """
    def new_issue(owner, repo, title, body):
        @param["access_token"] = @token["access_token"]
        @param["repo"] = repo
        @param["title"] = title
        @param["body"] = body
        @cmd += " 'https://gitee.com/api/v5/repos/#{owner}/issues'"
        @cmd += " -d '" + @param.to_json + "'"
        #puts @cmd
        resp = %x[#{@cmd}]
        #puts resp
    """
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

class openEuler_TC:
    def __init__(self):
        self.gitee = Gitee()
        self.list_url = "https://gitee.com/api/v5/repos/openeuler/community/pulls?access_token={token}&state=open&sort=created&direction=desc&page=1&per_page=100"
        self.desc_url = "https://gitee.com/api/v5/repos/openeuler/community/pulls/{number}/comments?access_token={token}&page=1&per_page=100"
        self.tc_members = ["myeuler", "cynthia_xh", "shinwell_hu", "dream0819", "hanjun-guo", "xiexiuqi", "zhanghai_lucky"]


    def get_prs(self):
        url = self.list_url.format(token=self.token["access_token"])
        return self.gitee.get_gitee(url)

    def get_pr_comments(self, number):
        url = self.desc_url.format(number=number, token=self.token["access_token"])
        return self.gitee.get_gitee(url)

    def filter_out_tc(self, users):
        return [x for x in self.tc_members if x in users]
        
if __name__ == "__main__":
S
Shinwell Hu 已提交
143
    pass
144