Skip to content

Commit

Permalink
Merge pull request #1132 from intuit/next-log
Browse files Browse the repository at this point in the history
Improve next branch preview changelog
  • Loading branch information
hipstersmoothie authored Apr 6, 2020
2 parents f022435 + d4f94f4 commit b4dd1cb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
8 changes: 8 additions & 0 deletions packages/core/src/__tests__/auto-in-pr-ci.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import SEMVER from "../semver";
import { dummyLog } from "../utils/logger";
import makeCommitFromMsg from "./make-commit-from-msg";
import endent from "endent";
import execPromise from "../utils/exec-promise";

const exec = jest.fn();
jest.mock("../utils/exec-promise");
// @ts-ignore
execPromise.mockImplementation(exec);
exec.mockResolvedValue("");

jest.mock("env-ci", () => () => ({
pr: 123,
Expand Down Expand Up @@ -160,6 +167,7 @@ describe("next in ci", () => {
test("should post comment with new version", async () => {
const auto = new Auto({ ...defaults, plugins: [] });

exec.mockResolvedValue("v1.0.0");
// @ts-ignore
auto.checkClean = () => Promise.resolve(true);
const prBody = jest.fn();
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/__tests__/auto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,7 @@ describe("Auto", () => {
auto.checkClean = () => Promise.resolve(true);
auto.logger = dummyLog();
await auto.loadConfig();
auto.remote = "origin";
auto.git!.getProject = () => Promise.resolve({ data: {} } as any);
auto.git!.getLatestRelease = () => Promise.resolve("1.2.3");
auto.release!.generateReleaseNotes = () => Promise.resolve("notes");
Expand All @@ -1217,6 +1218,7 @@ describe("Auto", () => {
auto.checkClean = () => Promise.resolve(true);
auto.logger = dummyLog();
await auto.loadConfig();
auto.remote = "origin";
auto.git!.publish = () => Promise.resolve({} as any);
auto.git!.getLatestTagInBranch = () => Promise.resolve("1.2.3");
auto.git!.getLatestRelease = () => Promise.resolve("1.2.3");
Expand Down
29 changes: 24 additions & 5 deletions packages/core/src/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export default class Auto {
options: ApiOptions;
/** The branch auto uses as master. */
baseBranch: string;
/** The remote git to push changes to */
/** The remote git to push changes to. This is the full URL with auth */
remote!: string;
/** The user configuration of auto (.autorc) */
config?: LoadedAutoRc;
Expand Down Expand Up @@ -1187,8 +1187,22 @@ export default class Auto {

await this.setGitUser();

const lastRelease = await this.git.getLatestRelease();
const lastTag = await this.git.getLatestTagInBranch();
const currentBranch = getCurrentBranch();
const initialForkCommit = (
(
await execPromise("git", [
"rev-list",
"--boundary",
`${currentBranch}...origin/${this.baseBranch}`,
"--left-only",
])
)
.split("\n")
.filter((line) => line.startsWith("-"))[0] || ""
).slice(1);
const lastRelease =
initialForkCommit || (await this.git.getLatestRelease());
const lastTag = await this.git.getLastTagNotInBaseBranch(currentBranch!);
const commits = await this.release.getCommitsInRelease(lastTag);
const releaseNotes = await this.release.generateReleaseNotes(lastTag);
const labels = commits.map((commit) => commit.labels);
Expand All @@ -1198,9 +1212,14 @@ export default class Auto {

if (options.dryRun) {
this.logger.log.success(
`Would have created prerelease version with: ${bump}`
`Would have created prerelease version with: ${bump} from ${lastTag}`
);

this.logger.log.info("Full Release notes for next release:");
console.log(await this.release.generateReleaseNotes(lastRelease));
this.logger.log.info("Release notes for last change in next release");
console.log(releaseNotes);

return { newVersion: "", commitsInRelease: commits, context: "next" };
}

Expand Down Expand Up @@ -1549,7 +1568,7 @@ export default class Auto {
const bump = await this.release.getSemverBump(lastRelease, to);
const releaseNotes = await this.release.generateReleaseNotes(
lastRelease,
to || undefined,
to,
this.versionBump
);

Expand Down
25 changes: 20 additions & 5 deletions packages/core/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { ILabelDefinition } from "./release";
import verifyAuth from "./utils/verify-auth";
import execPromise from "./utils/exec-promise";
import { dummyLog, ILogger } from "./utils/logger";
import { gt } from "semver";
import { gt, lt } from "semver";
import { ICommit } from "./log-parse";
import { buildSearchQuery, ISearchResult } from "./match-sha-to-pr";

Expand Down Expand Up @@ -822,14 +822,21 @@ export default class Git {
.filter(Boolean);
}

/** Get the last tag that isn't in the base branch */
async getLastTagNotInBaseBranch(branch: string) {
/** Get the a tag that isn't in the base branch */
async getTagNotInBaseBranch(
branch: string,
options: {
/** Return the first tag not in baseBrach, defaults to last tag. */
first?: boolean;
} = {}
) {
const baseTags = (
await this.getTags(`origin/${this.options.baseBranch}`)
).reverse();
const branchTags = (await this.getTags(`heads/${branch}`)).reverse();
const comparator = options.first ? lt : gt;
const firstGreatestUnique = branchTags.reduce((result, tag) => {
if (!baseTags.includes(tag) && (!result || gt(tag, result))) {
if (!baseTags.includes(tag) && (!result || comparator(tag, result))) {
return tag;
}

Expand All @@ -838,11 +845,19 @@ export default class Git {

this.logger.verbose.info("Tags found in base branch:", baseTags);
this.logger.verbose.info("Tags found in branch:", branchTags);
this.logger.verbose.info("Latest tag in branch:", firstGreatestUnique);
this.logger.verbose.info(
`${options.first ? "First" : "Latest"} tag in branch:`,
firstGreatestUnique
);

return firstGreatestUnique;
}

/** Get the last tag that isn't in the base branch */
async getLastTagNotInBaseBranch(branch: string) {
return this.getTagNotInBaseBranch(branch);
}

/** Determine the pull request for a commit hash */
async matchCommitToPr(sha: string) {
const query = buildSearchQuery(this.options.owner, this.options.repo, [
Expand Down

0 comments on commit b4dd1cb

Please sign in to comment.