(override *AmbiguousPackageVersionOverride, steps []*StepPackageVersion)
| 339 | } |
| 340 | |
| 341 | func ResolvePackageOverride(override *AmbiguousPackageVersionOverride, steps []*StepPackageVersion) (*PackageVersionOverride, error) { |
| 342 | // shortcut for wildcard matches; these match everything so we don't need to do any work |
| 343 | if override.PackageReferenceName == "" && override.ActionNameOrPackageID == "" { |
| 344 | return &PackageVersionOverride{ |
| 345 | ActionName: "", |
| 346 | PackageID: "", |
| 347 | PackageReferenceName: "", |
| 348 | Version: override.Version, |
| 349 | }, nil |
| 350 | } |
| 351 | |
| 352 | actionNameOrPackageID := override.ActionNameOrPackageID |
| 353 | |
| 354 | // it could be either a stepname or a package ID; match against the list of packages to try and guess. |
| 355 | // logic matching the server: |
| 356 | // - exact match on stepName + refName |
| 357 | // - then exact match on packageId + refName |
| 358 | // - then match on * + refName |
| 359 | // - then match on stepName + * |
| 360 | // - then match on packageID + * |
| 361 | type match struct { |
| 362 | priority int |
| 363 | actionName string // if set we matched on actionName, else we didn't |
| 364 | packageID string // if set we matched on packageID, else we didn't |
| 365 | packageReferenceName string // if set we matched on packageReferenceName, else we didn't |
| 366 | } |
| 367 | |
| 368 | matches := make([]match, 0, 2) // common case is likely to be 2; if we have a packageID then we may match both exactly and partially on the ID depending on referenceName |
| 369 | for _, p := range steps { |
| 370 | if p.ActionName != "" && p.ActionName == actionNameOrPackageID { |
| 371 | if p.PackageReferenceName == override.PackageReferenceName { |
| 372 | matches = append(matches, match{priority: 100, actionName: p.ActionName, packageReferenceName: p.PackageReferenceName}) |
| 373 | } else { |
| 374 | matches = append(matches, match{priority: 50, actionName: p.ActionName}) |
| 375 | } |
| 376 | } else if p.PackageID != "" && p.PackageID == actionNameOrPackageID { |
| 377 | if p.PackageReferenceName == override.PackageReferenceName { |
| 378 | matches = append(matches, match{priority: 90, packageID: p.PackageID, packageReferenceName: p.PackageReferenceName}) |
| 379 | } else { |
| 380 | matches = append(matches, match{priority: 40, packageID: p.PackageID}) |
| 381 | } |
| 382 | } else if p.PackageReferenceName != "" && p.PackageReferenceName == override.PackageReferenceName { |
| 383 | matches = append(matches, match{priority: 80, packageReferenceName: p.PackageReferenceName}) |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | if len(matches) == 0 { |
| 388 | return nil, fmt.Errorf("could not resolve step name or package matching %s", actionNameOrPackageID) |
| 389 | } |
| 390 | sort.SliceStable(matches, func(i, j int) bool { // want a stable sort so if there's more than one possible match we pick the first one |
| 391 | return matches[i].priority > matches[j].priority |
| 392 | }) |
| 393 | |
| 394 | return &PackageVersionOverride{ |
| 395 | ActionName: matches[0].actionName, |
| 396 | PackageID: matches[0].packageID, |
| 397 | PackageReferenceName: matches[0].packageReferenceName, |
| 398 | Version: override.Version, |
no outgoing calls
no test coverage detected