From 98955a2180d96f82c7aeb99c01c6bac6f96e46eb Mon Sep 17 00:00:00 2001 From: Shinwell Hu Date: Mon, 1 Jun 2020 15:31:03 +0000 Subject: [PATCH] alpha version of simple-update-robot --- gitee.py | 61 +++++++++++++++++++++++++++++++++++++++--- simple-update-robot.py | 30 +++++++++++++-------- 2 files changed, 77 insertions(+), 14 deletions(-) diff --git a/gitee.py b/gitee.py index ce7b69d4..3f2f97c3 100755 --- a/gitee.py +++ b/gitee.py @@ -5,24 +5,59 @@ This is a simple script to query that contact person for specific package import urllib import urllib.request +import urllib.parse +import urllib.error import argparse import yaml import re import os.path import json +import pprint class Gitee: def __init__(self): - self.secret = open(os.path.expanduser("~/.gitee_token.json"), "r") + self.secret = open(os.path.expanduser("~/.gitee_personal_token.json"), "r") self.token = json.load(self.secret) - self.headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW 64; rv:23.0) Gecko/20100101 Firefox/23.0'} + self.headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW 64; rv:50.0) Gecko/20100101 Firefox/50.0'} 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" + 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" + values["body"] = "This is a automatically created PR from openEuler-Advisor" + return self.post_gitee(url, values) + def get_gitee(self, url, headers=None): if headers == None: req = urllib.request.Request(url=url, headers=self.headers) @@ -32,7 +67,10 @@ class Gitee: return u.read().decode("utf-8") def get_gitee_json(self, url): - resp = self.get_gitee(url) + #headers = self.headers.copy() + headers = {} + headers["Content-Type"] = "application/json;charset=UTF-8" + resp = self.get_gitee(url, headers) return json.loads(resp) def get_spec_exception(self): @@ -65,6 +103,22 @@ class Gitee: else: return False +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 + """ class openEuler_TC: def __init__(self): @@ -104,3 +158,4 @@ if __name__ == "__main__": print("\n") + diff --git a/simple-update-robot.py b/simple-update-robot.py index 778c6b61..0084f5a5 100755 --- a/simple-update-robot.py +++ b/simple-update-robot.py @@ -53,7 +53,7 @@ def create_spec(repo, spec_str, o_ver, n_ver, src_fn=None): in_changelog = False for l in spec_str.splitlines(): if l.startswith("Release:"): - fn.write("Release:\t\t0\n") + fn.write("Release:\t0\n") continue if l.startswith("Source:") or l.startswith("Source0:"): if src_fn: @@ -80,6 +80,10 @@ if __name__ == "__main__": pars.add_argument("pkg", type=str, help="The package to be upgraded") pars.add_argument("-o", "--old_version", type=str, help="Current upstream version of package") pars.add_argument("-n", "--new_version", type=str, help="New upstream version of package will be upgrade to") + pars.add_argument("-s", "--create_spec", help="Create spec file", action="store_true") + pars.add_argument("-d", "--download", help="Download upstream source code", action="store_true") + pars.add_argument("-f", "--fork", help="fork src-openeuler repo into users", action="store_true") + pars.add_argument("-p", "--PR", help="Create upgrade PR", action="store_true") args = pars.parse_args() gt = gitee.Gitee() @@ -91,19 +95,23 @@ if __name__ == "__main__": print("This package has multiple in-house patches.") sys.exit(1) - source_file = download_source_url(spec, args.old_version, args.new_version) - if source_file: - print(source_file) - else: - source_file = download_upstream_url(gt, args.pkg, args.old_version, args.new_version) + if args.download: + source_file = download_source_url(spec, args.old_version, args.new_version) if source_file: print(source_file) else: - print("Failed to download the latest source code.") - sys.exit(1) + source_file = download_upstream_url(gt, args.pkg, args.old_version, args.new_version) + if source_file: + print(source_file) + else: + print("Failed to download the latest source code.") + sys.exit(1) - create_spec(args.pkg, spec_string, args.old_version, args.new_version) + if args.create_spec: + create_spec(args.pkg, spec_string, args.old_version, args.new_version) -""" + if args.fork: + gt.fork_repo(args.pkg) -""" + if args.PR: + gt.create_pr("shinwell_hu", args.pkg) -- GitLab