Skip to content

Commit

Permalink
added ommiting commits from merged branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Duda committed Jul 15, 2024
1 parent c1b9e43 commit 26b0771
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 24 deletions.
1 change: 0 additions & 1 deletion adf.json

This file was deleted.

Empty file removed file1
Empty file.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/runLogCommitCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function runLogCommitCommand(args: Array<string>, 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]);
Expand Down
12 changes: 6 additions & 6 deletions src/Commands/runVerifyUnpushedCommitsCommand.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -53,9 +53,9 @@ export async function verifyUnpushedCommits(args: Array<string>, 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`);
}

Expand Down
19 changes: 11 additions & 8 deletions src/Git/GitRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -322,6 +323,7 @@ export class GitRepository {
isSkipped: false,
includesOnlyIgnoredFiles: false,
isMergeCommit: false,
isFromAnotherBranch: false,
hasRelevancy: false,
relevancy: [],
};
Expand All @@ -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);

Expand Down Expand Up @@ -419,8 +429,6 @@ export class GitRepository {
}

public async branchesContainingCommit(commit: Commit): Promise<string[]> {

// Open the repository
const repo = await this._getRepository();

// Get all references (branches)
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Git/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export type VerificationInfo = {
isSkipped: boolean,
hasRelevancy: boolean,
isMergeCommit: boolean,
isFromAnotherBranch: boolean,
includesOnlyIgnoredFiles: boolean,
relevancy: Array<CommitMessageRelevancyInfo>,
}
82 changes: 75 additions & 7 deletions test/commits/verification.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
});
});

0 comments on commit 26b0771

Please sign in to comment.