Skip to content

Commit

Permalink
Merge pull request #129 from zowe-actions/fix/allow-first-time-publish2
Browse files Browse the repository at this point in the history
Fix wrong independent version on first time publish
  • Loading branch information
zFernand0 committed Feb 5, 2024
2 parents c8cbcd5 + 0345062 commit 3ee426d
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 34 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.ref }}

- name: Use Node.js 16
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 16.x

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Use Node.js LTS
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 'lts/*'

Expand All @@ -38,7 +38,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0

Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ inputs:
default: ''
# outputs:
runs:
using: 'node16'
using: 'node20'
main: 'dist/index.js'
5 changes: 4 additions & 1 deletion dist/changelog.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions dist/core.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion dist/github.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 25 additions & 7 deletions dist/lerna.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions packages/changelog/src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,14 @@ function getPackageChangelog(context: IContext, changelogFile: string, headerLin
}

function updatePackageChangelog(context: IContext, changelogFile: string, headerLine: string): boolean {
if (context.version.new !== context.version.old && fs.existsSync(changelogFile)) {
if (fs.existsSync(changelogFile)) {
const oldContents = fs.readFileSync(changelogFile, "utf-8");
const newVersion = context.version.overrides[path.dirname(changelogFile)]?.new || context.version.new;
const newContents = oldContents.replace(headerLine, `## \`${newVersion}\``);
if (oldContents.includes(`## \`${newVersion}\``)) {
return false;
}

const newContents = oldContents.replace(headerLine, `## \`${newVersion}\``);
if (newContents !== oldContents) {
fs.writeFileSync(changelogFile, newContents);
context.logger.info(`Updated version header in ${changelogFile}`)
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,17 @@ export async function verifyConditions(context: IContext): Promise<void> {
}

const semver = require("semver");
const semverLevel = semver.diff(context.version.old.split("-")[0], context.version.new.split("-")[0]);
const semverLevel = context.version.old !== "0.0.0" ?
semver.diff(context.version.old.split("-")[0], context.version.new.split("-")[0]) : null;
for (const versionInfo of Object.values(context.version.overrides)) {
versionInfo.new = semver.inc(versionInfo.old.split("-")[0], semverLevel);
versionInfo.new = semverLevel != null ?
semver.inc(versionInfo.old.split("-")[0], semverLevel) : versionInfo.old;
if (versionInfo.prerelease != null) {
versionInfo.new = `${versionInfo.new}-${versionInfo.prerelease}`;
}
}

if (semverLevel != null && context.branch.level != null && context.version.old != "0.0.0" &&
if (semverLevel != null && context.branch.level != null &&
SemverDiffLevels.indexOf(semverLevel) > SemverDiffLevels.indexOf(context.branch.level)) {
throw new Error(`Protected branch ${context.branch.name} does not allow ${semverLevel} version changes`);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/github/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export default async function (context: IContext, config: IPluginConfig): Promis

if (config.checkPrLabels && Inputs.newVersion == null) {
const releaseType = await getPrReleaseType(context, config);
const oldVersion = (context.version.new || context.version.old).split("-")[0];
context.version.old = context.version.new || context.version.old;
const oldVersion = context.version.old.split("-")[0];
context.version.new = releaseType != null ? require("semver").inc(oldVersion, releaseType) : oldVersion;
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/lerna/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

import { IPluginConfig as NpmPluginConfig } from "@octorelease/npm";

export const IS_LERNA_JSON_TEMP = Symbol();

export type IPluginConfig = NpmPluginConfig & {
pruneShrinkwrap?: boolean | string[];
versionIndependent?: string[];
[IS_LERNA_JSON_TEMP]?: boolean;
};
24 changes: 17 additions & 7 deletions packages/lerna/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ import * as fs from "fs";
import * as path from "path";
import { IContext } from "@octorelease/core";
import { DEFAULT_NPM_REGISTRY, utils as npmUtils } from "@octorelease/npm";
import { IPluginConfig } from "./config";
import { IPluginConfig, IS_LERNA_JSON_TEMP } from "./config";
import * as utils from "./utils";

export default async function (context: IContext, config: IPluginConfig): Promise<void> {
let publishConfig;
try {
const lernaJson = JSON.parse(fs.readFileSync("lerna.json", "utf-8"));
context.version.new = lernaJson.version;
publishConfig = lernaJson.command?.publish;
} catch {
throw new Error(`Missing or invalid lerna.json in branch ${context.branch.name}`);
config[IS_LERNA_JSON_TEMP] = !fs.existsSync("lerna.json");
if (!config[IS_LERNA_JSON_TEMP]) {
try {
const lernaJson = JSON.parse(fs.readFileSync("lerna.json", "utf-8"));
context.version.new = lernaJson.version;
publishConfig = lernaJson.command?.publish;
} catch {
throw new Error(`Missing or invalid lerna.json in branch ${context.branch.name}`);
}
}

try {
Expand All @@ -37,6 +40,13 @@ export default async function (context: IContext, config: IPluginConfig): Promis
if (publishConfig == null) {
publishConfig = packageJson.publishConfig;
}
if (config[IS_LERNA_JSON_TEMP]) {
context.version.new = packageJson.version;
fs.writeFileSync("lerna.json", JSON.stringify({
version: packageJson.version,
useWorkspaces: true
}, null, 2));
}
} catch {
throw new Error(`Missing or invalid package.json in branch ${context.branch.name}`);
}
Expand Down
7 changes: 5 additions & 2 deletions packages/lerna/src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import findUp from "find-up";
import * as glob from "@actions/glob";
import { IContext } from "@octorelease/core";
import { utils as npmUtils } from "@octorelease/npm";
import { IPluginConfig } from "./config";
import { IPluginConfig, IS_LERNA_JSON_TEMP } from "./config";
import * as utils from "./utils";

export default async function (context: IContext, config: IPluginConfig): Promise<void> {
Expand All @@ -47,7 +47,10 @@ export default async function (context: IContext, config: IPluginConfig): Promis
}

await utils.lernaPostVersion(); // Update lockfile because lerna doesn't
context.changedFiles.push("lerna.json", "package.json");
context.changedFiles.push("package.json");
if (!config[IS_LERNA_JSON_TEMP]) {
context.changedFiles.push("lerna.json");
}
const lockfilePath = await findUp(["pnpm-lock.yaml", "yarn.lock", "npm-shrinkwrap.json", "package-lock.json"]);
if (lockfilePath != null) {
context.changedFiles.push(path.relative(context.rootDir, lockfilePath));
Expand Down
2 changes: 1 addition & 1 deletion script/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ outputs:
result:
description: 'The output of the script for other steps to reference'
runs:
using: 'node16'
using: 'node20'
main: '../dist/run-script.js'

0 comments on commit 3ee426d

Please sign in to comment.