create admin info resource // fix rss feed parsing

This commit is contained in:
Eduard Heimbuch
2020-09-22 13:41:40 +02:00
parent c784c97acf
commit 851c5f9287
15 changed files with 423 additions and 62 deletions

View File

@@ -26,63 +26,61 @@ package sonia.scm.admin;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import sonia.scm.xml.XmlDateWithTimezoneAdapter;
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 javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.util.Date;
import java.util.List;
@XmlRootElement
@XmlRootElement(name = "rss")
@XmlAccessorType(XmlAccessType.FIELD)
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public final class ReleaseFeedDto {
@XmlElement(name = "rss")
private final RSS rss;
@XmlElement(name = "channel")
private Channel channel;
public RSS getRSS() {
return rss;
public Channel getChannel() {
return channel;
}
@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")
@NoArgsConstructor
@Getter
@AllArgsConstructor
@Setter
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;
private String title;
private String description;
private String link;
private String generator;
@XmlJavaTypeAdapter(XmlDateWithTimezoneAdapter.class)
private Date lastBuildDate;
@XmlElement(name = "item")
private List<Release> releases;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "conditions")
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public static class Release {
private final String title;
private final String description;
private final String link;
private final String guid;
private final Instant pubDate;
private String title;
private String description;
private String link;
private String guid;
@XmlJavaTypeAdapter(XmlDateWithTimezoneAdapter.class)
private Date pubDate;
}
}

View File

