Skip to content

Commit

Permalink
Merge branch 'master' into ts-v5
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 authored Mar 19, 2023
2 parents 7fb2775 + babc8eb commit 48afb55
Show file tree
Hide file tree
Showing 39 changed files with 601 additions and 368 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = {
},
overrides: [
{
files: ['*.ts', '*.tsx'],
files: ['*.ts', '*.mts', '*.cts', '*.tsx'],
extends: [
'standard-with-typescript',
'standard-jsx',
Expand Down
89 changes: 89 additions & 0 deletions .github/scripts/milestone.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import * as core from '@actions/core';
import * as github from '@actions/github';
import type { components } from '@octokit/openapi-types';

import packageJSON from '../../package.json';

type Milestone = components['schemas']['nullable-milestone'];

const { version } = packageJSON;

const repo = {
owner: 'bangumi',
repo: 'frontend',
};

async function main() {
const githubToken = process.env.GH_TOKEN;
if (!githubToken) {
throw new Error('process.env.GH_TOKEN is empty');
}

const octokit = github.getOctokit(githubToken);

const milestones = await octokit.paginate('GET /repos/{owner}/{repo}/milestones', { ...repo });

let oldNextMilestone: undefined | Milestone;

for (const milestone of milestones) {
if (milestone.title === 'next') {
oldNextMilestone = milestone;
break;
}
}

if (!oldNextMilestone) {
core.info('missing previous `next` milestone, skipping script');
return;
}

const openIssues = await octokit.paginate('GET /repos/{owner}/{repo}/issues', {
...repo,
state: 'open',
milestone: oldNextMilestone.number.toString(),
});

core.info(`close and rename old milestone ${oldNextMilestone.number}`);
await octokit.request('PATCH /repos/{owner}/{repo}/milestones/{milestone_number}', {
...repo,
milestone_number: oldNextMilestone.number,
title: version,
state: 'closed',
description:
'# script is still moving issues, DO NOT EDIT IT NOW\n' +
(oldNextMilestone.description ?? ''),
});

core.info(`create new next milestone`);
const newNextMileStone = await octokit.request('POST /repos/{owner}/{repo}/milestones', {
...repo,
title: 'next',
description: '# script is still moving issues, DO NOT EDIT IT NOW',
});

for (const issue of openIssues) {
core.info(`moving issue ${issue.number} to new milestone ${newNextMileStone.data.number}`);
await octokit.request('PATCH /repos/{owner}/{repo}/issues/{issue_number}', {
...repo,
issue_number: issue.number,
milestone: newNextMileStone.data.number,
});
}

core.info(`restore issue description`);
await octokit.request('PATCH /repos/{owner}/{repo}/milestones/{milestone_number}', {
...repo,
milestone_number: oldNextMilestone.number,
description: oldNextMilestone.description ?? '',
});

core.info(`update new issue next title`);
await octokit.request('PATCH /repos/{owner}/{repo}/milestones/{milestone_number}', {
...repo,
milestone_number: newNextMileStone.data.number,
title: 'next',
description: 'milestone for next release',
});
}

await main();
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
const fs = require('node:fs');
const path = require('node:path');
/* eslint-disable @typescript-eslint/no-use-before-define */

const { exec } = require('@actions/exec');
const github = require('@actions/github');
const { context } = require('@actions/github');
import * as fs from 'node:fs';
import * as path from 'node:path';

const artifacts = {
import { exec } from '@actions/exec';
import { context } from '@actions/github';
import * as github from '@actions/github';
import type { GitHub } from '@actions/github/lib/utils';

type Client = InstanceType<typeof GitHub>;

const artifacts: Record<string, string> = {
Build: 'sites',
'Storybook Build': 'storybook',
};
Expand All @@ -24,11 +29,15 @@ async function main() {
throw new Error('process.env.workflow_name is empty');
}

if (!Object.keys(artifacts).includes(workflowName)) {
const artifact = artifacts[workflowName];
if (!artifact) {
throw new Error(`not valid workflow name ${workflowName}`);
}

const artifact = artifacts[workflowName];
const RUN_ID = process.env.RUN_ID;
if (!RUN_ID) {
throw new Error('process.env.RUN_ID is empty');
}

await exec(
'gh',
Expand All @@ -37,15 +46,15 @@ async function main() {
`${github.context.repo.owner}/${github.context.repo.repo}`,
'run',
'download',
`${process.env.RUN_ID}`,
RUN_ID,
'--name',
artifact,
'--dir',
artifact,
],
{
env: {
GH_TOKEN: process.env.GH_TOKEN,
GH_TOKEN: process.env.GH_TOKEN ?? '',
},
},
);
Expand All @@ -56,37 +65,44 @@ async function main() {

await exec('netlify', ['deploy', `--dir=${artifact}`, `--alias="${alias}"`], {
env: {
NETLIFY_AUTH_TOKEN: process.env.NETLIFY_AUTH_TOKEN,
NETLIFY_SITE_ID: process.env.NETLIFY_SITE_ID,
PATH: process.env.PATH,
NETLIFY_AUTH_TOKEN: process.env.NETLIFY_AUTH_TOKEN ?? '',
NETLIFY_SITE_ID: process.env.NETLIFY_SITE_ID ?? '',
PATH: process.env.PATH ?? '',
},
});

for await (const { data: comments } of octokit.paginate.iterator(
octokit.rest.issues.listComments,
const comments = await octokit.paginate(
'GET /repos/{owner}/{repo}/issues/{issue_number}/comments',
{
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
},
)) {
for (const comment of comments) {
if (comment.user.login === 'github-actions[bot]' && comment.body.includes(commentComment)) {
return await updateComment(octokit, comment, artifact, alias);
}
);

for (const comment of comments) {
if (comment.user?.login === 'github-actions[bot]' && comment.body?.includes(commentComment)) {
return updateComment(
octokit,
{
id: comment.id,
body: comment.body ?? '',
},
artifact,
alias,
);
}
}

await createComment(octokit, prNumber, artifact, alias);
}

/**
* @param {InstanceType<typeof GitHub>} octokit
* @param {string} artifact
* @param {string} alias
* @param {{id:number;body:string;}} comment
*/
async function updateComment(octokit, comment, artifact, alias) {
async function updateComment(
octokit: Client,
comment: { id: number; body: string },
artifact: string,
alias: string,
) {
const links = [];
const s = comment.body.split('\n').filter(Boolean);

Expand All @@ -104,22 +120,16 @@ async function updateComment(octokit, comment, artifact, alias) {

links.unshift(commentComment, '# Preview Deployment');

await octokit.rest.issues.updateComment({
await octokit.request('POST /repos/{owner}/{repo}/issues/comments', {
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment.id,
body: links.join('\n'),
});
}

/**
* @param {InstanceType<typeof GitHub>} octokit
* @param {number} prNumber
* @param {string} artifact
* @param {string} alias
*/
async function createComment(octokit, prNumber, artifact, alias) {
await octokit.rest.issues.createComment({
async function createComment(octokit: Client, prNumber: number, artifact: string, alias: string) {
await octokit.request('POST /repos/{owner}/{repo}/issues/comments', {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
Expand All @@ -131,17 +141,12 @@ async function createComment(octokit, prNumber, artifact, alias) {
});
}

/**
* @param {string} s
*/
function toTitle(s) {
function toTitle(s: string) {
if (!s) {
return '';
}

return s[0].toUpperCase() + s.slice(1).toLowerCase();
return (s[0]?.toUpperCase() ?? '') + s.slice(1).toLowerCase();
}

main().catch((e) => {
throw e;
});
await main();
2 changes: 2 additions & 0 deletions .github/workflows/e2e-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ on:
branches: [master]
paths-ignore:
- '**.md'
- '.github/scripts/**'
pull_request:
branches: [master]
paths-ignore:
- '**.md'
- '.github/scripts/**'

jobs:
test:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/preview.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- uses: actions/checkout@v3
- uses: ./.github/actions/setup-js-env

- run: node ./.github/scripts/upload-preview.cjs
- run: npx tsx ./.github/scripts/upload-preview.mts
env:
workflow_name: '${{ github.event.workflow.name }}'
GH_TOKEN: ${{ github.token }}
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,14 @@ jobs:
run: gh release create "$TAG" bangumi-website.tar.gz bangumi-website.tar.gz.sha256
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

milestone:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/setup-js-env

- name: Update github milestone
run: npx tsx .github/scripts/milestone.mts
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ git push -u origin <YOUR_BRANCH>

- PR 的标题需要满足 [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) 规范的要求
- 尚未完成的 PR 请在标题中添加 `[WIP]`,或者设置为 draft 状态。
- ci 会对 PR 进行构建并进行部署,如果涉及 UI 修改,在预览上传成功后请修改 PR 说明添加预览 URL。

### 在合并之前

Expand Down
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dev": "pnpm dev:csr",
"dev:csr": "pnpm website dev",
"dev:ssr": "pnpm server dev",
"lint": "eslint ./ .github/scripts --ext cjs,mjs,js,jsx,ts,tsx",
"lint": "eslint ./ .github/scripts --ext cjs,mjs,js,jsx,ts,mts,cts,tsx",
"lint:fix": "pnpm lint --fix",
"lint:style": "stylelint \"./packages/**/*.{css,less}\"",
"lint:style-fix": "pnpm lint:style --fix",
Expand Down Expand Up @@ -41,20 +41,22 @@
"trailingComma": "all"
},
"lint-staged": {
"*.{js,ts,tsx,cjs,mjs}": [
"*.{js,ts,mts,cts,tsx,cjs,mjs}": [
"eslint --fix"
],
"*.{css,less}": [
"stylelint --fix"
],
"*.{md,html,js,ts,tsx,css,less,cjs,mjs,yaml,yml,json}": "prettier --write"
"*.{md,html,js,ts,mts,cts,tsx,css,less,cjs,mjs,yaml,yml,json}": "prettier --write"
},
"devDependencies": {
"@actions/core": "^1.10.0",
"@actions/exec": "^1.1.1",
"@actions/github": "^5.1.1",
"@babel/core": "^7.21.3",
"@babel/preset-env": "^7.20.2",
"@babel/preset-typescript": "^7.21.0",
"@octokit/openapi-types": "^16.0.0",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
"@types/node": "^18.15.3",
Expand Down Expand Up @@ -88,10 +90,12 @@
"prettier": "^2.8.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"stylelint": "^15.2.0",
"stylelint": "^15.3.0",
"stylelint-config-css-modules": "^4.2.0",
"stylelint-config-standard": "^30.0.1",
"stylelint-config-standard": "^31.0.0",
"stylelint-scss": "^4.5.0",
"timezone-mock": "^1.3.6",
"tsx": "^3.12.5",
"typescript": "^5.0.2",
"vite": "^4.2.0",
"vite-plugin-svgr": "^2.4.0",
Expand Down
Loading

0 comments on commit 48afb55

Please sign in to comment.