Skip to content

Commit

Permalink
Merge pull request #169 from emulsify-ds/36-default-system-install-pi…
Browse files Browse the repository at this point in the history
…n-latest

feat(36): adding latest tag when available
  • Loading branch information
mikeethedude authored Dec 16, 2022
2 parents 64968e7 + 88c4b74 commit 13f02d6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
7 changes: 6 additions & 1 deletion jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ jest.mock('simple-git', () => {
clone: jest.fn(),
branch: jest.fn(),
checkout: jest.fn(),
fetch: jest.fn(),
fetch: jest.fn().mockReturnThis(),
pull: jest.fn(),
init: jest.fn().mockReturnThis(),
addRemote: jest.fn().mockReturnThis(),
tags: jest.fn(() => {
return { latest: '' };
}),
};

return jest.fn(() => mockGit);
Expand Down
9 changes: 8 additions & 1 deletion src/handlers/systemInstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import getAvailableSystems from '../util/system/getAvailableSystems';
import getGitRepoNameFromUrl from '../util/getGitRepoNameFromUrl';
import cloneIntoCache from '../util/cache/cloneIntoCache';
import getCachedItemCheckout from '../util/cache/getCachedItemCheckout';
import getRepositoryLatestTag from '../util/getRepositoryLatestTag';
import installComponentFromCache from '../util/project/installComponentFromCache';
import installGeneralAssetsFromCache from '../util/project/installGeneralAssetsFromCache';
import getJsonFromCachedFile from '../util/cache/getJsonFromCachedFile';
Expand Down Expand Up @@ -109,10 +110,16 @@ export default async function systemInstall(
);
}

let checkout = repo.checkout;
// Attempt to get latest tag if no branch was supplied.
if (!checkout) {
checkout = await getRepositoryLatestTag(repo.repository);
}

// Clone the system into the cache.
await cloneIntoCache('systems', [repo.name])({
repository: repo.repository,
checkout: repo.checkout,
checkout: checkout,
});

// Load the system configuration file.
Expand Down
26 changes: 26 additions & 0 deletions src/util/getRepositoryLatestTag.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import getRepositoryLatestTag from './getRepositoryLatestTag';
import git from 'simple-git';

const gitTagsMock = git().tags as jest.Mock;

describe('getLatestRepositoryTag', () => {
beforeEach(() => {
gitTagsMock.mockClear();
});
it('Can get latest tag from repository url', async () => {
expect.assertions(1);
gitTagsMock.mockReturnValueOnce({ latest: '1.5.0' });
const latest = await getRepositoryLatestTag(
'[email protected]:emulsify-ds/compound.git'
);
expect(latest).toBe('1.5.0');
});

it('Can return empty if no latest tag is found', async () => {
expect.assertions(1);
const latest = await getRepositoryLatestTag(
'[email protected]:emulsify-ds/compoun.git'
);
expect(latest).toBe('');
});
});
20 changes: 20 additions & 0 deletions src/util/getRepositoryLatestTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import simpleGit from 'simple-git';

const git = simpleGit();

/**
* Helper function to get the latest tag for a given repository url.
*
* @param url git url to use
* @returns string of the latest tag or undefined.
*/
export default async function getRepositoryLatestTag(
url: string
): Promise<string | undefined> {
const repositoryTags = await git
.init()
.addRemote('origin', url)
.fetch()
.tags();
return repositoryTags.latest;
}

0 comments on commit 13f02d6

Please sign in to comment.