2021-02-24 08:17:40 +01:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2021-12-01 14:19:18 +01:00
|
|
|
import {
|
|
|
|
|
Branch,
|
|
|
|
|
BranchCollection,
|
|
|
|
|
BranchCreation,
|
2021-12-15 15:03:40 +01:00
|
|
|
BranchDetails,
|
2021-12-01 14:19:18 +01:00
|
|
|
BranchDetailsCollection,
|
2021-12-15 15:03:40 +01:00
|
|
|
Link,
|
|
|
|
|
NamespaceAndName,
|
2021-12-01 14:19:18 +01:00
|
|
|
Repository
|
|
|
|
|
} from "@scm-manager/ui-types";
|
2021-02-24 08:17:40 +01:00
|
|
|
import { requiredLink } from "./links";
|
2021-12-01 14:19:18 +01:00
|
|
|
import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from "react-query";
|
2021-10-20 13:29:47 +02:00
|
|
|
import { ApiResult, ApiResultWithFetching } from "./base";
|
2021-02-24 08:17:40 +01:00
|
|
|
import { branchQueryKey, repoQueryKey } from "./keys";
|
|
|
|
|
import { apiClient } from "./apiclient";
|
|
|
|
|
import { concat } from "./urls";
|
2021-12-01 14:19:18 +01:00
|
|
|
import { useEffect } from "react";
|
2021-02-24 08:17:40 +01:00
|
|
|
|
|
|
|
|
export const useBranches = (repository: Repository): ApiResult<BranchCollection> => {
|
2021-12-01 14:19:18 +01:00
|
|
|
const queryClient = useQueryClient();
|
2021-02-24 08:17:40 +01:00
|
|
|
const link = requiredLink(repository, "branches");
|
|
|
|
|
return useQuery<BranchCollection, Error>(
|
|
|
|
|
repoQueryKey(repository, "branches"),
|
2021-12-01 14:19:18 +01:00
|
|
|
() => apiClient.get(link).then(response => response.json()),
|
|
|
|
|
{
|
|
|
|
|
onSuccess: () => {
|
2022-03-14 09:46:11 +01:00
|
|
|
return queryClient.invalidateQueries(branchDetailsQueryKey(repository));
|
2021-12-01 14:19:18 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-02-24 08:17:40 +01:00
|
|
|
// we do not populate the cache for a single branch,
|
|
|
|
|
// because we have no pagination for branches and if we have a lot of them
|
|
|
|
|
// the population slows us down
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-20 13:29:47 +02:00
|
|
|
export const useBranch = (repository: Repository, name: string): ApiResultWithFetching<Branch> => {
|
2021-02-24 08:17:40 +01:00
|
|
|
const link = requiredLink(repository, "branches");
|
|
|
|
|
return useQuery<Branch, Error>(branchQueryKey(repository, name), () =>
|
2021-12-01 14:19:18 +01:00
|
|
|
apiClient.get(concat(link, encodeURIComponent(name))).then(response => response.json())
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function chunkBranches(branches: Branch[]) {
|
|
|
|
|
const chunks: Branch[][] = [];
|
|
|
|
|
const chunkSize = 5;
|
|
|
|
|
let chunkIndex = 0;
|
|
|
|
|
for (const branch of branches) {
|
|
|
|
|
if (!chunks[chunkIndex]) {
|
|
|
|
|
chunks[chunkIndex] = [];
|
|
|
|
|
}
|
|
|
|
|
chunks[chunkIndex].push(branch);
|
|
|
|
|
if (chunks[chunkIndex].length >= chunkSize) {
|
|
|
|
|
chunkIndex = chunkIndex + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return chunks;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-14 09:46:11 +01:00
|
|
|
const branchDetailsQueryKey = (repository: NamespaceAndName, branch: string | undefined = undefined) => {
|
2021-12-10 11:04:59 +01:00
|
|
|
let branchName;
|
|
|
|
|
if (!branch) {
|
|
|
|
|
branchName = "_";
|
|
|
|
|
} else {
|
|
|
|
|
branchName = branch;
|
|
|
|
|
}
|
2021-12-15 15:03:40 +01:00
|
|
|
return [...repoQueryKey(repository), "branch-details", branchName];
|
2021-12-10 11:04:59 +01:00
|
|
|
};
|
|
|
|
|
|
2021-12-01 14:19:18 +01:00
|
|
|
export const useBranchDetailsCollection = (repository: Repository, branches: Branch[]) => {
|
|
|
|
|
const link = requiredLink(repository, "branchDetailsCollection");
|
|
|
|
|
const chunks = chunkBranches(branches);
|
2021-12-15 15:03:40 +01:00
|
|
|
const queryClient = useQueryClient();
|
2021-12-01 14:19:18 +01:00
|
|
|
|
|
|
|
|
const { data, isLoading, error, fetchNextPage } = useInfiniteQuery<
|
|
|
|
|
BranchDetailsCollection,
|
|
|
|
|
Error,
|
|
|
|
|
BranchDetailsCollection
|
|
|
|
|
>(
|
2021-12-10 11:04:59 +01:00
|
|
|
branchDetailsQueryKey(repository),
|
2021-12-01 14:19:18 +01:00
|
|
|
({ pageParam = 0 }) => {
|
2021-12-02 14:21:46 +01:00
|
|
|
const encodedBranches = chunks[pageParam]?.map(b => encodeURIComponent(b.name)).join("&branches=");
|
2021-12-01 14:19:18 +01:00
|
|
|
return apiClient.get(concat(link, `?branches=${encodedBranches}`)).then(response => response.json());
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
getNextPageParam: (lastPage, allPages) => {
|
|
|
|
|
if (allPages.length >= chunks.length) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
return allPages.length;
|
2021-12-15 15:03:40 +01:00
|
|
|
},
|
|
|
|
|
onSuccess: newData => {
|
|
|
|
|
newData.pages
|
|
|
|
|
.flatMap(d => d._embedded?.branchDetails)
|
|
|
|
|
.filter(d => !!d)
|
|
|
|
|
.forEach(d => queryClient.setQueryData(branchDetailsQueryKey(repository, d!.branchName), () => d));
|
2021-12-01 14:19:18 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-02-24 08:17:40 +01:00
|
|
|
);
|
2021-12-01 14:19:18 +01:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
fetchNextPage();
|
|
|
|
|
}, [data, fetchNextPage]);
|
|
|
|
|
|
|
|
|
|
return {
|
2021-12-02 14:21:46 +01:00
|
|
|
data: data?.pages?.map(d => d._embedded?.branchDetails).flat(1),
|
2021-12-01 14:19:18 +01:00
|
|
|
isLoading,
|
|
|
|
|
error
|
|
|
|
|
};
|
2021-02-24 08:17:40 +01:00
|
|
|
};
|
|
|
|
|
|
2021-12-15 15:03:40 +01:00
|
|
|
export const useBranchDetails = (repository: Repository, branch: Branch) => {
|
|
|
|
|
const link = (branch._links.details as Link).href;
|
|
|
|
|
const queryKey = branchDetailsQueryKey(repository, branch.name);
|
|
|
|
|
return useQuery<BranchDetails, Error>(queryKey, () => apiClient.get(link).then(response => response.json()));
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-24 08:17:40 +01:00
|
|
|
const createBranch = (link: string) => {
|
|
|
|
|
return (branch: BranchCreation) => {
|
|
|
|
|
return apiClient
|
|
|
|
|
.post(link, branch, "application/vnd.scmm-branchRequest+json;v=2")
|
2021-12-01 14:19:18 +01:00
|
|
|
.then(response => {
|
2021-02-24 08:17:40 +01:00
|
|
|
const location = response.headers.get("Location");
|
|
|
|
|
if (!location) {
|
|
|
|
|
throw new Error("Server does not return required Location header");
|
|
|
|
|
}
|
|
|
|
|
return apiClient.get(location);
|
|
|
|
|
})
|
2021-12-01 14:19:18 +01:00
|
|
|
.then(response => response.json());
|
2021-02-24 08:17:40 +01:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useCreateBranch = (repository: Repository) => {
|
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
const link = requiredLink(repository, "branches");
|
|
|
|
|
const { mutate, isLoading, error, data } = useMutation<Branch, Error, BranchCreation>(createBranch(link), {
|
2021-12-01 14:19:18 +01:00
|
|
|
onSuccess: async branch => {
|
2021-02-24 08:17:40 +01:00
|
|
|
queryClient.setQueryData(branchQueryKey(repository, branch), branch);
|
|
|
|
|
await queryClient.invalidateQueries(repoQueryKey(repository, "branches"));
|
2021-12-01 14:19:18 +01:00
|
|
|
}
|
2021-02-24 08:17:40 +01:00
|
|
|
});
|
|
|
|
|
return {
|
|
|
|
|
create: (branch: BranchCreation) => mutate(branch),
|
|
|
|
|
isLoading,
|
|
|
|
|
error,
|
2021-12-01 14:19:18 +01:00
|
|
|
branch: data
|
2021-02-24 08:17:40 +01:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useDeleteBranch = (repository: Repository) => {
|
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
const { mutate, isLoading, error, data } = useMutation<unknown, Error, Branch>(
|
2021-12-01 14:19:18 +01:00
|
|
|
branch => {
|
2021-02-24 08:17:40 +01:00
|
|
|
const deleteUrl = (branch._links.delete as Link).href;
|
|
|
|
|
return apiClient.delete(deleteUrl);
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
onSuccess: async (_, branch) => {
|
2021-06-28 13:19:03 +02:00
|
|
|
queryClient.removeQueries(branchQueryKey(repository, branch));
|
2021-02-24 08:17:40 +01:00
|
|
|
await queryClient.invalidateQueries(repoQueryKey(repository, "branches"));
|
2021-12-01 14:19:18 +01:00
|
|
|
}
|
2021-02-24 08:17:40 +01:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
return {
|
|
|
|
|
remove: (branch: Branch) => mutate(branch),
|
|
|
|
|
isLoading,
|
|
|
|
|
error,
|
2021-12-01 14:19:18 +01:00
|
|
|
isDeleted: !!data
|
2021-02-24 08:17:40 +01:00
|
|
|
};
|
|
|
|
|
};
|
2021-07-28 15:04:00 +02:00
|
|
|
|
|
|
|
|
type DefaultBranch = { defaultBranch: string };
|
|
|
|
|
|
|
|
|
|
export const useDefaultBranch = (repository: Repository): ApiResult<DefaultBranch> => {
|
|
|
|
|
const link = requiredLink(repository, "defaultBranch");
|
|
|
|
|
return useQuery<DefaultBranch, Error>(branchQueryKey(repository, "__default-branch"), () =>
|
2021-12-01 14:19:18 +01:00
|
|
|
apiClient.get(link).then(response => response.json())
|
2021-07-28 15:04:00 +02:00
|
|
|
);
|
|
|
|
|
};
|