Skip to content

Commit

Permalink
chore(ci): integrate changeset & add github action to publish (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Col0ring authored Mar 7, 2024
1 parent 0cb2cff commit 7910ebf
Show file tree
Hide file tree
Showing 26 changed files with 1,829 additions and 133 deletions.
8 changes: 8 additions & 0 deletions .changeset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changesets

Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
with multi-package repos, or single-package repos to help you version and publish your code. You can
find the full documentation for it [in our repository](https://github.com/changesets/changesets)

We have a quick list of common questions to get you started engaging with this project in
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
14 changes: 14 additions & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": [
"@modelscope-studio/changelog",
{ "repo": "modelscope/modelscope-studio" }
],
"commit": false,
"fixed": [],
"linked": [],
"access": "restricted",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
}
4 changes: 2 additions & 2 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python 3.10
- name: Set up Python 3.9
uses: actions/setup-python@v5
with:
python-version: '3.10'
python-version: '3.9'
- name: Install Python Dependencies
run: pip install flake8 isort yapf
- name: Setup Node.js
Expand Down
53 changes: 53 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: publish

on:
push:
branches:
- main
env:
CI: true

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-publish
cancel-in-progress: true

jobs:
build-n-publish:
timeout-minutes: 15
runs-on: ubuntu-latest
steps:
- name: Checkout Code Repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python 3.9
uses: actions/setup-python@v5
with:
python-version: '3.9'
- name: Install Python Dependencies
run: pip install gradio twine build
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 16
- name: Install Pnpm
run: npm i pnpm@latest -g
- name: Install Node Dependencies
run: pnpm install
- name: Create Release Pull Request to Update Versions
id: changesets
uses: changesets/action@v1
with:
version: pnpm run ci:version
title: 'chore: update versions'
commit: 'chore: update versions'
publish: pnpm run ci:create-tag-n-release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.event.repository.name }}
OWNER: ${{ github.repository_owner }}
- name: Build Packages and Publish to PyPI
if: steps.changesets.outputs.hasChangesets != 'true'
run: pnpm run ci:publish
env:
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
31 changes: 31 additions & 0 deletions config/changelog/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@modelscope-studio/changelog",
"version": "0.1.0",
"private": false,
"description": "ModelScope Studio Changelog",
"repository": "[email protected]:modelscope/modelscope-studio.git",
"license": "Apache-2.0",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs",
"types": "./dist/index.d.ts"
}
},
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsup"
},
"dependencies": {
"@changesets/get-github-info": "^0.6.0",
"@manypkg/get-packages": "^2.2.0",
"detect-indent": "^7.0.1"
},
"devDependencies": {
"@changesets/types": "^6.0.0",
"@types/node": "^20.11.24",
"tsup": "^8.0.2"
}
}
118 changes: 118 additions & 0 deletions config/changelog/src/fix-changelog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { getPackagesSync, type Package } from '@manypkg/get-packages';
import detectIndent from 'detect-indent';
import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'fs';
import { join } from 'path';

import { ChangesetMeta, ChangesetMetaCollection } from './type';

const pkg_meta = getPackagesSync(process.cwd());

function updatePackageJson(pkgJsonPath: string, pkgJson: any) {
const pkgRaw = readFileSync(pkgJsonPath, 'utf-8');
const indent = detectIndent(pkgRaw).indent || ' ';
const stringified =
JSON.stringify(pkgJson, null, indent) + (pkgRaw.endsWith('\n') ? '\n' : '');

writeFileSync(pkgJsonPath, stringified);
}

function run() {
if (!existsSync(join(pkg_meta.rootDir, '.changeset', '_changelog.json'))) {
console.warn('No changesets to process');
return;
}

const { _handled, ...packages } = JSON.parse(
readFileSync(
join(pkg_meta.rootDir, '.changeset', '_changelog.json'),
'utf-8'
)
) as ChangesetMetaCollection;

const all_packages = pkg_meta.packages.reduce(
(acc, pkg) => {
acc[pkg.packageJson.name] = pkg;
return acc;
},
{} as Record<string, Package>
);

const version = pkg_meta.rootPackage?.packageJson.version;
if (!version) {
throw new Error('No root package version found');
}

for (const pkg_name in packages) {
const { dir, chore, feat, fix, current_changelog } = packages[
pkg_name
] as ChangesetMeta;

const features = feat.map((f) => `- ${f.summary}`);
const fixes = fix.map((f) => `- ${f.summary}`);
const others = chore.map((f) => `- ${f.summary}`);

const release_notes = (
[
[features, '### Features'],
[fixes, '### Fixes'],
[others, '### Misc Changes'],
] as const
)
.filter(([s]) => s.length > 0)
.map(([lines, title]) => {
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();
writeFileSync(join(dir, 'CHANGELOG.md'), new_changelog);
}

bump_packages(version);

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

function bump_packages(newVersion: string) {
for (const pkg_name in all_packages) {
const { dir, packageJson } = all_packages[pkg_name];
const newPackageJson = {
...packageJson,
};
newPackageJson.version = newVersion;
updatePackageJson(join(dir, 'package.json'), newPackageJson);
if (packages[pkg_name]) {
continue;
}
const current_changelog_path = join(dir, 'CHANGELOG.md');
const current_changelog = existsSync(current_changelog_path)
? readFileSync(current_changelog_path, 'utf-8')
: '';

const new_changelog = `# ${pkg_name}
## ${version}
No significant changes to this package were made in this release.
${current_changelog.replace(`# ${pkg_name}`, '').trim()}
`.trim();
writeFileSync(join(dir, 'CHANGELOG.md'), new_changelog);
}

const pyproject_path = join(pkg_meta.rootDir, 'pyproject.toml');
const pyproject = readFileSync(pyproject_path, 'utf-8');
writeFileSync(
pyproject_path,
pyproject.replace(/version = ".+"/, `version = "${newVersion}"`)
);
}
}

run();
Loading

0 comments on commit 7910ebf

Please sign in to comment.