Skip to content

Commit

Permalink
Merge pull request #28 from particle-iot/bug/sc-125818/fix-auto-versi…
Browse files Browse the repository at this point in the history
…oning-crash-on-new-git-repo

fix: [sc-125818] Fix auto-versioning crash on new git repo
  • Loading branch information
laupow authored Mar 6, 2024
2 parents 47429ea + 21161dd commit 831eb38
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
10 changes: 7 additions & 3 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 53 additions & 2 deletions src/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ describe('revisionOfLastVersionBump', () => {
productVersionMacroName: productVersionMacroName
});

expect(result).toEqual(commitHash);
expect(result).toHaveLength(7);
expect(result).toEqual(commitHash.substring(0, 7));
expect(gitMock).toHaveBeenCalledWith(gitRepo);
expect(rawMock).toHaveBeenCalledWith(['blame', versionFilePath]);
});
Expand All @@ -62,6 +63,41 @@ describe('revisionOfLastVersionBump', () => {
`Could not find the ${productVersionMacroName} line in the blame information.`
);
});

test('should handle the first commit of a file', async () => {
const gitRepo = '/path/to/repo';
const versionFilePath = '/path/to/version/file/application.cpp';
const productVersionMacroName = 'PRODUCT_VERSION';

// Git blame will return a commit hash starting with ^ for the first commit of a file
rawMock.mockResolvedValue(createBlameInfoMock('^1234abc', productVersionMacroName));

const result = await revisionOfLastVersionBump({
gitRepo,
versionFilePath,
productVersionMacroName
});

expect(result).toHaveLength(7);
expect(result).toEqual('1234abc');
});

test('should return the first 7 characters of the commit hash', async () => {
const commitHash = 'a1b2c3d4e5f6';
const gitRepo = '/path/to/repo';
const versionFilePath = '/path/to/version/file/application.cpp';
const productVersionMacroName = 'PRODUCT_VERSION';

rawMock.mockResolvedValue(createBlameInfoMock(commitHash, productVersionMacroName));

const result = await revisionOfLastVersionBump({
gitRepo,
versionFilePath,
productVersionMacroName
});

expect(result).toEqual(commitHash.substring(0, 7));
});
});

describe('currentFirmwareVersion', () => {
Expand Down Expand Up @@ -277,7 +313,8 @@ describe('mostRecentRevisionInFolder', () => {
const result = await mostRecentRevisionInFolder({ gitRepo: gitRepo, folderPath: folderPath });

expect(logMock).toHaveBeenCalledWith({ file: folderPath });
expect(result).toBe(latestHash.substring(0, 8));
expect(result).toHaveLength(7);
expect(result).toBe(latestHash.substring(0, 7));
});

test('should throw an error if no latest revision is found', async () => {
Expand All @@ -304,6 +341,20 @@ describe('mostRecentRevisionInFolder', () => {
);
});

test('should return the first 7 characters of the commit hash', async () => {
const gitRepo = '/path/to/repo';
const folderPath = '/path/to/repo/some/folder';
const latestHash = '123456789abcdef';
// Set up the mock to return the latest Git revision
logMock.mockResolvedValue({
latest: {
hash: latestHash
}
});
const result = await mostRecentRevisionInFolder({ gitRepo: gitRepo, folderPath: folderPath });
expect(result).toBe(latestHash.substring(0, 7));
});

});
describe('hasFullHistory', () => {
const gitRepo = '.'; // Use the current directory as the git repository for testing
Expand Down
12 changes: 8 additions & 4 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,16 @@ export async function revisionOfLastVersionBump(
const blameInfo = await git.raw(['blame', versionFilePath]);

// Use regex to find the line containing the PRODUCT_VERSION macro
const versionRegex = new RegExp(`^([a-f0-9]+).+${productVersionMacroName}.*\\(\\d+\\)`, 'm');
const versionRegex = new RegExp(`^(\\^?[a-f0-9]+).+${productVersionMacroName}.*\\(\\d+\\)`, 'm');
const match = versionRegex.exec(blameInfo);

if (match) {
const commitHash = match[1];
return commitHash;

// If the commit hash starts with '^', it represents an initial commit
// In this case, we remove the '^' before returning the hash
// Otherwise, we return the hash as is
// In both cases, we only return the first 7 characters of the hash
return commitHash.startsWith('^') ? commitHash.substring(1, 8) : commitHash.substring(0, 7);
} else {
throw new Error(`Could not find the ${productVersionMacroName} line in the blame information.`);
}
Expand Down Expand Up @@ -133,7 +137,7 @@ export async function mostRecentRevisionInFolder(
if (!log.latest) {
throw new Error('No latest revision found');
}
return log.latest.hash.substring(0, 8);
return log.latest.hash.substring(0, 7);
} catch (error) {
throw new Error(`Error getting the latest Git revision for folder "${folderPath}": ${error}`);
}
Expand Down

0 comments on commit 831eb38

Please sign in to comment.