Skip to content

Commit

Permalink
Continue looking through history even if some commits don't work
Browse files Browse the repository at this point in the history
the simple git tool threw an error, which crashed the script.
context: https://community.particle.io/t/github-actions-could-not-find-current-product-version-macro/66338/5
  • Loading branch information
laupow committed Mar 6, 2024
1 parent d58a68f commit 41081c4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,26 @@ describe('currentFirmwareVersion', () => {

expect(result).toBe(0);
});

test('should handle when the version file was deleted in a previous commit', async () => {
const commitHashes = ['a1b2c3d4e5f6', 'b2c3d4e5f6a1'];
const gitRepo = '/path/to/repo';
const versionFilePath = '/path/to/repo/project-folder/application.cpp';
const productVersionMacroName = 'PRODUCT_VERSION';

logMock.mockResolvedValue(createLogMock(commitHashes));
showMock
.mockRejectedValueOnce(new Error(`path '${versionFilePath}' exists on disk, but not in '${commitHashes[0]}'`))
.mockResolvedValueOnce(createCommitBodyMock(productVersionMacroName, 1));

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

expect(result).toBe(1);
});
});

describe('findProductVersionMacroFile', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ export async function currentFirmwareVersion(

for (const log of logs.all) {
const currentCommit = log.hash;
const commitBody = await git.show([`${currentCommit}:${versionFilePath}`]);

// Use regex to extract the PRODUCT_VERSION from the patch
const versionRegex = new RegExp(`^.*${productVersionMacroName}.*\\((\\d+)\\)`, 'gm');
let commitBody = '';
try {
commitBody = await git.show([`${currentCommit}:${versionFilePath}`]);
} catch (error) {
debug(`Error getting the file ${versionFilePath} from commit ${currentCommit}: ${error}. This can occur if the file was deleted in the commit. Skipping commit`);
}

const match = versionRegex.exec(commitBody);

Expand Down

0 comments on commit 41081c4

Please sign in to comment.