@@ -30,17 +30,18 @@ import sonia.scm.net.ahc.AdvancedHttpClient;
import javax.inject.Inject;
import java.io.IOException;
import java.time.Instant;
import java.util.Comparator;
import java.util.Optional;
public class ReleaseFeedReader {
public class ReleaseFeedParser {
private static final Logger LOG = LoggerFactory.getLogger(ReleaseFeedReader.class);
private static final Logger LOG = LoggerFactory.getLogger(ReleaseFeedParser.class);
private final AdvancedHttpClient client;
@Inject
public ReleaseFeedReader(AdvancedHttpClient client) {
public ReleaseFeedParser(AdvancedHttpClient client) {
this.client = client;
}
@@ -50,13 +51,13 @@ public class ReleaseFeedReader {
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.of(new ReleaseInfo(release.getTitle(), release.getLink(), Instant.now()));
}
return Optional.empty();
}
private Optional<ReleaseFeedDto.Release> filterForLatestRelease(ReleaseFeedDto releaseFeed) {
return releaseFeed.getRSS().getChannel().getReleases()
return releaseFeed.getChannel().getReleases()
.stream()
.max(Comparator.comparing(ReleaseFeedDto.Release::getPubDate));
}

View File

@@ -0,0 +1,37 @@
/*
* 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.mapstruct.Mapper;
import org.mapstruct.Mapping;
import sonia.scm.api.v2.resources.HalAppenderMapper;
import sonia.scm.api.v2.resources.ReleaseInfoDto;
@Mapper
public abstract class ReleaseInfoMapper extends HalAppenderMapper {
@Mapping(target = "attributes", ignore = true) // We do not map HAL attributes
public abstract ReleaseInfoDto map(ReleaseInfo releaseInfo);
}

View File

@@ -24,7 +24,12 @@
package sonia.scm.admin;
import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.SCMContextProvider;
import sonia.scm.cache.Cache;
import sonia.scm.cache.CacheManager;
import sonia.scm.config.ScmConfiguration;
import sonia.scm.version.Version;
@@ -34,28 +39,47 @@ import java.util.Optional;
public class ReleaseVersionChecker {
private final ReleaseFeedReader releaseFeedReader;
private static final Logger LOG = LoggerFactory.getLogger(ReleaseVersionChecker.class);
private static final String CACHE_NAME = "sonia.cache.releaseInfo";
private final ReleaseFeedParser releaseFeedParser;
private final ScmConfiguration scmConfiguration;
private final SCMContextProvider scmContextProvider;
private Cache<String, ReleaseInfo> cache;
@Inject
public ReleaseVersionChecker(ReleaseFeedReader releaseFeedReader, ScmConfiguration scmConfiguration, SCMContextProvider scmContextProvider) {
this.releaseFeedReader = releaseFeedReader;
public ReleaseVersionChecker(ReleaseFeedParser releaseFeedParser, ScmConfiguration scmConfiguration, SCMContextProvider scmContextProvider, CacheManager cacheManager) {
this.releaseFeedParser = releaseFeedParser;
this.scmConfiguration = scmConfiguration;
this.scmContextProvider = scmContextProvider;
this.cache = cacheManager.getCache(CACHE_NAME);
}
Optional<ReleaseInfo> checkForNewerVersion() {
@VisibleForTesting
void setCache(Cache<String, ReleaseInfo> cache) {
this.cache = cache;
}
public Optional<ReleaseInfo> checkForNewerVersion() {
ReleaseInfo cachedReleaseInfo = cache.get("latest");
if (cachedReleaseInfo != null) {
return Optional.of(cachedReleaseInfo);
} else {
return findLatestReleaseInRssFeed();
}
}
private Optional<ReleaseInfo> findLatestReleaseInRssFeed() {
try {
String releaseFeedUrl = scmConfiguration.getReleaseFeedUrl();
Optional<ReleaseInfo> latestRelease = releaseFeedReader.findLatestRelease(releaseFeedUrl);
if (latestRelease.isPresent()) {
if (isNewerVersion(latestRelease.get())) {
return latestRelease;
}
Optional<ReleaseInfo> latestRelease = releaseFeedParser.findLatestRelease(releaseFeedUrl);
if (latestRelease.isPresent() && isNewerVersion(latestRelease.get())) {
cache.put("latest", 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.
LOG.info("No newer version found for SCM-Manager");
return Optional.empty();
}
return Optional.empty();

View File

@@ -0,0 +1,80 @@
/*
* 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.api.v2.resources;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import sonia.scm.admin.ReleaseInfo;
import sonia.scm.admin.ReleaseInfoMapper;
import sonia.scm.admin.ReleaseVersionChecker;
import sonia.scm.web.VndMediaType;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.util.Optional;
@OpenAPIDefinition(tags = {
@Tag(name = "AdminInfo", description = "Admin information endpoints")
})
@Path("")
public class AdminInfoResource {
private final ReleaseVersionChecker checker;
private final ReleaseInfoMapper mapper;
@Inject
public AdminInfoResource(ReleaseVersionChecker checker, ReleaseInfoMapper mapper) {
this.checker = checker;
this.mapper = mapper;
}
/**
* Checks for a newer core version of SCM-Manager.
*/
@GET
@Path("releaseInfo")
@Produces(VndMediaType.ADMIN_INFO)
@Operation(summary = "Returns release info.", description = "Returns a release info if a newer version of SCM-Manager is available.", tags = "AdminInfo")
@ApiResponse(responseCode = "200", description = "success")
@ApiResponse(responseCode = "401", description = "not authenticated / invalid credentials")
@ApiResponse(responseCode = "403", description = "not authorized, the current user has no privileges to read the information")
@ApiResponse(
responseCode = "500",
description = "internal server error",
content = @Content(
mediaType = VndMediaType.ERROR_TYPE,
schema = @Schema(implementation = ErrorDto.class)
))
public ReleaseInfoDto getReleaseInfo() {
Optional<ReleaseInfo> releaseInfo = checker.checkForNewerVersion();
return releaseInfo.map(mapper::map).orElse(null);
}
}

View File

@@ -102,6 +102,7 @@ public class IndexDtoGenerator extends HalAppenderMapper {
}
if (ConfigurationPermissions.list().isPermitted()) {
builder.single(link("config", resourceLinks.config().self()));
builder.single(link("releaseInfo", resourceLinks.adminInfo().releaseInfo()));
}
builder.single(link("repositories", resourceLinks.repositoryCollection().self()));
builder.single(link("namespaces", resourceLinks.namespaceCollection().self()));

View File

@@ -27,6 +27,7 @@ package sonia.scm.api.v2.resources;
import com.google.inject.AbstractModule;
import com.google.inject.servlet.ServletScopes;
import org.mapstruct.factory.Mappers;
import sonia.scm.admin.ReleaseInfoMapper;
import sonia.scm.security.gpg.PublicKeyMapper;
import sonia.scm.web.api.RepositoryToHalMapper;
@@ -76,6 +77,7 @@ public class MapperModule extends AbstractModule {
bind(RepositoryToHalMapper.class).to(Mappers.getMapperClass(RepositoryToRepositoryDtoMapper.class));
bind(BlameResultToBlameDtoMapper.class).to(Mappers.getMapperClass(BlameResultToBlameDtoMapper.class));
bind(ReleaseInfoMapper.class).to(Mappers.getMapperClass(ReleaseInfoMapper.class));
// no mapstruct required
bind(MeDtoFactory.class);

View File

@@ -0,0 +1,43 @@
/*
* 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.api.v2.resources;
import de.otto.edison.hal.HalRepresentation;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.time.Instant;
@NoArgsConstructor
@Setter
@Getter
@SuppressWarnings("squid:S2160") // we do not need equals for dto
public class ReleaseInfoDto extends HalRepresentation {
private String title;
private String link;
private Instant releaseDate;
}

View File

@@ -264,6 +264,22 @@ class ResourceLinks {
}
}
AdminInfoLinks adminInfo() {
return new AdminInfoLinks(scmPathInfoStore.get());
}
static class AdminInfoLinks {
private final LinkBuilder adminInfoLinkBuilder;
AdminInfoLinks(ScmPathInfo pathInfo) {
adminInfoLinkBuilder = new LinkBuilder(pathInfo, AdminInfoResource.class);
}
String releaseInfo() {
return adminInfoLinkBuilder.method("getReleaseInfo").parameters().href();
}
}
public RepositoryLinks repository() {
return new RepositoryLinks(scmPathInfoStore.get());
}