|
| 1 | +import sys, os, json, subprocess |
| 2 | +from urllib.request import urlopen |
| 3 | +from rich import print |
| 4 | + |
| 5 | +class OSSPMaster(): |
| 6 | + |
| 7 | + def __init__(self, id: int, name: str) -> None: |
| 8 | + self.id = id |
| 9 | + self.name = name |
| 10 | + |
| 11 | + def check_issue_exists(self,id) -> None: |
| 12 | + try: |
| 13 | + urlopen('https://github.com/Be-Secure/Be-Secure/issues/'+str(id)) |
| 14 | + except Exception as e: |
| 15 | + exc_type, exc_obj, exc_tb = sys.exc_info() |
| 16 | + fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 17 | + print(exc_type, fname, exc_tb.tb_lineno) |
| 18 | + print("Could not find issue with id : "+str(id)) |
| 19 | + sys.exit() |
| 20 | + |
| 21 | + def check_issue_related_to_project(self): |
| 22 | + |
| 23 | + json_data = json.loads(urlopen(f'https://api.github.com/repos/Be-Secure/Be-Secure/issues/{self.id}').read()) |
| 24 | + issue_title = json_data["title"] |
| 25 | + project_name = str(str(issue_title).split(":")[1]).replace(" ","") |
| 26 | + if project_name != self.name: |
| 27 | + print(f"[bold red]Alert! [yellow]Mismatch issue_id-project : [green] Issue id {self.id} does not match the project {self.name}") |
| 28 | + sys.exit() |
| 29 | + else: |
| 30 | + pass |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + def check_repo_exists(self,name) -> None: |
| 35 | + try: |
| 36 | + urlopen('https://api.github.com/repos/Be-Secure/'+name) |
| 37 | + except Exception as e: |
| 38 | + exc_type, exc_obj, exc_tb = sys.exc_info() |
| 39 | + fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 40 | + print(exc_type, fname, exc_tb.tb_lineno) |
| 41 | + print("Could not find "+ name +" under Be-Secure") |
| 42 | + sys.exit() |
| 43 | + |
| 44 | + def write_tech_stack(self,bes_id): |
| 45 | + raw_data = urlopen("https://api.github.com/repos/Be-Secure/Be-Secure/issues/"+str(bes_id)) |
| 46 | + |
| 47 | + data = json.loads(raw_data.read()) |
| 48 | + |
| 49 | + body_data = iter(data["body"].splitlines()) |
| 50 | + found = "false" |
| 51 | + for i in body_data: |
| 52 | + if i == "### Tech Stack": |
| 53 | + found = "true" |
| 54 | + continue |
| 55 | + if len(i.strip()) == 0: |
| 56 | + continue |
| 57 | + if len(i.strip()) != 0 and found == "true": |
| 58 | + s = str(i.split(" [")[1]) |
| 59 | + s = str(s.split("]")[0]) |
| 60 | + break |
| 61 | + return s |
| 62 | + def write_project_repos_data(self,project_data): |
| 63 | + project_repos = { |
| 64 | + "main_github_url": "", |
| 65 | + "main_bes_url": "", |
| 66 | + "all_projects": [ |
| 67 | + { |
| 68 | + "id": 0, |
| 69 | + "name": "", |
| 70 | + "url": "" |
| 71 | + } |
| 72 | + ], |
| 73 | + "all_bes_repos": [ |
| 74 | + { |
| 75 | + "id": 0, |
| 76 | + "name": "", |
| 77 | + "url": "" |
| 78 | + } |
| 79 | + |
| 80 | + ] |
| 81 | + } |
| 82 | + project_repos.update({"main_github_url": project_data["parent"]["html_url"]}) |
| 83 | + project_repos.update({"main_bes_url": project_data["html_url"]}) |
| 84 | + project_repos["all_projects"][0]["id"] = project_data["parent"]["id"] |
| 85 | + project_repos["all_projects"][0]["name"] = project_data["parent"]["full_name"] |
| 86 | + project_repos["all_projects"][0]["url"] = project_data["parent"]["html_url"] |
| 87 | + project_repos["all_bes_repos"][0]["id"] = project_data["id"] |
| 88 | + project_repos["all_bes_repos"][0]["name"] = project_data["full_name"] |
| 89 | + project_repos["all_bes_repos"][0]["url"] = project_data["html_url"] |
| 90 | + return project_repos |
| 91 | + |
| 92 | + def write_tags(self, bes_id): |
| 93 | + url = 'https://api.github.com/repos/Be-Secure/Be-Secure/issues/'+str(bes_id)+'/labels' |
| 94 | + tags_json_data = urlopen(url) |
| 95 | + tags_dict = json.loads(tags_json_data.read()) |
| 96 | + tags = [] |
| 97 | + for i in range(len(tags_dict)): |
| 98 | + tags.append(tags_dict[i]["name"]) |
| 99 | + return tags |
| 100 | + |
| 101 | + def write_languages(self,name): |
| 102 | + raw_data = urlopen("https://api.github.com/repos/Be-Secure/"+name+"/languages") |
| 103 | + data = json.loads(raw_data.read()) |
| 104 | + return data |
| 105 | + |
| 106 | + def write_to_ossp_master(self, f, ossp_master_json, data, overwrite: bool): |
| 107 | + if overwrite: |
| 108 | + for i in range(len(ossp_master_json["items"])): |
| 109 | + if ossp_master_json["items"][i]["id"] == self.id: |
| 110 | + ossp_master_json["items"][i] = data |
| 111 | + break |
| 112 | + else: |
| 113 | + ossp_master_json["items"].append(data) |
| 114 | + f.seek(0) |
| 115 | + f.write(json.dumps(ossp_master_json, indent=4)) |
| 116 | + f.truncate() |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + def GenerateOsspMaster(self, overwrite: bool): |
| 121 | + self.check_issue_exists(self.id) |
| 122 | + self.check_repo_exists(self.name) |
| 123 | + self.check_issue_related_to_project() |
| 124 | + osspoi_dir = os.environ['OSSPOI_DIR'] |
| 125 | + namespace = os.environ['GITHUB_ORG'] |
| 126 | + token = os.environ['GITHUB_AUTH_TOKEN'] |
| 127 | + write_flag = True |
| 128 | + f = open(osspoi_dir+"/OSSP-Master.json", "r+") |
| 129 | + ossp_master_json = json.load(f) |
| 130 | + if not overwrite: |
| 131 | + for i in range(len(ossp_master_json["items"])): |
| 132 | + if ossp_master_json["items"][i]["id"] == self.id: |
| 133 | + print("[bold red]Alert! [green]Entry for "+str(self.id)+"-"+self.name+" already present under OSSP-Master.json") |
| 134 | + write_flag = False |
| 135 | + break |
| 136 | + else: |
| 137 | + write_flag = True |
| 138 | + if write_flag: |
| 139 | + # proc = subprocess.Popen([f'curl -L -H "Accept: application/vnd.github+json" -H "Authorization: Bearer <YOUR-TOKEN>"-H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com//{namespace}/{self.name}'], stdout=subprocess.PIPE, shell=True) |
| 140 | + # (out, err) = proc.communicate() |
| 141 | + url_data = urlopen('https://api.github.com/repos/Be-Secure/'+self.name) |
| 142 | + project_data = json.loads(url_data.read()) |
| 143 | + ossp_data = json.loads('{}') |
| 144 | + repo_keys = [ "id", "bes_tracking_id", "issue_url", "name", "full_name", "description", "bes_technology_stack", "watchers_count", "forks_count", "stargazers_count", "size", "open_issues", "created_at", "updated_at", "pushed_at", "git_url", "clone_url", "html_url", "homepage", "owner", "project_repos", "license", "language", "tags" ] |
| 145 | + for i in repo_keys: |
| 146 | + if i == "id" or i == "bes_tracking_id": |
| 147 | + ossp_data[i] = self.id |
| 148 | + elif i == "issue_url": |
| 149 | + ossp_data[i] = 'https://github.com/Be-Secure/Be-Secure/issues/'+str(self.id) |
| 150 | + elif i == "bes_technology_stack": |
| 151 | + ossp_data[i] = self.write_tech_stack(self.id) |
| 152 | + elif i == "project_repos": |
| 153 | + ossp_data[i] = self.write_project_repos_data(project_data) |
| 154 | + elif i == "tags": |
| 155 | + ossp_data[i] = self.write_tags(self.id) |
| 156 | + elif i == "language": |
| 157 | + ossp_data[i] = self.write_languages(self.name) |
| 158 | + else: |
| 159 | + ossp_data[i] = project_data[i] |
| 160 | + self.write_to_ossp_master(f, ossp_master_json, ossp_data, overwrite) |
| 161 | + f.close() |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + |
0 commit comments