Skip to content

Commit

Permalink
fix(backend): allow a custom API root for backend
Browse files Browse the repository at this point in the history
In case of a self-hosted Gitlab, the API request to get the default
branch always call gitlab.com.
This commit allow to pass a custom API root from the config to the API
call.

Fix decaporg#7168
  • Loading branch information
JbIPS committed May 23, 2024
1 parent cdd0899 commit 59bad2b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/decap-cms-backend-gitlab/src/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export default class GitLab implements Implementation {
backend: 'gitlab',
repo: this.repo,
token: this.token,
apiRoot: this.apiRoot,
});
if (defaultBranchName) {
this.branch = defaultBranchName;
Expand Down
8 changes: 5 additions & 3 deletions packages/decap-cms-lib-util/src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ type Backend = 'github' | 'gitlab' | 'bitbucket';
type RequestConfig = Omit<RequestInit, 'headers'> &
HeaderConfig & {
backend: Backend;
apiRoot?: string;
params?: ParamObject;
};

Expand Down Expand Up @@ -199,7 +200,7 @@ export async function apiRequest(
const { token, backend, ...props } = config;
const options = { cache: 'no-cache', ...props };
const headers = await constructRequestHeaders({ headers: options.headers || {}, token });
const baseUrl = apiRoots[backend];
const baseUrl = config.apiRoot ?? apiRoots[backend];
const url = constructUrlWithParams(`${baseUrl}${path}`, options.params);
let responseStatus = 500;
try {
Expand All @@ -220,9 +221,10 @@ export async function getDefaultBranchName(configs: {
backend: Backend;
repo: string;
token?: string;
apiRoot?: string;
}) {
let apiPath;
const { token, backend, repo } = configs;
const { token, backend, repo, apiRoot } = configs;
switch (backend) {
case 'gitlab': {
apiPath = `/projects/${encodeURIComponent(repo)}`;
Expand All @@ -236,7 +238,7 @@ export async function getDefaultBranchName(configs: {
apiPath = `/repos/${repo}`;
}
}
const repoInfo = await apiRequest(apiPath, { token, backend });
const repoInfo = await apiRequest(apiPath, { token, backend, apiRoot });
let defaultBranchName;
if (backend === 'bitbucket') {
const {
Expand Down

0 comments on commit 59bad2b

Please sign in to comment.