diff --git a/jest.setup.js b/jest.setup.js index 65bb8ae..c41feb5 100644 --- a/jest.setup.js +++ b/jest.setup.js @@ -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); diff --git a/src/handlers/systemInstall.ts b/src/handlers/systemInstall.ts index 4338689..7ccd64a 100644 --- a/src/handlers/systemInstall.ts +++ b/src/handlers/systemInstall.ts @@ -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'; @@ -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. diff --git a/src/util/getRepositoryLatestTag.test.ts b/src/util/getRepositoryLatestTag.test.ts new file mode 100644 index 0000000..ee7c923 --- /dev/null +++ b/src/util/getRepositoryLatestTag.test.ts @@ -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( + 'git@github.com: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( + 'git@github.com:emulsify-ds/compoun.git' + ); + expect(latest).toBe(''); + }); +}); diff --git a/src/util/getRepositoryLatestTag.ts b/src/util/getRepositoryLatestTag.ts new file mode 100644 index 0000000..b8542d2 --- /dev/null +++ b/src/util/getRepositoryLatestTag.ts @@ -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 { + const repositoryTags = await git + .init() + .addRemote('origin', url) + .fetch() + .tags(); + return repositoryTags.latest; +}