Skip to content

Commit e8ddeed

Browse files
committed
fix: convert semver to valid OCI tags when publishing to ORAS
1 parent d1f85bb commit e8ddeed

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/cls/IPM/Repo/Oras/PublishService.cls

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
Include %IPM.Common
2+
13
Class %IPM.Repo.Oras.PublishService Extends (%IPM.Repo.Oras.PackageService, %IPM.Repo.IPublishService)
24
{
35

@@ -6,7 +8,7 @@ Method PublishModule(pModule As %IPM.Repo.Remote.ModuleInfo) As %Status
68
Set status = $$$OK
79
Try {
810
Set repo = pModule.Name
9-
Set tag = pModule.VersionString
11+
Set tag = $$$Semver2Tag(pModule.VersionString)
1012

1113
#; Use a temp directory
1214
Set tDir = $$$FileTempDir
@@ -85,7 +87,7 @@ ClassMethod Push(registry As %String, package As %String, namespace As %String,
8587
{
8688
#; Remove the tag from the package name if it exists and add it to tags
8789
Set repo = $piece(package, ":", 1)
88-
Set tag = $piece(package, ":", 2)
90+
Set tag = $$$Semver2Tag($piece(package, ":", 2))
8991
If tag '= "" { Set tags = tags _ "," _ tag }
9092
If tags = "" {
9193
$$$ThrowStatus($$$ERROR($$$GeneralError,"Must specify version."))
@@ -108,31 +110,31 @@ ClassMethod PushPy(registry As %String, package As %String, namespace As %String
108110
import iris
109111
import os, sys
110112
import json
113+
import re
111114

112115
# Get all files in the directory and fully specify path
113-
files = os.listdir(directoryPath)
114-
result = map(lambda x: directoryPath + x, files)
115-
file_paths = list(result)
116-
116+
file_paths = [os.path.join(directoryPath, f) for f in os.listdir(directoryPath)]
117117
client = iris.cls("%IPM.Repo.Oras.PackageService").GetClient(registry, username, password, token, tokenAuthMethod)
118+
regex = re.compile(r"^[a-zA-Z0-9_][a-zA-Z0-9._-]{0,127}$")
118119

119120
# Push once for each tag
120121
for tag in tags.split(","):
121-
if tag == "":
122-
continue
122+
if not regex.search(tag):
123+
raise ValueError(f"Invalid OCI tag: {tag}")
123124

124125
# Annotations are manifest.xml and other metadata
125126
manifest_annotations = json.loads(metadata)
126127

128+
# TODO write a context manager for stdout/stderr redirection
127129
# Suppress console output
128130
sys.stdout = open(os.devnull, "w")
129131

130-
target = iris.cls("%IPM.Repo.Oras.PackageService").GetAPITarget(registry, package, namespace) + ":" + tag
131-
132132
try:
133+
target = iris.cls("%IPM.Repo.Oras.PackageService").GetAPITarget(registry, package, namespace) + ":" + tag
133134
res = client.push(files=file_paths, target=target, disable_path_validation=True, manifest_annotations=manifest_annotations)
134135
except Exception as e:
135-
print("Error: ", repr(e))
136+
print("Error: ", repr(e), file=sys.stderr)
137+
sys.stdout = sys.__stdout__
136138
raise e
137139

138140
# Reenable console output

src/inc/IPM/Common.inc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,8 @@ ROUTINE %IPM.Common [Type=INC]
6666
#def1arg ENDTAG ##Expression($$$ENDTAGQ)
6767

6868
#; Variable to mark deprecation warning has been shown in the current process
69-
#define DeprecationWarned %IPMModuleDeprecatedResource
69+
#define DeprecationWarned %IPMModuleDeprecatedResource
70+
71+
#; Convert module version from/to OCI tag
72+
#def1arg Semver2Tag(%semver) $Replace(%semver,"+","_")
73+
#def1arg Tag2Semver(%tag) $Replace(%tag,"_","+")

0 commit comments

Comments
 (0)