chore: autocommit feature branch (#34062)

## Description
The changes are related to the "auto-commit" migration feature.
**Server changes**
- Fixed issue with concurrent git calls for the same repo
- Trigger issues with auto-commit

**UI changes**
- Introduced new REST api for triggering auto-commit via client
- Updated logic for saga to use trigger auto-commit api for checking
whether to poll for auto-commit progress
- Updated logic to make status api call blocking
- Response structure change for auto-commit apis


Fixes #30110   
_or_  
Fixes `Issue URL`
> [!WARNING]  
> _If no issue exists, please create an issue first, and check with the
maintainers if the issue is valid._

## Automation

/ok-to-test tags="@tag.All"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/9419768186>
> Commit: 129eaee76d3810e5e0ea9b6391048ff9338c9c8a
> Cypress dashboard url: <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=9419768186&attempt=2"
target="_blank">Click here!</a>

<!-- end of auto-generated comment: Cypress test results  -->













## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Introduced enhanced Git autocommit functionality with improved error
handling and progress tracking.
- Added new autocommit actions and state management for better
synchronization.

- **Bug Fixes**
  - Improved error handling during Git synchronization processes.

- **Refactor**
- Updated method names and parameters for clarity and consistency in Git
synchronization.

- **Chores**
- Renamed feature flags and constants related to Git autocommit for
better readability and maintainability.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: brayn003 <rudra@appsmith.com>
This commit is contained in:
Manish Kumar 2024-06-10 12:35:23 +05:30 committed by GitHub
parent 187c543b9e
commit bf92a52f5a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
50 changed files with 1768 additions and 711 deletions

View File

@ -6,7 +6,10 @@ import {
ReduxActionErrorTypes,
ReduxActionTypes,
} from "@appsmith/constants/ReduxActionConstants";
import type { ConnectToGitPayload } from "api/GitSyncAPI";
import type {
ConnectToGitPayload,
GitAutocommitProgressResponse,
} from "api/GitSyncAPI";
import type { GitConfig, GitSyncModalTab, MergeStatus } from "entities/GitSync";
import type { GitApplicationMetadata } from "@appsmith/api/ApplicationApi";
import {
@ -480,6 +483,7 @@ export const setShowBranchPopupAction = (show: boolean) => {
};
};
// START autocommit
export const toggleAutocommitEnabledInit = () => ({
type: ReduxActionTypes.GIT_TOGGLE_AUTOCOMMIT_ENABLED_INIT,
});
@ -489,14 +493,60 @@ export const setIsAutocommitModalOpen = (isAutocommitModalOpen: boolean) => ({
payload: { isAutocommitModalOpen },
});
export const startAutocommitProgressPolling = () => ({
type: ReduxActionTypes.GIT_AUTOCOMMIT_INITIATE_PROGRESS_POLLING,
export const triggerAutocommitInitAction = () => ({
type: ReduxActionTypes.GIT_AUTOCOMMIT_TRIGGER_INIT,
});
export const stopAutocommitProgressPolling = () => ({
export const triggerAutocommitSuccessAction = () => ({
type: ReduxActionTypes.GIT_AUTOCOMMIT_TRIGGER_SUCCESS,
});
export interface TriggerAutocommitErrorActionPayload {
error: any;
show: boolean;
}
export const triggerAutocommitErrorAction = (
payload: TriggerAutocommitErrorActionPayload,
) => ({
type: ReduxActionErrorTypes.GIT_AUTOCOMMIT_TRIGGER_ERROR,
payload,
});
export const startAutocommitProgressPollingAction = () => ({
type: ReduxActionTypes.GIT_AUTOCOMMIT_START_PROGRESS_POLLING,
});
export const stopAutocommitProgressPollingAction = () => ({
type: ReduxActionTypes.GIT_AUTOCOMMIT_STOP_PROGRESS_POLLING,
});
export type SetAutocommitActionPayload = GitAutocommitProgressResponse;
export const setAutocommitProgressAction = (
payload: SetAutocommitActionPayload,
) => ({
type: ReduxActionTypes.GIT_SET_AUTOCOMMIT_PROGRESS,
payload,
});
export const resetAutocommitProgressAction = () => ({
type: ReduxActionTypes.GIT_RESET_AUTOCOMMIT_PROGRESS,
});
export interface AutocommitProgressErrorActionPayload {
error: any;
show: boolean;
}
export const autoCommitProgressErrorAction = (
payload: AutocommitProgressErrorActionPayload,
) => ({
type: ReduxActionErrorTypes.GIT_AUTOCOMMIT_PROGRESS_POLLING_ERROR,
payload,
});
// END autocommit
export const getGitMetadataInitAction = () => ({
type: ReduxActionTypes.GIT_GET_METADATA_INIT,
});

View File

@ -43,6 +43,27 @@ interface GitRemoteStatusParam {
branch: string;
}
export enum AutocommitResponseEnum {
IN_PROGRESS = "IN_PROGRESS",
LOCKED = "LOCKED",
PUBLISHED = "PUBLISHED",
IDLE = "IDLE",
NOT_REQUIRED = "NOT_REQUIRED",
NON_GIT_APP = "NON_GIT_APP",
}
export interface GitAutocommitProgressResponse {
autoCommitResponse: AutocommitResponseEnum;
progress: number;
branchName: string;
}
export interface GitTriggerAutocommitResponse {
autoCommitResponse: AutocommitResponseEnum;
progress: number;
branchName: string;
}
class GitSyncAPI extends Api {
static baseURL = `/v1/git`;
@ -227,6 +248,12 @@ class GitSyncAPI extends Api {
);
}
static async triggerAutocommit(applicationId: string, branchName: string) {
return Api.post(
`${GitSyncAPI.baseURL}/auto-commit/app/${applicationId}?branchName=${branchName}`,
);
}
static async getAutocommitProgress(applicationId: string) {
return Api.get(
`${GitSyncAPI.baseURL}/auto-commit/progress/app/${applicationId}`,

View File

@ -131,8 +131,8 @@ const ActionTypes = {
GIT_TOGGLE_AUTOCOMMIT_ENABLED_INIT: "GIT_TOGGLE_AUTOCOMMIT_ENABLED_INIT",
GIT_TOGGLE_AUTOCOMMIT_ENABLED_SUCCESS:
"GIT_TOGGLE_AUTOCOMMIT_ENABLED_SUCCESS",
GIT_AUTOCOMMIT_INITIATE_PROGRESS_POLLING:
"GIT_AUTOCOMMIT_INITIATE_PROGRESS_POLLING",
GIT_AUTOCOMMIT_TRIGGER_INIT: "GIT_AUTOCOMMIT_TRIGGER_INIT",
GIT_AUTOCOMMIT_TRIGGER_SUCCESS: "GIT_AUTOCOMMIT_TRIGGER_SUCCESS",
GIT_AUTOCOMMIT_START_PROGRESS_POLLING:
"GIT_AUTOCOMMIT_START_PROGRESS_POLLING",
GIT_AUTOCOMMIT_STOP_PROGRESS_POLLING: "GIT_AUTOCOMMIT_STOP_PROGRESS_POLLING",
@ -962,6 +962,7 @@ export const ReduxActionErrorTypes = {
GIT_TOGGLE_AUTOCOMMIT_ENABLED_ERROR: "GIT_TOGGLE_AUTOCOMMIT_ENABLED_ERROR",
GIT_AUTOCOMMIT_PROGRESS_POLLING_ERROR:
"GIT_AUTOCOMMIT_PROGRESS_POLLING_ERROR",
GIT_AUTOCOMMIT_TRIGGER_ERROR: "GIT_AUTOCOMMIT_TRIGGER_ERROR",
GIT_GET_METADATA_ERROR: "GIT_GET_METADATA_ERROR",
FETCH_FEATURE_FLAGS_ERROR: "FETCH_FEATURE_FLAGS_ERROR",
FETCH_NOTIFICATIONS_ERROR: "FETCH_NOTIFICATIONS_ERROR",

View File

@ -5,7 +5,7 @@ import {
remoteUrlInputValue,
resetPullMergeStatus,
fetchBranchesInit,
startAutocommitProgressPolling,
triggerAutocommitInitAction,
getGitMetadataInitAction,
} from "actions/gitSyncActions";
import { restoreRecentEntitiesRequest } from "actions/globalSearchActions";
@ -296,10 +296,8 @@ export default class AppEditorEngine extends AppEngine {
yield put(fetchGitProtectedBranchesInit());
yield put(fetchGitProtectedBranchesInit());
yield put(getGitMetadataInitAction());
yield put(triggerAutocommitInitAction());
yield put(fetchGitStatusInit({ compareRemote: true }));
yield put(startAutocommitProgressPolling());
yield put(resetPullMergeStatus());
}
}

View File

@ -9,6 +9,8 @@ import { getCurrentAppGitMetaData } from "@appsmith/selectors/applicationSelecto
import BranchList from "../components/BranchList";
import {
getGitStatus,
getIsPollingAutocommit,
getIsTriggeringAutocommit,
protectedModeSelector,
showBranchPopupSelector,
} from "selectors/gitSyncSelectors";
@ -28,6 +30,10 @@ const ButtonContainer = styled(Button)`
margin: 0 ${(props) => props.theme.spaces[4]}px;
max-width: 122px;
min-width: unset !important;
:active {
border: 1px solid var(--ads-v2-color-border-muted);
}
`;
function BranchButton() {
@ -38,6 +44,9 @@ function BranchButton() {
const labelTarget = useRef<HTMLSpanElement>(null);
const status = useSelector(getGitStatus);
const isOpen = useSelector(showBranchPopupSelector);
const triggeringAutocommit = useSelector(getIsTriggeringAutocommit);
const pollingAutocommit = useSelector(getIsPollingAutocommit);
const isBranchChangeDisabled = triggeringAutocommit || pollingAutocommit;
const setIsOpen = (isOpen: boolean) => {
dispatch(setShowBranchPopupAction(isOpen));
@ -47,6 +56,7 @@ function BranchButton() {
<Popover2
content={<BranchList setIsPopupOpen={setIsOpen} />}
data-testid={"t--git-branch-button-popover"}
disabled={isBranchChangeDisabled}
hasBackdrop
isOpen={isOpen}
minimal
@ -69,6 +79,7 @@ function BranchButton() {
<ButtonContainer
className="t--branch-button"
data-testid={"t--branch-button-currentBranch"}
isDisabled={isBranchChangeDisabled}
kind="secondary"
>
{isProtectedMode ? (

View File

@ -55,6 +55,7 @@ const initialState: GitSyncReducerState = {
isAutocommitModalOpen: false,
togglingAutocommit: false,
triggeringAutocommit: false,
pollingAutocommitStatus: false,
gitMetadata: null,
@ -619,6 +620,18 @@ const gitSyncReducer = createReducer(initialState, {
...state,
togglingAutocommit: false,
}),
[ReduxActionTypes.GIT_AUTOCOMMIT_TRIGGER_INIT]: (state) => ({
...state,
triggeringAutocommit: true,
}),
[ReduxActionTypes.GIT_AUTOCOMMIT_TRIGGER_SUCCESS]: (state) => ({
...state,
triggeringAutocommit: false,
}),
[ReduxActionErrorTypes.GIT_AUTOCOMMIT_TRIGGER_ERROR]: (state) => ({
...state,
triggeringAutocommit: false,
}),
[ReduxActionTypes.GIT_AUTOCOMMIT_START_PROGRESS_POLLING]: (state) => ({
...state,
pollingAutocommitStatus: true,
@ -833,6 +846,7 @@ export type GitSyncReducerState = GitBranchDeleteState & {
isAutocommitModalOpen: boolean;
togglingAutocommit: boolean;
triggeringAutocommit: boolean;
pollingAutocommitStatus: boolean;
gitMetadata: GitMetadata | null;

View File

@ -20,8 +20,13 @@ import {
takeLatest,
} from "redux-saga/effects";
import type { TakeableChannel } from "@redux-saga/core";
import type { MergeBranchPayload, MergeStatusPayload } from "api/GitSyncAPI";
import GitSyncAPI from "api/GitSyncAPI";
import type {
GitAutocommitProgressResponse,
GitTriggerAutocommitResponse,
MergeBranchPayload,
MergeStatusPayload,
} from "api/GitSyncAPI";
import GitSyncAPI, { AutocommitResponseEnum } from "api/GitSyncAPI";
import {
getCurrentApplicationId,
getCurrentPageId,
@ -40,6 +45,13 @@ import {
updateGitProtectedBranchesInit,
clearCommitSuccessfulState,
setShowBranchPopupAction,
stopAutocommitProgressPollingAction,
startAutocommitProgressPollingAction,
setAutocommitProgressAction,
autoCommitProgressErrorAction,
resetAutocommitProgressAction,
triggerAutocommitErrorAction,
triggerAutocommitSuccessAction,
} from "actions/gitSyncActions";
import {
commitToRepoSuccess,
@ -103,6 +115,7 @@ import { addBranchParam, GIT_BRANCH_QUERY_KEY } from "constants/routes";
import {
getCurrentGitBranch,
getDisconnectingGitApplication,
getGitMetadataSelector,
} from "selectors/gitSyncSelectors";
import { initEditor } from "actions/initActions";
import { fetchPage } from "actions/pageActions";
@ -113,7 +126,10 @@ import { log } from "loglevel";
import GIT_ERROR_CODES from "constants/GitErrorCodes";
import { builderURL } from "@appsmith/RouteBuilder";
import { APP_MODE } from "entities/App";
import type { GitDiscardResponse } from "reducers/uiReducers/gitSyncReducer";
import type {
GitDiscardResponse,
GitMetadata,
} from "reducers/uiReducers/gitSyncReducer";
import { FocusEntity, identifyEntityFromPath } from "navigation/FocusEntity";
import {
getActions,
@ -123,6 +139,8 @@ import type { Action } from "entities/Action";
import type { JSCollectionDataState } from "@appsmith/reducers/entityReducers/jsActionsReducer";
import { toast } from "design-system";
import { gitExtendedSagas } from "@appsmith/sagas/GitExtendedSagas";
import { selectFeatureFlagCheck } from "@appsmith/selectors/featureFlagsSelectors";
import { FEATURE_FLAG } from "@appsmith/entities/FeatureFlag";
export function* handleRepoLimitReachedError(response?: ApiResponse) {
const { responseMeta } = response || {};
@ -1159,78 +1177,110 @@ function* getGitMetadataSaga() {
}
}
function isAutocommitHappening(
response:
| GitTriggerAutocommitResponse
| GitAutocommitProgressResponse
| undefined,
): boolean {
return (
!!response &&
!!(
response.autoCommitResponse === AutocommitResponseEnum.PUBLISHED ||
response.autoCommitResponse === AutocommitResponseEnum.IN_PROGRESS ||
response.autoCommitResponse === AutocommitResponseEnum.LOCKED
)
);
}
function* pollAutocommitProgressSaga(): any {
const applicationId: string = yield select(getCurrentApplicationId);
const branchName: string = yield select(getCurrentGitBranch);
let triggerResponse: ApiResponse<GitTriggerAutocommitResponse> | undefined;
try {
const response: ApiResponse<any> = yield call(
GitSyncAPI.getAutocommitProgress,
const res = yield call(
GitSyncAPI.triggerAutocommit,
applicationId,
branchName,
);
const isValidResponse: boolean = yield validateResponse(
response,
res,
false,
getLogToSentryFromResponse(response),
getLogToSentryFromResponse(res),
);
if (isValidResponse && response?.data?.isRunning) {
yield put({
type: ReduxActionTypes.GIT_AUTOCOMMIT_START_PROGRESS_POLLING,
});
if (isValidResponse) {
triggerResponse = res;
yield put(triggerAutocommitSuccessAction());
} else {
yield put(
triggerAutocommitErrorAction({
error: res?.responseMeta?.error?.message,
show: true,
}),
);
}
} catch (err) {
yield put(triggerAutocommitErrorAction({ error: err, show: false }));
}
try {
if (isAutocommitHappening(triggerResponse?.data)) {
yield put(startAutocommitProgressPollingAction());
while (true) {
const response: ApiResponse<any> = yield call(
GitSyncAPI.getAutocommitProgress,
applicationId,
);
const progressResponse: ApiResponse<GitAutocommitProgressResponse> =
yield call(GitSyncAPI.getAutocommitProgress, applicationId);
const isValidResponse: boolean = yield validateResponse(
response,
progressResponse,
false,
getLogToSentryFromResponse(response),
getLogToSentryFromResponse(progressResponse),
);
if (isValidResponse) {
yield put({
type: ReduxActionTypes.GIT_SET_AUTOCOMMIT_PROGRESS,
payload: response.data,
});
if (!response?.data?.isRunning) {
yield put({
type: ReduxActionTypes.GIT_AUTOCOMMIT_STOP_PROGRESS_POLLING,
});
yield put(setAutocommitProgressAction(progressResponse.data));
if (!isAutocommitHappening(progressResponse?.data)) {
yield put(stopAutocommitProgressPollingAction());
}
} else {
yield put({
type: ReduxActionTypes.GIT_AUTOCOMMIT_STOP_PROGRESS_POLLING,
});
yield put({
type: ReduxActionErrorTypes.GIT_AUTOCOMMIT_PROGRESS_POLLING_ERROR,
payload: {
error: response?.responseMeta?.error?.message,
yield put(stopAutocommitProgressPollingAction());
yield put(
autoCommitProgressErrorAction({
error: progressResponse?.responseMeta?.error?.message,
show: true,
},
});
}),
);
}
yield delay(1000);
}
} else {
yield put({
type: ReduxActionTypes.GIT_AUTOCOMMIT_STOP_PROGRESS_POLLING,
});
yield put(stopAutocommitProgressPollingAction());
}
} catch (error) {
yield put({
type: ReduxActionTypes.GIT_AUTOCOMMIT_STOP_PROGRESS_POLLING,
});
yield put({
type: ReduxActionErrorTypes.GIT_AUTOCOMMIT_PROGRESS_POLLING_ERROR,
payload: { error },
});
yield put(stopAutocommitProgressPollingAction());
yield put(autoCommitProgressErrorAction({ error, show: false }));
} finally {
if (yield cancelled()) {
yield put({
type: ReduxActionTypes.GIT_RESET_AUTOCOMMIT_PROGRESS,
});
yield put(resetAutocommitProgressAction());
}
}
}
function* triggerAutocommitSaga() {
const isAutocommitFeatureEnabled: boolean = yield select(
selectFeatureFlagCheck,
FEATURE_FLAG.release_git_autocommit_feature_enabled,
);
const gitMetadata: GitMetadata = yield select(getGitMetadataSelector);
const isAutocommitEnabled: boolean = !!gitMetadata?.autoCommitConfig?.enabled;
if (isAutocommitFeatureEnabled && isAutocommitEnabled) {
/* @ts-expect-error: not sure how to do typings of this */
const pollTask = yield fork(pollAutocommitProgressSaga);
yield take(ReduxActionTypes.GIT_AUTOCOMMIT_STOP_PROGRESS_POLLING);
yield cancel(pollTask);
} else {
yield put(triggerAutocommitSuccessAction());
}
}
const gitRequestBlockingActions: Record<
(typeof ReduxActionTypes)[keyof typeof ReduxActionTypes],
(...args: any[]) => any
@ -1252,6 +1302,9 @@ const gitRequestBlockingActions: Record<
[ReduxActionTypes.GIT_DISCARD_CHANGES]: discardChanges,
[ReduxActionTypes.GIT_UPDATE_PROTECTED_BRANCHES_INIT]:
updateGitProtectedBranchesSaga,
[ReduxActionTypes.GIT_AUTOCOMMIT_TRIGGER_INIT]: triggerAutocommitSaga,
[ReduxActionTypes.FETCH_GIT_STATUS_INIT]: fetchGitStatusSaga,
[ReduxActionTypes.GIT_GET_METADATA_INIT]: getGitMetadataSaga,
};
const gitRequestNonBlockingActions: Record<
@ -1261,13 +1314,11 @@ const gitRequestNonBlockingActions: Record<
...gitExtendedSagas,
[ReduxActionTypes.FETCH_GLOBAL_GIT_CONFIG_INIT]: fetchGlobalGitConfig,
[ReduxActionTypes.FETCH_LOCAL_GIT_CONFIG_INIT]: fetchLocalGitConfig,
[ReduxActionTypes.FETCH_GIT_STATUS_INIT]: fetchGitStatusSaga,
[ReduxActionTypes.SHOW_CONNECT_GIT_MODAL]: showConnectGitModal,
[ReduxActionTypes.FETCH_SSH_KEY_PAIR_INIT]: getSSHKeyPairSaga,
[ReduxActionTypes.GIT_FETCH_PROTECTED_BRANCHES_INIT]:
fetchGitProtectedBranchesSaga,
[ReduxActionTypes.GIT_TOGGLE_AUTOCOMMIT_ENABLED_INIT]: toggleAutocommitSaga,
[ReduxActionTypes.GIT_GET_METADATA_INIT]: getGitMetadataSaga,
};
/**
@ -1298,18 +1349,7 @@ function* watchGitNonBlockingRequests() {
}
}
function* watchGitAutocommitPolling() {
while (true) {
yield take(ReduxActionTypes.GIT_AUTOCOMMIT_INITIATE_PROGRESS_POLLING);
/* @ts-expect-error: not sure how to do typings of this */
const pollTask = yield fork(pollAutocommitProgressSaga);
yield take(ReduxActionTypes.GIT_AUTOCOMMIT_STOP_PROGRESS_POLLING);
yield cancel(pollTask);
}
}
export default function* gitSyncSagas() {
yield fork(watchGitNonBlockingRequests);
yield fork(watchGitBlockingRequests);
yield fork(watchGitAutocommitPolling);
}

View File

@ -257,6 +257,9 @@ export const getIsAutocommitToggling = (state: AppState) =>
export const getIsAutocommitModalOpen = (state: AppState) =>
state.ui.gitSync.isAutocommitModalOpen;
export const getIsTriggeringAutocommit = (state: AppState) =>
state.ui.gitSync.triggeringAutocommit;
export const getIsPollingAutocommit = (state: AppState) =>
state.ui.gitSync.pollingAutocommitStatus;

View File

@ -288,8 +288,10 @@ public class FileUtilsCEImpl implements FileInterface {
// Save JS Libs if there's at least one change
if (modifiedResources != null
&& !CollectionUtils.isEmpty(
modifiedResources.getModifiedResourceMap().get(CUSTOM_JS_LIB_LIST))) {
&& (modifiedResources.isAllModified()
|| !CollectionUtils.isEmpty(
modifiedResources.getModifiedResourceMap().get(CUSTOM_JS_LIB_LIST)))) {
Path jsLibDirectory = baseRepo.resolve(JS_LIB_DIRECTORY);
Set<Map.Entry<String, Object>> jsLibEntries =
applicationGitReference.getJsLibraries().entrySet();
@ -953,24 +955,62 @@ public class FileUtilsCEImpl implements FileInterface {
@Override
public Mono<Object> reconstructMetadataFromGitRepo(
String workspaceId, String defaultArtifactId, String repoName, String branchName, Path baseRepoSuffix) {
String workspaceId,
String defaultArtifactId,
String repoName,
String branchName,
Path baseRepoSuffix,
Boolean isResetToLastCommitRequired) {
Mono<Object> metadataMono;
try {
// instead of checking out to last branch we are first cleaning the git repo,
// then checking out to the desired branch
metadataMono = gitExecutor
.resetToLastCommit(baseRepoSuffix, branchName)
.map(isSwitched -> {
Path baseRepoPath =
Paths.get(gitServiceConfig.getGitRootPath()).resolve(baseRepoSuffix);
Object metadata = fileOperations.readFile(
baseRepoPath.resolve(CommonConstants.METADATA + CommonConstants.JSON_EXTENSION));
return metadata;
});
Mono<Boolean> gitResetMono = Mono.just(Boolean.TRUE);
if (Boolean.TRUE.equals(isResetToLastCommitRequired)) {
// instead of checking out to last branch we are first cleaning the git repo,
// then checking out to the desired branch
gitResetMono = gitExecutor.resetToLastCommit(baseRepoSuffix, branchName);
}
metadataMono = gitResetMono.map(isSwitched -> {
Path baseRepoPath = Paths.get(gitServiceConfig.getGitRootPath()).resolve(baseRepoSuffix);
Object metadata = fileOperations.readFile(
baseRepoPath.resolve(CommonConstants.METADATA + CommonConstants.JSON_EXTENSION));
return metadata;
});
} catch (GitAPIException | IOException exception) {
metadataMono = Mono.error(exception);
}
return metadataMono.subscribeOn(scheduler);
}
@Override
public Mono<Object> reconstructPageFromGitRepo(
String pageName, String branchName, Path baseRepoSuffixPath, Boolean resetToLastCommitRequired) {
Mono<Object> pageObjectMono;
try {
Mono<Boolean> resetToLastCommit = Mono.just(Boolean.TRUE);
if (Boolean.TRUE.equals(resetToLastCommitRequired)) {
// instead of checking out to last branch we are first cleaning the git repo,
// then checking out to the desired branch
resetToLastCommit = gitExecutor.resetToLastCommit(baseRepoSuffixPath, branchName);
}
pageObjectMono = resetToLastCommit.map(isSwitched -> {
Path pageSuffix = Paths.get(PAGE_DIRECTORY, pageName);
Path repoPath = Paths.get(gitServiceConfig.getGitRootPath())
.resolve(baseRepoSuffixPath)
.resolve(pageSuffix);
Object pageObject =
fileOperations.readFile(repoPath.resolve(pageName + CommonConstants.JSON_EXTENSION));
return pageObject;
});
} catch (GitAPIException | IOException exception) {
pageObjectMono = Mono.error(exception);
}
return pageObjectMono.subscribeOn(scheduler);
}
}

View File

@ -70,7 +70,7 @@ public class FileOperationsCEv2Impl extends FileOperationsCEImpl implements File
this.observationHelper = observationHelper;
}
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_cleanup_feature_enabled)
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_autocommit_feature_enabled)
@Override
public void saveMetadataResource(ApplicationGitReference applicationGitReference, Path baseRepo) {
ObjectNode metadata = objectMapper.valueToTree(applicationGitReference.getMetadata());
@ -78,7 +78,7 @@ public class FileOperationsCEv2Impl extends FileOperationsCEImpl implements File
saveResource(metadata, baseRepo.resolve(CommonConstants.METADATA + CommonConstants.JSON_EXTENSION));
}
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_cleanup_feature_enabled)
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_autocommit_feature_enabled)
@Override
public void saveWidgets(JSONObject sourceEntity, String resourceName, Path path) {
Span span = observationHelper.createSpan(GitSpan.FILE_WRITE);
@ -98,7 +98,7 @@ public class FileOperationsCEv2Impl extends FileOperationsCEImpl implements File
}
}
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_cleanup_feature_enabled)
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_autocommit_feature_enabled)
@Override
public boolean writeToFile(Object sourceEntity, Path path) throws IOException {
Span span = observationHelper.createSpan(GitSpan.FILE_WRITE);
@ -123,7 +123,7 @@ public class FileOperationsCEv2Impl extends FileOperationsCEImpl implements File
* @param filePath file on which the read operation will be performed
* @return resource stored in the JSON file
*/
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_cleanup_feature_enabled)
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_autocommit_feature_enabled)
@Override
public Object readFile(Path filePath) {
Span span = observationHelper.createSpan(GitSpan.FILE_READ);
@ -147,7 +147,7 @@ public class FileOperationsCEv2Impl extends FileOperationsCEImpl implements File
* @param directoryPath directory path for files on which read operation will be performed
* @return resources stored in the directory
*/
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_cleanup_feature_enabled)
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_autocommit_feature_enabled)
@Override
public Map<String, Object> readFiles(Path directoryPath, String keySuffix) {
Map<String, Object> resource = new HashMap<>();
@ -168,7 +168,7 @@ public class FileOperationsCEv2Impl extends FileOperationsCEImpl implements File
return resource;
}
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_cleanup_feature_enabled)
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_autocommit_feature_enabled)
@Override
public Integer getFileFormatVersion(Object metadata) {
if (metadata == null) {
@ -179,7 +179,7 @@ public class FileOperationsCEv2Impl extends FileOperationsCEImpl implements File
return fileFormatVersion;
}
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_cleanup_feature_enabled)
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_autocommit_feature_enabled)
@Override
public JSONObject getMainContainer(Object pageJson) {
JsonNode pageJSON = objectMapper.valueToTree(pageJson);

View File

@ -15,7 +15,15 @@ public enum FeatureFlagEnum {
release_embed_hide_share_settings_enabled,
rollout_datasource_test_rate_limit_enabled,
release_git_autocommit_feature_enabled,
release_git_cleanup_feature_enabled,
release_git_server_autocommit_feature_enabled,
/**
* Since checking eligibility for autocommit is an expensive operation,
* We want to roll out this feature on cloud in a controlled manner.
* We could have used the autocommit flag itself, however it is on tenant level,
* and it can't be moved to user level due to its usage on non-reactive code paths.
* We would keep the main autocommit flag false on production for the version <= testing versions,
* and turn it to true for later versions
* We would remove this feature flag once the testing is complete.
*/
release_git_autocommit_eligibility_enabled,
// Add EE flags below this line, to avoid conflicts.
}

View File

@ -54,10 +54,19 @@ public interface FileInterface {
* @param repoName
* @param branchName
* @param repoSuffixPath
* @param isResetToLastCommitRequired
* @return
*/
Mono<Object> reconstructMetadataFromGitRepo(
String workspaceId, String defaultApplicationId, String repoName, String branchName, Path repoSuffixPath);
String workspaceId,
String defaultApplicationId,
String repoName,
String branchName,
Path repoSuffixPath,
Boolean isResetToLastCommitRequired);
Mono<Object> reconstructPageFromGitRepo(
String pageName, String branchName, Path repoSuffixPath, Boolean checkoutRequired);
/**
* Once the user connects the existing application to a remote repo, we will initialize the repo with Readme.md -

View File

@ -22,6 +22,8 @@ public class GitConstantsCE {
public static final String GIT_PROFILE_ERROR = "Unable to find git author configuration for logged-in user. You can"
+ " set up a git profile from the user profile section.";
public static final String RECONSTRUCT_PAGE = "reconstruct page";
public class GitMetricConstantsCE {
public static final String CHECKOUT_REMOTE = "checkout-remote";
public static final String HARD_RESET = "hard-reset";
@ -47,5 +49,7 @@ public class GitConstantsCE {
public static final String MERGE_BRANCH = "mergeBranch";
public static final String DELETE = "delete";
public static final String DISCARD = "discard";
public static final String PAGE_DSL_VERSION = "pageDslVersion";
public static final String AUTO_COMMIT_ELIGIBILITY = "autoCommitEligibility";
}
}

View File

@ -2,8 +2,10 @@ package com.appsmith.server.controllers;
import com.appsmith.server.constants.Url;
import com.appsmith.server.controllers.ce.GitControllerCE;
import com.appsmith.server.git.autocommit.AutoCommitService;
import com.appsmith.server.git.common.CommonGitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ -12,7 +14,8 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping(Url.GIT_URL)
public class GitController extends GitControllerCE {
public GitController(CommonGitService service) {
super(service);
@Autowired
public GitController(CommonGitService service, AutoCommitService autoCommitService) {
super(service, autoCommitService);
}
}

View File

@ -13,7 +13,7 @@ import com.appsmith.server.domains.GitArtifactMetadata;
import com.appsmith.server.domains.GitAuth;
import com.appsmith.server.domains.GitProfile;
import com.appsmith.server.dtos.ApplicationImportDTO;
import com.appsmith.server.dtos.AutoCommitProgressDTO;
import com.appsmith.server.dtos.AutoCommitResponseDTO;
import com.appsmith.server.dtos.BranchProtectionRequestDTO;
import com.appsmith.server.dtos.GitCommitDTO;
import com.appsmith.server.dtos.GitConnectDTO;
@ -22,6 +22,7 @@ import com.appsmith.server.dtos.GitDocsDTO;
import com.appsmith.server.dtos.GitMergeDTO;
import com.appsmith.server.dtos.GitPullDTO;
import com.appsmith.server.dtos.ResponseDTO;
import com.appsmith.server.git.autocommit.AutoCommitService;
import com.appsmith.server.git.common.CommonGitService;
import com.appsmith.server.helpers.GitDeployKeyGenerator;
import com.fasterxml.jackson.annotation.JsonView;
@ -53,6 +54,7 @@ import java.util.Map;
public class GitControllerCE {
private final CommonGitService service;
private final AutoCommitService autoCommitService;
/**
* applicationId is the defaultApplicationId
@ -333,16 +335,18 @@ public class GitControllerCE {
@JsonView(Views.Public.class)
@PostMapping("/auto-commit/app/{defaultApplicationId}")
public Mono<ResponseDTO<Boolean>> autoCommit(
@PathVariable String defaultApplicationId, @RequestParam String branchName) {
return service.autoCommitApplication(defaultApplicationId, branchName, ArtifactType.APPLICATION)
public Mono<ResponseDTO<AutoCommitResponseDTO>> autoCommitApplication(
@PathVariable String defaultApplicationId, @RequestHeader(name = FieldName.BRANCH_NAME) String branchName) {
return autoCommitService
.autoCommitApplication(defaultApplicationId, branchName)
.map(data -> new ResponseDTO<>(HttpStatus.OK.value(), data, null));
}
@JsonView(Views.Public.class)
@GetMapping("/auto-commit/progress/app/{defaultApplicationId}")
public Mono<ResponseDTO<AutoCommitProgressDTO>> getAutoCommitProgress(@PathVariable String defaultApplicationId) {
return service.getAutoCommitProgress(defaultApplicationId, ArtifactType.APPLICATION)
public Mono<ResponseDTO<AutoCommitResponseDTO>> getAutoCommitProgress(
@PathVariable String defaultApplicationId, @RequestHeader(name = FieldName.BRANCH_NAME) String branchName) {
return service.getAutoCommitProgress(defaultApplicationId, branchName, ArtifactType.APPLICATION)
.map(data -> new ResponseDTO<>(HttpStatus.OK.value(), data, null));
}

View File

@ -38,13 +38,13 @@ public class Theme extends BaseDomain {
@JsonView(Views.Public.class)
String workspaceId;
@JsonView(Views.Public.class)
@JsonView({Views.Public.class, Git.class})
private Object config;
@JsonView(Views.Public.class)
@JsonView({Views.Public.class, Git.class})
private Object properties;
@JsonView(Views.Public.class)
@JsonView({Views.Public.class, Git.class})
private Map<String, Object> stylesheet;
@JsonProperty("isSystemTheme") // manually setting property name to make sure it's compatible with Gson

View File

@ -1,17 +0,0 @@
package com.appsmith.server.dtos;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
@Data
@RequiredArgsConstructor
@NoArgsConstructor
public class AutoCommitProgressDTO {
@NonNull private Boolean isRunning;
// using primitive type int instead of Integer because we want to 0 as default value. Integer have default null
private int progress;
private String branchName;
}

View File

@ -0,0 +1,60 @@
package com.appsmith.server.dtos;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
@Data
@AllArgsConstructor
@RequiredArgsConstructor
public class AutoCommitResponseDTO {
public enum AutoCommitResponse {
/**
* This enum is used when an autocommit is in progress for a different branch from the branch on which
* the autocommit is requested.
*/
LOCKED,
/**
* This enum is used when an autocommit has been published.
*/
PUBLISHED,
/**
* This enum is used when an autocommit is in progress for the branch from which
* the autocommit is requested.
*/
IN_PROGRESS,
/**
* This enum is used when an autocommit has been requested however it did not fulfil the pre-requisite.
*/
REQUIRED,
/**
* This enum is used when an autocommit is requested however it's not required.
*/
IDLE,
/**
* This enum is used when the app on which the autocommit is requested is a non git app.
*/
NON_GIT_APP
}
/**
* Enum to denote the current state of autocommit
*/
private AutoCommitResponse autoCommitResponse;
/**
* progress of the already-running auto-commit.
*/
private int progress;
private String branchName;
public AutoCommitResponseDTO(AutoCommitResponse autoCommitResponse) {
this.autoCommitResponse = autoCommitResponse;
}
}

View File

@ -1,3 +1,3 @@
package com.appsmith.server.git;
package com.appsmith.server.git.autocommit;
public interface AutoCommitEventHandler extends AutoCommitEventHandlerCE {}

View File

@ -1,4 +1,4 @@
package com.appsmith.server.git;
package com.appsmith.server.git.autocommit;
import com.appsmith.server.events.AutoCommitEvent;
import reactor.core.publisher.Mono;

View File

@ -1,4 +1,4 @@
package com.appsmith.server.git;
package com.appsmith.server.git.autocommit;
import com.appsmith.external.constants.AnalyticsEvents;
import com.appsmith.external.dtos.ModifiedResources;
@ -13,6 +13,7 @@ import com.appsmith.server.dtos.ApplicationJson;
import com.appsmith.server.events.AutoCommitEvent;
import com.appsmith.server.exceptions.AppsmithError;
import com.appsmith.server.exceptions.AppsmithException;
import com.appsmith.server.git.GitRedisUtils;
import com.appsmith.server.helpers.CollectionUtils;
import com.appsmith.server.helpers.CommonGitFileUtils;
import com.appsmith.server.helpers.DSLMigrationUtils;

View File

@ -1,7 +1,8 @@
package com.appsmith.server.git;
package com.appsmith.server.git.autocommit;
import com.appsmith.external.git.GitExecutor;
import com.appsmith.server.configurations.ProjectProperties;
import com.appsmith.server.git.GitRedisUtils;
import com.appsmith.server.helpers.CommonGitFileUtils;
import com.appsmith.server.helpers.DSLMigrationUtils;
import com.appsmith.server.helpers.RedisUtils;

View File

@ -0,0 +1,3 @@
package com.appsmith.server.git.autocommit;
public interface AutoCommitService extends AutoCommitServiceCE {}

View File

@ -0,0 +1,9 @@
package com.appsmith.server.git.autocommit;
import com.appsmith.server.dtos.AutoCommitResponseDTO;
import reactor.core.publisher.Mono;
public interface AutoCommitServiceCE {
Mono<AutoCommitResponseDTO> autoCommitApplication(String defaultApplicationId, String branchName);
}

View File

@ -0,0 +1,147 @@
package com.appsmith.server.git.autocommit;
import com.appsmith.server.applications.base.ApplicationService;
import com.appsmith.server.constants.FieldName;
import com.appsmith.server.domains.Application;
import com.appsmith.server.domains.ApplicationMode;
import com.appsmith.server.domains.GitArtifactMetadata;
import com.appsmith.server.dtos.AutoCommitResponseDTO;
import com.appsmith.server.dtos.AutoCommitTriggerDTO;
import com.appsmith.server.dtos.PageDTO;
import com.appsmith.server.exceptions.AppsmithError;
import com.appsmith.server.exceptions.AppsmithException;
import com.appsmith.server.git.autocommit.helpers.AutoCommitEligibilityHelper;
import com.appsmith.server.git.autocommit.helpers.GitAutoCommitHelperImpl;
import com.appsmith.server.newpages.base.NewPageService;
import com.appsmith.server.solutions.ApplicationPermission;
import com.appsmith.server.solutions.PagePermission;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;
import java.util.Set;
import static com.appsmith.server.dtos.AutoCommitResponseDTO.AutoCommitResponse.IDLE;
import static com.appsmith.server.dtos.AutoCommitResponseDTO.AutoCommitResponse.IN_PROGRESS;
import static com.appsmith.server.dtos.AutoCommitResponseDTO.AutoCommitResponse.LOCKED;
import static com.appsmith.server.dtos.AutoCommitResponseDTO.AutoCommitResponse.NON_GIT_APP;
import static com.appsmith.server.dtos.AutoCommitResponseDTO.AutoCommitResponse.PUBLISHED;
import static com.appsmith.server.dtos.AutoCommitResponseDTO.AutoCommitResponse.REQUIRED;
import static java.lang.Boolean.TRUE;
import static org.springframework.util.StringUtils.hasText;
@Slf4j
@RequiredArgsConstructor
public class AutoCommitServiceCEImpl implements AutoCommitServiceCE {
private final ApplicationService applicationService;
private final ApplicationPermission applicationPermission;
private final NewPageService newPageService;
private final PagePermission pagePermission;
private final AutoCommitEligibilityHelper autoCommitEligibilityHelper;
private final GitAutoCommitHelperImpl gitAutoCommitHelper;
@Override
public Mono<AutoCommitResponseDTO> autoCommitApplication(String defaultApplicationId, String branchName) {
if (!hasText(defaultApplicationId)) {
return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.APPLICATION_ID));
}
if (!hasText(branchName)) {
return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.BRANCH_NAME));
}
Mono<Application> applicationMonoCached = applicationService
.findByBranchNameAndDefaultApplicationId(
branchName, defaultApplicationId, applicationPermission.getEditPermission())
.switchIfEmpty(Mono.error(new AppsmithException(
AppsmithError.NO_RESOURCE_FOUND, FieldName.APPLICATION, defaultApplicationId)))
.cache();
// A page-dto which must exist in the git file system is required,
// this existence can be guaranteed by using application mode = published
// in appsmith git system an application could be only published if it's commited, converse is also true,
// hence a published page would definitely be present in git fs.
Mono<PageDTO> pageDTOMono = applicationMonoCached
.flatMap(application -> newPageService
.findByApplicationIdAndApplicationMode(
application.getId(), pagePermission.getEditPermission(), ApplicationMode.PUBLISHED)
.next())
.switchIfEmpty(Mono.error(
new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, FieldName.PAGE, defaultApplicationId)));
return applicationMonoCached.zipWith(pageDTOMono).flatMap(tuple2 -> {
Application branchedApplication = tuple2.getT1();
PageDTO pageDTO = tuple2.getT2();
AutoCommitResponseDTO autoCommitResponseDTO = new AutoCommitResponseDTO();
if (branchedApplication.getGitApplicationMetadata() == null) {
autoCommitResponseDTO.setAutoCommitResponse(NON_GIT_APP);
return Mono.just(autoCommitResponseDTO);
}
String workspaceId = branchedApplication.getWorkspaceId();
GitArtifactMetadata gitArtifactMetadata = branchedApplication.getGitArtifactMetadata();
Mono<AutoCommitTriggerDTO> isAutoCommitRequiredMono =
autoCommitEligibilityHelper.isAutoCommitRequired(workspaceId, gitArtifactMetadata, pageDTO);
Mono<AutoCommitResponseDTO> autoCommitProgressDTOMono =
gitAutoCommitHelper.getAutoCommitProgress(defaultApplicationId, branchName);
return autoCommitProgressDTOMono.flatMap(autoCommitProgressDTO -> {
if (Set.of(LOCKED, IN_PROGRESS).contains(autoCommitProgressDTO.getAutoCommitResponse())) {
log.info(
"application with id: {}, has requested auto-commit for branch name: {}, however an event for branch name: {} is already in progress",
defaultApplicationId,
branchName,
autoCommitProgressDTO.getBranchName());
autoCommitResponseDTO.setAutoCommitResponse(autoCommitProgressDTO.getAutoCommitResponse());
autoCommitResponseDTO.setProgress(autoCommitProgressDTO.getProgress());
autoCommitResponseDTO.setBranchName(autoCommitProgressDTO.getBranchName());
return Mono.just(autoCommitResponseDTO);
}
return isAutoCommitRequiredMono.flatMap(autoCommitTriggerDTO -> {
if (!Boolean.TRUE.equals(autoCommitTriggerDTO.getIsAutoCommitRequired())) {
log.info(
"application with id: {}, and branch name: {} is not eligible for autocommit",
defaultApplicationId,
branchName);
autoCommitResponseDTO.setAutoCommitResponse(IDLE);
return Mono.just(autoCommitResponseDTO);
}
// Autocommit can be started
log.info(
"application with id: {}, and branch name: {} is eligible for autocommit",
defaultApplicationId,
branchName);
return gitAutoCommitHelper
.publishAutoCommitEvent(autoCommitTriggerDTO, defaultApplicationId, branchName)
.map(isEventPublished -> {
if (TRUE.equals(isEventPublished)) {
log.info(
"autocommit event for application with id: {}, and branch name: {} is published",
defaultApplicationId,
branchName);
autoCommitResponseDTO.setAutoCommitResponse(PUBLISHED);
return autoCommitResponseDTO;
}
log.info(
"application with id: {}, and branch name: {} does not fulfil the prerequisite for autocommit",
defaultApplicationId,
branchName);
autoCommitResponseDTO.setAutoCommitResponse(REQUIRED);
return autoCommitResponseDTO;
});
});
});
});
}
}

View File

@ -0,0 +1,29 @@
package com.appsmith.server.git.autocommit;
import com.appsmith.server.applications.base.ApplicationService;
import com.appsmith.server.git.autocommit.helpers.AutoCommitEligibilityHelper;
import com.appsmith.server.git.autocommit.helpers.GitAutoCommitHelperImpl;
import com.appsmith.server.newpages.base.NewPageService;
import com.appsmith.server.solutions.ApplicationPermission;
import com.appsmith.server.solutions.PagePermission;
import org.springframework.stereotype.Component;
@Component
public class AutoCommitServiceImpl extends AutoCommitServiceCEImpl implements AutoCommitService {
public AutoCommitServiceImpl(
ApplicationService applicationService,
ApplicationPermission applicationPermission,
NewPageService newPageService,
PagePermission pagePermission,
AutoCommitEligibilityHelper autoCommitEligibilityHelper,
GitAutoCommitHelperImpl gitAutoCommitHelper) {
super(
applicationService,
applicationPermission,
newPageService,
pagePermission,
autoCommitEligibilityHelper,
gitAutoCommitHelper);
}
}

View File

@ -11,6 +11,8 @@ public interface AutoCommitEligibilityHelper {
Mono<Boolean> isClientMigrationRequired(PageDTO pageDTO);
Mono<Boolean> isClientMigrationRequiredFSOps(String workspaceId, GitArtifactMetadata gitMetadata, PageDTO pageDTO);
Mono<AutoCommitTriggerDTO> isAutoCommitRequired(
String workspaceId, GitArtifactMetadata gitArtifactMetadata, PageDTO pageDTO);
}

View File

@ -23,6 +23,12 @@ public class AutoCommitEligibilityHelperFallbackImpl implements AutoCommitEligib
return Mono.just(FALSE);
}
@Override
public Mono<Boolean> isClientMigrationRequiredFSOps(
String workspaceId, GitArtifactMetadata gitMetadata, PageDTO pageDTO) {
return Mono.just(FALSE);
}
@Override
public Mono<AutoCommitTriggerDTO> isAutoCommitRequired(
String workspaceId, GitArtifactMetadata gitArtifactMetadata, PageDTO pageDTO) {

View File

@ -2,8 +2,6 @@ package com.appsmith.server.git.autocommit.helpers;
import com.appsmith.external.annotations.FeatureFlagged;
import com.appsmith.external.enums.FeatureFlagEnum;
import com.appsmith.external.git.constants.GitConstants.GitCommandConstants;
import com.appsmith.server.constants.ArtifactType;
import com.appsmith.server.domains.GitArtifactMetadata;
import com.appsmith.server.domains.Layout;
import com.appsmith.server.dtos.AutoCommitTriggerDTO;
@ -20,6 +18,8 @@ import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
import static com.appsmith.external.git.constants.GitConstants.GitCommandConstants.AUTO_COMMIT_ELIGIBILITY;
import static com.appsmith.server.constants.ArtifactType.APPLICATION;
import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
@ -35,16 +35,14 @@ public class AutoCommitEligibilityHelperImpl extends AutoCommitEligibilityHelper
private final GitRedisUtils gitRedisUtils;
@Override
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_server_autocommit_feature_enabled)
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_autocommit_eligibility_enabled)
public Mono<Boolean> isServerAutoCommitRequired(String workspaceId, GitArtifactMetadata gitMetadata) {
String defaultApplicationId = gitMetadata.getDefaultArtifactId();
String branchName = gitMetadata.getBranchName();
String repoName = gitMetadata.getRepoName();
Mono<Boolean> isServerMigrationRequiredMonoCached = commonGitFileUtils
.getMetadataServerSchemaMigrationVersion(
workspaceId, defaultApplicationId, repoName, branchName, ArtifactType.APPLICATION)
.getMetadataServerSchemaMigrationVersion(workspaceId, gitMetadata, FALSE, APPLICATION)
.map(serverSchemaVersion -> {
log.info(
"server schema for application id : {} and branch name : {} is : {}",
@ -56,22 +54,27 @@ public class AutoCommitEligibilityHelperImpl extends AutoCommitEligibilityHelper
.defaultIfEmpty(FALSE)
.cache();
return Mono.defer(() -> gitRedisUtils.addFileLock(defaultApplicationId, GitCommandConstants.METADATA, false))
.then(Mono.defer(() -> isServerMigrationRequiredMonoCached))
.then(Mono.defer(() -> gitRedisUtils.releaseFileLock(defaultApplicationId)))
.then(Mono.defer(() -> isServerMigrationRequiredMonoCached))
.onErrorResume(error -> {
log.debug(
"error while retrieving the metadata for defaultApplicationId : {}, branchName : {} error : {}",
defaultApplicationId,
branchName,
error.getMessage());
return gitRedisUtils.releaseFileLock(defaultApplicationId).then(Mono.just(FALSE));
});
return Mono.defer(() -> isServerMigrationRequiredMonoCached).onErrorResume(error -> {
log.debug(
"error while retrieving the metadata for defaultApplicationId : {}, branchName : {} error : {}",
defaultApplicationId,
branchName,
error.getMessage());
return Mono.just(FALSE);
});
}
/**
* This method has been deprecated and is not being used anymore.
* It's been deprecated because, we are using the absolute source of truth
* that is the version key in the layout.
* /pages/<Page-Name>.json in file system for the finding out the Dsl in layout.
* @param pageDTO : pageDTO for the page for which migration was required.
* @return : a boolean whether the client requires a migration or not
*/
@Override
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_autocommit_feature_enabled)
@Deprecated
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_autocommit_eligibility_enabled)
public Mono<Boolean> isClientMigrationRequired(PageDTO pageDTO) {
return dslMigrationUtils
.getLatestDslVersion()
@ -91,26 +94,71 @@ public class AutoCommitEligibilityHelperImpl extends AutoCommitEligibilityHelper
}
@Override
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_autocommit_feature_enabled)
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_autocommit_eligibility_enabled)
public Mono<Boolean> isClientMigrationRequiredFSOps(
String workspaceId, GitArtifactMetadata gitMetadata, PageDTO pageDTO) {
String defaultApplicationId = gitMetadata.getDefaultArtifactId();
String branchName = gitMetadata.getBranchName();
Mono<Integer> latestDslVersionMono = dslMigrationUtils.getLatestDslVersion();
Mono<Boolean> isClientMigrationRequired = latestDslVersionMono
.zipWith(commonGitFileUtils.getPageDslVersionNumber(
workspaceId, gitMetadata, pageDTO, TRUE, APPLICATION))
.map(tuple2 -> {
Integer latestDslVersion = tuple2.getT1();
org.json.JSONObject pageDSL = tuple2.getT2();
log.info("page dsl retrieved from file system");
return GitUtils.isMigrationRequired(pageDSL, latestDslVersion);
})
.defaultIfEmpty(FALSE)
.cache();
return Mono.defer(() -> isClientMigrationRequired).onErrorResume(error -> {
log.debug(
"error while fetching the dsl version for page : {}, defaultApplicationId : {}, branchName : {} error : {}",
pageDTO.getName(),
defaultApplicationId,
branchName,
error.getMessage());
return Mono.just(FALSE);
});
}
@Override
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_autocommit_eligibility_enabled)
public Mono<AutoCommitTriggerDTO> isAutoCommitRequired(
String workspaceId, GitArtifactMetadata gitArtifactMetadata, PageDTO pageDTO) {
String defaultApplicationId = gitArtifactMetadata.getDefaultApplicationId();
Mono<Boolean> isClientAutocommitRequiredMono =
isClientMigrationRequired(pageDTO).defaultIfEmpty(FALSE);
isClientMigrationRequiredFSOps(workspaceId, gitArtifactMetadata, pageDTO);
Mono<Boolean> isServerAutocommitRequiredMono = isServerAutoCommitRequired(workspaceId, gitArtifactMetadata);
Mono<Boolean> isServerAutocommitRequiredMono =
isServerAutoCommitRequired(workspaceId, gitArtifactMetadata).cache();
return isServerAutocommitRequiredMono
.zipWith(isClientAutocommitRequiredMono)
.map(tuple2 -> {
Boolean serverFlag = tuple2.getT1();
Boolean clientFlag = tuple2.getT2();
return Mono.defer(() -> gitRedisUtils.addFileLock(defaultApplicationId, AUTO_COMMIT_ELIGIBILITY))
.then(isClientAutocommitRequiredMono.zipWhen(clientFlag -> {
Mono<Boolean> serverFlagMono = isServerAutocommitRequiredMono;
// if client is required to migrate then,
// there is no requirement to fetch server flag as server is subset of client migration.
if (Boolean.TRUE.equals(clientFlag)) {
serverFlagMono = Mono.just(TRUE);
}
return serverFlagMono;
}))
.flatMap(tuple2 -> {
Boolean clientFlag = tuple2.getT1();
Boolean serverFlag = tuple2.getT2();
AutoCommitTriggerDTO autoCommitTriggerDTO = new AutoCommitTriggerDTO();
autoCommitTriggerDTO.setIsClientAutoCommitRequired(TRUE.equals(clientFlag));
autoCommitTriggerDTO.setIsServerAutoCommitRequired(TRUE.equals(serverFlag));
autoCommitTriggerDTO.setIsAutoCommitRequired((TRUE.equals(serverFlag) || TRUE.equals(clientFlag)));
return autoCommitTriggerDTO;
return gitRedisUtils.releaseFileLock(defaultApplicationId).then(Mono.just(autoCommitTriggerDTO));
});
}
}

View File

@ -1,17 +1,17 @@
package com.appsmith.server.git.autocommit.helpers;
import com.appsmith.server.dtos.AutoCommitProgressDTO;
import com.appsmith.server.dtos.AutoCommitResponseDTO;
import com.appsmith.server.dtos.AutoCommitTriggerDTO;
import reactor.core.publisher.Mono;
public interface GitAutoCommitHelper {
Mono<AutoCommitProgressDTO> getAutoCommitProgress(String applicationId);
Mono<AutoCommitResponseDTO> getAutoCommitProgress(String defaultApplicationId, String branchName);
Mono<Boolean> autoCommitClientMigration(String defaultApplicationId, String branchName);
Mono<Boolean> autoCommitServerMigration(String defaultApplicationId, String branchName);
Mono<Boolean> autoCommitApplication(
Mono<Boolean> publishAutoCommitEvent(
AutoCommitTriggerDTO autoCommitTriggerDTO, String defaultApplicationId, String branchName);
}

View File

@ -1,6 +1,6 @@
package com.appsmith.server.git.autocommit.helpers;
import com.appsmith.server.dtos.AutoCommitProgressDTO;
import com.appsmith.server.dtos.AutoCommitResponseDTO;
import com.appsmith.server.dtos.AutoCommitTriggerDTO;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
@ -19,12 +19,12 @@ public class GitAutoCommitHelperFallbackImpl implements GitAutoCommitHelper {
}
@Override
public Mono<AutoCommitProgressDTO> getAutoCommitProgress(String applicationId) {
return Mono.empty();
public Mono<AutoCommitResponseDTO> getAutoCommitProgress(String defaultApplicationId, String branchName) {
return Mono.just(new AutoCommitResponseDTO(AutoCommitResponseDTO.AutoCommitResponse.IDLE));
}
@Override
public Mono<Boolean> autoCommitApplication(
public Mono<Boolean> publishAutoCommitEvent(
AutoCommitTriggerDTO autoCommitTriggerDTO, String defaultApplicationId, String branchName) {
return Mono.just(Boolean.FALSE);
}

View File

@ -6,10 +6,10 @@ import com.appsmith.server.applications.base.ApplicationService;
import com.appsmith.server.domains.Application;
import com.appsmith.server.domains.GitArtifactMetadata;
import com.appsmith.server.domains.GitProfile;
import com.appsmith.server.dtos.AutoCommitProgressDTO;
import com.appsmith.server.dtos.AutoCommitResponseDTO;
import com.appsmith.server.dtos.AutoCommitTriggerDTO;
import com.appsmith.server.events.AutoCommitEvent;
import com.appsmith.server.git.AutoCommitEventHandler;
import com.appsmith.server.git.autocommit.AutoCommitEventHandler;
import com.appsmith.server.git.common.CommonGitService;
import com.appsmith.server.helpers.GitPrivateRepoHelper;
import com.appsmith.server.helpers.GitUtils;
@ -23,6 +23,10 @@ import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Mono;
import static com.appsmith.server.dtos.AutoCommitResponseDTO.AutoCommitResponse.IDLE;
import static com.appsmith.server.dtos.AutoCommitResponseDTO.AutoCommitResponse.IN_PROGRESS;
import static com.appsmith.server.dtos.AutoCommitResponseDTO.AutoCommitResponse.LOCKED;
@Slf4j
@Primary
@Service
@ -53,17 +57,26 @@ public class GitAutoCommitHelperImpl extends GitAutoCommitHelperFallbackImpl imp
}
@Override
public Mono<AutoCommitProgressDTO> getAutoCommitProgress(String applicationId) {
public Mono<AutoCommitResponseDTO> getAutoCommitProgress(String defaultApplicationId, String branchName) {
return redisUtils
.getRunningAutoCommitBranchName(applicationId)
.zipWith(redisUtils.getAutoCommitProgress(applicationId))
.getRunningAutoCommitBranchName(defaultApplicationId)
.zipWith(redisUtils.getAutoCommitProgress(defaultApplicationId))
.map(tuple2 -> {
AutoCommitProgressDTO autoCommitProgressDTO = new AutoCommitProgressDTO(Boolean.TRUE);
autoCommitProgressDTO.setBranchName(tuple2.getT1());
autoCommitProgressDTO.setProgress(tuple2.getT2());
return autoCommitProgressDTO;
String branchNameFromRedis = tuple2.getT1();
AutoCommitResponseDTO autoCommitResponseDTO = new AutoCommitResponseDTO();
autoCommitResponseDTO.setProgress(tuple2.getT2());
autoCommitResponseDTO.setBranchName(branchNameFromRedis);
if (branchNameFromRedis.equals(branchName)) {
autoCommitResponseDTO.setAutoCommitResponse(IN_PROGRESS);
} else {
autoCommitResponseDTO.setAutoCommitResponse(LOCKED);
}
return autoCommitResponseDTO;
})
.defaultIfEmpty(new AutoCommitProgressDTO(Boolean.FALSE));
.defaultIfEmpty(new AutoCommitResponseDTO(IDLE));
}
/**
@ -114,7 +127,7 @@ public class GitAutoCommitHelperImpl extends GitAutoCommitHelperFallbackImpl imp
}
@Override
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_server_autocommit_feature_enabled)
@FeatureFlagged(featureFlagName = FeatureFlagEnum.release_git_autocommit_feature_enabled)
public Mono<Boolean> autoCommitServerMigration(String defaultApplicationId, String branchName) {
return autoCommitApplication(defaultApplicationId, branchName, Boolean.FALSE);
}
@ -203,7 +216,7 @@ public class GitAutoCommitHelperImpl extends GitAutoCommitHelperFallbackImpl imp
});
}
public Mono<Boolean> autoCommitApplication(
public Mono<Boolean> publishAutoCommitEvent(
AutoCommitTriggerDTO autoCommitTriggerDTO, String defaultApplicationId, String branchName) {
if (!Boolean.TRUE.equals(autoCommitTriggerDTO.getIsAutoCommitRequired())) {

View File

@ -10,7 +10,7 @@ import com.appsmith.server.domains.GitArtifactMetadata;
import com.appsmith.server.domains.GitAuth;
import com.appsmith.server.domains.GitProfile;
import com.appsmith.server.dtos.ArtifactImportDTO;
import com.appsmith.server.dtos.AutoCommitProgressDTO;
import com.appsmith.server.dtos.AutoCommitResponseDTO;
import com.appsmith.server.dtos.GitCommitDTO;
import com.appsmith.server.dtos.GitConnectDTO;
import com.appsmith.server.dtos.GitDocsDTO;
@ -105,7 +105,8 @@ public interface CommonGitServiceCE {
Mono<Boolean> toggleAutoCommitEnabled(String defaultArtifactId, ArtifactType artifactType);
Mono<AutoCommitProgressDTO> getAutoCommitProgress(String applicationId, ArtifactType artifactType);
Mono<AutoCommitResponseDTO> getAutoCommitProgress(
String applicationId, String branchName, ArtifactType artifactType);
Mono<Boolean> autoCommitApplication(String defaultApplicationId, String branchName, ArtifactType artifactType);

View File

@ -34,7 +34,7 @@ import com.appsmith.server.domains.Workspace;
import com.appsmith.server.dtos.ApplicationImportDTO;
import com.appsmith.server.dtos.ArtifactExchangeJson;
import com.appsmith.server.dtos.ArtifactImportDTO;
import com.appsmith.server.dtos.AutoCommitProgressDTO;
import com.appsmith.server.dtos.AutoCommitResponseDTO;
import com.appsmith.server.dtos.GitCommitDTO;
import com.appsmith.server.dtos.GitConnectDTO;
import com.appsmith.server.dtos.GitDocsDTO;
@ -3377,8 +3377,9 @@ public class CommonGitServiceCEImpl implements CommonGitServiceCE {
}
@Override
public Mono<AutoCommitProgressDTO> getAutoCommitProgress(String artifactId, ArtifactType artifactType) {
return gitAutoCommitHelper.getAutoCommitProgress(artifactId);
public Mono<AutoCommitResponseDTO> getAutoCommitProgress(
String artifactId, String branchName, ArtifactType artifactType) {
return gitAutoCommitHelper.getAutoCommitProgress(artifactId, branchName);
}
@Override

View File

@ -1,11 +1,13 @@
package com.appsmith.server.helpers;
import com.appsmith.external.git.FileInterface;
import com.appsmith.external.git.operations.FileOperations;
import com.appsmith.external.models.ApplicationGitReference;
import com.appsmith.git.files.FileUtilsImpl;
import com.appsmith.server.applications.git.ApplicationGitFileUtilsImpl;
import com.appsmith.server.helpers.ce.CommonGitFileUtilsCE;
import com.appsmith.server.services.AnalyticsService;
import com.appsmith.server.services.SessionUserService;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
@ -16,10 +18,12 @@ import org.springframework.stereotype.Component;
public class CommonGitFileUtils extends CommonGitFileUtilsCE {
public CommonGitFileUtils(
ApplicationGitFileUtilsImpl applicationGitFileUtils,
ArtifactGitFileUtils<ApplicationGitReference> applicationGitFileUtils,
FileInterface fileUtils,
FileOperations fileOperations,
AnalyticsService analyticsService,
SessionUserService sessionUserService) {
super(applicationGitFileUtils, fileUtils, analyticsService, sessionUserService);
SessionUserService sessionUserService,
Gson gson) {
super(applicationGitFileUtils, fileUtils, fileOperations, analyticsService, sessionUserService);
}
}

View File

@ -177,6 +177,18 @@ public class GitUtils {
return isMigrationRequired;
}
public static boolean isMigrationRequired(org.json.JSONObject layoutDsl, Integer latestDslVersion) {
boolean isMigrationRequired = true;
String versionKey = "version";
if (layoutDsl.has(versionKey)) {
int currentDslVersion = layoutDsl.getInt(versionKey);
if (currentDslVersion >= latestDslVersion) {
isMigrationRequired = false;
}
}
return isMigrationRequired;
}
public static boolean isAutoCommitEnabled(GitArtifactMetadata gitArtifactMetadata) {
return gitArtifactMetadata.getAutoCommitConfig() == null
|| gitArtifactMetadata.getAutoCommitConfig().getEnabled();

View File

@ -2,6 +2,7 @@ package com.appsmith.server.helpers.ce;
import com.appsmith.external.constants.AnalyticsEvents;
import com.appsmith.external.git.FileInterface;
import com.appsmith.external.git.operations.FileOperations;
import com.appsmith.external.helpers.Stopwatch;
import com.appsmith.external.models.ApplicationGitReference;
import com.appsmith.external.models.ArtifactGitReference;
@ -11,8 +12,10 @@ import com.appsmith.git.constants.CommonConstants;
import com.appsmith.git.files.FileUtilsImpl;
import com.appsmith.server.constants.ArtifactType;
import com.appsmith.server.constants.FieldName;
import com.appsmith.server.domains.GitArtifactMetadata;
import com.appsmith.server.dtos.ApplicationJson;
import com.appsmith.server.dtos.ArtifactExchangeJson;
import com.appsmith.server.dtos.PageDTO;
import com.appsmith.server.exceptions.AppsmithError;
import com.appsmith.server.exceptions.AppsmithException;
import com.appsmith.server.helpers.ArtifactGitFileUtils;
@ -25,6 +28,7 @@ import com.google.gson.JsonObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
@ -37,6 +41,8 @@ import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import static com.appsmith.external.git.constants.ce.GitConstantsCE.GitCommandConstantsCE.CHECKOUT_BRANCH;
import static com.appsmith.external.git.constants.ce.GitConstantsCE.RECONSTRUCT_PAGE;
import static com.appsmith.git.constants.CommonConstants.CLIENT_SCHEMA_VERSION;
import static com.appsmith.git.constants.CommonConstants.FILE_FORMAT_VERSION;
import static com.appsmith.git.constants.CommonConstants.SERVER_SCHEMA_VERSION;
@ -50,6 +56,7 @@ public class CommonGitFileUtilsCE {
protected final ArtifactGitFileUtils<ApplicationGitReference> applicationGitFileUtils;
private final FileInterface fileUtils;
private final FileOperations fileOperations;
private final AnalyticsService analyticsService;
private final SessionUserService sessionUserService;
@ -282,15 +289,21 @@ public class CommonGitFileUtilsCE {
}
public Mono<Map<String, Integer>> reconstructMetadataFromRepo(
String workspaceId, String applicationId, String repoName, String branchName, ArtifactType artifactType) {
String workspaceId,
String applicationId,
String repoName,
String branchName,
Boolean isResetToLastCommitRequired,
ArtifactType artifactType) {
ArtifactGitFileUtils<?> artifactGitFileUtils = getArtifactBasedFileHelper(artifactType);
Path baseRepoSuffix = artifactGitFileUtils.getRepoSuffixPath(workspaceId, applicationId, repoName);
return fileUtils
.reconstructMetadataFromGitRepo(workspaceId, applicationId, repoName, branchName, baseRepoSuffix)
.reconstructMetadataFromGitRepo(
workspaceId, applicationId, repoName, branchName, baseRepoSuffix, isResetToLastCommitRequired)
.onErrorResume(error -> Mono.error(
new AppsmithException(AppsmithError.GIT_ACTION_FAILED, "checkout", error.getMessage())))
new AppsmithException(AppsmithError.GIT_ACTION_FAILED, CHECKOUT_BRANCH, error.getMessage())))
.map(metadata -> {
Gson gson = new Gson();
JsonObject metadataJsonObject =
@ -310,20 +323,22 @@ public class CommonGitFileUtilsCE {
/**
* Provides the server schema version in the application json for the given branch
*
* @param workspaceId : workspaceId of the artifact
* @param defaultArtifactId : default branch id of the artifact
* @param repoName : repository name
* @param branchName : current branch name of the artifact
* @param artifactType : artifact type of this operation
* @param workspaceId : workspaceId of the artifact
* @param gitArtifactMetadata : git artifact metadata of the application
* @param isResetToLastCommitRequired : would we need to execute reset command
* @param artifactType : artifact type of this operation
* @return the server schema migration version number
*/
public Mono<Integer> getMetadataServerSchemaMigrationVersion(
String workspaceId,
String defaultArtifactId,
String repoName,
String branchName,
GitArtifactMetadata gitArtifactMetadata,
Boolean isResetToLastCommitRequired,
ArtifactType artifactType) {
String defaultArtifactId = gitArtifactMetadata.getDefaultArtifactId();
String branchName = gitArtifactMetadata.getBranchName();
String repoName = gitArtifactMetadata.getRepoName();
if (!hasText(workspaceId)) {
return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.WORKSPACE_ID));
}
@ -340,11 +355,69 @@ public class CommonGitFileUtilsCE {
return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.REPO_NAME));
}
return reconstructMetadataFromRepo(workspaceId, defaultArtifactId, repoName, branchName, artifactType)
Mono<Integer> serverSchemaNumberMono = reconstructMetadataFromRepo(
workspaceId, defaultArtifactId, repoName, branchName, isResetToLastCommitRequired, artifactType)
.map(metadataMap -> {
return metadataMap.getOrDefault(
CommonConstants.SERVER_SCHEMA_VERSION, JsonSchemaVersions.serverVersion);
});
return Mono.create(
sink -> serverSchemaNumberMono.subscribe(sink::success, sink::error, null, sink.currentContext()));
}
/**
* Provides the server schema version in the application json for the given branch
*
* @param workspaceId : workspace id of the application
* @param gitArtifactMetadata : git artifact metadata
* @param isResetToLastCommitRequired : whether git reset hard is required
* @param artifactType : artifact type of this operation
* @return the server schema migration version number
*/
public Mono<JSONObject> getPageDslVersionNumber(
String workspaceId,
GitArtifactMetadata gitArtifactMetadata,
PageDTO pageDTO,
Boolean isResetToLastCommitRequired,
ArtifactType artifactType) {
String defaultArtifactId = gitArtifactMetadata.getDefaultArtifactId();
String branchName = gitArtifactMetadata.getBranchName();
String repoName = gitArtifactMetadata.getRepoName();
if (!hasText(workspaceId)) {
return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.WORKSPACE_ID));
}
if (!hasText(defaultArtifactId)) {
return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.ARTIFACT_ID));
}
if (!hasText(branchName)) {
return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.BRANCH_NAME));
}
if (!hasText(repoName)) {
return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.REPO_NAME));
}
if (pageDTO == null) {
return Mono.error(new AppsmithException(AppsmithError.PAGE_ID_NOT_GIVEN, FieldName.PAGE));
}
ArtifactGitFileUtils<?> artifactGitFileUtils = getArtifactBasedFileHelper(artifactType);
Path baseRepoSuffix = artifactGitFileUtils.getRepoSuffixPath(workspaceId, defaultArtifactId, repoName);
Mono<JSONObject> jsonObjectMono = fileUtils
.reconstructPageFromGitRepo(pageDTO.getName(), branchName, baseRepoSuffix, isResetToLastCommitRequired)
.onErrorResume(error -> Mono.error(
new AppsmithException(AppsmithError.GIT_ACTION_FAILED, RECONSTRUCT_PAGE, error.getMessage())))
.map(pageJson -> {
return fileOperations.getMainContainer(pageJson);
});
return Mono.create(sink -> jsonObjectMono.subscribe(sink::success, sink::error, null, sink.currentContext()));
}
private Integer getServerSchemaVersion(JsonObject metadataJsonObject) {

View File

@ -123,8 +123,10 @@ public class JsonSchemaMigration {
applicationJson.setServerSchemaVersion(6);
case 6:
MigrationHelperMethods.ensureXmlParserPresenceInCustomJsLibList(applicationJson);
applicationJson.setServerSchemaVersion(7);
case 7:
applicationJson.setServerSchemaVersion(8);
default:
// Unable to detect the serverSchema
}

View File

@ -12,6 +12,6 @@ import lombok.Getter;
*/
@Getter
public class JsonSchemaVersions {
public static final Integer serverVersion = 7;
public static final Integer serverVersion = 8;
public static final Integer clientVersion = 1;
}

View File

@ -101,4 +101,7 @@ public interface NewPageServiceCE extends CrudService<NewPage, String> {
Application branchedApplication, List<NewPage> newPages, boolean viewMode, boolean isRecentlyAccessed);
Mono<String> updateDependencyMap(String pageId, Map<String, List<String>> dependencyMap, String branchName);
Flux<PageDTO> findByApplicationIdAndApplicationMode(
String applicationId, AclPermission permission, ApplicationMode applicationMode);
}

