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

unified release process #4986

Merged
merged 9 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
260 changes: 260 additions & 0 deletions .changeset/changeset.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
const { getPackagesSync } = require("@manypkg/get-packages");
const gh = require("@changesets/get-github-info");
const { existsSync, readFileSync, writeFileSync } = require("fs");
const { join } = require("path");

const { getInfo, getInfoFromPullRequest } = gh;
const { packages, rootDir } = getPackagesSync(process.cwd());

/**
* @typedef {{packageJson: {name: string, python?: boolean}, dir: string}} Package
*/

/**
* @typedef {{summary: string, id: string, commit: string, releases: {name: string}}} Changeset
*/

/**
*
* @param {string} package_name
* @returns {[string, string|null]}
*/
function find_packages_dirs(package_name) {
/**
* @type {Package | undefined}
*/
const _package = packages.find((p) => p.packageJson.name === package_name);
if (!_package) throw new Error(`Package ${package_name} not found`);
return [
_package.dir,
_package.packageJson.python ? join(_package.dir, "..") : null
];
}

const changelogFunctions = {
/**
*
* @param {Changeset[]} changesets
* @param {any} dependenciesUpdated
* @param {any} options
* @returns
*/
getDependencyReleaseLine: async (
changesets,
dependenciesUpdated,
options
) => {
if (!options.repo) {
throw new Error(
'Please provide a repo to this changelog generator like this:\n"changelog": ["@changesets/changelog-github", { "repo": "org/repo" }]'
);
}
if (dependenciesUpdated.length === 0) return "";

const changesetLink = `- Updated dependencies [${(
await Promise.all(
changesets.map(async (cs) => {
if (cs.commit) {
let { links } = await getInfo({
repo: options.repo,
commit: cs.commit
});
return links.commit;
}
})
)
)
.filter((_) => _)
.join(", ")}]:`;

const updatedDepenenciesList = dependenciesUpdated.map(
/**
*
* @param {any} dependency
* @returns
*/
(dependency) => ` - ${dependency.name}@${dependency.newVersion}`
);

return [changesetLink, ...updatedDepenenciesList].join("\n");
},
/**
*
* @param {{summary: string, id: string, commit: string, releases: {name: string}[]}} changeset
* @param {any} type
* @param {any} options
* @returns
*/
getReleaseLine: async (changeset, type, options) => {
if (!options || !options.repo) {
throw new Error(
'Please provide a repo to this changelog generator like this:\n"changelog": ["@changesets/changelog-github", { "repo": "org/repo" }]'
);
}

let prFromSummary;
let commitFromSummary;
/**
* @type {string[]}
*/
let usersFromSummary = [];

const replacedChangelog = changeset.summary
.replace(/^\s*(?:pr|pull|pull\s+request):\s*#?(\d+)/im, (_, pr) => {
let num = Number(pr);
if (!isNaN(num)) prFromSummary = num;
return "";
})
.replace(/^\s*commit:\s*([^\s]+)/im, (_, commit) => {
commitFromSummary = commit;
return "";
})
.replace(/^\s*(?:author|user):\s*@?([^\s]+)/gim, (_, user) => {
usersFromSummary.push(user);
return "";
})
.trim();

const [firstLine, ...futureLines] = replacedChangelog
.split("\n")
.map((l) => l.trimRight());

const links = await (async () => {
if (prFromSummary !== undefined) {
let { links } = await getInfoFromPullRequest({
repo: options.repo,
pull: prFromSummary
});
if (commitFromSummary) {
links = {
...links,
commit: `[\`${commitFromSummary}\`](https://github.com/${options.repo}/commit/${commitFromSummary})`
};
}
return links;
}
const commitToFetchFrom = commitFromSummary || changeset.commit;
if (commitToFetchFrom) {
let { links } = await getInfo({
repo: options.repo,
commit: commitToFetchFrom
});
return links;
}
return {
commit: null,
pull: null,
user: null
};
})();

const users =
usersFromSummary && usersFromSummary.length
? usersFromSummary
.map(
(userFromSummary) =>
`[@${userFromSummary}](https://github.com/${userFromSummary})`
)
.join(", ")
: links.user;

const prefix = [
links.pull === null ? "" : `${links.pull}`,
links.commit === null ? "" : `${links.commit}`
]
.join(" ")
.trim();

const suffix = users === null ? "" : ` Thanks ${users}!`;

/**
* @typedef {{[key: string]: string[] | {dirs: [string, string|null], current_changelog: string, feat: {summary: string}[], fix: {summary: string}[], highlight: {summary: string}[]}}} ChangesetMeta
*/

/**
* @type { ChangesetMeta & { _handled: string[] } }}
*/
let lines;
if (existsSync(join(rootDir, ".changeset", "_changelog.json"))) {
lines = JSON.parse(
readFileSync(join(rootDir, ".changeset", "_changelog.json"), "utf-8")
);
} else {
lines = {
_handled: []
};
}

if (lines._handled.includes(changeset.id)) {
return "done";
} else {
lines._handled.push(changeset.id);
}

changeset.releases.forEach((release) => {
if (!lines[release.name])
lines[release.name] = {
dirs: find_packages_dirs(release.name),
current_changelog: "",
feat: [],
fix: [],
highlight: []
};

const changelog_path = join(
//@ts-ignore
lines[release.name].dirs[1] || lines[release.name].dirs[0],
"CHANGELOG.md"
);

if (existsSync(changelog_path)) {
//@ts-ignore
lines[release.name].current_changelog = readFileSync(
changelog_path,
"utf-8"
)
.replace(`# ${release.name}`, "")
.trim();
}

const [, _type, summary] = changeset.summary
.trim()
.match(/^(feat|fix|highlight)\s*:\s*([^]*)/im) || [
,
false,
changeset.summary
];

let formatted_summary = "";

if (_type === "highlight") {
const [heading, ...rest] = summary.trim().split("\n");
const _heading = `${heading} ${prefix ? `(${prefix})` : ""}`;
const _rest = rest.concat(["", suffix]);

formatted_summary = `${_heading}\n${_rest.join("\n")}`;
} else {
formatted_summary = `${prefix ? `${prefix} -` : ""} ${summary.replace(
/[\s\.]$/,
""
)}.${suffix}`;
}

//@ts-ignore
lines[release.name][_type].push({
summary: formatted_summary
});
});

writeFileSync(
join(rootDir, ".changeset", "_changelog.json"),
JSON.stringify(lines, null, 2)
);

return `\n\n-${prefix ? `${prefix} -` : ""} ${firstLine}\n${futureLines
.map((l) => ` ${l}`)
.join("\n")}`;
}
};

module.exports = changelogFunctions;
7 changes: 2 additions & 5 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
{
"$schema": "https://unpkg.com/@changesets/config@2.3.0/schema.json",
"changelog": [
"@changesets/changelog-github",
{ "repo": "gradio-app/gradio" }
],
"changelog": ["./changeset.cjs", { "repo": "gradio-app/gradio" }],
"commit": false,
"fixed": [],
"linked": [],
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": ["@gradio/app", "@gradio/spaces-test"]
"ignore": ["@gradio/spaces-test", "@gradio/cdn-test"]
}
111 changes: 111 additions & 0 deletions .changeset/fix_changelogs.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const { join } = require("path");
const { readFileSync, existsSync, writeFileSync, unlinkSync } = require("fs");
const { getPackagesSync } = require("@manypkg/get-packages");

const RE_PKG_NAME = /^[\w-]+\b/;
const pkg_meta = getPackagesSync(process.cwd());

/**
* @typedef {{dirs: string[], highlight: {summary: string}[], feat: {summary: string}[], fix: {summary: string}[], current_changelog: string}} ChangesetMeta
*/

/**
* @typedef {{[key: string]: ChangesetMeta}} ChangesetMetaCollection
*/

/**
* @type { ChangesetMetaCollection & { _handled: string[] } }}
*/
const { _handled, ...packages } = JSON.parse(
readFileSync(join(pkg_meta.rootDir, ".changeset", "_changelog.json"), "utf-8")
);

/**
* @typedef { {packageJson: {name: string, version: string, python: boolean}, dir: string} } PackageMeta
*/

/**
* @type { {[key:string]: PackageMeta} }
*/
const all_packages = pkg_meta.packages.reduce((acc, pkg) => {
acc[pkg.packageJson.name] = /**@type {PackageMeta} */ (
/** @type {unknown} */ (pkg)
);
return acc;
}, /** @type {{[key:string] : PackageMeta}} */ ({}));

for (const pkg_name in packages) {
const { dirs, highlight, feat, fix, current_changelog } =
/**@type {ChangesetMeta} */ (packages[pkg_name]);

const { version, python } = all_packages[pkg_name].packageJson;

const highlights = highlight.map((h) => `${h.summary}`);
const features = feat.map((f) => `- ${f.summary}`);
const fixes = fix.map((f) => `- ${f.summary}`);

const release_notes = /** @type {[string[], string][]} */ ([
[highlights, "### Highlights"],
[features, "### Features"],
[fixes, "### Fixes"]
])
.filter(([s], i) => s.length > 0)
.map(([lines, title]) => {
if (title === "### Highlights") {
return `${title}\n\n${lines.join("\n\n")}`;
} else {
return `${title}\n\n${lines.join("\n")}`;
}
})
.join("\n\n");

const new_changelog = `# ${pkg_name}

## ${version}

${release_notes}

${current_changelog.replace(`# ${pkg_name}`, "").trim()}
`.trim();

dirs.forEach((dir) => {
writeFileSync(join(dir, "CHANGELOG.md"), new_changelog);
});

if (python) {
writeFileSync(join(dirs[0], "version.txt"), version);
bump_local_dependents(pkg_name, version);
}
}

unlinkSync(join(pkg_meta.rootDir, ".changeset", "_changelog.json"));

/**
* @param {string} pkg_to_bump
* @param {string} version
* @returns {void}
* */
function bump_local_dependents(pkg_to_bump, version) {
for (const pkg_name in all_packages) {
const {
dir,
packageJson: { python }
} = all_packages[pkg_name];

if (!python) continue;

const requirements_path = join(dir, "..", "requirements.txt");
const requirements = readFileSync(requirements_path, "utf-8").split("\n");

const pkg_index = requirements.findIndex((line) => {
const m = line.trim().match(RE_PKG_NAME);
if (!m) return false;
return m[0] === pkg_to_bump;
});

if (pkg_index !== -1) {
requirements[pkg_index] = `${pkg_to_bump}>=${version}`;
writeFileSync(requirements_path, requirements.join("\n"));
}
}
}
Loading
Loading