Skip to content

Commit

Permalink
fix: improve logging (#31)
Browse files Browse the repository at this point in the history
Logging is improved by listing the actual name of the release branch and the modified files if rebase failed due to that.

* refactor: rename 'getStagedFiles' to 'getModifiedFiles'
The function 'getStagedFiles' (now 'getModifiedFiles') returns list of tracked and modified files in general
* feat: list modified files on failed rebase
* test: test error logging for unstaged changes during rebase
* feat: add actual branch names to log messages
* refactor: reduce complexity of nested if statement

Co-authored-by: Semaph0r <[email protected]>
  • Loading branch information
Blarkdackbyte and Semaph0r authored Feb 19, 2022
1 parent ad253eb commit e5a04d7
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 75 deletions.
4 changes: 2 additions & 2 deletions src/helpers/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ export default class Git {
}

/**
* Get list of files that are included in next commit
* Get list of files that are tracked and modified
* Each entry consists of the status and the file path.
*/
getStagedFiles(): Promise<string[]> {
getModifiedFiles(): Promise<string[]> {
return new Promise<string[]>(async (resolve, reject) => {
execa('git', ['status', '-s', '-uno'], this.execaOpts)
.then((result) => {
Expand Down
38 changes: 27 additions & 11 deletions src/perform-backmerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,51 @@ async function performBackmergeIntoBranch(git: Git, pluginConfig: Partial<Config
lastRelease,
nextRelease
}: any = context;
const masterBranchName = branch.name;
if (!options.allowSameBranchMerge && developBranchName === masterBranchName) {
const releaseBranchName = branch.name;
if (!options.allowSameBranchMerge && developBranchName === releaseBranchName) {
throw new Error(
'Branch for back-merge is the same as the branch which includes the release. ' +
'Aborting back-merge workflow.'
);
}

context.logger.log('Performing back-merge into branch "' + developBranchName + '".');
context.logger.log('Performing back-merge into develop branch "' + developBranchName + '".');

if (developBranchName !== masterBranchName) {
if (developBranchName !== releaseBranchName) {
// Branch is detached. Checkout master first to be able to check out other branches
context.logger.log('Branch is detached. Checking out master branch.');
await git.checkout(masterBranchName);
context.logger.log('Checking out develop branch.');
context.logger.log(`Branch is detached. Checking out release branch "${releaseBranchName}".`);
await git.checkout(releaseBranchName);
context.logger.log(`Checking out develop branch "${developBranchName}".`);
await git.checkout(developBranchName);
context.logger.log(`Performing backmerge with "${options.backmergeStrategy}" strategy.`);
if (options.backmergeStrategy === 'merge') {
await git.merge(masterBranchName, options.mergeMode);
await git.merge(releaseBranchName, options.mergeMode);
} else {
await git.rebase(masterBranchName);
try {
await git.rebase(releaseBranchName)
} catch (e) {
if (e.stderr == null || !e.stderr.includes('have unstaged changes')) {
throw e
}
context.logger.error('Rebase failed: You have unstaged changes.')
const modifiedFiles = await git.getModifiedFiles()
if (modifiedFiles.length) {
context.logger.error(`${modifiedFiles.length} modified file(s):`)
for (const file of modifiedFiles) {
context.logger.error(file)
}
}
return
}
}
} else {
context.logger.log('Checking out develop branch directly.');
context.logger.log(`Checking out develop branch "${developBranchName}" directly.`);
await git.checkout(developBranchName);
}

await triggerPluginHooks(options, context);
const stagedFiles = await git.getStagedFiles();
// in this case there are only staged files
const stagedFiles = await git.getModifiedFiles();
context.logger.log('Found ' + stagedFiles.length + ' staged files for back-merge commit');
if (stagedFiles.length) {
for (const file of stagedFiles) {
Expand Down
8 changes: 4 additions & 4 deletions test/helpers/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ describe("git", () => {
);
});

it("getStagedFiles", async () => {
it("getModifiedFiles", async () => {
const execaMock: any = execa;
execaMock.mockResolvedValueOnce({
stdout: `A file.txt\nB file2.txt`
});

const files = await subject.getStagedFiles();
const files = await subject.getModifiedFiles();
expect(execa).toHaveBeenCalledWith(
'git',
['status', '-s', '-uno'],
Expand All @@ -152,12 +152,12 @@ describe("git", () => {
expect(files).toEqual(['A file.txt','B file2.txt']);
});

it("getStagedFiles fails", (done) => {
it("getModifiedFiles fails", (done) => {
const execaMock: any = execa;
execaMock.mockRejectedValueOnce({
stderr: 'An error occurred'
});
subject.getStagedFiles()
subject.getModifiedFiles()
.then(() => done('Error expected'))
.catch(() => done());
});
Expand Down
Loading

0 comments on commit e5a04d7

Please sign in to comment.