Skip to content
Draft
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
32 changes: 25 additions & 7 deletions scripts/release-info.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@auto-it/core");

/**
* Auto plugin that posts a PR comment with version information when a release is created
*/
Expand All @@ -11,6 +12,7 @@ class ReleaseInfo {
this.context = options.context || "Release Info";
this.notes = options.notes || {};
}

/**
* Get the appropriate message based on the release context
* @param version The version string
Expand All @@ -20,6 +22,7 @@ class ReleaseInfo {
getVersionMessage(version, releaseContext) {
const currentDate = new Date().toUTCString();
let versionMessage = `### ${this.context}\n\n`;

// Customize message based on release context
switch (releaseContext) {
case "canary":
Expand All @@ -35,57 +38,72 @@ class ReleaseInfo {
versionMessage += `A new version was released on \`${currentDate}\`:\n\n`;
break;
}

versionMessage += "```\n";
versionMessage += `${version}\n`;
versionMessage += "```";

// Add the context-specific note if provided
const note = this.notes[releaseContext] || this.notes.default;
if (note) {
versionMessage += "\n\n";
versionMessage += note;
}

return versionMessage;
}

/** Apply the plugin to the Auto instance */
apply(auto) {
// Handle all releases through afterShipIt hook
auto.hooks.afterShipIt.tap(this.name, async (release) => {
const { newVersion, context: releaseContext } = release;

if (!newVersion) {
auto.logger.verbose.info(
"No release version produced, skipping comment",
);
return;
}

auto.logger.verbose.info(
`Processing ${releaseContext} release with version ${newVersion}`,
);

// Get the appropriate message for this release context
const message = this.getVersionMessage(newVersion, releaseContext);

// Check if we're in a PR context before attempting to comment
const prNumber = (0, core_1.getPrNumberFromEnv)();

if (!prNumber) {
auto.logger.verbose.info(
"Auto shipit was triggered outside of a PR context, skipping comment",
);
auto.logger.verbose.info("Not in PR context, skipping comment");
return;
}

if (!auto.comment) {
auto.logger.verbose.info("Auto comment not available, skipping");
return;
}
// We have auto.comment, so we're in a PR context

// Try to post comment
try {
await auto.comment({
message,
context: this.context,
});
auto.logger.verbose.info("Successfully posted version comment");
} catch (error) {
auto.logger.verbose.info("Error posting comment to PR:");
auto.logger.verbose.info("Comment posting failed, continuing build");
if (error instanceof Error) {
auto.logger.verbose.info(error.message);
} else {
auto.logger.verbose.info(String(error));
}
}
});
}
}

// Export in multiple formats for compatibility with different module loaders
exports.default = ReleaseInfo;
module.exports = ReleaseInfo;
module.exports.default = ReleaseInfo;