io.knowledgelinks.cicd.semantic.versioning.parsers.AbstractGitResponseParser Maven / Gradle / Ivy
package io.knowledgelinks.cicd.semantic.versioning.parsers;
/*-
* #%L
* Semantic Versioning
* %%
* Copyright (C) 2022 - 2023 Knowledgelinks
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import java.util.ArrayList;
import java.util.List;
import io.knowledgelinks.cicd.semantic.versioning.exceptions.BadVersionStringException;
import io.knowledgelinks.cicd.semantic.versioning.exceptions.NoTagException;
import io.knowledgelinks.cicd.semantic.versioning.exceptions.SemanticVersioningException;
import io.knowledgelinks.cicd.semantic.versioning.versions.VersionInfo;
/**
* Converts a git ls-remote text response and converts it to a list of VersionInfo objects by
* calling the parse method.
*
* @author mstabile75
*
*/
public abstract class AbstractGitResponseParser implements GitTagParser {
private VersionParser versionParser;
protected AbstractGitResponseParser() throws SemanticVersioningException {
this(null);
}
protected AbstractGitResponseParser(VersionParser versionParser)
throws SemanticVersioningException {
this.versionParser = versionParser == null ? new StandardVersionPatternParser() : versionParser;
}
@Override
public List parse(String tagText) {
String[] lines = tagText.split(System.lineSeparator());
List tagList = new ArrayList<>();
for (String line : lines) {
try {
tagList.add(processLine(line));
} catch (NoTagException e) {
// lines that are not tags will be ignored
}
}
return tagList;
}
/**
* Takes a line and converts it to a VersionInfo object or throws a NoTagException if the line
* cannot be converted to a VersionInfo object
*
* @param line The line string to process
* @return VersionInfo object for the line
* @throws NoTagException Thrown when the line cannot be converted to a VersionInfo object
*/
protected VersionInfo processLine(String line) throws NoTagException {
if (isTagLine(line)) {
String tagString = parseTagLine(line);
try {
return versionParser.parse(tagString);
} catch (BadVersionStringException e) {
throw (NoTagException) new NoTagException(e.getMessage()).initCause(e);
}
}
throw new NoTagException(
String.format("Line cannot be converted to a versionInfo object: %s", line));
}
/**
* Checks to see if the line contains a git tag
*
* @param line The line string to process
* @return True if the line contains a git tag
*/
protected abstract boolean isTagLine(String line);
/**
* returns the tag name from the supplied line
*
* @param line The line to parse
* @return The tag name of the line
* @throws NoTagException Thrown when the line cannot be converted to a VersionInfo object
*/
protected abstract String parseTagLine(String line) throws NoTagException;
}