Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: prune action #86

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add checkout action on branch menu
- Add create new branch action on branch menu
- Add update action on branch menu
- Add prune action on branch menu
- Draw component on definition click
- Add file tabs to wrap monaco editor on text view
- Add root folder in file explorer with project name as label
Expand Down
30 changes: 29 additions & 1 deletion src/components/menu/GitBranchMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@
<q-item-section>{{ $t('actions.git.newBranch') }}</q-item-section>
</q-item>

<q-item clickable @click="onPrune">
<q-item-section avatar>
<q-icon
color="primary"
name="fa-solid fa-rotate"
data-cy="git-menu-prune"
/>
</q-item-section>
<q-item-section>{{ $t('actions.git.prune') }}</q-item-section>
</q-item>
<q-linear-progress indeterminate v-if="pruneLoader"/>

<template v-if="filteredBranches.local.length > 0">
<git-branch-header-menu :title="$t('menu.git.localBranchesTitle')"/>
<template v-for="(branch, index) in filteredBranches.local">
Expand Down Expand Up @@ -94,7 +106,12 @@ import {
ref,
} from 'vue';
import { useRoute } from 'vue-router';
import { getBranches, fetchGit, getProjectById } from 'src/composables/Project';
import {
getBranches,
fetchGit,
getProjectById,
gitPrune,
} from 'src/composables/Project';
import GitEvent from 'src/composables/events/GitEvent';
import GitBranchExpandListMenu from 'components/menu/GitBranchExpandListMenu';
import DialogEvent from 'src/composables/events/DialogEvent';
Expand All @@ -120,6 +137,7 @@ const searchedBranch = ref('');
const showLocal = ref(false);
const showRemote = ref(false);
const searchInput = ref(null);
const pruneLoader = ref(false);
const hasNoBranches = computed(() => filteredBranches.value.local.length === 0
&& filteredBranches.value.remote.length === 0);
let fetchSubscription;
Expand Down Expand Up @@ -207,6 +225,16 @@ function newBranch() {
});
}

/**
* Execute prune action.
*/
function onPrune() {
pruneLoader.value = true;
gitPrune(getProjectById(route.params.projectName)).finally(() => {
pruneLoader.value = false;
});
}

onMounted(() => {
fetchSubscription = GitEvent.FetchEvent.subscribe(initBranches);
checkoutSubscription = GitEvent.CheckoutEvent.subscribe(initBranches);
Expand Down
25 changes: 25 additions & 0 deletions src/composables/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,28 @@ export async function gitUpdate(project, branchName, fastForward) {
corsProxy: 'https://cors.isomorphic-git.org',
});
}

/**
* Fetch with prune option project on git. Emit a FetchEvent on success.
* @param {Project} project - Project to update.
* @return {Promise<void>} Promise with nothing on success otherwise an error.
*/
export async function gitPrune(project) {
if (project.git && project.git.repository) {
await git.fetch({
fs,
http,
url: project.git.repository,
dir: `/${project.id}`,
prune: true,
pruneTags: true,
onAuth: () => ({
username: project.git.username,
password: project.git.token,
}),
corsProxy: 'https://cors.isomorphic-git.org',
});
}

return GitEvent.FetchEvent.next();
}
1 change: 1 addition & 0 deletions src/i18n/en-US/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default {
update: 'Branch is updated &#129395;!',
},
update: 'Update',
prune: 'Prune',
},
fileExplorer: {
empty: 'No files available in project.',
Expand Down
12 changes: 11 additions & 1 deletion tests/unit/components/menu/GitBranchMenu.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import DialogEvent from 'src/composables/events/DialogEvent';
import { shallowMount } from '@vue/test-utils';
import GitBranchMenu from 'components/menu/GitBranchMenu';
import { useRoute } from 'vue-router';
import { getBranches } from 'src/composables/Project';
import { getBranches, gitPrune } from 'src/composables/Project';
import Branch from 'src/models/git/Branch';
import { createI18n } from 'vue-i18n';
import i18nConfiguration from 'src/i18n';
Expand Down Expand Up @@ -32,6 +32,7 @@ jest.mock('src/composables/Project', () => ({
getBranches: jest.fn(() => Promise.resolve([])),
fetchGit: jest.fn(() => Promise.resolve()),
getProjectById: jest.fn(),
gitPrune: jest.fn(() => Promise.resolve()),
}));

describe('Test component: GitBranchMenu', () => {
Expand Down Expand Up @@ -223,6 +224,15 @@ describe('Test component: GitBranchMenu', () => {
});
});

describe('Test function: onPrune', () => {
it('should call gitPrune', async () => {
expect(wrapper.vm.pruneLoader).toEqual(false);
await wrapper.vm.onPrune();
expect(gitPrune).toBeCalled();
expect(wrapper.vm.pruneLoader).toEqual(false);
});
});

describe('Test function: openCloseExpandMenu', () => {
it('should invert local value', () => {
expect(wrapper.vm.showLocal).toEqual(false);
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/composables/Project.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
checkout,
createBranchFrom,
gitUpdate,
gitPrune,
PROJECT_STORAGE_KEY,
} from 'src/composables/Project';
import { FileInformation, FileInput } from 'leto-modelizer-plugin-core';
Expand Down Expand Up @@ -314,4 +315,32 @@ describe('Test composable: Project', () => {
expect(result).toEqual('pull');
});
});

describe('Test function: gitPrune', () => {
it('should call git fetch with repository', async () => {
git.fetch = jest.fn();
await gitPrune(
{
id: 'test',
git: {
repository: 'test',
username: 'username',
token: 'token',
},
},
);
expect(git.fetch).toBeCalled();
expect(GitEvent.FetchEvent.next).toBeCalled();
});
it('should not call git fetch without repository', async () => {
git.fetch = jest.fn();
await gitPrune(
{
id: 'test',
},
);
expect(git.fetch).not.toBeCalled();
expect(GitEvent.FetchEvent.next).toBeCalled();
});
});
});