View File

@ -687,4 +687,23 @@ public class NewPageServiceCEImpl extends BaseService<NewPageRepository, NewPage
return Mono.just(count.toString());
});
}
@Override
public Flux<PageDTO> findByApplicationIdAndApplicationMode(
String applicationId, AclPermission permission, ApplicationMode applicationMode) {
Boolean viewMode = ApplicationMode.PUBLISHED.equals(applicationMode);
return findNewPagesByApplicationId(applicationId, permission)
.filter(page -> {
PageDTO pageDTO;
if (ApplicationMode.PUBLISHED.equals(applicationMode)) {
pageDTO = page.getPublishedPage();
} else {
pageDTO = page.getUnpublishedPage();
}
boolean isDeletedOrNull = pageDTO == null || pageDTO.getDeletedAt() != null;
return !isDeletedOrNull;
})
.flatMap(page -> getPageByViewMode(page, viewMode));
}
}

View File

@ -95,8 +95,6 @@ public class ApplicationPageServiceImpl extends ApplicationPageServiceCEImpl imp
datasourceRepository,
datasourcePermission,
dslMigrationUtils,
gitAutoCommitHelper,
autoCommitEligibilityHelper,
actionClonePageService,
actionCollectionClonePageService);
}

View File

@ -34,9 +34,6 @@ import com.appsmith.server.dtos.PageNameIdDTO;
import com.appsmith.server.dtos.PluginTypeAndCountDTO;
import com.appsmith.server.exceptions.AppsmithError;
import com.appsmith.server.exceptions.AppsmithException;
import com.appsmith.server.git.autocommit.helpers.AutoCommitEligibilityHelper;
import com.appsmith.server.git.autocommit.helpers.GitAutoCommitHelper;
import com.appsmith.server.helpers.CollectionUtils;
import com.appsmith.server.helpers.CommonGitFileUtils;
import com.appsmith.server.helpers.DSLMigrationUtils;
import com.appsmith.server.helpers.GitUtils;
@ -127,8 +124,6 @@ public class ApplicationPageServiceCEImpl implements ApplicationPageServiceCE {
private final DatasourceRepository datasourceRepository;
private final DatasourcePermission datasourcePermission;
private final DSLMigrationUtils dslMigrationUtils;
private final GitAutoCommitHelper gitAutoCommitHelper;
private final AutoCommitEligibilityHelper autoCommitEligibilityHelper;
private final ClonePageService<NewAction> actionClonePageService;
private final ClonePageService<ActionCollection> actionCollectionClonePageService;
@ -299,76 +294,7 @@ public class ApplicationPageServiceCEImpl implements ApplicationPageServiceCE {
return newPageService
.findNewPagesByApplicationId(branchedApplication.getId(), pagePermission.getReadPermission())
.filter(newPage -> pageIds.contains(newPage.getId()))
.collectList()
.flatMap(newPageList -> {
if (Boolean.TRUE.equals(viewMode)) {
return Mono.just(newPageList);
}
// autocommit if migration is required
return migrateSchemasForGitConnectedApps(branchedApplication, newPageList)
.onErrorResume(error -> {
log.debug(
"Skipping the autocommit for applicationId : {} due to error; {}",
branchedApplication.getId(),
error.getMessage());
return Mono.just(Boolean.FALSE);
})
.thenReturn(newPageList);
});
}
/**
* Publishes the autocommit if it's eligible for one
* @param application : the branched application which requires schemaMigration
* @param newPages : list of pages from db
* @return : a boolean publisher
*/
private Mono<Boolean> migrateSchemasForGitConnectedApps(Application application, List<NewPage> newPages) {
if (CollectionUtils.isNullOrEmpty(newPages)) {
return Mono.just(Boolean.FALSE);
}
GitArtifactMetadata gitMetadata = application.getGitArtifactMetadata();
if (application.getGitArtifactMetadata() == null) {
return Mono.just(Boolean.FALSE);
}
String defaultApplicationId = gitMetadata.getDefaultArtifactId();
String branchName = gitMetadata.getBranchName();
String workspaceId = application.getWorkspaceId();
if (!StringUtils.hasText(branchName)) {
log.debug(
"Skipping the autocommit for applicationId : {}, branch name is not present", application.getId());
return Mono.just(Boolean.FALSE);
}
if (!StringUtils.hasText(defaultApplicationId)) {
log.debug(
"Skipping the autocommit for applicationId : {}, defaultApplicationId is not present",
application.getId());
return Mono.just(Boolean.FALSE);
}
// since this method is only called when the app is in edit mode
Mono<PageDTO> pageDTOMono = getPage(newPages.get(0), false);
return pageDTOMono.flatMap(pageDTO -> {
return autoCommitEligibilityHelper
.isAutoCommitRequired(workspaceId, gitMetadata, pageDTO)
.flatMap(autoCommitTriggerDTO -> {
if (Boolean.TRUE.equals(autoCommitTriggerDTO.getIsAutoCommitRequired())) {
return gitAutoCommitHelper.autoCommitApplication(
autoCommitTriggerDTO, defaultApplicationId, branchName);
}
return Mono.just(Boolean.FALSE);
});
});
.collectList();
}
@Override

View File

@ -1,25 +1,37 @@
package com.appsmith.server.git;
import com.appsmith.external.converters.ISOStringToInstantConverter;
import com.appsmith.external.dtos.ModifiedResources;
import com.appsmith.external.enums.FeatureFlagEnum;
import com.appsmith.external.git.GitExecutor;
import com.appsmith.external.models.ApplicationGitReference;
import com.appsmith.server.constants.ArtifactType;
import com.appsmith.server.constants.SerialiseArtifactObjective;
import com.appsmith.server.domains.Workspace;
import com.appsmith.server.dtos.ApplicationImportDTO;
import com.appsmith.server.dtos.ApplicationJson;
import com.appsmith.server.exports.internal.ExportService;
import com.appsmith.server.featureflags.CachedFeatures;
import com.appsmith.server.helpers.CommonGitFileUtils;
import com.appsmith.server.helpers.MockPluginExecutor;
import com.appsmith.server.helpers.PluginExecutorHelper;
import com.appsmith.server.imports.internal.ImportService;
import com.appsmith.server.services.FeatureFlagService;
import com.appsmith.server.services.WorkspaceService;
import com.appsmith.server.testhelpers.git.GitFileSystemTestHelper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.DiffEntry;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
import org.springframework.boot.test.context.SpringBootTest;
@ -37,10 +49,19 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static com.appsmith.server.constants.ArtifactType.APPLICATION;
import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
@ -78,9 +99,6 @@ import static org.mockito.ArgumentMatchers.any;
@DirtiesContext
public class ServerSchemaMigrationEnforcerTest {
@Autowired
Gson gson;
@Autowired
WorkspaceService workspaceService;
@ -93,9 +111,28 @@ public class ServerSchemaMigrationEnforcerTest {
@Autowired
CommonGitFileUtils commonGitFileUtils;
@Autowired
GitFileSystemTestHelper gitFileSystemTestHelper;
@SpyBean
GitExecutor gitExecutor;
@SpyBean
FeatureFlagService featureFlagService;
@MockBean
PluginExecutorHelper pluginExecutorHelper;
private final Gson gson = new GsonBuilder()
.registerTypeAdapter(Instant.class, new ISOStringToInstantConverter())
.setPrettyPrinting()
.create();
private static final String DEFAULT_APPLICATION_ID = "default-app-id",
BRANCH_NAME = "develop",
REPO_NAME = "repoName",
WORKSPACE_ID = "test-workspace-id";
public static final String CUSTOM_JS_LIB_LIST = "jsLibraries";
public static final String EXPORTED_APPLICATION = "application";
public static final String UNPUBLISHED_CUSTOM_JS_LIBS = "unpublishedCustomJSLibs";
@ -167,6 +204,7 @@ public class ServerSchemaMigrationEnforcerTest {
}
@Test
@Disabled
@WithUserDetails(value = "api_user")
public void importApplication_ThenExportApplication_MatchJson_equals_Success() throws URISyntaxException {
String filePath = "ce-automation-test.json";
@ -199,7 +237,7 @@ public class ServerSchemaMigrationEnforcerTest {
.exportByArtifactId(
applicationImportDTO.getApplication().getId(),
SerialiseArtifactObjective.VERSION_CONTROL,
ArtifactType.APPLICATION)
APPLICATION)
.map(artifactExchangeJson -> (ApplicationJson) artifactExchangeJson);
});
@ -254,4 +292,138 @@ public class ServerSchemaMigrationEnforcerTest {
}
}
}
@Test
public void savedFile_reSavedWithDifferentSerialisationLogic_diffOccurs()
throws URISyntaxException, IOException, GitAPIException {
ApplicationJson applicationJson =
gitFileSystemTestHelper.getApplicationJson(this.getClass().getResource("ce-automation-test.json"));
ModifiedResources modifiedResources = new ModifiedResources();
modifiedResources.setAllModified(true);
applicationJson.setModifiedResources(modifiedResources);
CachedFeatures cachedFeatures = new CachedFeatures();
cachedFeatures.setFeatures(Map.of(FeatureFlagEnum.release_git_autocommit_feature_enabled.name(), FALSE));
Mockito.when(featureFlagService.getCachedTenantFeatureFlags())
.thenAnswer((Answer<CachedFeatures>) invocations -> cachedFeatures);
gitFileSystemTestHelper.setupGitRepository(
WORKSPACE_ID, DEFAULT_APPLICATION_ID, BRANCH_NAME, REPO_NAME, applicationJson);
cachedFeatures.setFeatures(Map.of(FeatureFlagEnum.release_git_autocommit_feature_enabled.name(), TRUE));
Path suffixPath = Paths.get(WORKSPACE_ID, DEFAULT_APPLICATION_ID, REPO_NAME);
Path gitCompletePath = gitExecutor.createRepoPath(suffixPath);
commonGitFileUtils
.saveArtifactToLocalRepo(suffixPath, applicationJson, BRANCH_NAME)
.block();
try (Git gitRepo = Git.open(gitCompletePath.toFile())) {
List<DiffEntry> diffEntries = gitRepo.diff().call();
Set<String> fileChanges = Set.of(
"application.json",
"metadata.json",
"theme.json",
"datasources/JSON typicode API (1).json",
"datasources/TED postgres (1).json",
"datasources/mainGoogleSheetDS.json");
for (DiffEntry diff : diffEntries) {
assertThat(fileChanges).contains(diff.getOldPath());
assertThat(fileChanges).contains(diff.getNewPath());
assertThat(diff.getChangeType()).isEqualTo(DiffEntry.ChangeType.MODIFY);
}
assertThat(diffEntries.size()).isNotZero();
}
}
@Test
public void savedFile_reSavedWithSameSerialisationLogic_noDiffOccurs()
throws URISyntaxException, IOException, GitAPIException {
ApplicationJson applicationJson =
gitFileSystemTestHelper.getApplicationJson(this.getClass().getResource("ce-automation-test.json"));
ModifiedResources modifiedResources = new ModifiedResources();
modifiedResources.setAllModified(true);
applicationJson.setModifiedResources(modifiedResources);
gitFileSystemTestHelper.setupGitRepository(
WORKSPACE_ID, DEFAULT_APPLICATION_ID, BRANCH_NAME, REPO_NAME, applicationJson);
Path suffixPath = Paths.get(WORKSPACE_ID, DEFAULT_APPLICATION_ID, REPO_NAME);
Path gitCompletePath = gitExecutor.createRepoPath(suffixPath);
commonGitFileUtils
.saveArtifactToLocalRepo(suffixPath, applicationJson, BRANCH_NAME)
.block();
try (Git gitRepo = Git.open(gitCompletePath.toFile())) {
List<DiffEntry> diffEntries = gitRepo.diff().call();
assertThat(diffEntries.size()).isZero();
}
}
@Test
@WithUserDetails(value = "api_user")
public void saveGitRepo_ImportAndThenExport_diffOccurs() throws URISyntaxException, IOException, GitAPIException {
ApplicationJson applicationJson =
gitFileSystemTestHelper.getApplicationJson(this.getClass().getResource("ce-automation-test.json"));
ModifiedResources modifiedResources = new ModifiedResources();
modifiedResources.setAllModified(true);
applicationJson.setModifiedResources(modifiedResources);
CachedFeatures cachedFeatures = new CachedFeatures();
cachedFeatures.setFeatures(Map.of(FeatureFlagEnum.release_git_autocommit_feature_enabled.name(), TRUE));
Mockito.when(featureFlagService.getCachedTenantFeatureFlags())
.thenAnswer((Answer<CachedFeatures>) invocations -> cachedFeatures);
gitFileSystemTestHelper.setupGitRepository(
WORKSPACE_ID, DEFAULT_APPLICATION_ID, BRANCH_NAME, REPO_NAME, applicationJson);
ApplicationJson jsonToBeImported = commonGitFileUtils
.reconstructArtifactExchangeJsonFromGitRepo(
WORKSPACE_ID, DEFAULT_APPLICATION_ID, REPO_NAME, BRANCH_NAME, APPLICATION)
.map(artifactExchangeJson -> (ApplicationJson) artifactExchangeJson)
.block();
Workspace newWorkspace = new Workspace();
newWorkspace.setName("Template Workspace1");
Workspace workspace = workspaceService.create(newWorkspace).block();
ApplicationJson exportedJson = importService
.importNewArtifactInWorkspaceFromJson(workspace.getId(), jsonToBeImported)
.flatMap(artifactExchangeJson -> {
return exportService
.exportByArtifactId(
artifactExchangeJson.getId(),
SerialiseArtifactObjective.VERSION_CONTROL,
APPLICATION)
.map(exportArtifactJson -> {
ApplicationJson applicationJson1 = (ApplicationJson) exportArtifactJson;
applicationJson1.setModifiedResources(modifiedResources);
return applicationJson1;
});
})
.block();
Path suffixPath = Paths.get(WORKSPACE_ID, DEFAULT_APPLICATION_ID, REPO_NAME);
Path gitCompletePath = gitExecutor.createRepoPath(suffixPath);
// save back to the repository in order to compare the diff.
commonGitFileUtils
.saveArtifactToLocalRepo(suffixPath, exportedJson, BRANCH_NAME)
.block();
try (Git gitRepo = Git.open(gitCompletePath.toFile())) {
List<DiffEntry> diffEntries = gitRepo.diff().call();
assertThat(diffEntries.size()).isNotZero();
for (DiffEntry diffEntry : diffEntries) {
// assertion that no new file has been created
assertThat(diffEntry.getOldPath()).isEqualTo(diffEntry.getNewPath());
}
}
}
}

