Skip to content

Commit

Permalink
feat: support updating constants in go package
Browse files Browse the repository at this point in the history
Signed-off-by: Gareth Jones <[email protected]>
  • Loading branch information
G-Rath committed Oct 30, 2024
1 parent 486019f commit 9c1f922
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bindings/go/osvschema/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@ const (
EcosystemAndroid Ecosystem = "Android"
EcosystemBioconductor Ecosystem = "Bioconductor"
EcosystemBitnami Ecosystem = "Bitnami"
EcosystemChainguard Ecosystem = "Chainguard"
EcosystemConanCenter Ecosystem = "ConanCenter"
EcosystemCRAN Ecosystem = "CRAN"
EcosystemCratesIO Ecosystem = "crates.io"
EcosystemDebian Ecosystem = "Debian"
EcosystemGHC Ecosystem = "GHC"
EcosystemGitHubActions Ecosystem = "GitHub Actions"
EcosystemGo Ecosystem = "Go"
EcosystemHackage Ecosystem = "Hackage"
EcosystemHex Ecosystem = "Hex"
EcosystemLinux Ecosystem = "Linux"
EcosystemMageia Ecosystem = "Mageia"
EcosystemMaven Ecosystem = "Maven"
EcosystemNPM Ecosystem = "npm"
EcosystemNuGet Ecosystem = "NuGet"
EcosystemOpenSUSE Ecosystem = "openSUSE"
EcosystemOSSFuzz Ecosystem = "OSS-Fuzz"
EcosystemPackagist Ecosystem = "Packagist"
EcosystemPhotonOS Ecosystem = "Photon OS"
Expand All @@ -27,8 +32,10 @@ const (
EcosystemRedHat Ecosystem = "Red Hat"
EcosystemRockyLinux Ecosystem = "Rocky Linux"
EcosystemRubyGems Ecosystem = "RubyGems"
EcosystemSUSE Ecosystem = "SUSE"
EcosystemSwiftURL Ecosystem = "SwiftURL"
EcosystemUbuntu Ecosystem = "Ubuntu"
EcosystemWolfi Ecosystem = "Wolfi"
)

type SeverityType string
Expand Down
60 changes: 60 additions & 0 deletions scripts/update-ecosystems-lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,65 @@ def update_schema_md():
open('docs/schema.md', 'w').write(md)


def convert_to_go_constant_name(name: str) -> str:
"""
Converts the "human" name of an ecosystem to a Go constant name, mostly
by removing spaces and dashes and converting to PascalCase.
Some ecosystems have special cases, like "crates.io" which is converted to "CratesIO".
:param name:
:return:
"""
if name == 'crates.io':
return 'EcosystemCratesIO'

if name == 'npm':
return 'EcosystemNPM'

name = name[0].upper() + name[1:]
name = name.replace('-', '').replace(' ', '')

return f'Ecosystem{name}'


def generate_ecosystems_go_constants() -> str:
"""
Generates a list of Go constants, with a constant per ecosystem
:return:
"""

constants = list(map(lambda x: (convert_to_go_constant_name(x), x), ecosystems.keys()))
longest = max(map(lambda x: len(x[0]), constants))

code = 'const (\n'
for constant, name in constants:
code += f'\t{constant.ljust(longest)} Ecosystem = "{name}"\n'
code += ')\n'

return code


def update_go_constants():
"""
Updates the Go constants with the list of ecosystems
:return:
"""
go = open('bindings/go/osvschema/constants.go').read()

ecosystem_constants_start = go.index('type Ecosystem string\n\nconst (\n')
ecosystems_constants_end = go.index(')', ecosystem_constants_start)

go = '{0}{1}\n\n{2}{3}'.format(
go[:ecosystem_constants_start],
'type Ecosystem string',
generate_ecosystems_go_constants(),
go[ecosystems_constants_end + 2:]
)

open('bindings/go/osvschema/constants.go', 'w').write(go)


update_go_constants()
update_json_schema()
update_schema_md()

0 comments on commit 9c1f922

Please sign in to comment.