|
| 1 | +import pygit2 |
| 2 | +import json |
| 3 | +from datetime import datetime |
| 4 | +from enum import Enum |
| 5 | +import os |
| 6 | +import shutil |
| 7 | + |
| 8 | + |
| 9 | +class TestRepository: |
| 10 | + |
| 11 | + class VulnerabilityType(Enum): |
| 12 | + INTRODUCED = 1 |
| 13 | + FIXED = 2 |
| 14 | + LAST_AFFECTED = 3 |
| 15 | + LIMIT = 4 |
| 16 | + NONE = 5 |
| 17 | + |
| 18 | + __AUTHOR = pygit2. Signature( 'John Smith', '[email protected]') |
| 19 | + __COMMITER = pygit2. Signature( 'John Smith', '[email protected]') |
| 20 | + |
| 21 | + introduced = [] |
| 22 | + fixed = [] |
| 23 | + last_affected = [] |
| 24 | + limit = [] |
| 25 | + |
| 26 | + def __init__(self, name: str): |
| 27 | + self.name = name |
| 28 | + if (os.path.exists(f"osv/testdata/test_repositories/{name}")): |
| 29 | + shutil.rmtree(f"osv/testdata/test_repositories/{name}") |
| 30 | + self.repo = pygit2.init_repository( |
| 31 | + f"osv/testdata/test_repositories/{name}", bare=False) |
| 32 | + |
| 33 | + def add_empty_commit( |
| 34 | + self, |
| 35 | + vulnerability: VulnerabilityType = VulnerabilityType.NONE, |
| 36 | + parents: list[pygit2.Oid] = [], |
| 37 | + message: str = "Empty") -> pygit2.Oid: |
| 38 | + """ |
| 39 | + Adds a empty commit to the repository, tags it with the vulnerability type and adds it to the vulnerability list if specified |
| 40 | + """ |
| 41 | + |
| 42 | + tree = self.repo.TreeBuilder().write() |
| 43 | + |
| 44 | + commit = None |
| 45 | + |
| 46 | + if (self.repo.is_empty): |
| 47 | + commit = self.repo.create_commit('refs/heads/master', self.__AUTHOR, |
| 48 | + self.__COMMITER, message, tree, []) |
| 49 | + else: |
| 50 | + |
| 51 | + created_branch: pygit2.Branch = None |
| 52 | + branch_commit: pygit2.Commit = None |
| 53 | + for parent in parents: |
| 54 | + if (self.__has_children(parent)): |
| 55 | + created_branch = self.repo.create_branch( |
| 56 | + f'branch_{parent.hex}', self.repo.revparse_single(parent.hex)) |
| 57 | + branch_commit = parent |
| 58 | + self.repo.checkout(created_branch.name) |
| 59 | + break |
| 60 | + |
| 61 | + if (created_branch is None): |
| 62 | + commit = self.repo.create_commit('refs/heads/master', self.__AUTHOR, |
| 63 | + self.__COMMITER, message, tree, |
| 64 | + parents) |
| 65 | + else: |
| 66 | + parents.remove(branch_commit) |
| 67 | + parents.insert(0, branch_commit) |
| 68 | + commit = self.repo.create_commit(created_branch.name, self.__AUTHOR, |
| 69 | + self.__COMMITER, message, tree, |
| 70 | + parents) |
| 71 | + |
| 72 | + self.repo.checkout('refs/heads/master') |
| 73 | + |
| 74 | + #self.repo.branches.delete(created_branch.branch_name) |
| 75 | + |
| 76 | + match vulnerability: |
| 77 | + case self.VulnerabilityType.INTRODUCED: |
| 78 | + self.introduced.append(commit.hex) |
| 79 | + case self.VulnerabilityType.FIXED: |
| 80 | + self.fixed.append(commit.hex) |
| 81 | + case self.VulnerabilityType.LAST_AFFECTED: |
| 82 | + self.last_affected.append(commit.hex) |
| 83 | + case self.VulnerabilityType.LIMIT: |
| 84 | + self.limit.append(commit.hex) |
| 85 | + case self.VulnerabilityType.NONE: |
| 86 | + pass |
| 87 | + case _: |
| 88 | + raise ValueError("Invalid vulnerability type") |
| 89 | + return commit |
| 90 | + |
| 91 | + def __has_children(self, commit: pygit2.Commit) -> bool: |
| 92 | + for current_commit in self.repo.walk(self.repo.head.target, |
| 93 | + pygit2.GIT_SORT_TOPOLOGICAL): |
| 94 | + for parent in current_commit.parents: |
| 95 | + if parent.hex == commit.hex: |
| 96 | + return True |
| 97 | + return False |
| 98 | + |
| 99 | + def get_ranges(self): |
| 100 | + """ |
| 101 | + return the ranges of the repository |
| 102 | + """ |
| 103 | + return (self.introduced, self.fixed, self.last_affected, self.limit) |
| 104 | + |
| 105 | + def print_commits(self): |
| 106 | + |
| 107 | + commits = [] |
| 108 | + for ref in self.repo.listall_reference_objects(): |
| 109 | + print(ref.target) |
| 110 | + for commit in self.repo.walk(ref.target, pygit2.GIT_SORT_TIME): |
| 111 | + |
| 112 | + curCommit = { |
| 113 | + 'hash': |
| 114 | + commit.hex, |
| 115 | + 'message': |
| 116 | + commit.message, |
| 117 | + 'commit_date': |
| 118 | + datetime.utcfromtimestamp(commit.commit_time |
| 119 | + ).strftime('%Y-%m-%dT%H:%M:%SZ'), |
| 120 | + 'author_name': |
| 121 | + commit.author.name, |
| 122 | + 'author_email': |
| 123 | + commit.author.email, |
| 124 | + 'parents': [c.hex for c in commit.parents], |
| 125 | + } |
| 126 | + if (curCommit in commits): |
| 127 | + break |
| 128 | + commits.append(curCommit) |
| 129 | + |
| 130 | + print(json.dumps(commits, indent=2)) |
0 commit comments