• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jreleaser / jreleaser / #477

04 Apr 2025 05:53PM UTC coverage: 35.124% (-5.1%) from 40.183%
#477

push

github

aalmiray
fix(deploy): Add missing Forgejo messages

Related to #1842

18210 of 51845 relevant lines covered (35.12%)

0.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

13.4
/core/jreleaser-model-impl/src/main/java/org/jreleaser/model/internal/util/VersionUtils.java
1
/*
2
 * SPDX-License-Identifier: Apache-2.0
3
 *
4
 * Copyright 2020-2025 The JReleaser authors.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     https://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
package org.jreleaser.model.internal.util;
19

20
import org.jreleaser.logging.JReleaserLogger;
21
import org.jreleaser.model.internal.JReleaserContext;
22
import org.jreleaser.model.internal.release.BaseReleaser;
23
import org.jreleaser.version.CalVer;
24
import org.jreleaser.version.ChronVer;
25
import org.jreleaser.version.CustomVersion;
26
import org.jreleaser.version.JavaModuleVersion;
27
import org.jreleaser.version.JavaRuntimeVersion;
28
import org.jreleaser.version.SemanticVersion;
29
import org.jreleaser.version.Version;
30

31
import java.util.LinkedHashSet;
32
import java.util.Set;
33
import java.util.regex.Matcher;
34
import java.util.regex.Pattern;
35

36
/**
37
 * @author Andres Almiray
38
 * @since 1.2.0
39
 */
