mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 15:35:49 +01:00
This adds the "resolved revision" of the HEAD (of the current branch, if branches are supported) to the extension point repos.sources.extensions. This "resolved revision" holds the current HEAD revision of the repository (or the selected branch, if branches are supported). This means you can check, whether the release has changed since an extension has been rendered for the first time. Co-authored-by: Konstantin Schaper <konstantin.schaper@cloudogu.com>
112 lines
4.3 KiB
TypeScript
112 lines
4.3 KiB
TypeScript
/*
|
|
* 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.
|
|
*/
|
|
import { Branch, BranchCollection, BranchCreation, Link, Repository } from "@scm-manager/ui-types";
|
|
import { requiredLink } from "./links";
|
|
import { useMutation, useQuery, useQueryClient } from "react-query";
|
|
import { ApiResult, ApiResultWithFetching } from "./base";
|
|
import { branchQueryKey, repoQueryKey } from "./keys";
|
|
import { apiClient } from "./apiclient";
|
|
import { concat } from "./urls";
|
|
|
|
export const useBranches = (repository: Repository): ApiResult<BranchCollection> => {
|
|
const link = requiredLink(repository, "branches");
|
|
return useQuery<BranchCollection, Error>(
|
|
repoQueryKey(repository, "branches"),
|
|
() => apiClient.get(link).then((response) => response.json())
|
|
// 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
|
|
);
|
|
};
|
|
|
|
export const useBranch = (repository: Repository, name: string): ApiResultWithFetching<Branch> => {
|
|
const link = requiredLink(repository, "branches");
|
|
return useQuery<Branch, Error>(branchQueryKey(repository, name), () =>
|
|
apiClient.get(concat(link, encodeURIComponent(name))).then((response) => response.json())
|
|
);
|
|
};
|
|
|
|
const createBranch = (link: string) => {
|
|
return (branch: BranchCreation) => {
|
|
return apiClient
|
|
.post(link, branch, "application/vnd.scmm-branchRequest+json;v=2")
|
|
.then((response) => {
|
|
const location = response.headers.get("Location");
|
|
if (!location) {
|
|
throw new Error("Server does not return required Location header");
|
|
}
|
|
return apiClient.get(location);
|
|
})
|
|
.then((response) => response.json());
|
|
};
|
|
};
|
|
|
|
export const useCreateBranch = (repository: Repository) => {
|
|
const queryClient = useQueryClient();
|
|
const link = requiredLink(repository, "branches");
|
|
const { mutate, isLoading, error, data } = useMutation<Branch, Error, BranchCreation>(createBranch(link), {
|
|
onSuccess: async (branch) => {
|
|
queryClient.setQueryData(branchQueryKey(repository, branch), branch);
|
|
await queryClient.invalidateQueries(repoQueryKey(repository, "branches"));
|
|
},
|
|
});
|
|
return {
|
|
create: (branch: BranchCreation) => mutate(branch),
|
|
isLoading,
|
|
error,
|
|
branch: data,
|
|
};
|
|
};
|
|
|
|
export const useDeleteBranch = (repository: Repository) => {
|
|
const queryClient = useQueryClient();
|
|
const { mutate, isLoading, error, data } = useMutation<unknown, Error, Branch>(
|
|
(branch) => {
|
|
const deleteUrl = (branch._links.delete as Link).href;
|
|
return apiClient.delete(deleteUrl);
|
|
},
|
|
{
|
|
onSuccess: async (_, branch) => {
|
|
queryClient.removeQueries(branchQueryKey(repository, branch));
|
|
await queryClient.invalidateQueries(repoQueryKey(repository, "branches"));
|
|
},
|
|
}
|
|
);
|
|
return {
|
|
remove: (branch: Branch) => mutate(branch),
|
|
isLoading,
|
|
error,
|
|
isDeleted: !!data,
|
|
};
|
|
};
|
|
|
|
type DefaultBranch = { defaultBranch: string };
|
|
|
|
export const useDefaultBranch = (repository: Repository): ApiResult<DefaultBranch> => {
|
|
const link = requiredLink(repository, "defaultBranch");
|
|
return useQuery<DefaultBranch, Error>(branchQueryKey(repository, "__default-branch"), () =>
|
|
apiClient.get(link).then((response) => response.json())
|
|
);
|
|
};
|