diff --git a/dev-test/backends/azure/config.yml b/dev-test/backends/azure/config.yml index 1fed4943d378..84f2c5faa972 100644 --- a/dev-test/backends/azure/config.yml +++ b/dev-test/backends/azure/config.yml @@ -30,6 +30,7 @@ collections: - label: Publish Date name: date widget: datetime + format: 'YYYY-MM-DDTHH:mm' - label: Description name: description widget: text diff --git a/dev-test/backends/bitbucket/config.yml b/dev-test/backends/bitbucket/config.yml index b7ad4d5c13eb..f3a5d24a1e2c 100644 --- a/dev-test/backends/bitbucket/config.yml +++ b/dev-test/backends/bitbucket/config.yml @@ -28,6 +28,7 @@ collections: - label: Publish Date name: date widget: datetime + format: 'YYYY-MM-DDTHH:mm' - label: Description name: description widget: text diff --git a/dev-test/backends/git-gateway/config.yml b/dev-test/backends/git-gateway/config.yml index d6c689c80fac..b0a27e01b263 100644 --- a/dev-test/backends/git-gateway/config.yml +++ b/dev-test/backends/git-gateway/config.yml @@ -27,6 +27,7 @@ collections: - label: Publish Date name: date widget: datetime + format: 'YYYY-MM-DDTHH:mm' - label: Description name: description widget: text diff --git a/dev-test/backends/gitea/config.yml b/dev-test/backends/gitea/config.yml index 483c154e15bb..1552ad473292 100644 --- a/dev-test/backends/gitea/config.yml +++ b/dev-test/backends/gitea/config.yml @@ -28,6 +28,7 @@ collections: - label: Publish Date name: date widget: datetime + format: 'YYYY-MM-DDTHH:mm' - label: Description name: description widget: text diff --git a/dev-test/backends/github/config.yml b/dev-test/backends/github/config.yml index bf5ff5a10166..0f024c067bd6 100644 --- a/dev-test/backends/github/config.yml +++ b/dev-test/backends/github/config.yml @@ -28,6 +28,7 @@ collections: - label: Publish Date name: date widget: datetime + format: 'YYYY-MM-DDTHH:mm' - label: Description name: description widget: text diff --git a/dev-test/backends/gitlab/config.yml b/dev-test/backends/gitlab/config.yml index d7650a96c206..0b3836a1d8f5 100644 --- a/dev-test/backends/gitlab/config.yml +++ b/dev-test/backends/gitlab/config.yml @@ -28,6 +28,7 @@ collections: - label: Publish Date name: date widget: datetime + format: 'YYYY-MM-DDTHH:mm' - label: Description name: description widget: text diff --git a/dev-test/backends/proxy/config.yml b/dev-test/backends/proxy/config.yml index d5647311a18d..c8cbc9b92b9e 100644 --- a/dev-test/backends/proxy/config.yml +++ b/dev-test/backends/proxy/config.yml @@ -32,6 +32,7 @@ collections: - label: Publish Date name: date widget: datetime + format: 'YYYY-MM-DDTHH:mm' - label: Description name: description widget: text diff --git a/package-lock.json b/package-lock.json index f4425304ecd5..397f417bd711 100644 --- a/package-lock.json +++ b/package-lock.json @@ -33206,12 +33206,12 @@ } }, "packages/decap-cms": { - "version": "3.1.7", + "version": "3.1.8", "license": "MIT", "dependencies": { "codemirror": "^5.46.0", "create-react-class": "^15.7.0", - "decap-cms-app": "^3.1.7", + "decap-cms-app": "^3.1.8", "decap-cms-media-library-cloudinary": "^3.0.3", "decap-cms-media-library-uploadcare": "^3.0.2", "file-loader": "^6.2.0", @@ -33221,7 +33221,7 @@ } }, "packages/decap-cms-app": { - "version": "3.1.7", + "version": "3.1.8", "license": "MIT", "dependencies": { "@emotion/react": "^11.11.1", @@ -33230,23 +33230,23 @@ "dayjs": "^1.11.10", "decap-cms-backend-aws-cognito-github-proxy": "^3.1.2", "decap-cms-backend-azure": "^3.1.1", - "decap-cms-backend-bitbucket": "^3.1.1", + "decap-cms-backend-bitbucket": "^3.1.2", "decap-cms-backend-git-gateway": "^3.1.1", - "decap-cms-backend-github": "^3.1.1", - "decap-cms-backend-gitlab": "^3.1.2", + "decap-cms-backend-github": "^3.1.2", + "decap-cms-backend-gitlab": "^3.1.3", "decap-cms-backend-proxy": "^3.1.1", "decap-cms-backend-test": "^3.1.1", "decap-cms-core": "^3.3.6", "decap-cms-editor-component-image": "^3.1.1", "decap-cms-lib-auth": "^3.0.5", - "decap-cms-lib-util": "^3.0.3", + "decap-cms-lib-util": "^3.0.4", "decap-cms-lib-widgets": "^3.0.2", "decap-cms-locales": "^3.1.4", "decap-cms-ui-default": "^3.1.1", "decap-cms-widget-boolean": "^3.1.1", "decap-cms-widget-code": "^3.1.2", "decap-cms-widget-colorstring": "^3.1.1", - "decap-cms-widget-datetime": "^3.1.2", + "decap-cms-widget-datetime": "^3.1.3", "decap-cms-widget-file": "^3.1.1", "decap-cms-widget-image": "^3.1.1", "decap-cms-widget-list": "^3.1.1", @@ -33315,7 +33315,7 @@ } }, "packages/decap-cms-backend-bitbucket": { - "version": "3.1.1", + "version": "3.1.2", "license": "MIT", "dependencies": { "common-tags": "^1.8.0", @@ -33388,7 +33388,7 @@ } }, "packages/decap-cms-backend-github": { - "version": "3.1.1", + "version": "3.1.2", "license": "MIT", "dependencies": { "apollo-cache-inmemory": "^1.6.2", @@ -33413,7 +33413,7 @@ } }, "packages/decap-cms-backend-gitlab": { - "version": "3.1.2", + "version": "3.1.3", "license": "MIT", "dependencies": { "apollo-cache-inmemory": "^1.6.2", @@ -33610,7 +33610,7 @@ } }, "packages/decap-cms-lib-util": { - "version": "3.0.3", + "version": "3.0.4", "license": "MIT", "dependencies": { "js-sha256": "^0.9.0", @@ -33710,7 +33710,7 @@ } }, "packages/decap-cms-widget-datetime": { - "version": "3.1.2", + "version": "3.1.3", "license": "MIT", "dependencies": { "dayjs": "^1.11.10" @@ -34008,7 +34008,7 @@ } }, "packages/decap-server": { - "version": "3.0.3", + "version": "3.0.4", "license": "MIT", "dependencies": { "@hapi/joi": "^17.0.2", @@ -34032,7 +34032,7 @@ "@types/morgan": "^1.7.37", "@types/node": "^16.0.0", "@types/vfile-message": "^2.0.0", - "decap-cms-lib-util": "^3.0.3", + "decap-cms-lib-util": "^3.0.4", "jest": "^27.0.0", "nodemon": "^2.0.2", "ts-jest": "^27.0.0", diff --git a/packages/decap-cms-app/CHANGELOG.md b/packages/decap-cms-app/CHANGELOG.md index 143e412cc26f..226592b8dd2e 100644 --- a/packages/decap-cms-app/CHANGELOG.md +++ b/packages/decap-cms-app/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [3.1.8](https://github.com/decaporg/decap-cms/compare/decap-cms-app@3.1.7...decap-cms-app@3.1.8) (2024-04-03) + +**Note:** Version bump only for package decap-cms-app + ## [3.1.7](https://github.com/decaporg/decap-cms/compare/decap-cms-app@3.1.6...decap-cms-app@3.1.7) (2024-04-02) **Note:** Version bump only for package decap-cms-app diff --git a/packages/decap-cms-app/package.json b/packages/decap-cms-app/package.json index abef1f2e1366..6c0cb901a4d8 100644 --- a/packages/decap-cms-app/package.json +++ b/packages/decap-cms-app/package.json @@ -1,7 +1,7 @@ { "name": "decap-cms-app", "description": "An extensible, open source, Git-based, React CMS for static sites. Reusable congiuration with React as peer.", - "version": "3.1.7", + "version": "3.1.8", "homepage": "https://www.decapcms.org", "repository": "https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-app", "bugs": "https://github.com/decaporg/decap-cms/issues", @@ -32,23 +32,23 @@ "dayjs": "^1.11.10", "decap-cms-backend-aws-cognito-github-proxy": "^3.1.2", "decap-cms-backend-azure": "^3.1.1", - "decap-cms-backend-bitbucket": "^3.1.1", + "decap-cms-backend-bitbucket": "^3.1.2", "decap-cms-backend-git-gateway": "^3.1.1", - "decap-cms-backend-github": "^3.1.1", - "decap-cms-backend-gitlab": "^3.1.2", + "decap-cms-backend-github": "^3.1.2", + "decap-cms-backend-gitlab": "^3.1.3", "decap-cms-backend-proxy": "^3.1.1", "decap-cms-backend-test": "^3.1.1", "decap-cms-core": "^3.3.6", "decap-cms-editor-component-image": "^3.1.1", "decap-cms-lib-auth": "^3.0.5", - "decap-cms-lib-util": "^3.0.3", + "decap-cms-lib-util": "^3.0.4", "decap-cms-lib-widgets": "^3.0.2", "decap-cms-locales": "^3.1.4", "decap-cms-ui-default": "^3.1.1", "decap-cms-widget-boolean": "^3.1.1", "decap-cms-widget-code": "^3.1.2", "decap-cms-widget-colorstring": "^3.1.1", - "decap-cms-widget-datetime": "^3.1.2", + "decap-cms-widget-datetime": "^3.1.3", "decap-cms-widget-file": "^3.1.1", "decap-cms-widget-image": "^3.1.1", "decap-cms-widget-list": "^3.1.1", diff --git a/packages/decap-cms-backend-bitbucket/CHANGELOG.md b/packages/decap-cms-backend-bitbucket/CHANGELOG.md index 2f39c6d7db59..c3ff4ebf4f1c 100644 --- a/packages/decap-cms-backend-bitbucket/CHANGELOG.md +++ b/packages/decap-cms-backend-bitbucket/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [3.1.2](https://github.com/decaporg/decap-cms/compare/decap-cms-backend-bitbucket@3.1.1...decap-cms-backend-bitbucket@3.1.2) (2024-04-03) + +**Note:** Version bump only for package decap-cms-backend-bitbucket + ## [3.1.1](https://github.com/decaporg/decap-cms/compare/decap-cms-backend-bitbucket@3.1.0-beta.1...decap-cms-backend-bitbucket@3.1.1) (2024-03-21) **Note:** Version bump only for package decap-cms-backend-bitbucket diff --git a/packages/decap-cms-backend-bitbucket/package.json b/packages/decap-cms-backend-bitbucket/package.json index fbd92367d054..60b8626e23f9 100644 --- a/packages/decap-cms-backend-bitbucket/package.json +++ b/packages/decap-cms-backend-bitbucket/package.json @@ -1,7 +1,7 @@ { "name": "decap-cms-backend-bitbucket", "description": "Bitbucket backend for Decap CMS", - "version": "3.1.1", + "version": "3.1.2", "repository": "https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-backend-bitbucket", "bugs": "https://github.com/decaporg/decap-cms/issues", "module": "dist/esm/index.js", diff --git a/packages/decap-cms-backend-bitbucket/src/implementation.ts b/packages/decap-cms-backend-bitbucket/src/implementation.ts index e58afed51a64..2f6ce39d706e 100644 --- a/packages/decap-cms-backend-bitbucket/src/implementation.ts +++ b/packages/decap-cms-backend-bitbucket/src/implementation.ts @@ -60,6 +60,8 @@ type BitbucketStatusComponent = { status: string; }; +const { fetchWithTimeout: fetch } = unsentRequest; + // Implementation wrapper class export default class BitbucketBackend implements Implementation { lock: AsyncLock; @@ -72,6 +74,7 @@ export default class BitbucketBackend implements Implementation { initialWorkflowStatus: string; }; repo: string; + isBranchConfigured: boolean; branch: string; apiRoot: string; baseUrl: string; @@ -111,6 +114,7 @@ export default class BitbucketBackend implements Implementation { this.repo = config.backend.repo || ''; this.branch = config.backend.branch || 'master'; + this.isBranchConfigured = config.backend.branch ? true : false; this.apiRoot = config.backend.api_root || 'https://api.bitbucket.org/2.0'; this.baseUrl = config.base_url || ''; this.siteId = config.site_id || ''; @@ -190,6 +194,18 @@ export default class BitbucketBackend implements Implementation { async authenticate(state: Credentials) { this.token = state.token as string; + if (!this.isBranchConfigured) { + const repo = await fetch(`${this.apiRoot}/repositories/${this.repo}`, { + headers: { + Authorization: `token ${this.token}`, + }, + }) + .then(res => res.json()) + .catch(() => null); + if (repo) { + this.branch = repo.mainbranch.name; + } + } this.refreshToken = state.refresh_token; this.api = new API({ requestFunction: this.apiRequestFunction, @@ -216,7 +232,16 @@ export default class BitbucketBackend implements Implementation { if (!isCollab) { throw new Error('Your BitBucket user account does not have access to this repo.'); } - + // if (!this.isBranchConfigured) { + // const defaultBranchName = await getDefaultBranchName({ + // backend: 'bitbucket', + // repo: this.repo, + // token: this.token, + // }); + // if (defaultBranchName) { + // this.branch = defaultBranchName; + // } + // } const user = await this.api.user(); // Authorized user diff --git a/packages/decap-cms-backend-github/CHANGELOG.md b/packages/decap-cms-backend-github/CHANGELOG.md index 2319955e8499..abfc213ed331 100644 --- a/packages/decap-cms-backend-github/CHANGELOG.md +++ b/packages/decap-cms-backend-github/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [3.1.2](https://github.com/decaporg/decap-cms/compare/decap-cms-backend-github@3.1.1...decap-cms-backend-github@3.1.2) (2024-04-03) + +**Note:** Version bump only for package decap-cms-backend-github + ## [3.1.1](https://github.com/decaporg/decap-cms/compare/decap-cms-backend-github@3.1.0-beta.2...decap-cms-backend-github@3.1.1) (2024-03-21) **Note:** Version bump only for package decap-cms-backend-github diff --git a/packages/decap-cms-backend-github/package.json b/packages/decap-cms-backend-github/package.json index 33e4a07b3248..6fe111974d89 100644 --- a/packages/decap-cms-backend-github/package.json +++ b/packages/decap-cms-backend-github/package.json @@ -1,7 +1,7 @@ { "name": "decap-cms-backend-github", "description": "GitHub backend for Decap CMS", - "version": "3.1.1", + "version": "3.1.2", "license": "MIT", "repository": "https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-backend-github", "bugs": "https://github.com/decaporg/decap-cms/issues", diff --git a/packages/decap-cms-backend-github/src/implementation.tsx b/packages/decap-cms-backend-github/src/implementation.tsx index f5412b974c2d..31149d8e9f26 100644 --- a/packages/decap-cms-backend-github/src/implementation.tsx +++ b/packages/decap-cms-backend-github/src/implementation.tsx @@ -69,6 +69,7 @@ export default class GitHub implements Implementation { initialWorkflowStatus: string; }; originRepo: string; + isBranchConfigured: boolean; repo?: string; openAuthoringEnabled: boolean; useOpenAuthoring?: boolean; @@ -106,7 +107,7 @@ export default class GitHub implements Implementation { } this.api = this.options.API || null; - + this.isBranchConfigured = config.backend.branch ? true : false; this.openAuthoringEnabled = config.backend.open_authoring || false; if (this.openAuthoringEnabled) { if (!this.options.useWorkflow) { @@ -320,6 +321,18 @@ export default class GitHub implements Implementation { async authenticate(state: Credentials) { this.token = state.token as string; + // Query the default branch name when the `branch` property is missing + // in the config file + if (!this.isBranchConfigured) { + const repoInfo = await fetch(`${this.apiRoot}/repos/${this.originRepo}`, { + headers: { Authorization: `token ${this.token}` }, + }) + .then(res => res.json()) + .catch(() => null); + if (repoInfo && repoInfo.default_branch) { + this.branch = repoInfo.default_branch; + } + } const apiCtor = this.useGraphql ? GraphQLAPI : API; this.api = new apiCtor({ token: this.token, @@ -354,6 +367,13 @@ export default class GitHub implements Implementation { throw new Error('Your GitHub user account does not have access to this repo.'); } + // if (!this.isBranchConfigured) { + // const defaultBranchName = await this.api.getDefaultBranchName() + // if (defaultBranchName) { + // this.branch = defaultBranchName; + // } + // } + // Authorized user return { ...user, token: state.token as string, useOpenAuthoring: this.useOpenAuthoring }; } diff --git a/packages/decap-cms-backend-gitlab/CHANGELOG.md b/packages/decap-cms-backend-gitlab/CHANGELOG.md index ced17215a384..ccadf761e4f2 100644 --- a/packages/decap-cms-backend-gitlab/CHANGELOG.md +++ b/packages/decap-cms-backend-gitlab/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [3.1.3](https://github.com/decaporg/decap-cms/compare/decap-cms-backend-gitlab@3.1.2...decap-cms-backend-gitlab@3.1.3) (2024-04-03) + +**Note:** Version bump only for package decap-cms-backend-gitlab + ## [3.1.2](https://github.com/decaporg/decap-cms/compare/decap-cms-backend-gitlab@3.1.1...decap-cms-backend-gitlab@3.1.2) (2024-03-21) **Note:** Version bump only for package decap-cms-backend-gitlab diff --git a/packages/decap-cms-backend-gitlab/package.json b/packages/decap-cms-backend-gitlab/package.json index a8954a6bb929..8648d4a7ffb1 100644 --- a/packages/decap-cms-backend-gitlab/package.json +++ b/packages/decap-cms-backend-gitlab/package.json @@ -1,7 +1,7 @@ { "name": "decap-cms-backend-gitlab", "description": "GitLab backend for Decap CMS", - "version": "3.1.2", + "version": "3.1.3", "repository": "https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-backend-gitlab", "bugs": "https://github.com/decaporg/decap-cms/issues", "license": "MIT", diff --git a/packages/decap-cms-backend-gitlab/src/__tests__/gitlab.spec.js b/packages/decap-cms-backend-gitlab/src/__tests__/gitlab.spec.js index e5afc7ce783d..4953ac3e4a0d 100644 --- a/packages/decap-cms-backend-gitlab/src/__tests__/gitlab.spec.js +++ b/packages/decap-cms-backend-gitlab/src/__tests__/gitlab.spec.js @@ -109,6 +109,7 @@ const resp = { access_level: 30, }, }, + default_branch: 'main', }, readOnly: { permissions: { @@ -194,7 +195,16 @@ describe('gitlab backend', () => { .reply(200, userResponse || resp.user.success); api + // The `authenticate` method of the API class from netlify-cms-backend-gitlab + // calls the same endpoint twice for gettng a single project. + // First time through `this.api.hasWriteAccess() + // Second time through the method `getDefaultBranchName` from lib-util + // As a result, we need to repeat the same response twice. + // Otherwise, we'll get an error: "No match for request to + // https://gitlab.com/api/v4" + .get(expectedRepoUrl) + .times(2) .query(true) .reply(200, projectResponse || resp.project.success); } diff --git a/packages/decap-cms-backend-gitlab/src/implementation.ts b/packages/decap-cms-backend-gitlab/src/implementation.ts index 50853f438319..769257f0bea3 100644 --- a/packages/decap-cms-backend-gitlab/src/implementation.ts +++ b/packages/decap-cms-backend-gitlab/src/implementation.ts @@ -21,6 +21,7 @@ import { allEntriesByFolder, filterByExtension, branchFromContentKey, + getDefaultBranchName, } from 'decap-cms-lib-util'; import AuthenticationPage from './AuthenticationPage'; @@ -53,6 +54,7 @@ export default class GitLab implements Implementation { initialWorkflowStatus: string; }; repo: string; + isBranchConfigured: boolean; branch: string; apiRoot: string; token: string | null; @@ -84,6 +86,7 @@ export default class GitLab implements Implementation { this.repo = config.backend.repo || ''; this.branch = config.backend.branch || 'master'; + this.isBranchConfigured = config.backend.branch ? true : false; this.apiRoot = config.backend.api_root || 'https://gitlab.com/api/v4'; this.token = ''; this.squashMerges = config.backend.squash_merges || false; @@ -150,6 +153,16 @@ export default class GitLab implements Implementation { throw new Error('Your GitLab user account does not have access to this repo.'); } + if (!this.isBranchConfigured) { + const defaultBranchName = await getDefaultBranchName({ + backend: 'gitlab', + repo: this.repo, + token: this.token, + }); + if (defaultBranchName) { + this.branch = defaultBranchName; + } + } // Authorized user return { ...user, login: user.username, token: state.token as string }; } diff --git a/packages/decap-cms-lib-util/CHANGELOG.md b/packages/decap-cms-lib-util/CHANGELOG.md index f2436c8a345d..e2afd5e37df9 100644 --- a/packages/decap-cms-lib-util/CHANGELOG.md +++ b/packages/decap-cms-lib-util/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [3.0.4](https://github.com/decaporg/decap-cms/compare/decap-cms-lib-util@3.0.3...decap-cms-lib-util@3.0.4) (2024-04-03) + +**Note:** Version bump only for package decap-cms-lib-util + ## [3.0.3](https://github.com/decaporg/decap-cms/compare/decap-cms-lib-util@3.0.2...decap-cms-lib-util@3.0.3) (2024-03-21) **Note:** Version bump only for package decap-cms-lib-util diff --git a/packages/decap-cms-lib-util/package.json b/packages/decap-cms-lib-util/package.json index b4946d411493..b874570e96f4 100644 --- a/packages/decap-cms-lib-util/package.json +++ b/packages/decap-cms-lib-util/package.json @@ -1,7 +1,7 @@ { "name": "decap-cms-lib-util", "description": "Shared utilities for Decap CMS.", - "version": "3.0.3", + "version": "3.0.4", "repository": "https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-lib-util", "bugs": "https://github.com/decaporg/decap-cms/issues", "module": "dist/esm/index.js", diff --git a/packages/decap-cms-lib-util/src/API.ts b/packages/decap-cms-lib-util/src/API.ts index f328cf78292e..277f045d3603 100644 --- a/packages/decap-cms-lib-util/src/API.ts +++ b/packages/decap-cms-lib-util/src/API.ts @@ -40,6 +40,26 @@ class RateLimitError extends Error { } } +async function parseJsonResponse(response: Response) { + const json = await response.json(); + if (!response.ok) { + return Promise.reject(json); + } + return json; +} + +export function parseResponse(response: Response) { + const contentType = response.headers.get('Content-Type'); + if (contentType && contentType.match(/json/)) { + return parseJsonResponse(response); + } + const textPromise = response.text().then(text => { + if (!response.ok) return Promise.reject(text); + return text; + }); + return textPromise; +} + export async function requestWithBackoff( api: API, req: ApiRequest, @@ -96,6 +116,140 @@ export async function requestWithBackoff( } } +// Options is an object which contains all the standard network request properties +// for modifying HTTP requests and may contains `params` property + +type Param = string | number; + +type ParamObject = Record; + +type HeaderObj = Record; + +type HeaderConfig = { + headers?: HeaderObj; + token?: string | undefined; +}; + +type Backend = 'github' | 'gitlab' | 'bitbucket'; + +// RequestConfig contains all the standard properties of a Request object and +// several custom properties: +// - "headers" property is an object whose properties and values are string types +// - `token` property to allow passing tokens for users using a private repo. +// - `params` property for customizing response +// - `backend`(compulsory) to specify which backend to be used: Github, Gitlab etc. + +type RequestConfig = Omit & + HeaderConfig & { + backend: Backend; + params?: ParamObject; + }; + +export const apiRoots = { + github: 'https://api.github.com', + gitlab: 'https://gitlab.com/api/v4', + bitbucket: 'https://api.bitbucket.org/2.0', +}; + +export const endpointConstants = { + singleRepo: { + bitbucket: '/repositories', + github: '/repos', + gitlab: '/projects', + }, +}; + +const api = { + buildRequest(req: ApiRequest) { + return req; + }, +}; + +function constructUrlWithParams(url: string, params?: ParamObject) { + if (params) { + const paramList = []; + for (const key in params) { + paramList.push(`${key}=${encodeURIComponent(params[key])}`); + } + if (paramList.length) { + url += `?${paramList.join('&')}`; + } + } + return url; +} + +async function constructRequestHeaders(headerConfig: HeaderConfig) { + const { token, headers } = headerConfig; + const baseHeaders: HeaderObj = { 'Content-Type': 'application/json; charset=utf-8', ...headers }; + if (token) { + baseHeaders['Authorization'] = `token ${token}`; + } + return Promise.resolve(baseHeaders); +} + +function handleRequestError(error: FetchError, responseStatus: number, backend: Backend) { + throw new APIError(error.message, responseStatus, backend); +} + +export async function apiRequest( + path: string, + config: RequestConfig, + parser = (response: Response) => parseResponse(response), +) { + const { token, backend, ...props } = config; + const options = { cache: 'no-cache', ...props }; + const headers = await constructRequestHeaders({ headers: options.headers || {}, token }); + const baseUrl = apiRoots[backend]; + const url = constructUrlWithParams(`${baseUrl}${path}`, options.params); + let responseStatus = 500; + try { + const req = unsentRequest.fromFetchArguments(url, { + ...options, + headers, + }) as unknown as ApiRequest; + const response = await requestWithBackoff(api, req); + responseStatus = response.status; + const parsedResponse = await parser(response); + return parsedResponse; + } catch (error) { + return handleRequestError(error, responseStatus, backend); + } +} + +export async function getDefaultBranchName(configs: { + backend: Backend; + repo: string; + token?: string; +}) { + let apiPath; + const { token, backend, repo } = configs; + switch (backend) { + case 'gitlab': { + apiPath = `/projects/${encodeURIComponent(repo)}`; + break; + } + case 'bitbucket': { + apiPath = `/repositories/${repo}`; + break; + } + default: { + apiPath = `/repos/${repo}`; + } + } + const repoInfo = await apiRequest(apiPath, { token, backend }); + let defaultBranchName; + if (backend === 'bitbucket') { + const { + mainbranch: { name }, + } = repoInfo; + defaultBranchName = name; + } else { + const { default_branch } = repoInfo; + defaultBranchName = default_branch; + } + return defaultBranchName; +} + export async function readFile( id: string | null | undefined, fetchContent: () => Promise, diff --git a/packages/decap-cms-lib-util/src/__tests__/api.spec.js b/packages/decap-cms-lib-util/src/__tests__/api.spec.js index 675ff0458e36..6cf5e07806d0 100644 --- a/packages/decap-cms-lib-util/src/__tests__/api.spec.js +++ b/packages/decap-cms-lib-util/src/__tests__/api.spec.js @@ -1,4 +1,5 @@ import * as api from '../API'; + describe('Api', () => { describe('getPreviewStatus', () => { it('should return preview status on matching context', () => { diff --git a/packages/decap-cms-lib-util/src/index.ts b/packages/decap-cms-lib-util/src/index.ts index 962c91fd3e25..9881366de927 100644 --- a/packages/decap-cms-lib-util/src/index.ts +++ b/packages/decap-cms-lib-util/src/index.ts @@ -34,6 +34,7 @@ import { getPreviewStatus, PreviewState, requestWithBackoff, + getDefaultBranchName, throwOnConflictingBranches, } from './API'; import { @@ -148,6 +149,7 @@ export const DecapCmsLibUtil = { contentKeyFromBranch, blobToFileObj, requestWithBackoff, + getDefaultBranchName, allEntriesByFolder, AccessTokenError, throwOnConflictingBranches, @@ -204,6 +206,7 @@ export { contentKeyFromBranch, blobToFileObj, requestWithBackoff, + getDefaultBranchName, allEntriesByFolder, AccessTokenError, throwOnConflictingBranches, diff --git a/packages/decap-cms-widget-datetime/CHANGELOG.md b/packages/decap-cms-widget-datetime/CHANGELOG.md index d1c069c2da2b..42487d4fc4ae 100644 --- a/packages/decap-cms-widget-datetime/CHANGELOG.md +++ b/packages/decap-cms-widget-datetime/CHANGELOG.md @@ -3,6 +3,12 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [3.1.3](https://github.com/decaporg/decap-cms/compare/decap-cms-widget-datetime@3.1.2...decap-cms-widget-datetime@3.1.3) (2024-04-03) + +### Bug Fixes + +- **datetime-widget:** revert default date format to include timezone ([#7165](https://github.com/decaporg/decap-cms/issues/7165)) ([46294d6](https://github.com/decaporg/decap-cms/commit/46294d6fcd7f785f8c6346baeaa703d72b833b3b)) + ## [3.1.2](https://github.com/decaporg/decap-cms/compare/decap-cms-widget-datetime@3.1.1...decap-cms-widget-datetime@3.1.2) (2024-03-29) **Note:** Version bump only for package decap-cms-widget-datetime diff --git a/packages/decap-cms-widget-datetime/package.json b/packages/decap-cms-widget-datetime/package.json index 8bc473b5bdf0..adef263601ac 100644 --- a/packages/decap-cms-widget-datetime/package.json +++ b/packages/decap-cms-widget-datetime/package.json @@ -1,7 +1,7 @@ { "name": "decap-cms-widget-datetime", "description": "Widget for editing dates and times in Decap CMS.", - "version": "3.1.2", + "version": "3.1.3", "homepage": "https://www.decapcms.org/docs/widgets/#datetime", "repository": "https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-widget-datetime", "bugs": "https://github.com/decaporg/decap-cms/issues", diff --git a/packages/decap-cms-widget-datetime/src/DateTimeControl.js b/packages/decap-cms-widget-datetime/src/DateTimeControl.js index 78659bb4af78..42d456bc38bc 100644 --- a/packages/decap-cms-widget-datetime/src/DateTimeControl.js +++ b/packages/decap-cms-widget-datetime/src/DateTimeControl.js @@ -64,7 +64,7 @@ class DateTimeControl extends React.Component { getFormat() { const { field } = this.props; - const format = field?.get('format') || 'YYYY-MM-DDTHH:mm'; + const format = field?.get('format') || 'YYYY-MM-DDTHH:mm:ss.SSS[Z]'; const dateFormat = field?.get('date_format'); const timeFormat = field?.get('time_format'); let inputFormat = 'YYYY-MM-DDTHH:mm'; diff --git a/packages/decap-cms/CHANGELOG.md b/packages/decap-cms/CHANGELOG.md index 501d9a3d5973..105a2cba1e17 100644 --- a/packages/decap-cms/CHANGELOG.md +++ b/packages/decap-cms/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [3.1.8](https://github.com/decaporg/decap-cms/compare/decap-cms@3.1.7...decap-cms@3.1.8) (2024-04-03) + +**Note:** Version bump only for package decap-cms + ## [3.1.7](https://github.com/decaporg/decap-cms/compare/decap-cms@3.1.6...decap-cms@3.1.7) (2024-04-02) **Note:** Version bump only for package decap-cms diff --git a/packages/decap-cms/package.json b/packages/decap-cms/package.json index ba530d14ba5d..b28ae9ab7ce6 100644 --- a/packages/decap-cms/package.json +++ b/packages/decap-cms/package.json @@ -1,7 +1,7 @@ { "name": "decap-cms", "description": "An extensible, open source, Git-based, React CMS for static sites.", - "version": "3.1.7", + "version": "3.1.8", "homepage": "https://www.decapcms.org", "repository": "https://github.com/decaporg/decap-cms", "bugs": "https://github.com/decaporg/decap-cms/issues", @@ -22,7 +22,7 @@ "dependencies": { "codemirror": "^5.46.0", "create-react-class": "^15.7.0", - "decap-cms-app": "^3.1.7", + "decap-cms-app": "^3.1.8", "decap-cms-media-library-cloudinary": "^3.0.3", "decap-cms-media-library-uploadcare": "^3.0.2", "file-loader": "^6.2.0", diff --git a/packages/decap-server/CHANGELOG.md b/packages/decap-server/CHANGELOG.md index fcfab347958d..7a28cc5de038 100644 --- a/packages/decap-server/CHANGELOG.md +++ b/packages/decap-server/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [3.0.4](https://github.com/decaporg/decap-cms/compare/decap-server@3.0.3...decap-server@3.0.4) (2024-04-03) + +**Note:** Version bump only for package decap-server + ## [3.0.3](https://github.com/decaporg/decap-cms/compare/decap-server@3.0.2...decap-server@3.0.3) (2024-03-21) **Note:** Version bump only for package decap-server diff --git a/packages/decap-server/package.json b/packages/decap-server/package.json index 0052615122bc..252e33b46728 100644 --- a/packages/decap-server/package.json +++ b/packages/decap-server/package.json @@ -1,7 +1,7 @@ { "name": "decap-server", "description": "Proxy server to be used with Decap CMS proxy backend", - "version": "3.0.3", + "version": "3.0.4", "repository": "https://github.com/decaporg/decap-cms/tree/main/packages/decap-server", "bugs": "https://github.com/decaporg/decap-cms/issues", "license": "MIT", @@ -39,7 +39,7 @@ "@types/morgan": "^1.7.37", "@types/node": "^16.0.0", "@types/vfile-message": "^2.0.0", - "decap-cms-lib-util": "^3.0.3", + "decap-cms-lib-util": "^3.0.4", "jest": "^27.0.0", "nodemon": "^2.0.2", "ts-jest": "^27.0.0",