mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-07 22:15:45 +01:00
Improve repository information page (#1636)
Only show relevant information for repository on repository information page. The initialization code example is only shown if the repository is still empty.
This commit is contained in:
2
gradle/changelog/repo_information_page.yaml
Normal file
2
gradle/changelog/repo_information_page.yaml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
- type: changed
|
||||||
|
description: Show only relevant information on repository information page ([#1636](https://github.com/scm-manager/scm-manager/pull/1636))
|
||||||
@@ -24,16 +24,19 @@
|
|||||||
|
|
||||||
package sonia.scm.api.v2.resources;
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
|
import com.google.common.base.Strings;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.media.Content;
|
import io.swagger.v3.oas.annotations.media.Content;
|
||||||
import io.swagger.v3.oas.annotations.media.ExampleObject;
|
import io.swagger.v3.oas.annotations.media.ExampleObject;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import io.swagger.v3.oas.annotations.parameters.RequestBody;
|
import io.swagger.v3.oas.annotations.parameters.RequestBody;
|
||||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import sonia.scm.repository.GitRepositoryConfig;
|
import sonia.scm.repository.GitRepositoryConfig;
|
||||||
|
import sonia.scm.repository.GitRepositoryHandler;
|
||||||
import sonia.scm.repository.NamespaceAndName;
|
import sonia.scm.repository.NamespaceAndName;
|
||||||
import sonia.scm.repository.Repository;
|
import sonia.scm.repository.Repository;
|
||||||
import sonia.scm.repository.RepositoryManager;
|
import sonia.scm.repository.RepositoryManager;
|
||||||
@@ -61,12 +64,14 @@ public class GitRepositoryConfigResource {
|
|||||||
private final GitRepositoryConfigMapper repositoryConfigMapper;
|
private final GitRepositoryConfigMapper repositoryConfigMapper;
|
||||||
private final RepositoryManager repositoryManager;
|
private final RepositoryManager repositoryManager;
|
||||||
private final GitRepositoryConfigStoreProvider gitRepositoryConfigStoreProvider;
|
private final GitRepositoryConfigStoreProvider gitRepositoryConfigStoreProvider;
|
||||||
|
private final GitRepositoryHandler repositoryHandler;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public GitRepositoryConfigResource(GitRepositoryConfigMapper repositoryConfigMapper, RepositoryManager repositoryManager, GitRepositoryConfigStoreProvider gitRepositoryConfigStoreProvider) {
|
public GitRepositoryConfigResource(GitRepositoryConfigMapper repositoryConfigMapper, RepositoryManager repositoryManager, GitRepositoryConfigStoreProvider gitRepositoryConfigStoreProvider, GitRepositoryHandler repositoryHandler) {
|
||||||
this.repositoryConfigMapper = repositoryConfigMapper;
|
this.repositoryConfigMapper = repositoryConfigMapper;
|
||||||
this.repositoryManager = repositoryManager;
|
this.repositoryManager = repositoryManager;
|
||||||
this.gitRepositoryConfigStoreProvider = gitRepositoryConfigStoreProvider;
|
this.gitRepositoryConfigStoreProvider = gitRepositoryConfigStoreProvider;
|
||||||
|
this.repositoryHandler = repositoryHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@@ -100,12 +105,55 @@ public class GitRepositoryConfigResource {
|
|||||||
public Response getRepositoryConfig(@PathParam("namespace") String namespace, @PathParam("name") String name) {
|
public Response getRepositoryConfig(@PathParam("namespace") String namespace, @PathParam("name") String name) {
|
||||||
Repository repository = getRepository(namespace, name);
|
Repository repository = getRepository(namespace, name);
|
||||||
RepositoryPermissions.read(repository).check();
|
RepositoryPermissions.read(repository).check();
|
||||||
ConfigurationStore<GitRepositoryConfig> repositoryConfigStore = getStore(repository);
|
GitRepositoryConfig config = getStore(repository).get();
|
||||||
GitRepositoryConfig config = repositoryConfigStore.get();
|
|
||||||
GitRepositoryConfigDto dto = repositoryConfigMapper.map(config, repository);
|
GitRepositoryConfigDto dto = repositoryConfigMapper.map(config, repository);
|
||||||
return Response.ok(dto).build();
|
return Response.ok(dto).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Path("default-branch")
|
||||||
|
@Produces(GitVndMediaType.GIT_REPOSITORY_DEFAULT_BRANCH)
|
||||||
|
@Operation(summary = "Git repository default branch", description = "Returns the default branch for the repository.", tags = "Git")
|
||||||
|
@ApiResponse(
|
||||||
|
responseCode = "200",
|
||||||
|
description = "success",
|
||||||
|
content = @Content(
|
||||||
|
mediaType = GitVndMediaType.GIT_REPOSITORY_CONFIG,
|
||||||
|
schema = @Schema(implementation = GitRepositoryConfigDto.class)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
@ApiResponse(responseCode = "401", description = "not authenticated / invalid credentials")
|
||||||
|
@ApiResponse(responseCode = "403", description = "not authorized, the current user has no privileges to read the repository config")
|
||||||
|
@ApiResponse(
|
||||||
|
responseCode = "404",
|
||||||
|
description = "not found, no repository with the specified namespace and name available",
|
||||||
|
content = @Content(
|
||||||
|
mediaType = VndMediaType.ERROR_TYPE,
|
||||||
|
schema = @Schema(implementation = ErrorDto.class)
|
||||||
|
))
|
||||||
|
@ApiResponse(
|
||||||
|
responseCode = "500",
|
||||||
|
description = "internal server error",
|
||||||
|
content = @Content(
|
||||||
|
mediaType = VndMediaType.ERROR_TYPE,
|
||||||
|
schema = @Schema(implementation = ErrorDto.class)
|
||||||
|
))
|
||||||
|
public Response getDefaultBranch(@PathParam("namespace") String namespace, @PathParam("name") String name) {
|
||||||
|
Repository repository = getRepository(namespace, name);
|
||||||
|
RepositoryPermissions.read(repository).check();
|
||||||
|
GitRepositoryConfig config = getStore(repository).get();
|
||||||
|
|
||||||
|
String defaultBranch = "main";
|
||||||
|
|
||||||
|
if (!Strings.isNullOrEmpty(config.getDefaultBranch())) {
|
||||||
|
defaultBranch = config.getDefaultBranch();
|
||||||
|
} else if (!Strings.isNullOrEmpty(repositoryHandler.getConfig().getDefaultBranch())) {
|
||||||
|
defaultBranch = repositoryHandler.getConfig().getDefaultBranch();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Response.ok(new DefaultBranchDto(defaultBranch)).build();
|
||||||
|
}
|
||||||
|
|
||||||
@PUT
|
@PUT
|
||||||
@Path("/")
|
@Path("/")
|
||||||
@Consumes(GitVndMediaType.GIT_REPOSITORY_CONFIG)
|
@Consumes(GitVndMediaType.GIT_REPOSITORY_CONFIG)
|
||||||
@@ -167,4 +215,10 @@ public class GitRepositoryConfigResource {
|
|||||||
private ConfigurationStore<GitRepositoryConfig> getStore(Repository repository) {
|
private ConfigurationStore<GitRepositoryConfig> getStore(Repository repository) {
|
||||||
return gitRepositoryConfigStoreProvider.get(repository);
|
return gitRepositoryConfigStoreProvider.get(repository);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class DefaultBranchDto {
|
||||||
|
private final String defaultBranch;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* 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 sonia.scm.plugin.Extension;
|
||||||
|
import sonia.scm.repository.Repository;
|
||||||
|
import sonia.scm.repository.RepositoryPermissions;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.inject.Provider;
|
||||||
|
|
||||||
|
@Extension
|
||||||
|
@Enrich(Repository.class)
|
||||||
|
public class RepositoryLinkEnricher implements HalEnricher {
|
||||||
|
|
||||||
|
private final Provider<ScmPathInfoStore> scmPathInfoStore;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public RepositoryLinkEnricher(Provider<ScmPathInfoStore> scmPathInfoStore) {
|
||||||
|
this.scmPathInfoStore = scmPathInfoStore;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void enrich(HalEnricherContext context, HalAppender appender) {
|
||||||
|
Repository repository = context.oneRequireByType(Repository.class);
|
||||||
|
|
||||||
|
LinkBuilder linkBuilder = new LinkBuilder(scmPathInfoStore.get().get(), GitConfigResource.class, GitRepositoryConfigResource.class);
|
||||||
|
|
||||||
|
if (RepositoryPermissions.read(repository).isPermitted()) {
|
||||||
|
appender.appendLink("defaultBranch", getDefaultBranchLink(repository, linkBuilder));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getDefaultBranchLink(Repository repository, LinkBuilder linkBuilder) {
|
||||||
|
return linkBuilder
|
||||||
|
.method("getRepositoryConfig").parameters(repository.getNamespace(), repository.getName())
|
||||||
|
.method("getDefaultBranch").parameters()
|
||||||
|
.href();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -27,6 +27,7 @@ package sonia.scm.web;
|
|||||||
public class GitVndMediaType {
|
public class GitVndMediaType {
|
||||||
public static final String GIT_CONFIG = VndMediaType.PREFIX + "gitConfig" + VndMediaType.SUFFIX;
|
public static final String GIT_CONFIG = VndMediaType.PREFIX + "gitConfig" + VndMediaType.SUFFIX;
|
||||||
public static final String GIT_REPOSITORY_CONFIG = VndMediaType.PREFIX + "gitConfig" + VndMediaType.SUFFIX;
|
public static final String GIT_REPOSITORY_CONFIG = VndMediaType.PREFIX + "gitConfig" + VndMediaType.SUFFIX;
|
||||||
|
public static final String GIT_REPOSITORY_DEFAULT_BRANCH = VndMediaType.PREFIX + "gitDefaultBranch" + VndMediaType.SUFFIX;
|
||||||
|
|
||||||
private GitVndMediaType() {
|
private GitVndMediaType() {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,25 +21,60 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
import React from "react";
|
import React, { FC, useEffect, useState } from "react";
|
||||||
import { WithTranslation, withTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { Repository } from "@scm-manager/ui-types";
|
import { Link, Repository } from "@scm-manager/ui-types";
|
||||||
|
import { apiClient } from "@scm-manager/ui-components";
|
||||||
|
|
||||||
type Props = WithTranslation & {
|
type Props = {
|
||||||
url: string;
|
url: string;
|
||||||
repository: Repository;
|
repository: Repository;
|
||||||
};
|
};
|
||||||
|
|
||||||
class CloneInformation extends React.Component<Props> {
|
const CloneInformation: FC<Props> = ({ url, repository }) => {
|
||||||
render() {
|
const [t] = useTranslation("plugins");
|
||||||
const { url, repository, t } = this.props;
|
const [defaultBranch, setDefaultBranch] = useState<string>("main");
|
||||||
|
const [emptyRepository, setEmptyRepository] = useState<boolean>();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (repository) {
|
||||||
|
apiClient
|
||||||
|
.get((repository._links.changesets as Link).href + "?limit=1")
|
||||||
|
.then(r => r.json())
|
||||||
|
.then(result => {
|
||||||
|
const empty = result._embedded.changesets.length === 0;
|
||||||
|
setEmptyRepository(empty);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [repository]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (repository) {
|
||||||
|
apiClient
|
||||||
|
.get((repository._links.defaultBranch as Link).href)
|
||||||
|
.then(r => r.json())
|
||||||
|
.then(r => r.defaultBranch && setDefaultBranch(r.defaultBranch));
|
||||||
|
}
|
||||||
|
}, [repository]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h4>{t("scm-git-plugin.information.clone")}</h4>
|
<h4>{t("scm-git-plugin.information.clone")}</h4>
|
||||||
<pre>
|
<pre>
|
||||||
<code>git clone {url}</code>
|
<code>
|
||||||
|
git clone {url}
|
||||||
|
<br />
|
||||||
|
cd {repository.name}
|
||||||
|
{emptyRepository && (
|
||||||
|
<>
|
||||||
|
<br />
|
||||||
|
git checkout -b {defaultBranch}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</code>
|
||||||
</pre>
|
</pre>
|
||||||
|
{emptyRepository && (
|
||||||
|
<>
|
||||||
<h4>{t("scm-git-plugin.information.create")}</h4>
|
<h4>{t("scm-git-plugin.information.create")}</h4>
|
||||||
<pre>
|
<pre>
|
||||||
<code>
|
<code>
|
||||||
@@ -47,6 +82,8 @@ class CloneInformation extends React.Component<Props> {
|
|||||||
<br />
|
<br />
|
||||||
cd {repository.name}
|
cd {repository.name}
|
||||||
<br />
|
<br />
|
||||||
|
git checkout -b {defaultBranch}
|
||||||
|
<br />
|
||||||
echo "# {repository.name}
|
echo "# {repository.name}
|
||||||
" > README.md
|
" > README.md
|
||||||
<br />
|
<br />
|
||||||
@@ -56,22 +93,21 @@ class CloneInformation extends React.Component<Props> {
|
|||||||
<br />
|
<br />
|
||||||
git remote add origin {url}
|
git remote add origin {url}
|
||||||
<br />
|
<br />
|
||||||
git push -u origin master
|
git push -u origin {defaultBranch}
|
||||||
<br />
|
<br />
|
||||||
</code>
|
</code>
|
||||||
</pre>
|
</pre>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
<h4>{t("scm-git-plugin.information.replace")}</h4>
|
<h4>{t("scm-git-plugin.information.replace")}</h4>
|
||||||
<pre>
|
<pre>
|
||||||
<code>
|
<code>
|
||||||
git remote add origin {url}
|
git remote add origin {url}
|
||||||
<br />
|
<br />
|
||||||
git push -u origin master
|
|
||||||
<br />
|
|
||||||
</code>
|
</code>
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
|
||||||
export default withTranslation("plugins")(CloneInformation);
|
export default CloneInformation;
|
||||||
|
|||||||
@@ -22,7 +22,6 @@
|
|||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React from "react";
|
|
||||||
import { binder } from "@scm-manager/ui-extensions";
|
import { binder } from "@scm-manager/ui-extensions";
|
||||||
import ProtocolInformation from "./ProtocolInformation";
|
import ProtocolInformation from "./ProtocolInformation";
|
||||||
import GitAvatar from "./GitAvatar";
|
import GitAvatar from "./GitAvatar";
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"information": {
|
"information": {
|
||||||
"clone": "Repository klonen",
|
"clone": "Repository klonen",
|
||||||
"create": "Neues Repository erstellen",
|
"create": "Neues Repository erstellen",
|
||||||
"replace": "Ein bestehendes Repository aktualisieren",
|
"replace": "Quelle zum bestehenden Repository hinzufügen",
|
||||||
"fetch": "Remote-Änderungen herunterladen",
|
"fetch": "Remote-Änderungen herunterladen",
|
||||||
"checkout": "Branch wechseln",
|
"checkout": "Branch wechseln",
|
||||||
"checkoutTag": "Tag als neuen Branch auschecken",
|
"checkoutTag": "Tag als neuen Branch auschecken",
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"information": {
|
"information": {
|
||||||
"clone": "Clone the Repository",
|
"clone": "Clone the Repository",
|
||||||
"create": "Create a New Repository",
|
"create": "Create a New Repository",
|
||||||
"replace": "Push an Existing Repository",
|
"replace": "Add Remote Origin to an Existing Repository",
|
||||||
"fetch": "Get Remote Changes",
|
"fetch": "Get Remote Changes",
|
||||||
"checkout": "Switch Branch",
|
"checkout": "Switch Branch",
|
||||||
"checkoutTag": "Checkout Tag as New Branch",
|
"checkoutTag": "Checkout Tag as New Branch",
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ public class GitConfigResourceTest {
|
|||||||
@Rule
|
@Rule
|
||||||
public ShiroRule shiro = new ShiroRule();
|
public ShiroRule shiro = new ShiroRule();
|
||||||
|
|
||||||
private RestDispatcher dispatcher = new RestDispatcher();
|
private final RestDispatcher dispatcher = new RestDispatcher();
|
||||||
|
|
||||||
private final URI baseUri = URI.create("/");
|
private final URI baseUri = URI.create("/");
|
||||||
|
|
||||||
@@ -106,7 +106,7 @@ public class GitConfigResourceTest {
|
|||||||
public void prepareEnvironment() {
|
public void prepareEnvironment() {
|
||||||
GitConfig gitConfig = createConfiguration();
|
GitConfig gitConfig = createConfiguration();
|
||||||
when(repositoryHandler.getConfig()).thenReturn(gitConfig);
|
when(repositoryHandler.getConfig()).thenReturn(gitConfig);
|
||||||
GitRepositoryConfigResource gitRepositoryConfigResource = new GitRepositoryConfigResource(repositoryConfigMapper, repositoryManager, new GitRepositoryConfigStoreProvider(configurationStoreFactory));
|
GitRepositoryConfigResource gitRepositoryConfigResource = new GitRepositoryConfigResource(repositoryConfigMapper, repositoryManager, new GitRepositoryConfigStoreProvider(configurationStoreFactory), repositoryHandler);
|
||||||
GitConfigResource gitConfigResource = new GitConfigResource(dtoToConfigMapper, configToDtoMapper, repositoryHandler, of(gitRepositoryConfigResource));
|
GitConfigResource gitConfigResource = new GitConfigResource(dtoToConfigMapper, configToDtoMapper, repositoryHandler, of(gitRepositoryConfigResource));
|
||||||
dispatcher.addSingletonResource(gitConfigResource);
|
dispatcher.addSingletonResource(gitConfigResource);
|
||||||
when(scmPathInfoStore.get().getApiRestUri()).thenReturn(baseUri);
|
when(scmPathInfoStore.get().getApiRestUri()).thenReturn(baseUri);
|
||||||
@@ -251,6 +251,71 @@ public class GitConfigResourceTest {
|
|||||||
.isEqualTo("new");
|
.isEqualTo("new");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SubjectAware(username = "readOnly")
|
||||||
|
public void shouldGetDefaultBranchFromRepoConfig() throws URISyntaxException, UnsupportedEncodingException {
|
||||||
|
when(repositoryManager.get(new NamespaceAndName("space", "X"))).thenReturn(new Repository("id", "git", "space", "X"));
|
||||||
|
when(configurationStore.get()).thenReturn(new GitRepositoryConfig("default"));
|
||||||
|
MockHttpRequest request = MockHttpRequest
|
||||||
|
.get("/" + GitConfigResource.GIT_CONFIG_PATH_V2 + "/space/X/default-branch")
|
||||||
|
.contentType(GitVndMediaType.GIT_REPOSITORY_CONFIG);
|
||||||
|
MockHttpResponse response = new MockHttpResponse();
|
||||||
|
|
||||||
|
dispatcher.invoke(request, response);
|
||||||
|
|
||||||
|
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
|
||||||
|
assertEquals("{\"defaultBranch\":\"default\"}", response.getContentAsString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SubjectAware(username = "readOnly")
|
||||||
|
public void shouldGetDefaultBranchFromGlobalConfig() throws URISyntaxException, UnsupportedEncodingException {
|
||||||
|
when(repositoryManager.get(new NamespaceAndName("space", "X"))).thenReturn(new Repository("id", "git", "space", "X"));
|
||||||
|
when(configurationStore.get()).thenReturn(new GitRepositoryConfig());
|
||||||
|
GitConfig globalGitConfig = createConfiguration();
|
||||||
|
globalGitConfig.setDefaultBranch("global-default");
|
||||||
|
when(repositoryHandler.getConfig()).thenReturn(globalGitConfig);
|
||||||
|
MockHttpRequest request = MockHttpRequest
|
||||||
|
.get("/" + GitConfigResource.GIT_CONFIG_PATH_V2 + "/space/X/default-branch")
|
||||||
|
.contentType(GitVndMediaType.GIT_REPOSITORY_CONFIG);
|
||||||
|
MockHttpResponse response = new MockHttpResponse();
|
||||||
|
|
||||||
|
dispatcher.invoke(request, response);
|
||||||
|
|
||||||
|
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
|
||||||
|
assertEquals("{\"defaultBranch\":\"global-default\"}", response.getContentAsString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SubjectAware(username = "readOnly")
|
||||||
|
public void shouldGetFallbackDefaultBranchIfBothConfigsEmpty() throws URISyntaxException, UnsupportedEncodingException {
|
||||||
|
when(repositoryManager.get(new NamespaceAndName("space", "X"))).thenReturn(new Repository("id", "git", "space", "X"));
|
||||||
|
when(configurationStore.get()).thenReturn(new GitRepositoryConfig());
|
||||||
|
when(repositoryHandler.getConfig()).thenReturn(createConfiguration());
|
||||||
|
MockHttpRequest request = MockHttpRequest
|
||||||
|
.get("/" + GitConfigResource.GIT_CONFIG_PATH_V2 + "/space/X/default-branch")
|
||||||
|
.contentType(GitVndMediaType.GIT_REPOSITORY_CONFIG);
|
||||||
|
MockHttpResponse response = new MockHttpResponse();
|
||||||
|
|
||||||
|
dispatcher.invoke(request, response);
|
||||||
|
|
||||||
|
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
|
||||||
|
assertEquals("{\"defaultBranch\":\"main\"}", response.getContentAsString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldThrowAuthorizationExceptionIfNotPermittedToGetDefaultBranch() throws URISyntaxException {
|
||||||
|
when(repositoryManager.get(new NamespaceAndName("space", "X"))).thenReturn(new Repository("id", "git", "space", "X"));
|
||||||
|
MockHttpRequest request = MockHttpRequest
|
||||||
|
.get("/" + GitConfigResource.GIT_CONFIG_PATH_V2 + "/space/X/default-branch")
|
||||||
|
.contentType(GitVndMediaType.GIT_REPOSITORY_CONFIG);
|
||||||
|
MockHttpResponse response = new MockHttpResponse();
|
||||||
|
|
||||||
|
dispatcher.invoke(request, response);
|
||||||
|
|
||||||
|
assertEquals(HttpServletResponse.SC_FORBIDDEN, response.getStatus());
|
||||||
|
}
|
||||||
|
|
||||||
private MockHttpResponse get() throws URISyntaxException {
|
private MockHttpResponse get() throws URISyntaxException {
|
||||||
MockHttpRequest request = MockHttpRequest.get("/" + GitConfigResource.GIT_CONFIG_PATH_V2);
|
MockHttpRequest request = MockHttpRequest.get("/" + GitConfigResource.GIT_CONFIG_PATH_V2);
|
||||||
MockHttpResponse response = new MockHttpResponse();
|
MockHttpResponse response = new MockHttpResponse();
|
||||||
|
|||||||
@@ -0,0 +1,90 @@
|
|||||||
|
/*
|
||||||
|
* 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 com.google.inject.util.Providers;
|
||||||
|
import org.github.sdorra.jse.ShiroExtension;
|
||||||
|
import org.github.sdorra.jse.SubjectAware;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
import sonia.scm.repository.Repository;
|
||||||
|
import sonia.scm.repository.RepositoryTestData;
|
||||||
|
|
||||||
|
import javax.inject.Provider;
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
@ExtendWith(ShiroExtension.class)
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
@SubjectAware(
|
||||||
|
value = "trillian"
|
||||||
|
)
|
||||||
|
class RepositoryLinkEnricherTest {
|
||||||
|
|
||||||
|
private static final Repository REPOSITORY = RepositoryTestData.create42Puzzle();
|
||||||
|
|
||||||
|
private RepositoryLinkEnricher repositoryLinkEnricher;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private HalAppender appender;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void initEnricher() {
|
||||||
|
ScmPathInfoStore scmPathInfoStore = new ScmPathInfoStore();
|
||||||
|
scmPathInfoStore.set(() -> URI.create("/api/"));
|
||||||
|
Provider<ScmPathInfoStore> scmPathInfoStoreProvider = Providers.of(scmPathInfoStore);
|
||||||
|
repositoryLinkEnricher = new RepositoryLinkEnricher(scmPathInfoStoreProvider);
|
||||||
|
REPOSITORY.setId("id-1");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldNotAppendLinkIfNotPermitted() {
|
||||||
|
HalEnricherContext context = HalEnricherContext.of(REPOSITORY);
|
||||||
|
|
||||||
|
repositoryLinkEnricher.enrich(context, appender);
|
||||||
|
|
||||||
|
verify(appender, never()).appendLink(eq("defaultBranch"), any(String.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SubjectAware(
|
||||||
|
permissions = "repository:read:id-1"
|
||||||
|
)
|
||||||
|
void shouldAppendDefaultBranchLink() {
|
||||||
|
HalEnricherContext context = HalEnricherContext.of(REPOSITORY);
|
||||||
|
|
||||||
|
repositoryLinkEnricher.enrich(context, appender);
|
||||||
|
|
||||||
|
verify(appender).appendLink("defaultBranch", "/api/v2/config/git/hitchhiker/42Puzzle/default-branch");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -21,19 +21,33 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
import React from "react";
|
import React, { FC, useEffect, useState } from "react";
|
||||||
import { WithTranslation, withTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { Repository } from "@scm-manager/ui-types";
|
import { Link, Repository } from "@scm-manager/ui-types";
|
||||||
import { repositories } from "@scm-manager/ui-components";
|
import { apiClient, repositories } from "@scm-manager/ui-components";
|
||||||
|
|
||||||
type Props = WithTranslation & {
|
type Props = {
|
||||||
repository: Repository;
|
repository: Repository;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ProtocolInformation extends React.Component<Props> {
|
const ProtocolInformation: FC<Props> = ({ repository }) => {
|
||||||
render() {
|
const [t] = useTranslation("plugins");
|
||||||
const { repository, t } = this.props;
|
const [emptyRepository, setEmptyRepository] = useState<boolean>();
|
||||||
|
|
||||||
const href = repositories.getProtocolLinkByType(repository, "http");
|
const href = repositories.getProtocolLinkByType(repository, "http");
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (repository) {
|
||||||
|
apiClient
|
||||||
|
.get((repository._links.changesets as Link).href)
|
||||||
|
.then(r => r.json())
|
||||||
|
.then(result => {
|
||||||
|
const empty = result._embedded.changesets.length === 0;
|
||||||
|
setEmptyRepository(empty);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [repository]);
|
||||||
|
|
||||||
if (!href) {
|
if (!href) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -43,6 +57,8 @@ class ProtocolInformation extends React.Component<Props> {
|
|||||||
<pre>
|
<pre>
|
||||||
<code>hg clone {href}</code>
|
<code>hg clone {href}</code>
|
||||||
</pre>
|
</pre>
|
||||||
|
{emptyRepository && (
|
||||||
|
<>
|
||||||
<h4>{t("scm-hg-plugin.information.create")}</h4>
|
<h4>{t("scm-hg-plugin.information.create")}</h4>
|
||||||
<pre>
|
<pre>
|
||||||
<code>
|
<code>
|
||||||
@@ -67,6 +83,8 @@ class ProtocolInformation extends React.Component<Props> {
|
|||||||
<br />
|
<br />
|
||||||
</code>
|
</code>
|
||||||
</pre>
|
</pre>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
<h4>{t("scm-hg-plugin.information.replace")}</h4>
|
<h4>{t("scm-hg-plugin.information.replace")}</h4>
|
||||||
<pre>
|
<pre>
|
||||||
<code>
|
<code>
|
||||||
@@ -81,7 +99,6 @@ class ProtocolInformation extends React.Component<Props> {
|
|||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
|
||||||
export default withTranslation("plugins")(ProtocolInformation);
|
export default ProtocolInformation;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"information": {
|
"information": {
|
||||||
"clone" : "Repository klonen",
|
"clone" : "Repository klonen",
|
||||||
"create" : "Neues Repository erstellen",
|
"create" : "Neues Repository erstellen",
|
||||||
"replace" : "Ein bestehendes Repository aktualisieren",
|
"replace" : "Quelle zum bestehenden Repository hinzufügen",
|
||||||
"fetch": "Remote-Änderungen herunterladen",
|
"fetch": "Remote-Änderungen herunterladen",
|
||||||
"checkout": "Branch wechseln",
|
"checkout": "Branch wechseln",
|
||||||
"checkoutTag": "Tag auschecken"
|
"checkoutTag": "Tag auschecken"
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"information": {
|
"information": {
|
||||||
"clone" : "Clone the repository",
|
"clone" : "Clone the repository",
|
||||||
"create" : "Create a New Repository",
|
"create" : "Create a New Repository",
|
||||||
"replace" : "Push an Existing Repository",
|
"replace" : "Add Remote Origin to an Existing Repository",
|
||||||
"fetch": "Get Remote Changes",
|
"fetch": "Get Remote Changes",
|
||||||
"checkout": "Switch Branch",
|
"checkout": "Switch Branch",
|
||||||
"checkoutTag": "Checkout Tag"
|
"checkoutTag": "Checkout Tag"
|
||||||
|
|||||||
Reference in New Issue
Block a user