From 26b07714ab570cbf04f4fafe4d2a8d4b8db894ff Mon Sep 17 00:00:00 2001 From: Mateusz Duda Date: Mon, 15 Jul 2024 12:31:33 +0200 Subject: [PATCH] added ommiting commits from merged branch --- adf.json | 1 - file1 | 0 package.json | 2 +- src/Commands/runLogCommitCommand.ts | 2 +- .../runVerifyUnpushedCommitsCommand.ts | 12 +-- src/Git/GitRepository.ts | 19 +++-- src/Git/Types.ts | 1 + test/commits/verification.test.ts | 82 +++++++++++++++++-- 8 files changed, 95 insertions(+), 24 deletions(-) delete mode 100644 adf.json delete mode 100644 file1 diff --git a/adf.json b/adf.json deleted file mode 100644 index 4f8e1c9..0000000 --- a/adf.json +++ /dev/null @@ -1 +0,0 @@ - {"type":"doc","version":1,"content":[{"type":"expand","attrs":{"title":"'Project' scope tags v0.3.0 │ 20.05.2024 13:57 │ -"},"content":[{"type":"table","content":[{"type":"tableRow","content":[{"type":"tableHeader","content":[{"type":"paragraph","content":[{"type":"text","text":"Affected tags","marks":[{"type":"strong"}]}]}]},{"type":"tableHeader","attrs":{"colwidth":[20]},"content":[{"type":"paragraph","content":[{"type":"text","text":"Lines","marks":[{"type":"strong"}]}]}]},{"type":"tableHeader","content":[{"type":"paragraph","content":[{"type":"text","text":"Used by module","marks":[{"type":"strong"}]}]}]},{"type":"tableHeader","content":[{"type":"paragraph","content":[{"type":"text","text":"Used by tags","marks":[{"type":"strong"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableHeader","attrs":{},"content":[{"type":"paragraph","content":[{"type":"text","text":"Default module / Tag"}]}]},{"type":"tableHeader","attrs":{},"content":[{"type":"paragraph","content":[{"type":"text","text":"++ 60\n-- 27"}]}]},{"type":"tableHeader","attrs":{},"content":[{"type":"nestedExpand","attrs":{"title":"Default module"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Tag"}]}]},{"type":"nestedExpand","attrs":{"title":"Third module"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Some new tag"}]}]},{"type":"nestedExpand","attrs":{"title":"Fourth module"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Fourth tag"}]}]},{"type":"nestedExpand","attrs":{"title":"5 untagged files"},"content":[{"type":"paragraph","content":[{"type":"text","text":"src/Relevancy/RelevancyManager.ts\nsrc/References/TSReferenceFinder.ts\nsrc/Logger/Logger.ts\nsrc/Git/Types.ts\nsrc/HTMLCreator/HTMLCreator.ts"}]}]}]},{"type":"tableHeader","attrs":{},"content":[{"type":"nestedExpand","attrs":{"title":"Tag"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Default module"}]}]},{"type":"nestedExpand","attrs":{"title":"Some new tag"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Third module"}]}]},{"type":"nestedExpand","attrs":{"title":"Fourth tag"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Fourth module"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableHeader","attrs":{},"content":[{"type":"paragraph","content":[{"type":"text","text":"Fourth module / Fourth tag"}]}]},{"type":"tableHeader","attrs":{},"content":[{"type":"paragraph","content":[{"type":"text","text":"++ 56\n-- 21"}]}]},{"type":"tableHeader","attrs":{},"content":[{"type":"paragraph","content":[{"type":"text","text":"1 modules hidden by low relevancy"}]}]},{"type":"tableHeader","attrs":{},"content":[{"type":"paragraph","content":[{"type":"text","text":"1 tags hidden by low relevancy"}]}]}]},{"type":"tableRow","content":[{"type":"tableHeader","attrs":{},"content":[{"type":"paragraph","content":[{"type":"text","text":"Third module / Some new tag"}]}]},{"type":"tableHeader","attrs":{},"content":[{"type":"paragraph","content":[{"type":"text","text":"++ 21\n-- 12"}]}]},{"type":"tableHeader","attrs":{},"content":[{"type":"nestedExpand","attrs":{"title":"Default module"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Tag"}]}]},{"type":"nestedExpand","attrs":{"title":"Fourth module"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Fourth tag"}]}]},{"type":"nestedExpand","attrs":{"title":"Second module"},"content":[{"type":"paragraph","content":[{"type":"text","text":"asdflkj"}]}]},{"type":"nestedExpand","attrs":{"title":"2 untagged files"},"content":[{"type":"paragraph","content":[{"type":"text","text":"src/Logger/Logger.ts\nsrc/HTMLCreator/HTMLCreator.ts"}]}]}]},{"type":"tableHeader","attrs":{},"content":[{"type":"nestedExpand","attrs":{"title":"Tag"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Default module"}]}]},{"type":"nestedExpand","attrs":{"title":"Fourth tag"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Fourth module"}]}]},{"type":"nestedExpand","attrs":{"title":"asdflkj"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Second module"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableHeader","attrs":{},"content":[{"type":"nestedExpand","attrs":{"title":"1 untagged file"},"content":[{"type":"paragraph","content":[{"type":"text","text":"src/Relevancy/RelevancyManager.ts"}]}]}]},{"type":"tableHeader","attrs":{},"content":[{"type":"paragraph","content":[{"type":"text","text":"++ 1\n-- 1"}]}]},{"type":"tableHeader","attrs":{},"content":[{"type":"nestedExpand","attrs":{"title":"Default module"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Tag"}]}]},{"type":"nestedExpand","attrs":{"title":"Fourth module"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Fourth tag"}]}]},{"type":"nestedExpand","attrs":{"title":"Second module"},"content":[{"type":"paragraph","content":[{"type":"text","text":"asdflkj"}]}]},{"type":"nestedExpand","attrs":{"title":"5 untagged files"},"content":[{"type":"paragraph","content":[{"type":"text","text":"src/Git/GitRepository.ts\nsrc/Commands/runVerifyCommand.ts\nsrc/Commands/runVerifyUnpushedCommitsCommand.ts\nsrc/Logger/Logger.ts\nsrc/Commands/runLogCommitCommand.ts"}]}]}]},{"type":"tableHeader","attrs":{},"content":[{"type":"nestedExpand","attrs":{"title":"Tag"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Default module"}]}]},{"type":"nestedExpand","attrs":{"title":"Fourth tag"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Fourth module"}]}]},{"type":"nestedExpand","attrs":{"title":"asdflkj"},"content":[{"type":"paragraph","content":[{"type":"text","text":"Second module"}]}]}]}]}],"attrs":{"layout":"full-width"}}]}]} \ No newline at end of file diff --git a/file1 b/file1 deleted file mode 100644 index e69de29..0000000 diff --git a/package.json b/package.json index fba50bb..952edb5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "scope-tags", - "version": "0.3.5", + "version": "0.3.6", "description": "Output human readable test scope report for QA", "main": "dist/scope.js", "types": "dist/scope.d.ts", diff --git a/src/Commands/runLogCommitCommand.ts b/src/Commands/runLogCommitCommand.ts index 92ec1ec..161074e 100644 --- a/src/Commands/runLogCommitCommand.ts +++ b/src/Commands/runLogCommitCommand.ts @@ -26,7 +26,7 @@ export function runLogCommitCommand(args: Array, root: string) { console.log(`Commit summary: ${commit.summary()}`); console.log(`Relevancy info?: '${commitHasRelevancy ? "yes" : "no"}`); - console.log(`Branches: ${branches.join(', ')}`); + console.log(`Branches: ${branches.length ? branches.join(', ') : "-"}`); if (commitHasRelevancy) { const relevancyMap = relevancyTagger.loadRelevancyMapFromCommits([commit]); diff --git a/src/Commands/runVerifyUnpushedCommitsCommand.ts b/src/Commands/runVerifyUnpushedCommitsCommand.ts index 95fab8b..0262839 100644 --- a/src/Commands/runVerifyUnpushedCommitsCommand.ts +++ b/src/Commands/runVerifyUnpushedCommitsCommand.ts @@ -1,10 +1,10 @@ import { Commit } from "nodegit"; +import { exitIfScopeTagsNotInitialized } from "../Console/ExitIfScopeTagsNotInitialized"; import { GitRepository } from "../Git/GitRepository"; +import { VerificationInfo } from "../Git/Types"; +import { RelevancyManager } from "../Relevancy/RelevancyManager"; import { ConfigFile } from "../Scope/ConfigFile"; import { FileTagsDatabase } from "../Scope/FileTagsDatabase"; -import { exitIfScopeTagsNotInitialized } from "../Console/ExitIfScopeTagsNotInitialized"; -import { RelevancyManager } from "../Relevancy/RelevancyManager"; -import { VerificationInfo } from "../Git/Types"; export enum VerificationStatus { VERIFIED = "VERIFIED", @@ -53,9 +53,9 @@ export async function verifyUnpushedCommits(args: Array, root: string, u if (verificationInfo.isSkipped) { console.log(`[Scope Tags] Skipped check for '${commit.summary()}'`); - } - - if (verificationInfo.isMergeCommit) { + } else if(verificationInfo.isFromAnotherBranch) { + console.log(`[Scope Tags] Skipped check for '${commit.summary()}' because some other branches contain it`); + } else if (verificationInfo.isMergeCommit) { console.log(`[Scope Tags] Skipped check for '${commit.summary()}' because it's a merge commit`); } diff --git a/src/Git/GitRepository.ts b/src/Git/GitRepository.ts index d297c6c..95fc243 100644 --- a/src/Git/GitRepository.ts +++ b/src/Git/GitRepository.ts @@ -63,6 +63,7 @@ export class GitRepository { if (commits.length === maxCommitCount) { console.warn(`Found ${maxCommitCount} commits, which is the limit. Some commits may have been ommited. To remove this warning set higher gitCommitCountLimit in .scope/config.json`); } + return commits; } @@ -322,6 +323,7 @@ export class GitRepository { isSkipped: false, includesOnlyIgnoredFiles: false, isMergeCommit: false, + isFromAnotherBranch: false, hasRelevancy: false, relevancy: [], }; @@ -337,6 +339,14 @@ export class GitRepository { return commitInfo; } + const branchesContainingCommit = await this.branchesContainingCommit(commit); + + if(branchesContainingCommit.length !== 1) { + commitInfo.isFromAnotherBranch = true; + commitInfo.isSkipped = true; + return commitInfo; + } + const fileDataArray = useGitNatively ? this.getFileDataUsingNativeGitCommand(commit) : await this.getFileDataForCommit(commit); const statusMap = database.checkMultipleFileStatusInDatabase(fileDataArray, config); @@ -419,8 +429,6 @@ export class GitRepository { } public async branchesContainingCommit(commit: Commit): Promise { - - // Open the repository const repo = await this._getRepository(); // Get all references (branches) @@ -430,13 +438,8 @@ export class GitRepository { for (const ref of references) { const branchCommit = await repo.getBranchCommit(ref); - - // Check if the commit is in the branch history - - // for branch_name in self._repository.listall_branches(self._flag): - // if self._commit is None or self.get(branch_name) is not None: - // yield branch_name + // Check if the commit is in the branch history if(branchCommit.sha() === commit.sha()) { branchesContainingCommit.push(ref.shorthand()); continue; diff --git a/src/Git/Types.ts b/src/Git/Types.ts index a287205..acdbb33 100644 --- a/src/Git/Types.ts +++ b/src/Git/Types.ts @@ -49,6 +49,7 @@ export type VerificationInfo = { isSkipped: boolean, hasRelevancy: boolean, isMergeCommit: boolean, + isFromAnotherBranch: boolean, includesOnlyIgnoredFiles: boolean, relevancy: Array, } \ No newline at end of file diff --git a/test/commits/verification.test.ts b/test/commits/verification.test.ts index d6eb8e2..a8fceb7 100644 --- a/test/commits/verification.test.ts +++ b/test/commits/verification.test.ts @@ -1,13 +1,13 @@ -import { appendSomeTextToFile, cloneMockRepositoryToFolder, commitEmptyFiles, commitFiles, commitModitication, createEmptyFiles, makeUniqueFolderForTest, mergeBranchToCurrent } from "../utils/utils"; +import { readdirSync } from "fs-extra"; +import { join } from "path"; import { VerificationStatus, verifyUnpushedCommits } from "../../src/Commands/runVerifyUnpushedCommitsCommand"; import { GitRepository } from "../../src/Git/GitRepository"; +import { FileData } from "../../src/Git/Types"; +import { RelevancyManager } from "../../src/Relevancy/RelevancyManager"; import { ConfigFile } from "../../src/Scope/ConfigFile"; import { FileTagsDatabase } from "../../src/Scope/FileTagsDatabase"; -import { RelevancyManager } from "../../src/Relevancy/RelevancyManager"; import { Utils } from "../../src/Scope/Utils"; -import { join } from "path"; -import { readdirSync } from "fs-extra"; -import { FileData } from "../../src/Git/Types"; +import { appendSomeTextToFile, cloneMockRepositoryToFolder, commitEmptyFiles, commitFiles, commitModitication, createEmptyFiles, makeUniqueFolderForTest, mergeBranchToCurrent } from "../utils/utils"; describe("Commit verification by scope tags script", () => { @@ -175,11 +175,12 @@ describe("Commit verification by scope tags script", () => { expect(mergeCommitInfo.isMergeCommit).toBe(true); expect(realCommitInfo.isMergeCommit).toBe(false); - expect(realCommitInfo.isSkipped).toBe(false); + expect(realCommitInfo.isSkipped).toBe(true); + expect(realCommitInfo.isFromAnotherBranch).toBe(true); const verificationStatus = await verifyUnpushedCommits([], REPO_PATH, true); - expect(verificationStatus).toBe(VerificationStatus.NOT_VERIFIED); + expect(verificationStatus).toBe(VerificationStatus.VERIFIED); }); // Not pretty fix for squashed commits, at least in BitBucket @@ -266,4 +267,71 @@ describe("Commit verification by scope tags script", () => { expect(verificationStatus).toBe(VerificationStatus.VERIFIED); }); + it("When another branch is merged into current branch, commits from this branch are ommited from verification", async () => { + const FOLDER_PATH = makeUniqueFolderForTest(); + const REPO_PATH = cloneMockRepositoryToFolder(FOLDER_PATH); + + // Contains 3 commits each with some new file + const branchToMergeName = "branch-with-multiple-new-commits"; + + mergeBranchToCurrent(REPO_PATH, branchToMergeName); + + const repository = new GitRepository(REPO_PATH); + const config = new ConfigFile(REPO_PATH); + const database = new FileTagsDatabase(REPO_PATH); + const relevancy = new RelevancyManager(); + + const [mergeCommit, ...commitsFronAnotherBranch] = await repository.getUnpushedCommits(); + + expect(mergeCommit).toBeDefined(); + expect(commitsFronAnotherBranch).toBeDefined(); + expect(commitsFronAnotherBranch.length).toBe(3); + + const mergeCommitInfo = await repository.verifyCommit(mergeCommit, config, database, relevancy, undefined, true); + + expect(mergeCommitInfo.isMergeCommit).toBe(true); + + for(const commit of commitsFronAnotherBranch) { + const commitInfo = await repository.verifyCommit(commit, config, database, relevancy, undefined, true); + + expect(commitInfo.isMergeCommit).toBe(false); + expect(commitInfo.isSkipped).toBe(true); + expect(commitInfo.isFromAnotherBranch).toBe(true); + } + + const verificationStatus = await verifyUnpushedCommits([], REPO_PATH, true); + + expect(verificationStatus).toBe(VerificationStatus.VERIFIED); + + // Check if adding new commit works as expected, just to be 100% sure + + const newFiles = [ + "src/newly-added-file1.js", + "src/newly-added-file2.js", + "src/newly-added-file3.js", + ]; + + newFiles.forEach(newFile => { + expect(database.isFileInDatabase(newFile)).toBe(false); + expect(database.isIgnored(newFile, config.getIgnoredFileExtenstions())).toBe(false); + }); + + await commitEmptyFiles(newFiles, REPO_PATH); + + const unpushedCommits = await repository.getUnpushedCommits(); + + expect(unpushedCommits.length).toBe(5); + + const newCommitInfo = await repository.verifyCommit(unpushedCommits[0], config, database, relevancy, undefined, true); + + expect(newCommitInfo.isVerified).toBe(false); + expect(newCommitInfo.isMergeCommit).toBe(false); + expect(newCommitInfo.isFromAnotherBranch).toBe(false); + + const newVerificationStatus = await verifyUnpushedCommits([], REPO_PATH, true); + + console.debug(`Verification status: ${Utils.getEnumKeyByEnumValue(VerificationStatus, newVerificationStatus)}`); + + expect(newVerificationStatus).toBe(VerificationStatus.NOT_VERIFIED); + }); });