tc_reminder.py 2.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 58 59 60 61 62 63 64
#!/usr/bin/python3
"""
This is a command line tool to create reminder list for TC member
"""

import urllib
import urllib.request
import urllib.parse
import argparse
import json
import sys
import os
from pprint import pprint

class Advisor:
    def __init__(self):
        self.secret = open(os.path.expanduser("~/.gitee_token.json"), "r")
        self.token = json.load(self.secret)
#        self.req = urllib.request.Request('https://gitee.com/api/v5/repos/#{owner}/issues')
        self.header = {"Content-Type": "application/json;charset=UTF-8", "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0"}
        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_gitee(self, url):
        req = urllib.request.Request(url = url, 
                headers = self.header,
                method = "GET")
       
        with urllib.request.urlopen(req) as u:
            resp = json.loads(u.read().decode("utf-8"))
        return resp


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

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

    def filter_out_tc(self, users):
        return [x for x in self.tc_members if x in users]
        
if __name__ == "__main__":
    par = argparse.ArgumentParser()

    args = par.parse_args()

    adv = Advisor()
    PRs = adv.get_prs()
    for pr in PRs:
        pprint("https://gitee.com/openeuler/community/pulls/{number}".format(number=pr["number"]))
        pprint(pr["title"])
        comm = adv.get_pr_comments(pr["number"])
        users = []
        for c in comm:
           users.append(c["user"]["login"]) 
        tc = adv.filter_out_tc(users)
        pprint("Involved TC member:")
        pprint(tc)