|
| 1 | +// https://github.com/SocketDev/socket-python-cli/blob/6d4fc56faee68d3a4764f1f80f84710635bdaf05/socketsecurity/core/classes.py |
| 2 | +import { components } from '@socketsecurity/sdk/types/api' |
| 3 | + |
| 4 | +type IntroducedBy = [string, string][] |
| 5 | + |
| 6 | +export class Alert { |
| 7 | + key = '' |
| 8 | + type = '' |
| 9 | + severity = '' |
| 10 | + category = '' |
| 11 | + props = {} |
| 12 | + |
| 13 | + constructor(arg: Partial<Alert> = {}) { |
| 14 | + this.key = arg.key ?? this.key |
| 15 | + this.type = arg.type ?? this.type |
| 16 | + this.severity = arg.severity ?? this.severity |
| 17 | + this.category = arg.category ?? this.category |
| 18 | + this.props = arg.props ?? this.props |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +export class Comment { |
| 23 | + id = 0 |
| 24 | + body = '' |
| 25 | + body_list: string[] = [] |
| 26 | + |
| 27 | + constructor(arg: Comment) { |
| 28 | + this.id = arg.id ?? this.id |
| 29 | + this.body = arg.body ?? this.body |
| 30 | + this.body_list = arg.body_list ?? this.body_list |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +export class Diff { |
| 35 | + newPackages: Purl[] = [] |
| 36 | + newCapabilities: Record<string, any> = {} |
| 37 | + removedPackages: Purl[] = [] |
| 38 | + newAlerts: Issue[] = [] |
| 39 | + id = '' |
| 40 | + sbom = '' |
| 41 | + packages: Record<string, Package> = {} |
| 42 | + reportUrl = '' |
| 43 | + diffUrl = '' |
| 44 | +} |
| 45 | + |
| 46 | +export class FullScan { |
| 47 | + id = '' |
| 48 | + created_at = '' |
| 49 | + updated_at = '' |
| 50 | + organizationId = '' |
| 51 | + repositoryId = '' |
| 52 | + branch = '' |
| 53 | + commit_message = '' |
| 54 | + commit_hash = '' |
| 55 | + pull_request = 0 |
| 56 | + sbom_artifacts: components['schemas']['SocketArtifact'][] = [] |
| 57 | + packages = {} |
| 58 | + |
| 59 | + constructor(obj: Partial<FullScan> = {}) { |
| 60 | + this.id = obj.id ?? this.id |
| 61 | + this.created_at = obj.created_at ?? this.created_at |
| 62 | + this.updated_at = obj.updated_at ?? this.updated_at |
| 63 | + this.organizationId = obj.organizationId ?? this.organizationId |
| 64 | + this.repositoryId = obj.repositoryId ?? this.repositoryId |
| 65 | + this.branch = obj.branch ?? this.branch |
| 66 | + this.commit_message = obj.commit_message ?? this.commit_message |
| 67 | + this.commit_hash = obj.commit_hash ?? this.commit_hash |
| 68 | + this.pull_request = obj.pull_request ?? this.pull_request |
| 69 | + this.sbom_artifacts = obj.sbom_artifacts ?? this.sbom_artifacts |
| 70 | + this.packages = obj.packages ?? this.packages |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +export class Issue { |
| 75 | + pkg_type = '' |
| 76 | + pkg_name = '' |
| 77 | + pkg_version = '' |
| 78 | + category = '' |
| 79 | + type = '' |
| 80 | + severity = '' |
| 81 | + pkg_id = '' |
| 82 | + props = {} |
| 83 | + key = '' |
| 84 | + error = false |
| 85 | + warn = false |
| 86 | + ignore = false |
| 87 | + monitor = false |
| 88 | + description = '' |
| 89 | + title = '' |
| 90 | + emoji = '' |
| 91 | + next_step_title = '' |
| 92 | + suggestion = '' |
| 93 | + introduced_by: IntroducedBy = [] |
| 94 | + manifests = '' |
| 95 | + url = '' |
| 96 | + purl = '' |
| 97 | + |
| 98 | + constructor(arg: { |
| 99 | + pkg_type: string | undefined |
| 100 | + pkg_name: string | undefined |
| 101 | + pkg_version: string | undefined |
| 102 | + type: string | undefined |
| 103 | + severity: string | undefined |
| 104 | + pkg_id: string | undefined |
| 105 | + props: Record<string, any> | undefined |
| 106 | + key: string | undefined |
| 107 | + error: boolean | undefined |
| 108 | + warn: boolean | undefined |
| 109 | + ignore: boolean | undefined |
| 110 | + monitor: boolean | undefined |
| 111 | + description: string | undefined |
| 112 | + title: string | undefined |
| 113 | + next_step_title: string | undefined |
| 114 | + suggestion: string | undefined |
| 115 | + introduced_by: IntroducedBy | undefined |
| 116 | + url: string | undefined |
| 117 | + purl: string | undefined |
| 118 | + }) { |
| 119 | + this.pkg_type = arg.pkg_type ?? this.pkg_type |
| 120 | + this.pkg_name = arg.pkg_name ?? this.pkg_name |
| 121 | + this.pkg_version = arg.pkg_version ?? this.pkg_version |
| 122 | + this.type = arg.type ?? this.type |
| 123 | + this.severity = arg.severity ?? this.severity |
| 124 | + this.pkg_id = arg.pkg_id ?? this.pkg_id |
| 125 | + this.props = arg.props ?? this.props |
| 126 | + this.key = arg.key ?? this.key |
| 127 | + this.error = arg.error ?? this.error |
| 128 | + this.warn = arg.warn ?? this.warn |
| 129 | + this.ignore = arg.ignore ?? this.ignore |
| 130 | + this.monitor = arg.monitor ?? this.monitor |
| 131 | + this.description = arg.description ?? this.description |
| 132 | + this.title = arg.title ?? this.title |
| 133 | + this.next_step_title = arg.next_step_title ?? this.next_step_title |
| 134 | + this.suggestion = arg.suggestion ?? this.suggestion |
| 135 | + |
| 136 | + if (arg.introduced_by) { |
| 137 | + const arr = [] |
| 138 | + for (const item of arg.introduced_by) { |
| 139 | + const [, manifest] = item |
| 140 | + arr.push(manifest) |
| 141 | + } |
| 142 | + this.manifests = arr.join(';') |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +export class Package { |
| 148 | + type = '' |
| 149 | + name = '' |
| 150 | + version = '' |
| 151 | + release = '' |
| 152 | + id = '' |
| 153 | + direct = false |
| 154 | + manifestFiles: { file: string }[] = [] |
| 155 | + author: string[] = [] |
| 156 | + size = 0 |
| 157 | + score: Score |
| 158 | + scores = {} |
| 159 | + alerts: NonNullable<components['schemas']['SocketArtifact']['alerts']> = [] |
| 160 | + alert_counts = {} |
| 161 | + topLevelAncestors: string[] = [] |
| 162 | + url = '' |
| 163 | + transitives = 0 |
| 164 | + license = 'NoLicenseFound' |
| 165 | + license_text = '' |
| 166 | + purl = '' |
| 167 | + |
| 168 | + constructor(arg: { |
| 169 | + type: string | undefined |
| 170 | + name: string | undefined |
| 171 | + version: string | undefined |
| 172 | + release: string | undefined |
| 173 | + id: string | undefined |
| 174 | + direct: boolean | undefined |
| 175 | + manifestFiles: { file: string }[] | undefined |
| 176 | + author: string[] | undefined |
| 177 | + size: number | undefined |
| 178 | + score: Score | undefined |
| 179 | + alerts: components['schemas']['SocketArtifact']['alerts'] | undefined |
| 180 | + topLevelAncestors: string[] | undefined |
| 181 | + license: string | undefined |
| 182 | + }) { |
| 183 | + this.type = arg.type ?? this.type |
| 184 | + this.name = arg.name ?? this.name |
| 185 | + this.version = arg.version ?? this.version |
| 186 | + this.release = arg.release ?? this.release |
| 187 | + this.id = arg.id ?? this.id |
| 188 | + this.manifestFiles = arg.manifestFiles ?? this.manifestFiles |
| 189 | + this.author = arg.author ?? this.author |
| 190 | + this.size = arg.size ?? this.size |
| 191 | + this.alerts = arg.alerts ?? this.alerts |
| 192 | + this.topLevelAncestors = arg.topLevelAncestors ?? this.topLevelAncestors |
| 193 | + this.license = arg.license ?? this.license |
| 194 | + |
| 195 | + this.url = `https://socket.dev/${this.type}/package/${this.name}/overview/${this.version}` |
| 196 | + this.score = new Score( |
| 197 | + arg.score ?? { |
| 198 | + supplyChain: 0, |
| 199 | + quality: 0, |
| 200 | + license: 0, |
| 201 | + overall: 0, |
| 202 | + vulnerability: 0 |
| 203 | + } |
| 204 | + ) |
| 205 | + this.alert_counts = { |
| 206 | + critical: 0, |
| 207 | + high: 0, |
| 208 | + middle: 0, |
| 209 | + low: 0 |
| 210 | + } |
| 211 | + this.purl = `${this.type}/${this.name}@${this.version}` |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +export class Purl { |
| 216 | + id = '' |
| 217 | + name = '' |
| 218 | + version = '' |
| 219 | + ecosystem = '' |
| 220 | + direct = false |
| 221 | + author: string[] = [] |
| 222 | + size = 0 |
| 223 | + transitives = 0 |
| 224 | + introduced_by: IntroducedBy = [] |
| 225 | + capabilities: string[] = [] |
| 226 | + // is_new = false |
| 227 | + author_url = '' |
| 228 | + url = '' |
| 229 | + purl = '' |
| 230 | + |
| 231 | + constructor(arg: { |
| 232 | + id: string | undefined |
| 233 | + name: string | undefined |
| 234 | + version: string | undefined |
| 235 | + ecosystem: string | undefined |
| 236 | + direct: boolean | undefined |
| 237 | + introduced_by: IntroducedBy | undefined |
| 238 | + author: string[] | undefined |
| 239 | + size: number | undefined |
| 240 | + transitives: number | undefined |
| 241 | + url: string | undefined |
| 242 | + purl: string | undefined |
| 243 | + }) { |
| 244 | + this.id = arg.id ?? this.id |
| 245 | + this.name = arg.name ?? this.name |
| 246 | + this.version = arg.version ?? this.version |
| 247 | + this.ecosystem = arg.ecosystem ?? this.ecosystem |
| 248 | + this.direct = arg.direct ?? this.direct |
| 249 | + this.author = arg.author ?? this.author |
| 250 | + this.size = arg.size ?? this.size |
| 251 | + this.transitives = arg.transitives ?? this.transitives |
| 252 | + this.introduced_by = arg.introduced_by ?? this.introduced_by |
| 253 | + this.url = arg.url ?? this.url |
| 254 | + this.purl = arg.purl ?? this.purl |
| 255 | + |
| 256 | + this.author_url = this.generateAuthorData(this.author, this.ecosystem) |
| 257 | + } |
| 258 | + |
| 259 | + private generateAuthorData(authors: string[], ecosystem: string): string { |
| 260 | + const arr = [] |
| 261 | + for (const author of authors) { |
| 262 | + const url = `https://socket.dev/${ecosystem}/user/${author}` |
| 263 | + arr.push(`[${author}](${url})`) |
| 264 | + } |
| 265 | + return arr.join(',') |
| 266 | + } |
| 267 | +} |
| 268 | + |
| 269 | +export class Score { |
| 270 | + supplyChain = 0 |
| 271 | + quality = 0 |
| 272 | + license = 0 |
| 273 | + overall = 0 |
| 274 | + vulnerability = 0 |
| 275 | + |
| 276 | + constructor(arg: Score) { |
| 277 | + this.supplyChain = (arg.supplyChain ?? 0) * 100 |
| 278 | + this.quality = (arg.quality ?? 0) * 100 |
| 279 | + this.license = (arg.license ?? 0) * 100 |
| 280 | + this.overall = (arg.overall ?? 0) * 100 |
| 281 | + this.vulnerability = (arg.vulnerability ?? 0) * 100 |
| 282 | + } |
| 283 | +} |
0 commit comments