All Downloads are FREE. Search and download functionalities are using the official Maven repository.

team.yi.tools.semanticcommit.model.ReleaseCommit Maven / Gradle / Ivy

package team.yi.tools.semanticcommit.model;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import team.yi.tools.semanticcommit.parser.ParserConstants;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@SuppressWarnings("PMD.TooManyFields")
@Getter
@EqualsAndHashCode(callSuper = true)
public class ReleaseCommit extends GitCommit implements Serializable {
    private static final long serialVersionUID = 8295825971812538595L;

    private final Map> quickActions = new ConcurrentHashMap<>();
    private final List closeIssues = new ArrayList<>();
    private final List subjectIssues = new ArrayList<>();
    private final List bodyIssues = new ArrayList<>();
    private final List mentions = new ArrayList<>();
    private final List locales = new ArrayList<>();
    private final String defaultLang;

    @Setter
    private String commitUrl;
    @Setter
    private String commitType;
    @Setter
    private String commitPackage;
    @Setter
    private String commitScope;
    @Setter
    private String commitSubject;
    @Setter
    private String commitBody;
    @Setter
    private boolean attention;
    @Setter
    private boolean breakingChange;
    @Setter
    private boolean deprecated;

    public ReleaseCommit(final GitCommit gitCommit, final String defaultLang) {
        super(
            gitCommit.getHashFull(),
            gitCommit.getCommitTime(),
            gitCommit.getMessage(),
            gitCommit.isMerge(),
            gitCommit.getAuthorIdent(),
            gitCommit.getCommitterIdent()
        );

        this.defaultLang = StringUtils.defaultIfEmpty(defaultLang, ParserConstants.DEFAULT_COMMIT_LANG);
    }

    public IssueRef getCommitIssue() {
        return this.subjectIssues.isEmpty() ? null : this.subjectIssues.get(0);
    }

    @EqualsAndHashCode.Include
    @Override
    public String getHashFull() {
        return super.getHashFull();
    }

    public void add(final String action, final IssueRef issueRef) {
        final String issueAction = StringUtils.lowerCase(action);

        if (this.quickActions.containsKey(issueAction)) {
            final List issues = quickActions.get(issueAction);

            if (!issues.contains(issueRef)) issues.add(issueRef);
        } else {
            final List issues = new ArrayList<>();
            issues.add(issueRef);

            quickActions.put(issueAction, issues);
        }
    }

    public boolean hasQuickActions() {
        return !this.quickActions.isEmpty();
    }

    public boolean hasSubjectIssues() {
        return !this.subjectIssues.isEmpty();
    }

    public boolean hasBodyIssues() {
        return !this.bodyIssues.isEmpty();
    }

    public boolean hasCloseIssues() {
        return !this.closeIssues.isEmpty();
    }

    public Map getLocaleMap() {
        if (this.locales.isEmpty()) return null;

        return this.locales.stream()
            .collect(Collectors.toMap(ReleaseCommitLocale::getLang, locale -> locale, (a, b) -> b));
    }

    public String getFirstLocaleCommitType() {
        return this.locales.stream()
            .map(ReleaseCommitLocale::getCommitType)
            .filter(StringUtils::isNotEmpty)
            .findFirst()
            .orElse(null);
    }

    public String getRawCommitType() {
        return this.commitType;
    }

    public String getCommitType() {
        return StringUtils.defaultIfEmpty(this.getFirstLocaleCommitType(), this.commitType);
    }

    public String getRawCommitSubject() {
        return this.commitSubject;
    }

    public String getCommitSubject() {
        return this.locales.stream()
            .filter(x -> this.defaultLang.equals(x.getLang()))
            .map(ReleaseCommitLocale::getSubject)
            .findFirst()
            .orElse(this.commitSubject);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy