debian-mirror-gitlab/app/assets/javascripts/ide/stores/getters.js

267 lines
8.4 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import Api from '~/api';
import { addNumericSuffix } from '~/ide/utils';
2020-03-13 15:44:24 +05:30
import {
leftSidebarViews,
packageJsonPath,
2021-03-11 19:13:27 +05:30
DEFAULT_PERMISSIONS,
2020-03-13 15:44:24 +05:30
PERMISSION_READ_MR,
PERMISSION_CREATE_MR,
2020-04-22 19:07:51 +05:30
PERMISSION_PUSH_CODE,
2021-04-17 20:07:23 +05:30
PUSH_RULE_REJECT_UNSIGNED_COMMITS,
2020-03-13 15:44:24 +05:30
} from '../constants';
2021-04-17 20:07:23 +05:30
import {
MSG_CANNOT_PUSH_CODE,
2021-04-29 21:17:54 +05:30
MSG_CANNOT_PUSH_CODE_SHOULD_FORK,
MSG_CANNOT_PUSH_CODE_GO_TO_FORK,
2021-04-17 20:07:23 +05:30
MSG_CANNOT_PUSH_UNSIGNED,
MSG_CANNOT_PUSH_UNSIGNED_SHORT,
2021-04-29 21:17:54 +05:30
MSG_FORK,
MSG_GO_TO_FORK,
2021-04-17 20:07:23 +05:30
} from '../messages';
2021-03-11 19:13:27 +05:30
import { getChangesCountForFiles, filePathMatches } from './utils';
2018-10-15 14:42:47 +05:30
2021-04-29 21:17:54 +05:30
const getCannotPushCodeViewModel = (state) => {
const { ide_path: idePath, fork_path: forkPath } = state.links.forkInfo || {};
if (idePath) {
return {
message: MSG_CANNOT_PUSH_CODE_GO_TO_FORK,
action: {
href: idePath,
text: MSG_GO_TO_FORK,
},
};
} else if (forkPath) {
return {
message: MSG_CANNOT_PUSH_CODE_SHOULD_FORK,
action: {
href: forkPath,
isForm: true,
text: MSG_FORK,
},
};
}
return {
message: MSG_CANNOT_PUSH_CODE,
};
};
2021-03-08 18:12:59 +05:30
export const activeFile = (state) => state.openFiles.find((file) => file.active) || null;
2018-05-09 12:01:36 +05:30
2021-03-08 18:12:59 +05:30
export const addedFiles = (state) => state.changedFiles.filter((f) => f.tempFile);
2018-05-09 12:01:36 +05:30
2021-03-08 18:12:59 +05:30
export const modifiedFiles = (state) => state.changedFiles.filter((f) => !f.tempFile);
2018-05-09 12:01:36 +05:30
2021-03-08 18:12:59 +05:30
export const projectsWithTrees = (state) =>
Object.keys(state.projects).map((projectId) => {
2018-05-09 12:01:36 +05:30
const project = state.projects[projectId];
return {
...project,
2021-03-08 18:12:59 +05:30
branches: Object.keys(project.branches).map((branchId) => {
2018-05-09 12:01:36 +05:30
const branch = project.branches[branchId];
return {
...branch,
tree: state.trees[branch.treeId],
};
}),
};
});
2021-03-08 18:12:59 +05:30
export const currentMergeRequest = (state) => {
2019-07-07 11:18:12 +05:30
if (
state.projects[state.currentProjectId] &&
state.projects[state.currentProjectId].mergeRequests
) {
2018-05-09 12:01:36 +05:30
return state.projects[state.currentProjectId].mergeRequests[state.currentMergeRequestId];
}
return null;
};
2021-03-08 18:12:59 +05:30
export const findProject = (state) => (projectId) => state.projects[projectId];
2019-12-26 22:10:19 +05:30
export const currentProject = (state, getters) => getters.findProject(state.currentProjectId);
2018-10-15 14:42:47 +05:30
2021-03-08 18:12:59 +05:30
export const emptyRepo = (state) =>
2019-09-04 21:01:54 +05:30
state.projects[state.currentProjectId] && state.projects[state.currentProjectId].empty_repo;
2021-03-08 18:12:59 +05:30
export const currentTree = (state) =>
2018-10-15 14:42:47 +05:30
state.trees[`${state.currentProjectId}/${state.currentBranchId}`];
2018-05-09 12:01:36 +05:30
2021-03-08 18:12:59 +05:30
export const hasMergeRequest = (state) => Boolean(state.currentMergeRequestId);
2018-10-15 14:42:47 +05:30
2021-03-08 18:12:59 +05:30
export const allBlobs = (state) =>
2018-10-15 14:42:47 +05:30
Object.keys(state.entries)
.reduce((acc, key) => {
const entry = state.entries[key];
if (entry.type === 'blob') {
acc.push(entry);
}
return acc;
}, [])
.sort((a, b) => b.lastOpenedAt - a.lastOpenedAt);
2021-03-08 18:12:59 +05:30
export const getChangedFile = (state) => (path) => state.changedFiles.find((f) => f.path === path);
export const getStagedFile = (state) => (path) => state.stagedFiles.find((f) => f.path === path);
export const getOpenFile = (state) => (path) => state.openFiles.find((f) => f.path === path);
2018-10-15 14:42:47 +05:30
2021-03-08 18:12:59 +05:30
export const lastOpenedFile = (state) =>
2018-10-15 14:42:47 +05:30
[...state.changedFiles, ...state.stagedFiles].sort((a, b) => b.lastOpenedAt - a.lastOpenedAt)[0];
2021-03-08 18:12:59 +05:30
export const isEditModeActive = (state) => state.currentActivityView === leftSidebarViews.edit.name;
export const isCommitModeActive = (state) =>
2020-03-13 15:44:24 +05:30
state.currentActivityView === leftSidebarViews.commit.name;
2021-03-08 18:12:59 +05:30
export const isReviewModeActive = (state) =>
2020-03-13 15:44:24 +05:30
state.currentActivityView === leftSidebarViews.review.name;
2018-10-15 14:42:47 +05:30
2021-03-08 18:12:59 +05:30
export const someUncommittedChanges = (state) =>
2019-09-04 21:01:54 +05:30
Boolean(state.changedFiles.length || state.stagedFiles.length);
2018-10-15 14:42:47 +05:30
2021-03-08 18:12:59 +05:30
export const getChangesInFolder = (state) => (path) => {
const changedFilesCount = state.changedFiles.filter((f) => filePathMatches(f.path, path)).length;
2018-10-15 14:42:47 +05:30
const stagedFilesCount = state.stagedFiles.filter(
2021-03-08 18:12:59 +05:30
(f) => filePathMatches(f.path, path) && !getChangedFile(state)(f.path),
2018-10-15 14:42:47 +05:30
).length;
return changedFilesCount + stagedFilesCount;
};
2021-03-08 18:12:59 +05:30
export const getUnstagedFilesCountForPath = (state) => (path) =>
2018-10-15 14:42:47 +05:30
getChangesCountForFiles(state.changedFiles, path);
2021-03-08 18:12:59 +05:30
export const getStagedFilesCountForPath = (state) => (path) =>
2018-10-15 14:42:47 +05:30
getChangesCountForFiles(state.stagedFiles, path);
export const lastCommit = (state, getters) => {
2018-11-08 19:23:39 +05:30
const branch = getters.currentProject && getters.currentBranch;
2018-10-15 14:42:47 +05:30
return branch ? branch.commit : null;
};
2019-12-26 22:10:19 +05:30
export const findBranch = (state, getters) => (projectId, branchId) => {
const project = getters.findProject(projectId);
return project && project.branches[branchId];
};
2018-11-08 19:23:39 +05:30
export const currentBranch = (state, getters) =>
2019-12-26 22:10:19 +05:30
getters.findBranch(state.currentProjectId, state.currentBranchId);
2018-11-08 19:23:39 +05:30
2019-09-04 21:01:54 +05:30
export const branchName = (_state, getters) => getters.currentBranch && getters.currentBranch.name;
2021-03-08 18:12:59 +05:30
export const packageJson = (state) => state.entries[packageJsonPath];
2018-11-18 11:00:15 +05:30
2019-09-04 21:01:54 +05:30
export const isOnDefaultBranch = (_state, getters) =>
getters.currentProject && getters.currentProject.default_branch === getters.branchName;
2020-04-22 19:07:51 +05:30
export const canPushToBranch = (_state, getters) => {
return Boolean(getters.currentBranch ? getters.currentBranch.can_push : getters.canPushCode);
};
2019-12-04 20:38:33 +05:30
2021-03-08 18:12:59 +05:30
export const isFileDeletedAndReadded = (state, getters) => (path) => {
2020-01-01 13:55:28 +05:30
const stagedFile = getters.getStagedFile(path);
const file = state.entries[path];
return Boolean(stagedFile && stagedFile.deleted && file.tempFile);
};
// checks if any diff exists in the staged or unstaged changes for this path
2021-03-08 18:12:59 +05:30
export const getDiffInfo = (state, getters) => (path) => {
2020-01-01 13:55:28 +05:30
const stagedFile = getters.getStagedFile(path);
const file = state.entries[path];
const renamed = file.prevPath ? file.path !== file.prevPath : false;
const deletedAndReadded = getters.isFileDeletedAndReadded(path);
const deleted = deletedAndReadded ? false : file.deleted;
const tempFile = deletedAndReadded ? false : file.tempFile;
const changed = file.content !== (deletedAndReadded ? stagedFile.raw : file.raw);
return {
exists: changed || renamed || deleted || tempFile,
changed,
renamed,
deleted,
tempFile,
};
};
2021-03-08 18:12:59 +05:30
export const findProjectPermissions = (state, getters) => (projectId) =>
2021-03-11 19:13:27 +05:30
getters.findProject(projectId)?.userPermissions || DEFAULT_PERMISSIONS;
2020-03-13 15:44:24 +05:30
2021-04-17 20:07:23 +05:30
export const findPushRules = (state, getters) => (projectId) =>
getters.findProject(projectId)?.pushRules || {};
2020-03-13 15:44:24 +05:30
export const canReadMergeRequests = (state, getters) =>
Boolean(getters.findProjectPermissions(state.currentProjectId)[PERMISSION_READ_MR]);
export const canCreateMergeRequests = (state, getters) =>
Boolean(getters.findProjectPermissions(state.currentProjectId)[PERMISSION_CREATE_MR]);
2021-04-17 20:07:23 +05:30
/**
* Returns an object with `isAllowed` and `message` based on why the user cant push code
*/
export const canPushCodeStatus = (state, getters) => {
const canPushCode = getters.findProjectPermissions(state.currentProjectId)[PERMISSION_PUSH_CODE];
const rejectUnsignedCommits = getters.findPushRules(state.currentProjectId)[
PUSH_RULE_REJECT_UNSIGNED_COMMITS
];
2021-04-29 21:17:54 +05:30
if (window.gon?.features?.rejectUnsignedCommitsByGitlab && rejectUnsignedCommits) {
2021-04-17 20:07:23 +05:30
return {
isAllowed: false,
message: MSG_CANNOT_PUSH_UNSIGNED,
messageShort: MSG_CANNOT_PUSH_UNSIGNED_SHORT,
};
}
if (!canPushCode) {
return {
isAllowed: false,
2021-04-29 21:17:54 +05:30
messageShort: MSG_CANNOT_PUSH_CODE,
...getCannotPushCodeViewModel(state),
2021-04-17 20:07:23 +05:30
};
}
return {
isAllowed: true,
message: '',
messageShort: '',
};
};
export const canPushCode = (state, getters) => getters.canPushCodeStatus.isAllowed;
2020-04-22 19:07:51 +05:30
2021-03-08 18:12:59 +05:30
export const entryExists = (state) => (path) =>
2020-06-23 00:09:42 +05:30
Boolean(state.entries[path] && !state.entries[path].deleted);
2021-03-08 18:12:59 +05:30
export const getAvailableFileName = (state, getters) => (path) => {
2020-06-23 00:09:42 +05:30
let newPath = path;
while (getters.entryExists(newPath)) {
2021-01-03 14:25:43 +05:30
newPath = addNumericSuffix(newPath);
2020-06-23 00:09:42 +05:30
}
return newPath;
};
2020-11-24 15:15:51 +05:30
2021-03-08 18:12:59 +05:30
export const getUrlForPath = (state) => (path) =>
2020-11-24 15:15:51 +05:30
`/project/${state.currentProjectId}/tree/${state.currentBranchId}/-/${path}/`;
2021-03-08 18:12:59 +05:30
export const getJsonSchemaForPath = (state, getters) => (path) => {
2020-11-24 15:15:51 +05:30
const [namespace, ...project] = state.currentProjectId.split('/');
return {
uri:
// eslint-disable-next-line no-restricted-globals
location.origin +
Api.buildUrl(Api.projectFileSchemaPath)
.replace(':namespace_path', namespace)
.replace(':project_path', project.join('/'))
.replace(':ref', getters.currentBranch?.commit.id || state.currentBranchId)
.replace(':filename', path),
fileMatch: [`*${path}`],
};
};
2021-06-08 01:23:25 +05:30
export * from './getters/alert';