Skip to content
This repository has been archived by the owner on Sep 25, 2024. It is now read-only.

Latest commit

 

History

History
89 lines (73 loc) · 3.4 KB

pattern-matching.md

File metadata and controls

89 lines (73 loc) · 3.4 KB

PatternMatching

Credentials and ManagedFiles supports pattern matching. During pattern matching the pattern defined in PatternMatchable objects is used as regular expression and evaluated against a search value.

This mechanism is used in execMaven and checkoutScm step in order to auto lookup maven settins, npm repository settings, ruby bundler settings or credentials for repository urls.

The class PatternMatcher can be used to get items from a PatternMatchable list.

💡 It is recommended to use the new Generic Configuration mechanism. This mechanism supports yaml-Format and multiple-patterns for one id.

Table of contents

getBestMatch mechanism

Given a JSON at resources/credentials/scm/credentials-example.json with this content

[
  {
    "pattern": "domain.tld[:/]group",
    "id": "group-credentials-id"
  },
  {
    "pattern": "domain.tld[:/]group/specific-project",
    "id": "specific-project-credentials-id"
  }
]

And you loaded and parsed this json by using the JsonLibraryResource and the CredentialParser by using this snippet (without import statements)

// load the json
JsonLibraryResource jsonRes = new JsonLibraryResource((DSL) this.steps, CredentialConstants.SCM_CREDENTIALS_PATH)
JSON credentialJson = jsonRes.load()
// parse the credentials
CredentialParser parser = new CredentialParser()
List<Credential> credentials = parser.parse(credentialJson)
// try to find matching credential and return the credential
PatternMatcher matcher = new PatternMatcher()

Example for basic matching

When you call matcher.getBestMatch with "git@domain.tld:group/project.git"

Credential result = matcher.getBestMatch("git@domain.tld:group/project.git", credentials)

The resulting Credential will be the group-credentials-id object from the json.

Example for specific project matching

When you call matcher.getBestMatch with "git@domain.tld:group/specific-project.git"

Credential result = matcher.getBestMatch("git@domain.tld:group/specific-project.git", credentials)

The result Credential will be the specific-project-credentials-id object from the json.

Even when the first entry matched, the more specific pattern from the second entry had a better match, so this Credential will be returned.

Related classes