| 10 | |
| 11 | |
| 12 | class GithubResult(object): |
| 13 | def __init__(self, item): |
| 14 | self.raw_data = item |
| 15 | self.git_url = item["git_url"] |
| 16 | self.html_url = item["html_url"] |
| 17 | self.repo_full_name = item["repository"]["full_name"] |
| 18 | self.path = item["path"] |
| 19 | self.hash_md5 = gen_md5(self.repo_full_name + "/" + item["path"]) |
| 20 | self._commit_date = None # 记录文件最后一次 Commit 时间 |
| 21 | self._content = None |
| 22 | |
| 23 | def __str__(self): |
| 24 | return "{} {}".format(self.repo_full_name, self.path) |
| 25 | |
| 26 | def __hash__(self): |
| 27 | return self.hash_md5 |
| 28 | |
| 29 | def __eq__(self, other): |
| 30 | return self.hash_md5 == other.hash_md5 |
| 31 | |
| 32 | def __repr__(self): |
| 33 | return "<GithubResult>{} {}".format(self.hash_md5, str(self)) |
| 34 | |
| 35 | @property |
| 36 | def content(self): |
| 37 | if self._content is None: |
| 38 | try: |
| 39 | content_base64 = github_client(self.git_url)["content"] |
| 40 | decode_bytes = base64.decodebytes(content_base64.encode("utf-8")) |
| 41 | self._content = decode_bytes.decode("utf-8", errors="replace") |
| 42 | except Exception as e: |
| 43 | logger.info("error on {}".format(self.git_url)) |
| 44 | logger.exception(e) |
| 45 | self._content = "" |
| 46 | |
| 47 | return self._content |
| 48 | else: |
| 49 | return self._content |
| 50 | |
| 51 | @property |
| 52 | def commit_date(self): |
| 53 | if self._commit_date is None: |
| 54 | commit_url = "https://api.github.com/repos/{}/commits".format( |
| 55 | self.repo_full_name) |
| 56 | params = { |
| 57 | "per_page": 1, |
| 58 | "path": self.path |
| 59 | } |
| 60 | try: |
| 61 | |
| 62 | commit_info = github_client(commit_url, params=params) |
| 63 | assert commit_info |
| 64 | |
| 65 | # 先保存为字符串吧 |
| 66 | self._commit_date = str(parse_datetime(commit_info[0]["commit"]["author"]["date"])) |
| 67 | except Exception as e: |
| 68 | logger.info("error on {}, {}".format(commit_url, self.path)) |
| 69 | logger.exception(e) |
no outgoing calls
no test coverage detected