Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(rules_task): Use custom semantic release plugin to release rules_task #587

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion rules/rules_task/.releaserc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
"name": "master"
}
],
"extends": "semantic-release-monorepo",
"tagFormat": "rules_task-v${version}",
"extends": [
"./commit-paths.js"
],
"commitPaths": [
"rules/rules_task/*"
],
"plugins": [
[
"@semantic-release/commit-analyzer",
Expand Down
95 changes: 95 additions & 0 deletions rules/rules_task/commit-paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const gitLogParser = require("git-log-parser");
const oldParse = gitLogParser.parse;
const { execSync } = require("child_process");
const { wrap } = require("module");

let commitPaths = null;

// Copied from https://github.com/pmowrer/semantic-release-plugin-decorators/blob/master/src/wrapStep.js
// This executes the "stepName" method of all plugins and afterwards executes the "wrapFn" method
const wrapStep = (
stepName,
wrapFn,
{ defaultReturn = undefined, wrapperName = "" } = {}
) => {
const wrapperFn = async function (globalPluginConfig, context) {
const {
options: { plugins },
} = context;

for (const pluginDefinition of plugins) {
const [pluginName, pluginConfig] = Array.isArray(pluginDefinition)
? pluginDefinition
: [pluginDefinition, {}];

if (!pluginName) {
// Still needed ?
context.logger.log(`Falsy plugin name at index "${index}"`);
continue;
} else if (typeof pluginName !== "string") {
throw new Error(
`${
wrapperName ? wrapperName : "semantic-release-plugin-decorators"
}: Incorrect plugin name type. Expected string but was ${JSON.stringify(
pluginName
)}.`
);
}

const plugin = await import(pluginName);
const step = plugin && plugin[stepName];

if (!step) {
context.logger.log(
`Plugin "${pluginName}" does not provide step "${stepName}"`
);
continue;
}

context.logger.log(`Start step "${stepName}" of plugin "${pluginName}"`);

await step(globalPluginConfig, context);

context.logger.log(
`Completed step "${stepName}" of plugin "${pluginName}"`
);
}

return await wrapFn(globalPluginConfig, context);
};

Object.defineProperty(wrapperFn, "name", { value: wrapperName });

return wrapperFn;
};

// Wrapping the verifyConditions step to get the config so we can globally set the commitPaths for the git-log-parser
const verifyConditions = wrapStep(
"verifyConditions",
(globalPluginConfig) => {
commitPaths = globalPluginConfig["commitPaths"];
},
{
wrapperName: "get-commit-paths",
}
);

gitLogParser.parse = function (config, options) {
// get the git root and change the cwd so all the commitPaths are relative to the git root
const newCwd = execSync("git rev-parse --show-toplevel", {
encoding: "utf-8",
}).trim();
const configValue = config["_"];
const newConfig = {
_: [configValue, "--", ...commitPaths],
};
const newOptions = {
...options,
cwd: newCwd,
};
return oldParse(newConfig, newOptions);
};

module.exports = {
verifyConditions,
};
13 changes: 6 additions & 7 deletions rules/rules_task/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
"author": "",
"license": "ISC",
"dependencies": {
"@semantic-release/commit-analyzer": "9.0.2",
"@semantic-release/exec": "^6.0.3",
"@semantic-release/github": "8.1.0",
"@semantic-release/release-notes-generator": "10.0.3",
"conventional-changelog-conventionalcommits": "^7.0.2",
"semantic-release": "^22.0.5",
"semantic-release-monorepo": "^7.0.5"
"@semantic-release/commit-analyzer": "11.0.0",
"@semantic-release/exec": "6.0.3",
"@semantic-release/github": "9.2.1",
"@semantic-release/release-notes-generator": "12.0.0",
"conventional-changelog-conventionalcommits": "7.0.2",
"semantic-release": "22.0.5"
}
}
Loading