summaryrefslogtreecommitdiff
path: root/worker/GiteaWorker.py
blob: f3e6e50ee8036f685fef698f04cf6bad58a884fd (plain)
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
from util import Logger, getProjRepoName
import requests


class GiteaWorker():
    def __init__(self, args, baseUrl, orgName, token, hgroups,
                 logger=Logger()):
        self.args = args
        self.logger = logger
        self.names = {
            item[1]: item[0]
            for items in hgroups.values() for item in items
        }
        self.ids = {
            item[0]: item[1]
            for items in hgroups.values() for item in items
        }
        self.hgroups = hgroups
        self.baseUrl = baseUrl
        self.orgName = orgName
        self.sess = requests.Session()
        self.sess.params.update({"access_token": token})

    def raiseIssues(self, scores):
        for key, value in scores.items():
            if not value.get('projComment'):
                value['projComment'] = ['good job']
            if not value.get('jojComment'):
                value['jojComment'] = ['']
            id_ = self.names[key]
            repoName = getProjRepoName([id_, key, self.args.proj])
            url = f"{self.baseUrl}/repos/{self.orgName}/{repoName}/issues"
            data = {
                "title": f"m{self.args.ms} feedback",
                "body": '\n'.join([*value['projComment'], *value['jojComment']]),
            }
            req = self.sess.post(url, data)
            self.logger.debug(f"{repoName} issue {req.status_code} {req.text}")

    def checkReview(self):
        hwNum = self.args.hw
        res = {key: {"noReview": 1} for key in self.names.keys()}
        for repoName, users in self.hgroups.items():
            url = f"{self.baseUrl}/repos/{self.orgName}/{repoName}/pulls"
            pulls = self.sess.get(url).json()
            for pull in pulls:
                if not pull["title"].startswith(f"h{hwNum}"): continue
                url = f"{self.baseUrl}/repos/{self.orgName}/{repoName}/pulls/{pull['number']}/reviews"
                self.logger.info(f"{repoName} h{hwNum} get pr: {url}")
                for item in self.sess.get(url).json():
                    stuID = ''.join(
                        [s for s in item['user']['full_name'] if s.isdigit()])
                    if self.ids.get(stuID):
                        name = self.ids[stuID]
                        res[name]["noReview"] = 0
                        self.logger.info(f"{repoName} h{hwNum} {stuID} {name} reviewed")
        return res