Skip to content

Commit 5ade893

Browse files
fix: truncate api names for old versions (#405)
We append the stability of apis before the pivot date This is made after the truncation to 63 characters Which results in invalid names if api names are too long This refactors the code to truncate the name after the full prefix and suffix are built
1 parent 2eba7ac commit 5ade893

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

internal/backstage/backstage.go

+36-21
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func LoadCatalogInfo(r io.Reader) (*CatalogInfo, error) {
169169
if catalog.service != nil {
170170
var apiNames []string
171171
for _, apiName := range catalog.serviceComponent.Spec.ProvidesAPIs {
172-
// Preserve manually added entries, things are are NOT vervet APIs
172+
// Preserve manually added entries, things that are NOT vervet APIs
173173
if _, ok := vervetAPINames[apiName]; !ok {
174174
apiNames = append(apiNames, apiName)
175175
}
@@ -273,27 +273,22 @@ func (c *CatalogInfo) vervetAPI(doc *vervet.Document, root string, pivotDate tim
273273
return nil, err
274274
}
275275

276-
name_suffix := fmt.Sprintf("_%s_%s", toBackstageName(apiName), version.DateString())
277-
// Backstage names can only be a maximum of 63 characters
278-
name_len := 63 - len(name_suffix)
279-
name := fmt.Sprintf("%."+strconv.Itoa(name_len)+"s%s", toBackstageName(doc.Info.Title), name_suffix)
280-
title := doc.Info.Title + " " + version.DateString()
276+
apiNameBS := toBackstageName(apiName)
277+
docTitleBS := toBackstageName(doc.Info.Title)
278+
dateStr := version.DateString()
279+
nameSuffix := fmt.Sprintf("_%s_%s", apiNameBS, dateStr)
280+
281+
title := doc.Info.Title + " " + dateStr
281282
labels := map[string]string{
282-
snykApiVersionDate: version.DateString(),
283+
snykApiVersionDate: dateStr,
283284
}
284285
tags := []string{version.Date.Format("2006-01")}
285-
spec := APISpec{
286-
Type: "openapi",
287-
Owner: c.serviceComponent.Spec.Owner,
288-
Lifecycle: "production",
289-
Definition: DefinitionRef{
290-
Text: ref,
291-
},
292-
}
286+
specLifecycle := "production"
293287

294288
// Specs generated after the pivot date have per operation stability, so
295289
// there is no global stability for the whole document. To preserve
296290
// backwards compatibility we still output metadata for the older specs.
291+
var name string
297292
if version.Date.Before(pivotDate) {
298293
lifecycle := version.LifecycleAt(time.Time{})
299294
var backstageLifecycle string
@@ -302,13 +297,26 @@ func (c *CatalogInfo) vervetAPI(doc *vervet.Document, root string, pivotDate tim
302297
} else {
303298
backstageLifecycle = lifecycle.String()
304299
}
300+
specLifecycle = backstageLifecycle
301+
302+
stabilityStr := version.Stability.String()
303+
stabilitySuffix := "_" + stabilityStr
304+
305+
// Backstage names can only be a maximum of 63 characters
306+
availableTitleLen := 63 - len(nameSuffix) - len(stabilitySuffix)
307+
if availableTitleLen < 0 {
308+
availableTitleLen = 0
309+
}
310+
name = fmt.Sprintf("%."+strconv.Itoa(availableTitleLen)+"s%s%s", docTitleBS, nameSuffix, stabilitySuffix)
311+
title = title + " " + stabilityStr
305312

306-
name = name + "_" + version.Stability.String()
307-
title = title + " " + version.Stability.String()
308-
labels[snykApiStability] = version.Stability.String()
313+
labels[snykApiStability] = stabilityStr
309314
labels[snykApiLifecycle] = lifecycle.String()
310-
tags = append(tags, version.Stability.String(), lifecycle.String())
311-
spec.Lifecycle = backstageLifecycle
315+
tags = append(tags, stabilityStr, lifecycle.String())
316+
} else {
317+
// Backstage names can only be a maximum of 63 characters
318+
availableTitleLen := 63 - len(nameSuffix)
319+
name = fmt.Sprintf("%."+strconv.Itoa(availableTitleLen)+"s%s", docTitleBS, nameSuffix)
312320
}
313321

314322
return &API{
@@ -324,7 +332,14 @@ func (c *CatalogInfo) vervetAPI(doc *vervet.Document, root string, pivotDate tim
324332
snykApiGeneratedBy: "vervet",
325333
},
326334
},
327-
Spec: spec,
335+
Spec: APISpec{
336+
Type: "openapi",
337+
Owner: c.serviceComponent.Spec.Owner,
338+
Lifecycle: specLifecycle,
339+
Definition: DefinitionRef{
340+
Text: ref,
341+
},
342+
},
328343
}, nil
329344
}
330345

0 commit comments

Comments
 (0)