View File

@ -1,394 +0,0 @@
package com.appsmith.server.git.autocommit;
import com.appsmith.external.dtos.GitLogDTO;
import com.appsmith.external.enums.FeatureFlagEnum;
import com.appsmith.external.git.GitExecutor;
import com.appsmith.external.helpers.AppsmithBeanUtils;
import com.appsmith.server.acl.AclPermission;
import com.appsmith.server.applications.base.ApplicationService;
import com.appsmith.server.domains.Application;
import com.appsmith.server.domains.ApplicationMode;
import com.appsmith.server.domains.ApplicationPage;
import com.appsmith.server.domains.GitArtifactMetadata;
import com.appsmith.server.domains.GitAuth;
import com.appsmith.server.domains.GitProfile;
import com.appsmith.server.domains.Layout;
import com.appsmith.server.domains.NewPage;
import com.appsmith.server.dtos.ApplicationJson;
import com.appsmith.server.dtos.AutoCommitTriggerDTO;
import com.appsmith.server.dtos.PageDTO;
import com.appsmith.server.git.autocommit.helpers.AutoCommitEligibilityHelper;
import com.appsmith.server.git.common.CommonGitService;
import com.appsmith.server.helpers.DSLMigrationUtils;
import com.appsmith.server.helpers.GitPrivateRepoHelper;
import com.appsmith.server.migrations.JsonSchemaMigration;
import com.appsmith.server.migrations.JsonSchemaVersions;
import com.appsmith.server.newpages.base.NewPageService;
import com.appsmith.server.services.ApplicationPageService;
import com.appsmith.server.services.FeatureFlagService;
import com.appsmith.server.services.UserDataService;
import com.appsmith.server.testhelpers.git.GitFileSystemTestHelper;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONObject;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.BranchTrackingStatus;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static com.appsmith.server.git.AutoCommitEventHandlerCEImpl.AUTO_COMMIT_MSG_FORMAT;
import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
@ExtendWith(SpringExtension.class)
@SpringBootTest
@Slf4j
public class ApplicationPageServiceAutoCommitTest {
@SpyBean
ApplicationPageService applicationPageService;
@Autowired
GitFileSystemTestHelper gitFileSystemTestHelper;
@SpyBean
GitExecutor gitExecutor;
@MockBean
FeatureFlagService featureFlagService;
@MockBean
DSLMigrationUtils dslMigrationUtils;
@MockBean
ApplicationService applicationService;
@MockBean
NewPageService newPageService;
@MockBean
CommonGitService commonGitService;
@MockBean
GitPrivateRepoHelper gitPrivateRepoHelper;
@SpyBean
AutoCommitEligibilityHelper autoCommitEligibilityHelper;
@MockBean
BranchTrackingStatus branchTrackingStatus;
@MockBean
UserDataService userDataService;
@SpyBean
JsonSchemaMigration jsonSchemaMigration;
Application testApplication;
Path baseRepoSuffix;
private static final Integer DSL_VERSION_NUMBER = 88;
private static final String WORKSPACE_ID = "test-workspace";
private static final String REPO_NAME = "test-repo";
private static final String BRANCH_NAME = "develop";
private static final String APP_JSON_NAME = "autocommit.json";
private static final String APP_NAME = "autocommit";
private static final Integer WAIT_DURATION_FOR_ASYNC_EVENT = 5;
private static final String PUBLIC_KEY = "public-key";
private static final String PRIVATE_KEY = "private-key";
private static final String REPO_URL = "domain.xy";
private static final String DEFAULT_APP_ID = "default-app-id", DEFAULT_BRANCH_NAME = "master";
private Application createApplication() {
Application application = new Application();
application.setName(APP_NAME);
application.setWorkspaceId(WORKSPACE_ID);
application.setId(DEFAULT_APP_ID);
ApplicationPage applicationPage = new ApplicationPage();
applicationPage.setId("testPageId");
applicationPage.setIsDefault(TRUE);
application.setPages(List.of(applicationPage));
GitArtifactMetadata gitArtifactMetadata = new GitArtifactMetadata();
gitArtifactMetadata.setBranchName(BRANCH_NAME);
gitArtifactMetadata.setDefaultBranchName(DEFAULT_BRANCH_NAME);
gitArtifactMetadata.setRepoName(REPO_NAME);
gitArtifactMetadata.setDefaultApplicationId(DEFAULT_APP_ID);
gitArtifactMetadata.setRemoteUrl(REPO_URL);
GitAuth gitAuth = new GitAuth();
gitAuth.setPrivateKey(PRIVATE_KEY);
gitAuth.setPublicKey(PUBLIC_KEY);
gitArtifactMetadata.setGitAuth(gitAuth);
application.setGitApplicationMetadata(gitArtifactMetadata);
return application;
}
private GitProfile createGitProfile() {
GitProfile gitProfile = new GitProfile();
gitProfile.setAuthorName("authorName");
gitProfile.setAuthorEmail("author@domain.xy");
return gitProfile;
}
private NewPage createNewPage() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("key", "value");
jsonObject.put("version", DSL_VERSION_NUMBER);
Layout layout1 = new Layout();
layout1.setId("testLayoutId");
layout1.setDsl(jsonObject);
PageDTO pageDTO = new PageDTO();
pageDTO.setId("testPageId");
pageDTO.setApplicationId(DEFAULT_APP_ID);
pageDTO.setLayouts(List.of(layout1));
NewPage newPage = new NewPage();
newPage.setId("testPageId");
newPage.setApplicationId(DEFAULT_APP_ID);
newPage.setUnpublishedPage(pageDTO);
return newPage;
}
@BeforeEach
public void beforeTest() {
Mockito.when(featureFlagService.check(FeatureFlagEnum.release_git_server_autocommit_feature_enabled))
.thenReturn(Mono.just(TRUE));
Mockito.when(featureFlagService.check(FeatureFlagEnum.release_git_autocommit_feature_enabled))
.thenReturn(Mono.just(TRUE));
Mockito.when(commonGitService.fetchRemoteChanges(
any(Application.class), any(Application.class), anyString(), anyBoolean()))
.thenReturn(Mono.just(branchTrackingStatus));
Mockito.when(branchTrackingStatus.getBehindCount()).thenReturn(0);
// create New Pages
NewPage newPage = createNewPage();
// create application
testApplication = createApplication();
baseRepoSuffix = Paths.get(WORKSPACE_ID, DEFAULT_APP_ID, REPO_NAME);
doReturn(Mono.just("success"))
.when(gitExecutor)
.pushApplication(baseRepoSuffix, REPO_URL, PUBLIC_KEY, PRIVATE_KEY, BRANCH_NAME);
doReturn(Mono.just(newPage.getUnpublishedPage()))
.when(applicationPageService)
.getPage(any(NewPage.class), anyBoolean());
Mockito.when(newPageService.findNewPagesByApplicationId(anyString(), any(AclPermission.class)))
.thenReturn(Flux.just(newPage));
Mockito.when(applicationService.findByBranchNameAndDefaultApplicationId(
anyString(), anyString(), any(AclPermission.class)))
.thenReturn(Mono.just(testApplication));
Mockito.when(applicationService.findById(anyString(), any(AclPermission.class)))
.thenReturn(Mono.just(testApplication));
Mockito.when(gitPrivateRepoHelper.isBranchProtected(any(), anyString())).thenReturn(Mono.just(FALSE));
Mockito.when(userDataService.getGitProfileForCurrentUser(any())).thenReturn(Mono.just(createGitProfile()));
}
@AfterEach
public void afterTest() {
gitFileSystemTestHelper.deleteWorkspaceDirectory(WORKSPACE_ID);
}
@Test
@Disabled
public void testAutoCommit_whenOnlyServerIsEligibleForMigration_commitSuccess()
throws URISyntaxException, IOException, GitAPIException {
ApplicationJson applicationJson =
gitFileSystemTestHelper.getApplicationJson(this.getClass().getResource(APP_JSON_NAME));
doReturn(Mono.just(new AutoCommitTriggerDTO(TRUE, FALSE, TRUE)))
.when(autoCommitEligibilityHelper)
.isAutoCommitRequired(anyString(), any(GitArtifactMetadata.class), any(PageDTO.class));
ApplicationJson applicationJson1 = new ApplicationJson();
AppsmithBeanUtils.copyNewFieldValuesIntoOldObject(applicationJson, applicationJson1);
applicationJson1.setServerSchemaVersion(JsonSchemaVersions.serverVersion + 1);
doReturn(Mono.just(applicationJson1))
.when(jsonSchemaMigration)
.migrateApplicationJsonToLatestSchema(any(ApplicationJson.class));
gitFileSystemTestHelper.setupGitRepository(
WORKSPACE_ID, DEFAULT_APP_ID, BRANCH_NAME, REPO_NAME, applicationJson);
// verifying the initial number of commits
StepVerifier.create(gitExecutor.getCommitHistory(baseRepoSuffix))
.assertNext(gitLogDTOs -> {
assertThat(gitLogDTOs).isNotEmpty();
assertThat(gitLogDTOs.size()).isEqualTo(2);
Set<String> commitMessages =
gitLogDTOs.stream().map(GitLogDTO::getCommitMessage).collect(Collectors.toSet());
assertThat(commitMessages).doesNotContain(String.format(AUTO_COMMIT_MSG_FORMAT, "UNKNOWN"));
})
.verifyComplete();
// this would trigger autocommit
Mono<List<GitLogDTO>> gitlogDTOsMono = applicationPageService
.getPagesBasedOnApplicationMode(testApplication, ApplicationMode.EDIT)
.then(Mono.delay(Duration.ofSeconds(WAIT_DURATION_FOR_ASYNC_EVENT)))
.then(gitExecutor.getCommitHistory(baseRepoSuffix));
// verifying final number of commits
StepVerifier.create(gitlogDTOsMono)
.assertNext(gitLogDTOs -> {
assertThat(gitLogDTOs).isNotEmpty();
assertThat(gitLogDTOs.size()).isEqualTo(3);
Set<String> commitMessages =
gitLogDTOs.stream().map(GitLogDTO::getCommitMessage).collect(Collectors.toSet());
assertThat(commitMessages).contains(String.format(AUTO_COMMIT_MSG_FORMAT, "UNKNOWN"));
})
.verifyComplete();
}
@Test
@Disabled
public void testAutoCommit_whenOnlyClientIsEligibleForMigration_commitSuccess()
throws GitAPIException, IOException, URISyntaxException {
ApplicationJson applicationJson =
gitFileSystemTestHelper.getApplicationJson(this.getClass().getResource(APP_JSON_NAME));
int pageDSLNumber = applicationJson
.getPageList()
.get(0)
.getUnpublishedPage()
.getLayouts()
.get(0)
.getDsl()
.getAsNumber("version")
.intValue();
doReturn(Mono.just(new AutoCommitTriggerDTO(TRUE, TRUE, FALSE)))
.when(autoCommitEligibilityHelper)
.isAutoCommitRequired(anyString(), any(GitArtifactMetadata.class), any(PageDTO.class));
Mockito.when(dslMigrationUtils.getLatestDslVersion()).thenReturn(Mono.just(pageDSLNumber + 1));
JSONObject dslAfterMigration = new JSONObject();
dslAfterMigration.put("key", "after migration");
// mock the dsl migration utils to return updated dsl when requested with older dsl
Mockito.when(dslMigrationUtils.migratePageDsl(any(JSONObject.class))).thenReturn(Mono.just(dslAfterMigration));
gitFileSystemTestHelper.setupGitRepository(
WORKSPACE_ID, DEFAULT_APP_ID, BRANCH_NAME, REPO_NAME, applicationJson);
// verifying the initial number of commits
StepVerifier.create(gitExecutor.getCommitHistory(baseRepoSuffix))
.assertNext(gitLogDTOs -> {
assertThat(gitLogDTOs).isNotEmpty();
assertThat(gitLogDTOs.size()).isEqualTo(2);
Set<String> commitMessages =
gitLogDTOs.stream().map(GitLogDTO::getCommitMessage).collect(Collectors.toSet());
assertThat(commitMessages).doesNotContain(String.format(AUTO_COMMIT_MSG_FORMAT, "UNKNOWN"));
})
.verifyComplete();
// this would trigger autocommit
Mono<List<GitLogDTO>> gitlogDTOsMono = applicationPageService
.getPagesBasedOnApplicationMode(testApplication, ApplicationMode.EDIT)
.then(Mono.delay(Duration.ofSeconds(WAIT_DURATION_FOR_ASYNC_EVENT)))
.then(gitExecutor.getCommitHistory(baseRepoSuffix));
// verifying final number of commits
StepVerifier.create(gitlogDTOsMono)
.assertNext(gitLogDTOs -> {
assertThat(gitLogDTOs).isNotEmpty();
assertThat(gitLogDTOs.size()).isEqualTo(3);
Set<String> commitMessages =
gitLogDTOs.stream().map(GitLogDTO::getCommitMessage).collect(Collectors.toSet());
assertThat(commitMessages).contains(String.format(AUTO_COMMIT_MSG_FORMAT, "UNKNOWN"));
})
.verifyComplete();
}
@Test
@Disabled
public void testAutoCommit_whenAutoCommitNotEligible_returnsFalse()
throws URISyntaxException, IOException, GitAPIException {
ApplicationJson applicationJson =
gitFileSystemTestHelper.getApplicationJson(this.getClass().getResource(APP_JSON_NAME));
doReturn(Mono.just(new AutoCommitTriggerDTO(FALSE, FALSE, FALSE)))
.when(autoCommitEligibilityHelper)
.isAutoCommitRequired(anyString(), any(GitArtifactMetadata.class), any(PageDTO.class));
gitFileSystemTestHelper.setupGitRepository(
WORKSPACE_ID, DEFAULT_APP_ID, BRANCH_NAME, REPO_NAME, applicationJson);
// verifying the initial number of commits
StepVerifier.create(gitExecutor.getCommitHistory(baseRepoSuffix))
.assertNext(gitLogDTOs -> {
assertThat(gitLogDTOs).isNotEmpty();
assertThat(gitLogDTOs.size()).isEqualTo(2);
Set<String> commitMessages =
gitLogDTOs.stream().map(GitLogDTO::getCommitMessage).collect(Collectors.toSet());
assertThat(commitMessages).doesNotContain(String.format(AUTO_COMMIT_MSG_FORMAT, "UNKNOWN"));
})
.verifyComplete();
// this would not trigger autocommit
Mono<List<GitLogDTO>> gitlogDTOsMono = applicationPageService
.getPagesBasedOnApplicationMode(testApplication, ApplicationMode.EDIT)
.then(Mono.delay(Duration.ofSeconds(WAIT_DURATION_FOR_ASYNC_EVENT)))
.then(gitExecutor.getCommitHistory(baseRepoSuffix));
// verifying final number of commits
StepVerifier.create(gitlogDTOsMono)
.assertNext(gitLogDTOs -> {
assertThat(gitLogDTOs).isNotEmpty();
assertThat(gitLogDTOs.size()).isEqualTo(2);
Set<String> commitMessages =
gitLogDTOs.stream().map(GitLogDTO::getCommitMessage).collect(Collectors.toSet());
assertThat(commitMessages).doesNotContain(String.format(AUTO_COMMIT_MSG_FORMAT, "UNKNOWN"));
})
.verifyComplete();
}
}