40
public final class VersionUtils {
41
    private static final UnparseableTags UNPARSEABLE_TAGS = new UnparseableTags();
1✔
42

43
    private VersionUtils() {
44
        // noop
45
    }
46

47
    public static Pattern resolveVersionPattern(JReleaserContext context) {
48
        BaseReleaser<?, ?> gitService = context.getModel().getRelease().getReleaser();
1✔
49
        String tagName = gitService.getTagName();
1✔
50
        Pattern vp = Pattern.compile(tagName.replaceAll("\\{\\{.*}}", "\\(\\.\\*\\)"));
1✔
51
        if (!tagName.contains("{{")) {
1✔
52
            vp = Pattern.compile("(.*)");
×
53
        }
54

55
        return vp;
1✔
56
    }
57

58
    public static void clearUnparseableTags() {
59
        UNPARSEABLE_TAGS.clear();
×
60
    }
×
61

62
    public static Version<?> version(JReleaserContext context, String tagName, Pattern versionPattern) {
63
        return version(context, tagName, versionPattern, false);
1✔
64
    }
65

66
    public static Version<?> version(JReleaserContext context, String tagName, Pattern versionPattern, boolean strict) {
67
        switch (context.getModel().getProject().versionPattern().getType()) {
1✔
68
            case SEMVER:
69
                return semverOf(context.getLogger(), tagName, versionPattern, strict);
1✔
70
            case JAVA_RUNTIME:
71
                return javaRuntimeVersionOf(context.getLogger(), tagName, versionPattern, strict);
×
72
            case JAVA_MODULE:
73
                return javaModuleVersionOf(context.getLogger(), tagName, versionPattern, strict);
×
74
            case CALVER:
75
                return calverOf(context, tagName, versionPattern, strict);
×
76
            case CHRONVER:
77
                return chronVer(context.getLogger(), tagName, versionPattern, strict);
×
78
            case CUSTOM:
79
            default:
80
                return versionOf(tagName, versionPattern);
×
81
        }
82
    }
83

84
    public static Version<?> defaultVersion(JReleaserContext context) {
85
        switch (context.getModel().getProject().versionPattern().getType()) {
×
86
            case SEMVER:
87
                return SemanticVersion.defaultOf();
×
88
            case JAVA_RUNTIME:
89
                return JavaRuntimeVersion.defaultOf();
×
90
            case JAVA_MODULE:
91
                return JavaModuleVersion.defaultOf();
×
92
            case CALVER:
93
                String format = context.getModel().getProject().versionPattern().getFormat();
×
94
                return CalVer.defaultOf(format);
×
95
            case CHRONVER:
96
                return ChronVer.defaultOf();
×
97
            case CUSTOM:
98
            default:
99
                return CustomVersion.defaultOf();
×
100
        }
101
    }
102

103
    private static SemanticVersion semverOf(JReleaserLogger logger, String tagName, Pattern versionPattern, boolean strict) {
104
        Matcher matcher = versionPattern.matcher(tagName);
1✔
105
        if (matcher.matches()) {
1✔
106
            String tag = matcher.group(1);
×
107
            try {
108
                return SemanticVersion.of(tag);
×
109
            } catch (IllegalArgumentException e) {
×
110
                UNPARSEABLE_TAGS.unparseable(logger, tag, e);
×
111
            }
112
        }
113

114
        if (!strict && tagName.startsWith("v")) {
1✔
115
            String tag = tagName.substring(1);
×
116
            try {
117
                return SemanticVersion.of(tag);
×
118
            } catch (IllegalArgumentException e) {
×
119
                UNPARSEABLE_TAGS.unparseable(logger, tag, e);
×
120
            }
121
        }
122

123
        return SemanticVersion.defaultOf();
1✔
124
    }
125

126
    private static JavaRuntimeVersion javaRuntimeVersionOf(JReleaserLogger logger, String tagName, Pattern versionPattern, boolean strict) {
127
        Matcher matcher = versionPattern.matcher(tagName);
×
128
        if (matcher.matches()) {
×
129
            String tag = matcher.group(1);
×
130
            try {
131
                return JavaRuntimeVersion.of(tag);
×
132
            } catch (IllegalArgumentException e) {
×
133
                UNPARSEABLE_TAGS.unparseable(logger, tag, e);
×
134
            }
135
        }
136

137
        if (!strict && tagName.startsWith("v")) {
×
138
            String tag = tagName.substring(1);
×
139
            try {
140
                return JavaRuntimeVersion.of(tag);
×
141
            } catch (IllegalArgumentException e) {
×
142
                UNPARSEABLE_TAGS.unparseable(logger, tag, e);
×
143
            }
144
        }
145

146
        return JavaRuntimeVersion.defaultOf();
×
147
    }
148

149
    private static JavaModuleVersion javaModuleVersionOf(JReleaserLogger logger, String tagName, Pattern versionPattern, boolean strict) {
150
        Matcher matcher = versionPattern.matcher(tagName);
×
151
        if (matcher.matches()) {
×
152
            String tag = matcher.group(1);
×
153
            try {
154
                return JavaModuleVersion.of(tag);
×
155
            } catch (IllegalArgumentException e) {
×
156
                UNPARSEABLE_TAGS.unparseable(logger, tag, e);
×
157
            }
158
        }
159

160
        if (!strict && tagName.startsWith("v")) {
×
161
            String tag = tagName.substring(1);
×
162
            try {
163
                return JavaModuleVersion.of(tag);
×
164
            } catch (IllegalArgumentException e) {
×
165
                UNPARSEABLE_TAGS.unparseable(logger, tag, e);
×
166
            }
167
        }
168

169
        return JavaModuleVersion.defaultOf();
×
170
    }
171

172
    private static CalVer calverOf(JReleaserContext context, String tagName, Pattern versionPattern, boolean strict) {
173
        String format = context.getModel().getProject().versionPattern().getFormat();
×
174
        Matcher matcher = versionPattern.matcher(tagName);
×
175
        if (matcher.matches()) {
×
176
            String tag = matcher.group(1);
×
177
            try {
178
                return CalVer.of(format, tag);
×
179
            } catch (IllegalArgumentException e) {
×
180
                UNPARSEABLE_TAGS.unparseable(context.getLogger(), tag, e);
×
181
            }
182
        }
183

184
        if (!strict && tagName.startsWith("v")) {
×
185
            String tag = tagName.substring(1);
×
186
            try {
187
                return CalVer.of(format, tag);
×
188
            } catch (IllegalArgumentException e) {
×
189
                UNPARSEABLE_TAGS.unparseable(context.getLogger(), tag, e);
×
190
            }
191
        }
192

193
        return CalVer.defaultOf(format);
×
194
    }
195

196
    private static ChronVer chronVer(JReleaserLogger logger, String tagName, Pattern versionPattern, boolean strict) {
197
        Matcher matcher = versionPattern.matcher(tagName);
×
198
        if (matcher.matches()) {
×
199
            String tag = matcher.group(1);
×
200
            try {
201
                return ChronVer.of(tag);
×
202
            } catch (IllegalArgumentException e) {
×
203
                UNPARSEABLE_TAGS.unparseable(logger, tag, e);
×
204
            }
205
        }
206

207
        if (!strict && tagName.startsWith("v")) {
×
208
            String tag = tagName.substring(1);
×
209
            try {
210
                return ChronVer.of(tag);
×
211
            } catch (IllegalArgumentException e) {
×
212
                UNPARSEABLE_TAGS.unparseable(logger, tag, e);
×
213
            }
214
        }
215

216
        return ChronVer.defaultOf();
×
217
    }
218

219
    private static CustomVersion versionOf(String tagName, Pattern versionPattern) {
220
        Matcher matcher = versionPattern.matcher(tagName);
×
221
        if (matcher.matches()) {
×
222
            return CustomVersion.of(matcher.group(1));
×
223
        }
224
        return CustomVersion.defaultOf();
×
225
    }
226

227
    private static class UnparseableTags extends ThreadLocal<Set<String>> {
228
        @Override
229
        protected Set<String> initialValue() {
230
            return new LinkedHashSet<>();
×
231
        }
232

233
        public void clear() {
234
            get().clear();
×
235
        }
×
236

237
        public void unparseable(JReleaserLogger logger, String tag, Exception exception) {
238
            if (!get().contains(tag)) {
×
239
                get().add(tag);
×
240
                logger.warn(exception.getMessage());
×
241
            }
242
        }
×
243
    }
244
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc