Skip to content

Commit 810ceaa

Browse files
authored
Porchctl: Upgrade fallback when there is no original edit task (#320)
* find upstream by git lock fallback Signed-off-by: lapentafd <francesco.lapenta@est.tech> * unit tests Signed-off-by: lapentafd <francesco.lapenta@est.tech> --------- Signed-off-by: lapentafd <francesco.lapenta@est.tech>
1 parent c5a6027 commit 810ceaa

2 files changed

Lines changed: 520 additions & 1 deletion

File tree

pkg/cli/commands/rpkg/upgrade/command.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,15 @@ func (r *runner) findUpstreamName(pr *porchapi.PackageRevision) string {
276276
case porchapi.TaskTypeClone:
277277
return pr.Spec.Tasks[0].Clone.Upstream.UpstreamRef.Name
278278
case porchapi.TaskTypeEdit:
279-
return r.findEditOrigin(pr)
279+
if n := r.findEditOrigin(pr); n != "" {
280+
return n
281+
}
282+
if pr.Status.UpstreamLock != nil {
283+
if up := r.findUpstreamByLock(pr.Status.UpstreamLock); up != nil {
284+
return up.Name
285+
}
286+
}
287+
return ""
280288
case porchapi.TaskTypeUpgrade:
281289
return pr.Spec.Tasks[0].Upgrade.NewUpstream.Name
282290
default:
@@ -301,3 +309,46 @@ func (r *runner) findEditOrigin(currentPr *porchapi.PackageRevision) string {
301309
}
302310
return ""
303311
}
312+
313+
func (r *runner) findUpstreamByLock(lock *porchapi.UpstreamLock) *porchapi.PackageRevision {
314+
if lock == nil || lock.Git == nil {
315+
return nil
316+
}
317+
318+
target := lock.Git
319+
var bestMatch *porchapi.PackageRevision
320+
321+
for i := range r.prs {
322+
candidate := r.prs[i]
323+
324+
if !r.matchesTarget(candidate, target) {
325+
continue
326+
}
327+
328+
if target.Ref != "" && candidate.Status.UpstreamLock.Git.Ref == target.Ref {
329+
if bestMatch == nil || candidate.Spec.Revision > bestMatch.Spec.Revision {
330+
tmp := candidate
331+
bestMatch = &tmp
332+
}
333+
}
334+
}
335+
336+
return bestMatch
337+
}
338+
339+
func (r *runner) matchesTarget(candidate porchapi.PackageRevision, target *porchapi.GitLock) bool {
340+
if !candidate.IsPublished() {
341+
return false
342+
}
343+
upstream := candidate.Status.UpstreamLock
344+
if upstream == nil || upstream.Git == nil {
345+
return false
346+
}
347+
348+
cGit := upstream.Git
349+
if cGit.Repo != target.Repo || cGit.Directory != target.Directory {
350+
return false
351+
}
352+
353+
return true
354+
}

0 commit comments

Comments
 (0)