create release version checker

This commit is contained in:
Eduard Heimbuch
2020-09-21 17:04:30 +02:00
parent 404e6fec66
commit c784c97acf
8 changed files with 389 additions and 3 deletions

View File

@@ -0,0 +1,88 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.admin;
import lombok.AllArgsConstructor;
import lombok.Getter;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.time.Instant;
import java.util.List;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@AllArgsConstructor
public final class ReleaseFeedDto {
@XmlElement(name = "rss")
private final RSS rss;
public RSS getRSS() {
return rss;
}
@XmlRootElement(name = "rss")
@XmlAccessorType(XmlAccessType.FIELD)
@AllArgsConstructor
public static class RSS {
@XmlElement(name = "channel")
private final Channel channel;
public Channel getChannel() {
return channel;
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "channel")
@Getter
@AllArgsConstructor
public static class Channel {
private final String title;
private final String description;
private final String link;
private final String generator;
private final Instant lastBuildDate;
@XmlElement(name = "releases")
private final List<Release> releases;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "conditions")
@Getter
@AllArgsConstructor
public static class Release {
private final String title;
private final String description;
private final String link;
private final String guid;
private final Instant pubDate;
}
}

View File

@@ -0,0 +1,63 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.admin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.net.ahc.AdvancedHttpClient;
import javax.inject.Inject;
import java.io.IOException;
import java.util.Comparator;
import java.util.Optional;
public class ReleaseFeedReader {
private static final Logger LOG = LoggerFactory.getLogger(ReleaseFeedReader.class);
private final AdvancedHttpClient client;
@Inject
public ReleaseFeedReader(AdvancedHttpClient client) {
this.client = client;
}
Optional<ReleaseInfo> findLatestRelease(String url) throws IOException {
LOG.info("Search for newer versions of SCM-Manager");
ReleaseFeedDto releaseFeed = client.get(url).request().contentFromXml(ReleaseFeedDto.class);
Optional<ReleaseFeedDto.Release> latestRelease = filterForLatestRelease(releaseFeed);
if (latestRelease.isPresent()) {
ReleaseFeedDto.Release release = latestRelease.get();
return Optional.of(new ReleaseInfo(release.getTitle(), release.getLink(), release.getPubDate()));
}
return Optional.empty();
}
private Optional<ReleaseFeedDto.Release> filterForLatestRelease(ReleaseFeedDto releaseFeed) {
return releaseFeed.getRSS().getChannel().getReleases()
.stream()
.max(Comparator.comparing(ReleaseFeedDto.Release::getPubDate));
}
}

View File

@@ -0,0 +1,38 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.admin;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.time.Instant;
@AllArgsConstructor
@Getter
public class ReleaseInfo {
private final String title;
private final String link;
private final Instant releaseDate;
}

View File

@@ -0,0 +1,68 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.admin;
import sonia.scm.SCMContextProvider;
import sonia.scm.config.ScmConfiguration;
import sonia.scm.version.Version;
import javax.inject.Inject;
import java.io.IOException;
import java.util.Optional;
public class ReleaseVersionChecker {
private final ReleaseFeedReader releaseFeedReader;
private final ScmConfiguration scmConfiguration;
private final SCMContextProvider scmContextProvider;
@Inject
public ReleaseVersionChecker(ReleaseFeedReader releaseFeedReader, ScmConfiguration scmConfiguration, SCMContextProvider scmContextProvider) {
this.releaseFeedReader = releaseFeedReader;
this.scmConfiguration = scmConfiguration;
this.scmContextProvider = scmContextProvider;
}
Optional<ReleaseInfo> checkForNewerVersion() {
try {
String releaseFeedUrl = scmConfiguration.getReleaseFeedUrl();
Optional<ReleaseInfo> latestRelease = releaseFeedReader.findLatestRelease(releaseFeedUrl);
if (latestRelease.isPresent()) {
if (isNewerVersion(latestRelease.get())) {
return latestRelease;
}
}
} catch (IOException e) {
// This is an silent action. We don't want the user to get any kind of error for this.
return Optional.empty();
}
return Optional.empty();
}
private boolean isNewerVersion(ReleaseInfo releaseInfo) {
Version versionFromReleaseFeed = Version.parse(releaseInfo.getTitle());
return versionFromReleaseFeed.isNewer(scmContextProvider.getVersion());
}
}