View File

@ -14,8 +14,6 @@ import com.appsmith.server.dtos.ApplicationJson;
import com.appsmith.server.dtos.PageDTO;
import com.appsmith.server.events.AutoCommitEvent;
import com.appsmith.server.featureflags.CachedFeatures;
import com.appsmith.server.git.AutoCommitEventHandler;
import com.appsmith.server.git.AutoCommitEventHandlerImpl;
import com.appsmith.server.git.GitRedisUtils;
import com.appsmith.server.helpers.CommonGitFileUtils;
import com.appsmith.server.helpers.DSLMigrationUtils;
@ -54,7 +52,7 @@ import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import static com.appsmith.server.git.AutoCommitEventHandlerCEImpl.AUTO_COMMIT_MSG_FORMAT;
import static com.appsmith.server.git.autocommit.AutoCommitEventHandlerCEImpl.AUTO_COMMIT_MSG_FORMAT;
import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
import static org.assertj.core.api.Assertions.assertThat;
@ -498,12 +496,12 @@ public class AutoCommitEventHandlerImplTest {
autoCommitEvent.getBranchName());
CachedFeatures cachedFeatures = new CachedFeatures();
cachedFeatures.setFeatures(Map.of(FeatureFlagEnum.release_git_cleanup_feature_enabled.name(), FALSE));
cachedFeatures.setFeatures(Map.of(FeatureFlagEnum.release_git_autocommit_feature_enabled.name(), FALSE));
Mockito.when(featureFlagService.getCachedTenantFeatureFlags())
.thenAnswer((Answer<CachedFeatures>) invocations -> cachedFeatures);
gitFileSystemTestHelper.setupGitRepository(autoCommitEvent, applicationJson);
cachedFeatures.setFeatures(Map.of(FeatureFlagEnum.release_git_cleanup_feature_enabled.name(), TRUE));
cachedFeatures.setFeatures(Map.of(FeatureFlagEnum.release_git_autocommit_feature_enabled.name(), TRUE));
StepVerifier.create(autoCommitEventHandler
.autoCommitServerMigration(autoCommitEvent)
@ -536,7 +534,7 @@ public class AutoCommitEventHandlerImplTest {
gitFileSystemTestHelper.getApplicationJson(this.getClass().getResource("application.json"));
CachedFeatures cachedFeatures = new CachedFeatures();
cachedFeatures.setFeatures(Map.of(FeatureFlagEnum.release_git_cleanup_feature_enabled.name(), FALSE));
cachedFeatures.setFeatures(Map.of(FeatureFlagEnum.release_git_autocommit_feature_enabled.name(), FALSE));
Mockito.when(featureFlagService.getCachedTenantFeatureFlags())
.thenAnswer((Answer<CachedFeatures>) invocations -> cachedFeatures);

View File

@ -0,0 +1,686 @@
package com.appsmith.server.git.autocommit;
import com.appsmith.external.dtos.GitLogDTO;
import com.appsmith.external.enums.FeatureFlagEnum;
import com.appsmith.external.git.GitExecutor;
import com.appsmith.external.helpers.AppsmithBeanUtils;
import com.appsmith.server.acl.AclPermission;
import com.appsmith.server.applications.base.ApplicationService;
import com.appsmith.server.domains.Application;
import com.appsmith.server.domains.ApplicationMode;
import com.appsmith.server.domains.ApplicationPage;
import com.appsmith.server.domains.GitArtifactMetadata;
import com.appsmith.server.domains.GitAuth;
import com.appsmith.server.domains.GitProfile;
import com.appsmith.server.domains.Layout;
import com.appsmith.server.dtos.ApplicationJson;
import com.appsmith.server.dtos.AutoCommitResponseDTO;
import com.appsmith.server.dtos.PageDTO;
import com.appsmith.server.git.autocommit.helpers.AutoCommitEligibilityHelper;
import com.appsmith.server.git.common.CommonGitService;
import com.appsmith.server.helpers.CommonGitFileUtils;
import com.appsmith.server.helpers.DSLMigrationUtils;
import com.appsmith.server.helpers.GitPrivateRepoHelper;
import com.appsmith.server.helpers.RedisUtils;
import com.appsmith.server.migrations.JsonSchemaMigration;
import com.appsmith.server.migrations.JsonSchemaVersions;
import com.appsmith.server.newpages.base.NewPageService;
import com.appsmith.server.services.FeatureFlagService;
import com.appsmith.server.services.UserDataService;
import com.appsmith.server.solutions.PagePermission;
import com.appsmith.server.testhelpers.git.GitFileSystemTestHelper;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONObject;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.BranchTrackingStatus;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static com.appsmith.server.git.autocommit.AutoCommitEventHandlerCEImpl.AUTO_COMMIT_MSG_FORMAT;
import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
@ExtendWith(SpringExtension.class)
@SpringBootTest
@Slf4j
public class AutoCommitServiceTest {
@SpyBean
AutoCommitService autoCommitService;
@Autowired
GitFileSystemTestHelper gitFileSystemTestHelper;
@SpyBean
GitExecutor gitExecutor;
@MockBean
FeatureFlagService featureFlagService;
@MockBean
DSLMigrationUtils dslMigrationUtils;
@MockBean
ApplicationService applicationService;
@MockBean
NewPageService newPageService;
@Autowired
PagePermission pagePermission;
@SpyBean
RedisUtils redisUtils;
@MockBean
CommonGitService commonGitService;
@SpyBean
CommonGitFileUtils commonGitFileUtils;
@MockBean
GitPrivateRepoHelper gitPrivateRepoHelper;
@SpyBean
AutoCommitEligibilityHelper autoCommitEligibilityHelper;
@MockBean
BranchTrackingStatus branchTrackingStatus;
@MockBean
UserDataService userDataService;
@SpyBean
JsonSchemaMigration jsonSchemaMigration;
Application testApplication;
Path baseRepoSuffix;
private static final Integer DSL_VERSION_NUMBER = 88;
private static final String WORKSPACE_ID = "test-workspace";
private static final String REPO_NAME = "test-repo";
private static final String BRANCH_NAME = "develop";
private static final String APP_JSON_NAME = "autocommit.json";
private static final String APP_NAME = "autocommit";
private static final Integer WAIT_DURATION_FOR_ASYNC_EVENT = 5;
private static final String PUBLIC_KEY = "public-key";
private static final String PRIVATE_KEY = "private-key";
private static final String REPO_URL = "domain.xy";
private static final String DEFAULT_APP_ID = "default-app-id", DEFAULT_BRANCH_NAME = "master";
private static final Integer SERVER_SCHEMA_VERSION = JsonSchemaVersions.serverVersion;
private Application createApplication() {
Application application = new Application();
application.setName(APP_NAME);
application.setWorkspaceId(WORKSPACE_ID);
application.setId(DEFAULT_APP_ID);
ApplicationPage applicationPage = new ApplicationPage();
applicationPage.setId("testPageId");
applicationPage.setIsDefault(TRUE);
application.setPages(List.of(applicationPage));
GitArtifactMetadata gitArtifactMetadata = new GitArtifactMetadata();
gitArtifactMetadata.setBranchName(BRANCH_NAME);
gitArtifactMetadata.setDefaultBranchName(DEFAULT_BRANCH_NAME);
gitArtifactMetadata.setRepoName(REPO_NAME);
gitArtifactMetadata.setDefaultApplicationId(DEFAULT_APP_ID);
gitArtifactMetadata.setRemoteUrl(REPO_URL);
GitAuth gitAuth = new GitAuth();
gitAuth.setPrivateKey(PRIVATE_KEY);
gitAuth.setPublicKey(PUBLIC_KEY);
gitArtifactMetadata.setGitAuth(gitAuth);
application.setGitApplicationMetadata(gitArtifactMetadata);
return application;
}
private GitProfile createGitProfile() {
GitProfile gitProfile = new GitProfile();
gitProfile.setAuthorName("authorName");
gitProfile.setAuthorEmail("author@domain.xy");
return gitProfile;
}
private PageDTO createPageDTO() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("key", "value");
jsonObject.put("version", DSL_VERSION_NUMBER);
Layout layout1 = new Layout();
layout1.setId("testLayoutId");
layout1.setDsl(jsonObject);
PageDTO pageDTO = new PageDTO();
pageDTO.setId("testPageId");
pageDTO.setApplicationId(DEFAULT_APP_ID);
pageDTO.setLayouts(List.of(layout1));
return pageDTO;
}
private org.json.JSONObject getMockedDsl() {
org.json.JSONObject jsonObject = new org.json.JSONObject();
jsonObject.put("version", DSL_VERSION_NUMBER);
return jsonObject;
}
private void mockAutoCommitTriggerResponse(Boolean serverMigration, Boolean clientMigration) {
doReturn(Mono.just(getMockedDsl()))
.when(commonGitFileUtils)
.getPageDslVersionNumber(anyString(), any(), any(), anyBoolean(), any());
Integer dslVersionNumber = clientMigration ? DSL_VERSION_NUMBER + 1 : DSL_VERSION_NUMBER;
Integer serverSchemaVersionNumber = serverMigration ? SERVER_SCHEMA_VERSION - 1 : SERVER_SCHEMA_VERSION;
doReturn(Mono.just(dslVersionNumber)).when(dslMigrationUtils).getLatestDslVersion();
// server as true
doReturn(Mono.just(serverSchemaVersionNumber))
.when(commonGitFileUtils)
.getMetadataServerSchemaMigrationVersion(anyString(), any(), anyBoolean(), any());
}
@BeforeEach
public void beforeTest() {
// create application
testApplication = createApplication();
baseRepoSuffix = Paths.get(WORKSPACE_ID, DEFAULT_APP_ID, REPO_NAME);
// used for fetching application on autocommit service and gitAutoCommitHelper.autocommit
Mockito.when(applicationService.findByBranchNameAndDefaultApplicationId(
anyString(), anyString(), any(AclPermission.class)))
.thenReturn(Mono.just(testApplication));
// create page-dto
PageDTO pageDTO = createPageDTO();
Mockito.when(newPageService.findByApplicationIdAndApplicationMode(
DEFAULT_APP_ID, pagePermission.getEditPermission(), ApplicationMode.PUBLISHED))
.thenReturn(Flux.just(pageDTO));
Mockito.when(featureFlagService.check(FeatureFlagEnum.release_git_autocommit_eligibility_enabled))
.thenReturn(Mono.just(TRUE));
Mockito.when(featureFlagService.check(FeatureFlagEnum.release_git_autocommit_feature_enabled))
.thenReturn(Mono.just(TRUE));
Mockito.when(commonGitService.fetchRemoteChanges(
any(Application.class), any(Application.class), anyString(), anyBoolean()))
.thenReturn(Mono.just(branchTrackingStatus));
Mockito.when(branchTrackingStatus.getBehindCount()).thenReturn(0);
doReturn(Mono.just("success"))
.when(gitExecutor)
.pushApplication(baseRepoSuffix, REPO_URL, PUBLIC_KEY, PRIVATE_KEY, BRANCH_NAME);
Mockito.when(applicationService.findById(anyString(), any(AclPermission.class)))
.thenReturn(Mono.just(testApplication));
Mockito.when(gitPrivateRepoHelper.isBranchProtected(any(), anyString())).thenReturn(Mono.just(FALSE));
Mockito.when(userDataService.getGitProfileForCurrentUser(any())).thenReturn(Mono.just(createGitProfile()));
}
@AfterEach
public void afterTest() {
gitFileSystemTestHelper.deleteWorkspaceDirectory(WORKSPACE_ID);
}
@Test
public void testAutoCommit_whenOnlyServerIsEligibleForMigration_commitSuccess()
throws URISyntaxException, IOException, GitAPIException {
ApplicationJson applicationJson =
gitFileSystemTestHelper.getApplicationJson(this.getClass().getResource(APP_JSON_NAME));
mockAutoCommitTriggerResponse(TRUE, FALSE);
ApplicationJson applicationJson1 = new ApplicationJson();
AppsmithBeanUtils.copyNewFieldValuesIntoOldObject(applicationJson, applicationJson1);
applicationJson1.setServerSchemaVersion(JsonSchemaVersions.serverVersion + 1);
doReturn(Mono.just(applicationJson1))
.when(jsonSchemaMigration)
.migrateApplicationJsonToLatestSchema(any(ApplicationJson.class));
gitFileSystemTestHelper.setupGitRepository(
WORKSPACE_ID, DEFAULT_APP_ID, BRANCH_NAME, REPO_NAME, applicationJson);
// verifying the initial number of commits
StepVerifier.create(gitExecutor.getCommitHistory(baseRepoSuffix))
.assertNext(gitLogDTOs -> {
assertThat(gitLogDTOs).isNotEmpty();
assertThat(gitLogDTOs.size()).isEqualTo(2);
Set<String> commitMessages =
gitLogDTOs.stream().map(GitLogDTO::getCommitMessage).collect(Collectors.toSet());
assertThat(commitMessages).doesNotContain(String.format(AUTO_COMMIT_MSG_FORMAT, "UNKNOWN"));
})
.verifyComplete();
// redis-utils fixing
Mockito.when(redisUtils.getRunningAutoCommitBranchName(DEFAULT_APP_ID)).thenReturn(Mono.empty());
Mockito.when(redisUtils.getAutoCommitProgress(DEFAULT_APP_ID)).thenReturn(Mono.empty());
Mono<AutoCommitResponseDTO> autoCommitResponseDTOMono =
autoCommitService.autoCommitApplication(testApplication.getId(), BRANCH_NAME);
StepVerifier.create(autoCommitResponseDTOMono)
.assertNext(autoCommitResponseDTO -> {
assertThat(autoCommitResponseDTO.getAutoCommitResponse())
.isEqualTo(AutoCommitResponseDTO.AutoCommitResponse.PUBLISHED);
})
.verifyComplete();
// this would trigger autocommit
Mono<List<GitLogDTO>> gitlogDTOsMono = Mono.delay(Duration.ofSeconds(WAIT_DURATION_FOR_ASYNC_EVENT))
.then(gitExecutor.getCommitHistory(baseRepoSuffix));
// verifying final number of commits
StepVerifier.create(gitlogDTOsMono)
.assertNext(gitLogDTOs -> {
assertThat(gitLogDTOs).isNotEmpty();
assertThat(gitLogDTOs.size()).isEqualTo(3);
Set<String> commitMessages =
gitLogDTOs.stream().map(GitLogDTO::getCommitMessage).collect(Collectors.toSet());
assertThat(commitMessages).contains(String.format(AUTO_COMMIT_MSG_FORMAT, "UNKNOWN"));
})
.verifyComplete();
}
@Test
public void testAutoCommit_whenOnlyClientIsEligibleForMigration_commitSuccess()
throws GitAPIException, IOException, URISyntaxException {
ApplicationJson applicationJson =
gitFileSystemTestHelper.getApplicationJson(this.getClass().getResource(APP_JSON_NAME));
int pageDSLNumber = applicationJson
.getPageList()
.get(0)
.getUnpublishedPage()
.getLayouts()
.get(0)
.getDsl()
.getAsNumber("version")
.intValue();
mockAutoCommitTriggerResponse(FALSE, TRUE);
Mockito.when(dslMigrationUtils.getLatestDslVersion()).thenReturn(Mono.just(pageDSLNumber + 1));
JSONObject dslAfterMigration = new JSONObject();
dslAfterMigration.put("key", "after migration");
// mock the dsl migration utils to return updated dsl when requested with older dsl
Mockito.when(dslMigrationUtils.migratePageDsl(any(JSONObject.class))).thenReturn(Mono.just(dslAfterMigration));
gitFileSystemTestHelper.setupGitRepository(
WORKSPACE_ID, DEFAULT_APP_ID, BRANCH_NAME, REPO_NAME, applicationJson);
// verifying the initial number of commits
StepVerifier.create(gitExecutor.getCommitHistory(baseRepoSuffix))
.assertNext(gitLogDTOs -> {
assertThat(gitLogDTOs).isNotEmpty();
assertThat(gitLogDTOs.size()).isEqualTo(2);
Set<String> commitMessages =
gitLogDTOs.stream().map(GitLogDTO::getCommitMessage).collect(Collectors.toSet());
assertThat(commitMessages).doesNotContain(String.format(AUTO_COMMIT_MSG_FORMAT, "UNKNOWN"));
})
.verifyComplete();
// redis-utils fixing
Mockito.when(redisUtils.getRunningAutoCommitBranchName(DEFAULT_APP_ID)).thenReturn(Mono.empty());
Mockito.when(redisUtils.getAutoCommitProgress(DEFAULT_APP_ID)).thenReturn(Mono.empty());
// this would trigger autocommit
Mono<AutoCommitResponseDTO> autoCommitResponseDTOMono =
autoCommitService.autoCommitApplication(testApplication.getId(), BRANCH_NAME);
StepVerifier.create(autoCommitResponseDTOMono)
.assertNext(autoCommitResponseDTO -> {
assertThat(autoCommitResponseDTO.getAutoCommitResponse())
.isEqualTo(AutoCommitResponseDTO.AutoCommitResponse.PUBLISHED);
})
.verifyComplete();
Mono<List<GitLogDTO>> gitlogDTOsMono = Mono.delay(Duration.ofSeconds(WAIT_DURATION_FOR_ASYNC_EVENT))
.then(gitExecutor.getCommitHistory(baseRepoSuffix));
// verifying final number of commits
StepVerifier.create(gitlogDTOsMono)
.assertNext(gitLogDTOs -> {
assertThat(gitLogDTOs).isNotEmpty();
assertThat(gitLogDTOs.size()).isEqualTo(3);
Set<String> commitMessages =
gitLogDTOs.stream().map(GitLogDTO::getCommitMessage).collect(Collectors.toSet());
assertThat(commitMessages).contains(String.format(AUTO_COMMIT_MSG_FORMAT, "UNKNOWN"));
})
.verifyComplete();
}
@Test
public void testAutoCommit_whenAutoCommitNotEligible_returnsFalse()
throws URISyntaxException, IOException, GitAPIException {
ApplicationJson applicationJson =
gitFileSystemTestHelper.getApplicationJson(this.getClass().getResource(APP_JSON_NAME));
mockAutoCommitTriggerResponse(FALSE, FALSE);
gitFileSystemTestHelper.setupGitRepository(
WORKSPACE_ID, DEFAULT_APP_ID, BRANCH_NAME, REPO_NAME, applicationJson);
// verifying the initial number of commits
StepVerifier.create(gitExecutor.getCommitHistory(baseRepoSuffix))
.assertNext(gitLogDTOs -> {
assertThat(gitLogDTOs).isNotEmpty();
assertThat(gitLogDTOs.size()).isEqualTo(2);
Set<String> commitMessages =
gitLogDTOs.stream().map(GitLogDTO::getCommitMessage).collect(Collectors.toSet());
assertThat(commitMessages).doesNotContain(String.format(AUTO_COMMIT_MSG_FORMAT, "UNKNOWN"));
})
.verifyComplete();
// redis-utils fixing
Mockito.when(redisUtils.getRunningAutoCommitBranchName(DEFAULT_APP_ID)).thenReturn(Mono.empty());
Mockito.when(redisUtils.getAutoCommitProgress(DEFAULT_APP_ID)).thenReturn(Mono.empty());
// this would not trigger autocommit
Mono<AutoCommitResponseDTO> autoCommitResponseDTOMono =
autoCommitService.autoCommitApplication(testApplication.getId(), BRANCH_NAME);
StepVerifier.create(autoCommitResponseDTOMono)
.assertNext(autoCommitResponseDTO -> {
assertThat(autoCommitResponseDTO.getAutoCommitResponse())
.isEqualTo(AutoCommitResponseDTO.AutoCommitResponse.IDLE);
})
.verifyComplete();
Mono<List<GitLogDTO>> gitlogDTOsMono = Mono.delay(Duration.ofSeconds(WAIT_DURATION_FOR_ASYNC_EVENT))
.then(gitExecutor.getCommitHistory(baseRepoSuffix));
// verifying final number of commits
StepVerifier.create(gitlogDTOsMono)
.assertNext(gitLogDTOs -> {
assertThat(gitLogDTOs).isNotEmpty();
assertThat(gitLogDTOs.size()).isEqualTo(2);
Set<String> commitMessages =
gitLogDTOs.stream().map(GitLogDTO::getCommitMessage).collect(Collectors.toSet());
assertThat(commitMessages).doesNotContain(String.format(AUTO_COMMIT_MSG_FORMAT, "UNKNOWN"));
})
.verifyComplete();
}
@Test
public void testAutoCommit_whenAutoCommitAlreadyInProgressOnAnotherBranch_returnsLocked() {
// redis-utils fixing
Mockito.when(redisUtils.getRunningAutoCommitBranchName(DEFAULT_APP_ID))
.thenReturn(Mono.just(DEFAULT_BRANCH_NAME));
Mockito.when(redisUtils.getAutoCommitProgress(DEFAULT_APP_ID)).thenReturn(Mono.just(70));
// this would not trigger autocommit
Mono<AutoCommitResponseDTO> autoCommitResponseDTOMono =
autoCommitService.autoCommitApplication(testApplication.getId(), BRANCH_NAME);
StepVerifier.create(autoCommitResponseDTOMono)
.assertNext(autoCommitResponseDTO -> {
assertThat(autoCommitResponseDTO.getAutoCommitResponse())
.isEqualTo(AutoCommitResponseDTO.AutoCommitResponse.LOCKED);
assertThat(autoCommitResponseDTO.getBranchName()).isEqualTo(DEFAULT_BRANCH_NAME);
assertThat(autoCommitResponseDTO.getProgress()).isEqualTo(70);
})
.verifyComplete();
}
@Test
public void testAutoCommit_whenAutoCommitAlreadyInProgressOnSameBranch_returnsInProgress() {
// redis-utils fixing
Mockito.when(redisUtils.getRunningAutoCommitBranchName(DEFAULT_APP_ID)).thenReturn(Mono.just(BRANCH_NAME));
Mockito.when(redisUtils.getAutoCommitProgress(DEFAULT_APP_ID)).thenReturn(Mono.just(70));
// this would not trigger autocommit
Mono<AutoCommitResponseDTO> autoCommitResponseDTOMono =
autoCommitService.autoCommitApplication(testApplication.getId(), BRANCH_NAME);
StepVerifier.create(autoCommitResponseDTOMono)
.assertNext(autoCommitResponseDTO -> {
assertThat(autoCommitResponseDTO.getAutoCommitResponse())
.isEqualTo(AutoCommitResponseDTO.AutoCommitResponse.IN_PROGRESS);
assertThat(autoCommitResponseDTO.getBranchName()).isEqualTo(BRANCH_NAME);
assertThat(autoCommitResponseDTO.getProgress()).isEqualTo(70);
})
.verifyComplete();
}
@Test
public void testAutoCommit_whenNoGitMetadata_returnsNonGitApp() {
testApplication.setGitApplicationMetadata(null);
// used for fetching application on autocommit service and gitAutoCommitHelper.autocommit
Mockito.when(applicationService.findByBranchNameAndDefaultApplicationId(
anyString(), anyString(), any(AclPermission.class)))
.thenReturn(Mono.just(testApplication));
// this would not trigger autocommit
Mono<AutoCommitResponseDTO> autoCommitResponseDTOMono =
autoCommitService.autoCommitApplication(testApplication.getId(), BRANCH_NAME);
StepVerifier.create(autoCommitResponseDTOMono)
.assertNext(autoCommitResponseDTO -> {
assertThat(autoCommitResponseDTO.getAutoCommitResponse())
.isEqualTo(AutoCommitResponseDTO.AutoCommitResponse.NON_GIT_APP);
})
.verifyComplete();
}
@Test
public void testAutoCommit_whenAutoCommitEligibleButPrerequisiteNotComplete_returnsRequired() {
mockAutoCommitTriggerResponse(TRUE, FALSE);
// redis-utils fixing
Mockito.when(redisUtils.getRunningAutoCommitBranchName(DEFAULT_APP_ID)).thenReturn(Mono.empty());
Mockito.when(redisUtils.getAutoCommitProgress(DEFAULT_APP_ID)).thenReturn(Mono.empty());
// number of commits behind to make the pre-req fail
Mockito.when(branchTrackingStatus.getBehindCount()).thenReturn(1);
// this would not trigger autocommit
Mono<AutoCommitResponseDTO> autoCommitResponseDTOMono =
autoCommitService.autoCommitApplication(testApplication.getId(), BRANCH_NAME);
StepVerifier.create(autoCommitResponseDTOMono)
.assertNext(autoCommitResponseDTO -> {
assertThat(autoCommitResponseDTO.getAutoCommitResponse())
.isEqualTo(AutoCommitResponseDTO.AutoCommitResponse.REQUIRED);
})
.verifyComplete();
}
@Test
public void
testAutoCommit_whenServerIsRunningMigrationCallsAutocommitAgainOnSameBranch_ReturnsAutoCommitInProgress()
throws URISyntaxException, IOException, GitAPIException {
ApplicationJson applicationJson =
gitFileSystemTestHelper.getApplicationJson(this.getClass().getResource(APP_JSON_NAME));
mockAutoCommitTriggerResponse(TRUE, FALSE);
ApplicationJson applicationJson1 = new ApplicationJson();
AppsmithBeanUtils.copyNewFieldValuesIntoOldObject(applicationJson, applicationJson1);
applicationJson1.setServerSchemaVersion(JsonSchemaVersions.serverVersion + 1);
doReturn(Mono.just(applicationJson1))
.when(jsonSchemaMigration)
.migrateApplicationJsonToLatestSchema(any(ApplicationJson.class));
gitFileSystemTestHelper.setupGitRepository(
WORKSPACE_ID, DEFAULT_APP_ID, BRANCH_NAME, REPO_NAME, applicationJson);
// verifying the initial number of commits
StepVerifier.create(gitExecutor.getCommitHistory(baseRepoSuffix))
.assertNext(gitLogDTOs -> {
assertThat(gitLogDTOs).isNotEmpty();
assertThat(gitLogDTOs.size()).isEqualTo(2);
Set<String> commitMessages =
gitLogDTOs.stream().map(GitLogDTO::getCommitMessage).collect(Collectors.toSet());
assertThat(commitMessages).doesNotContain(String.format(AUTO_COMMIT_MSG_FORMAT, "UNKNOWN"));
})
.verifyComplete();
// redis-utils fixing
Mockito.when(redisUtils.getRunningAutoCommitBranchName(DEFAULT_APP_ID)).thenReturn(Mono.empty());
Mockito.when(redisUtils.getAutoCommitProgress(DEFAULT_APP_ID)).thenReturn(Mono.empty());
Mono<AutoCommitResponseDTO> autoCommitResponseDTOMono =
autoCommitService.autoCommitApplication(testApplication.getId(), BRANCH_NAME);
StepVerifier.create(autoCommitResponseDTOMono)
.assertNext(autoCommitResponseDTO -> assertThat(autoCommitResponseDTO.getAutoCommitResponse())
.isEqualTo(AutoCommitResponseDTO.AutoCommitResponse.PUBLISHED))
.verifyComplete();
// redis-utils fixing
Mockito.when(redisUtils.getRunningAutoCommitBranchName(DEFAULT_APP_ID)).thenReturn(Mono.just(BRANCH_NAME));
Mockito.when(redisUtils.getAutoCommitProgress(DEFAULT_APP_ID)).thenReturn(Mono.just(20));
StepVerifier.create(autoCommitService.autoCommitApplication(testApplication.getId(), BRANCH_NAME))
.assertNext(autoCommitResponseDTO -> {
assertThat(autoCommitResponseDTO.getAutoCommitResponse())
.isEqualTo(AutoCommitResponseDTO.AutoCommitResponse.IN_PROGRESS);
assertThat(autoCommitResponseDTO.getBranchName()).isEqualTo(BRANCH_NAME);
})
.verifyComplete();
// this would trigger autocommit
Mono<List<GitLogDTO>> gitlogDTOsMono = Mono.delay(Duration.ofSeconds(WAIT_DURATION_FOR_ASYNC_EVENT))
.then(gitExecutor.getCommitHistory(baseRepoSuffix));
// verifying final number of commits
StepVerifier.create(gitlogDTOsMono)
.assertNext(gitLogDTOs -> {
assertThat(gitLogDTOs).isNotEmpty();
assertThat(gitLogDTOs.size()).isEqualTo(3);
Set<String> commitMessages =
gitLogDTOs.stream().map(GitLogDTO::getCommitMessage).collect(Collectors.toSet());
assertThat(commitMessages).contains(String.format(AUTO_COMMIT_MSG_FORMAT, "UNKNOWN"));
})
.verifyComplete();
}
@Test
public void testAutoCommit_whenServerIsRunningMigrationCallsAutocommitAgainOnDiffBranch_ReturnsAutoCommitLocked()
throws URISyntaxException, IOException, GitAPIException {
ApplicationJson applicationJson =
gitFileSystemTestHelper.getApplicationJson(this.getClass().getResource(APP_JSON_NAME));
mockAutoCommitTriggerResponse(TRUE, FALSE);
ApplicationJson applicationJson1 = new ApplicationJson();
AppsmithBeanUtils.copyNewFieldValuesIntoOldObject(applicationJson, applicationJson1);
applicationJson1.setServerSchemaVersion(JsonSchemaVersions.serverVersion + 1);
doReturn(Mono.just(applicationJson1))
.when(jsonSchemaMigration)
.migrateApplicationJsonToLatestSchema(any(ApplicationJson.class));
gitFileSystemTestHelper.setupGitRepository(
WORKSPACE_ID, DEFAULT_APP_ID, BRANCH_NAME, REPO_NAME, applicationJson);
// verifying the initial number of commits
StepVerifier.create(gitExecutor.getCommitHistory(baseRepoSuffix))
.assertNext(gitLogDTOs -> {
assertThat(gitLogDTOs).isNotEmpty();
assertThat(gitLogDTOs.size()).isEqualTo(2);
Set<String> commitMessages =
gitLogDTOs.stream().map(GitLogDTO::getCommitMessage).collect(Collectors.toSet());
assertThat(commitMessages).doesNotContain(String.format(AUTO_COMMIT_MSG_FORMAT, "UNKNOWN"));
})
.verifyComplete();
// redis-utils fixing
Mockito.when(redisUtils.getRunningAutoCommitBranchName(DEFAULT_APP_ID)).thenReturn(Mono.empty());
Mockito.when(redisUtils.getAutoCommitProgress(DEFAULT_APP_ID)).thenReturn(Mono.empty());
Mono<AutoCommitResponseDTO> autoCommitResponseDTOMono =
autoCommitService.autoCommitApplication(testApplication.getId(), BRANCH_NAME);
StepVerifier.create(autoCommitResponseDTOMono)
.assertNext(autoCommitResponseDTO -> assertThat(autoCommitResponseDTO.getAutoCommitResponse())
.isEqualTo(AutoCommitResponseDTO.AutoCommitResponse.PUBLISHED))
.verifyComplete();
// redis-utils fixing
Mockito.when(redisUtils.getRunningAutoCommitBranchName(DEFAULT_APP_ID)).thenReturn(Mono.just(BRANCH_NAME));
Mockito.when(redisUtils.getAutoCommitProgress(DEFAULT_APP_ID)).thenReturn(Mono.just(20));
StepVerifier.create(autoCommitService.autoCommitApplication(testApplication.getId(), DEFAULT_BRANCH_NAME))
.assertNext(autoCommitResponseDTO -> {
assertThat(autoCommitResponseDTO.getAutoCommitResponse())
.isEqualTo(AutoCommitResponseDTO.AutoCommitResponse.LOCKED);
assertThat(autoCommitResponseDTO.getBranchName()).isEqualTo(BRANCH_NAME);
})
.verifyComplete();
// this would trigger autocommit
Mono<List<GitLogDTO>> gitlogDTOsMono = Mono.delay(Duration.ofSeconds(WAIT_DURATION_FOR_ASYNC_EVENT))
.then(gitExecutor.getCommitHistory(baseRepoSuffix));
// verifying final number of commits
StepVerifier.create(gitlogDTOsMono)
.assertNext(gitLogDTOs -> {
assertThat(gitLogDTOs).isNotEmpty();
assertThat(gitLogDTOs.size()).isEqualTo(3);
Set<String> commitMessages =
gitLogDTOs.stream().map(GitLogDTO::getCommitMessage).collect(Collectors.toSet());
assertThat(commitMessages).contains(String.format(AUTO_COMMIT_MSG_FORMAT, "UNKNOWN"));
})
.verifyComplete();
}
}

View File

@ -2,7 +2,6 @@ package com.appsmith.server.git.autocommit.helpers;
import com.appsmith.external.dtos.ModifiedResources;
import com.appsmith.external.enums.FeatureFlagEnum;
import com.appsmith.external.git.constants.GitConstants;
import com.appsmith.server.constants.ArtifactType;
import com.appsmith.server.domains.Application;
import com.appsmith.server.domains.GitArtifactMetadata;
@ -36,6 +35,7 @@ import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import static com.appsmith.external.git.constants.ce.GitConstantsCE.GitCommandConstantsCE.AUTO_COMMIT_ELIGIBILITY;
import static org.assertj.core.api.Assertions.assertThat;
@Slf4j
@ -92,18 +92,23 @@ public class AutoCommitEligibilityHelperTest {
return pageDTO;
}
private org.json.JSONObject getPageDSl(Integer dslVersionNumber) {
org.json.JSONObject jsonObject = new org.json.JSONObject();
jsonObject.put("version", dslVersionNumber);
return jsonObject;
}
@BeforeEach
public void beforeEach() {
Mockito.when(featureFlagService.check(FeatureFlagEnum.release_git_autocommit_feature_enabled))
.thenReturn(Mono.just(Boolean.TRUE));
Mockito.when(featureFlagService.check(FeatureFlagEnum.release_git_server_autocommit_feature_enabled))
Mockito.when(featureFlagService.check(FeatureFlagEnum.release_git_autocommit_eligibility_enabled))
.thenReturn(Mono.just(Boolean.TRUE));
Mockito.when(dslMigrationUtils.getLatestDslVersion()).thenReturn(Mono.just(RANDOM_DSL_VERSION_NUMBER));
Mockito.when(gitRedisUtils.addFileLock(
DEFAULT_APPLICATION_ID, GitConstants.GitCommandConstants.METADATA, false))
Mockito.when(gitRedisUtils.addFileLock(DEFAULT_APPLICATION_ID, AUTO_COMMIT_ELIGIBILITY))
.thenReturn(Mono.just(Boolean.TRUE));
Mockito.when(gitRedisUtils.releaseFileLock(DEFAULT_APPLICATION_ID)).thenReturn(Mono.just(Boolean.TRUE));
}
@ -114,11 +119,16 @@ public class AutoCommitEligibilityHelperTest {
GitArtifactMetadata gitArtifactMetadata = createGitMetadata();
PageDTO pageDTO = createPageDTO(RANDOM_DSL_VERSION_NUMBER - 1);
Mockito.doReturn(Mono.just(getPageDSl(RANDOM_DSL_VERSION_NUMBER - 1)))
.when(commonGitFileUtils)
.getPageDslVersionNumber(
WORKSPACE_ID, gitArtifactMetadata, pageDTO, Boolean.TRUE, ArtifactType.APPLICATION);
// this leads to server migration requirement as true
Mockito.doReturn(Mono.just(JsonSchemaVersions.serverVersion - 1))
.when(commonGitFileUtils)
.getMetadataServerSchemaMigrationVersion(
WORKSPACE_ID, DEFAULT_APPLICATION_ID, REPO_NAME, BRANCH_NAME, ArtifactType.APPLICATION);
WORKSPACE_ID, gitArtifactMetadata, Boolean.TRUE, ArtifactType.APPLICATION);
Mono<AutoCommitTriggerDTO> autoCommitTriggerDTOMono =
autoCommitEligibilityHelper.isAutoCommitRequired(WORKSPACE_ID, gitArtifactMetadata, pageDTO);
@ -140,11 +150,16 @@ public class AutoCommitEligibilityHelperTest {
GitArtifactMetadata gitArtifactMetadata = createGitMetadata();
PageDTO pageDTO = createPageDTO(RANDOM_DSL_VERSION_NUMBER);
Mockito.doReturn(Mono.just(getPageDSl(RANDOM_DSL_VERSION_NUMBER)))
.when(commonGitFileUtils)
.getPageDslVersionNumber(
WORKSPACE_ID, gitArtifactMetadata, pageDTO, Boolean.TRUE, ArtifactType.APPLICATION);
// this leads to server migration requirement as false
Mockito.doReturn(Mono.just(JsonSchemaVersions.serverVersion))
.when(commonGitFileUtils)
.getMetadataServerSchemaMigrationVersion(
WORKSPACE_ID, DEFAULT_APPLICATION_ID, REPO_NAME, BRANCH_NAME, ArtifactType.APPLICATION);
WORKSPACE_ID, gitArtifactMetadata, Boolean.FALSE, ArtifactType.APPLICATION);
Mono<AutoCommitTriggerDTO> autoCommitTriggerDTOMono =
autoCommitEligibilityHelper.isAutoCommitRequired(WORKSPACE_ID, gitArtifactMetadata, pageDTO);
@ -166,11 +181,16 @@ public class AutoCommitEligibilityHelperTest {
GitArtifactMetadata gitArtifactMetadata = createGitMetadata();
PageDTO pageDTO = createPageDTO(RANDOM_DSL_VERSION_NUMBER - 1);
Mockito.doReturn(Mono.just(getPageDSl(RANDOM_DSL_VERSION_NUMBER - 1)))
.when(commonGitFileUtils)
.getPageDslVersionNumber(
WORKSPACE_ID, gitArtifactMetadata, pageDTO, Boolean.TRUE, ArtifactType.APPLICATION);
// this leads to server migration requirement as false
Mockito.doReturn(Mono.just(JsonSchemaVersions.serverVersion))
.when(commonGitFileUtils)
.getMetadataServerSchemaMigrationVersion(
WORKSPACE_ID, DEFAULT_APPLICATION_ID, REPO_NAME, BRANCH_NAME, ArtifactType.APPLICATION);
WORKSPACE_ID, gitArtifactMetadata, Boolean.FALSE, ArtifactType.APPLICATION);
Mono<AutoCommitTriggerDTO> autoCommitTriggerDTOMono =
autoCommitEligibilityHelper.isAutoCommitRequired(WORKSPACE_ID, gitArtifactMetadata, pageDTO);
@ -178,8 +198,9 @@ public class AutoCommitEligibilityHelperTest {
StepVerifier.create(autoCommitTriggerDTOMono)
.assertNext(autoCommitTriggerDTO -> {
assertThat(autoCommitTriggerDTO.getIsAutoCommitRequired()).isTrue();
// Since client is true, server is true as well
assertThat(autoCommitTriggerDTO.getIsServerAutoCommitRequired())
.isFalse();
.isTrue();
assertThat(autoCommitTriggerDTO.getIsClientAutoCommitRequired())
.isTrue();
})
@ -192,11 +213,16 @@ public class AutoCommitEligibilityHelperTest {
GitArtifactMetadata gitArtifactMetadata = createGitMetadata();
PageDTO pageDTO = createPageDTO(RANDOM_DSL_VERSION_NUMBER);
Mockito.doReturn(Mono.just(getPageDSl(RANDOM_DSL_VERSION_NUMBER)))
.when(commonGitFileUtils)
.getPageDslVersionNumber(
WORKSPACE_ID, gitArtifactMetadata, pageDTO, Boolean.TRUE, ArtifactType.APPLICATION);
// this leads to server migration requirement as true
Mockito.doReturn(Mono.just(JsonSchemaVersions.serverVersion - 1))
.when(commonGitFileUtils)
.getMetadataServerSchemaMigrationVersion(
WORKSPACE_ID, DEFAULT_APPLICATION_ID, REPO_NAME, BRANCH_NAME, ArtifactType.APPLICATION);
WORKSPACE_ID, gitArtifactMetadata, Boolean.FALSE, ArtifactType.APPLICATION);
Mono<AutoCommitTriggerDTO> autoCommitTriggerDTOMono =
autoCommitEligibilityHelper.isAutoCommitRequired(WORKSPACE_ID, gitArtifactMetadata, pageDTO);
@ -220,7 +246,7 @@ public class AutoCommitEligibilityHelperTest {
Mockito.doReturn(Mono.just(JsonSchemaVersions.serverVersion))
.when(commonGitFileUtils)
.getMetadataServerSchemaMigrationVersion(
WORKSPACE_ID, DEFAULT_APPLICATION_ID, REPO_NAME, BRANCH_NAME, ArtifactType.APPLICATION);
WORKSPACE_ID, gitArtifactMetadata, Boolean.TRUE, ArtifactType.APPLICATION);
Mono<Boolean> isServerMigrationRequiredMono =
autoCommitEligibilityHelper.isServerAutoCommitRequired(WORKSPACE_ID, gitArtifactMetadata);
@ -239,7 +265,7 @@ public class AutoCommitEligibilityHelperTest {
Mockito.doReturn(Mono.just(JsonSchemaVersions.serverVersion - 1))
.when(commonGitFileUtils)
.getMetadataServerSchemaMigrationVersion(
WORKSPACE_ID, DEFAULT_APPLICATION_ID, REPO_NAME, BRANCH_NAME, ArtifactType.APPLICATION);
WORKSPACE_ID, gitArtifactMetadata, Boolean.FALSE, ArtifactType.APPLICATION);
Mono<Boolean> isServerMigrationRequiredMono =
autoCommitEligibilityHelper.isServerAutoCommitRequired(WORKSPACE_ID, gitArtifactMetadata);
@ -252,7 +278,7 @@ public class AutoCommitEligibilityHelperTest {
@Test
public void isServerMigrationRequired_whenFeatureIsFlagFalse_returnsFalse() {
Mockito.when(featureFlagService.check(FeatureFlagEnum.release_git_server_autocommit_feature_enabled))
Mockito.when(featureFlagService.check(FeatureFlagEnum.release_git_autocommit_eligibility_enabled))
.thenReturn(Mono.just(Boolean.FALSE));
GitArtifactMetadata gitArtifactMetadata = createGitMetadata();
@ -268,10 +294,18 @@ public class AutoCommitEligibilityHelperTest {
@Test
public void isClientMigrationRequired_whenLatestDslIsNotAhead_returnsFalse() {
GitArtifactMetadata gitArtifactMetadata = createGitMetadata();
PageDTO pageDTO = createPageDTO(RANDOM_DSL_VERSION_NUMBER);
Mockito.doReturn(Mono.just(getPageDSl(RANDOM_DSL_VERSION_NUMBER)))
.when(commonGitFileUtils)
.getPageDslVersionNumber(
WORKSPACE_ID, gitArtifactMetadata, pageDTO, Boolean.TRUE, ArtifactType.APPLICATION);
Mockito.when(dslMigrationUtils.getLatestDslVersion()).thenReturn(Mono.just(RANDOM_DSL_VERSION_NUMBER));
Mono<Boolean> isClientMigrationRequiredMono = autoCommitEligibilityHelper.isClientMigrationRequired(pageDTO);
Mono<Boolean> isClientMigrationRequiredMono =
autoCommitEligibilityHelper.isClientMigrationRequiredFSOps(WORKSPACE_ID, gitArtifactMetadata, pageDTO);
StepVerifier.create(isClientMigrationRequiredMono)
.assertNext(isClientMigrationRequired ->
@ -281,10 +315,18 @@ public class AutoCommitEligibilityHelperTest {
@Test
public void isClientMigrationRequired_whenLatestDslIsAhead_returnsTrue() {
GitArtifactMetadata gitArtifactMetadata = createGitMetadata();
PageDTO pageDTO = createPageDTO(RANDOM_DSL_VERSION_NUMBER - 1);
Mockito.doReturn(Mono.just(getPageDSl(RANDOM_DSL_VERSION_NUMBER - 1)))
.when(commonGitFileUtils)
.getPageDslVersionNumber(
WORKSPACE_ID, gitArtifactMetadata, pageDTO, Boolean.TRUE, ArtifactType.APPLICATION);
Mockito.when(dslMigrationUtils.getLatestDslVersion()).thenReturn(Mono.just(RANDOM_DSL_VERSION_NUMBER));
Mono<Boolean> isClientMigrationRequiredMono = autoCommitEligibilityHelper.isClientMigrationRequired(pageDTO);
Mono<Boolean> isClientMigrationRequiredMono =
autoCommitEligibilityHelper.isClientMigrationRequiredFSOps(WORKSPACE_ID, gitArtifactMetadata, pageDTO);
StepVerifier.create(isClientMigrationRequiredMono)
.assertNext(isClientMigrationRequired ->
@ -294,7 +336,7 @@ public class AutoCommitEligibilityHelperTest {
@Test
public void isClientMigrationRequired_whenFeatureFlagIsFalse_returnsFalse() {
Mockito.when(featureFlagService.check(FeatureFlagEnum.release_git_autocommit_feature_enabled))
Mockito.when(featureFlagService.check(FeatureFlagEnum.release_git_autocommit_eligibility_enabled))
.thenReturn(Mono.just(Boolean.FALSE));
PageDTO pageDTO = createPageDTO(RANDOM_DSL_VERSION_NUMBER);
@ -308,14 +350,11 @@ public class AutoCommitEligibilityHelperTest {
@Test
public void isAutoCommitRequired_whenFeatureIsFlagFalse_returnsAllFalse() {
Mockito.when(featureFlagService.check(FeatureFlagEnum.release_git_autocommit_feature_enabled))
.thenReturn(Mono.just(Boolean.FALSE));
Mockito.when(featureFlagService.check(FeatureFlagEnum.release_git_server_autocommit_feature_enabled))
Mockito.when(featureFlagService.check(FeatureFlagEnum.release_git_autocommit_eligibility_enabled))
.thenReturn(Mono.just(Boolean.FALSE));
GitArtifactMetadata gitArtifactMetadata = createGitMetadata();
PageDTO pageDTO = createPageDTO(RANDOM_DSL_VERSION_NUMBER);
PageDTO pageDTO = createPageDTO(RANDOM_DSL_VERSION_NUMBER - 1);
Mono<AutoCommitTriggerDTO> autoCommitTriggerDTOMono =
autoCommitEligibilityHelper.isAutoCommitRequired(WORKSPACE_ID, gitArtifactMetadata, pageDTO);

View File

@ -8,9 +8,9 @@ import com.appsmith.server.domains.AutoCommitConfig;
import com.appsmith.server.domains.GitArtifactMetadata;
import com.appsmith.server.domains.GitAuth;
import com.appsmith.server.domains.GitProfile;
import com.appsmith.server.dtos.AutoCommitProgressDTO;
import com.appsmith.server.dtos.AutoCommitResponseDTO;
import com.appsmith.server.events.AutoCommitEvent;
import com.appsmith.server.git.AutoCommitEventHandler;
import com.appsmith.server.git.autocommit.AutoCommitEventHandler;
import com.appsmith.server.git.common.CommonGitService;
import com.appsmith.server.helpers.GitPrivateRepoHelper;
import com.appsmith.server.helpers.RedisUtils;
@ -32,6 +32,8 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import static com.appsmith.server.dtos.AutoCommitResponseDTO.AutoCommitResponse.IDLE;
import static com.appsmith.server.dtos.AutoCommitResponseDTO.AutoCommitResponse.IN_PROGRESS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
@ -241,14 +243,14 @@ public class GitAutoCommitHelperImplTest {
@Test
public void getAutoCommitProgress_WhenAutoCommitRunning_ReturnsValidResponse() {
Mono<AutoCommitProgressDTO> progressDTOMono = redisUtils
Mono<AutoCommitResponseDTO> progressDTOMono = redisUtils
.startAutoCommit(defaultApplicationId, branchName)
.then(redisUtils.setAutoCommitProgress(defaultApplicationId, 20))
.then(gitAutoCommitHelper.getAutoCommitProgress(defaultApplicationId));
.then(gitAutoCommitHelper.getAutoCommitProgress(defaultApplicationId, branchName));
StepVerifier.create(progressDTOMono)
.assertNext(dto -> {
assertThat(dto.getIsRunning()).isTrue();
assertThat(dto.getAutoCommitResponse()).isEqualTo(IN_PROGRESS);
assertThat(dto.getProgress()).isEqualTo(20);
assertThat(dto.getBranchName()).isEqualTo(branchName);
})
@ -257,15 +259,15 @@ public class GitAutoCommitHelperImplTest {
@Test
public void getAutoCommitProgress_WhenNoAutoCommitFinished_ReturnsValidResponse() {
Mono<AutoCommitProgressDTO> progressDTOMono = redisUtils
Mono<AutoCommitResponseDTO> progressDTOMono = redisUtils
.startAutoCommit(defaultApplicationId, branchName)
.then(redisUtils.setAutoCommitProgress(defaultApplicationId, 20))
.then(redisUtils.finishAutoCommit(defaultApplicationId))
.then(gitAutoCommitHelper.getAutoCommitProgress(defaultApplicationId));
.then(gitAutoCommitHelper.getAutoCommitProgress(defaultApplicationId, branchName));
StepVerifier.create(progressDTOMono)
.assertNext(dto -> {
assertThat(dto.getIsRunning()).isFalse();
assertThat(dto.getAutoCommitResponse()).isEqualTo(IDLE);
assertThat(dto.getProgress()).isZero();
assertThat(dto.getBranchName()).isNull();
})
@ -274,10 +276,11 @@ public class GitAutoCommitHelperImplTest {
@Test
public void getAutoCommitProgress_WhenNoAutoCommitRunning_ReturnsValidResponse() {
Mono<AutoCommitProgressDTO> progressDTOMono = gitAutoCommitHelper.getAutoCommitProgress(defaultApplicationId);
Mono<AutoCommitResponseDTO> progressDTOMono =
gitAutoCommitHelper.getAutoCommitProgress(defaultApplicationId, branchName);
StepVerifier.create(progressDTOMono)
.assertNext(dto -> {
assertThat(dto.getIsRunning()).isFalse();
assertThat(dto.getAutoCommitResponse()).isEqualTo(IDLE);
assertThat(dto.getProgress()).isZero();
assertThat(dto.getBranchName()).isNull();
})
@ -333,7 +336,7 @@ public class GitAutoCommitHelperImplTest {
application.setGitApplicationMetadata(metaData);
Mockito.when(featureFlagService.check(FeatureFlagEnum.release_git_server_autocommit_feature_enabled))
Mockito.when(featureFlagService.check(FeatureFlagEnum.release_git_autocommit_feature_enabled))
.thenReturn(Mono.just(Boolean.FALSE));
StepVerifier.create(gitAutoCommitHelper.autoCommitServerMigration(defaultApplicationId, branchName))
@ -358,7 +361,7 @@ public class GitAutoCommitHelperImplTest {
application.setGitApplicationMetadata(metaData);
Mockito.when(featureFlagService.check(FeatureFlagEnum.release_git_server_autocommit_feature_enabled))
Mockito.when(featureFlagService.check(FeatureFlagEnum.release_git_autocommit_feature_enabled))
.thenReturn(Mono.just(Boolean.TRUE));
Mockito.when(applicationService.findById(anyString(), any(AclPermission.class)))
@ -396,7 +399,7 @@ public class GitAutoCommitHelperImplTest {
application.setGitApplicationMetadata(metaData);
Mockito.when(featureFlagService.check(FeatureFlagEnum.release_git_server_autocommit_feature_enabled))
Mockito.when(featureFlagService.check(FeatureFlagEnum.release_git_autocommit_feature_enabled))
.thenReturn(Mono.just(Boolean.TRUE));
Mockito.when(applicationService.findById(anyString(), any(AclPermission.class)))