From bdc02c29d5390d5a3af78d3d37d05b94952622d2 Mon Sep 17 00:00:00 2001 From: rogerogers Date: Fri, 21 Jun 2024 14:15:33 +0800 Subject: [PATCH 1/2] feat: add pr action Signed-off-by: rogerogers --- .github/workflows/pr-actions.yml | 107 +++++++++++++++++++++++++++++++ .gitignore | 2 + .nvmrc | 1 + package.json | 21 +++--- 4 files changed, 122 insertions(+), 9 deletions(-) create mode 100644 .github/workflows/pr-actions.yml create mode 100644 .nvmrc diff --git a/.github/workflows/pr-actions.yml b/.github/workflows/pr-actions.yml new file mode 100644 index 00000000000..fba430e4ef6 --- /dev/null +++ b/.github/workflows/pr-actions.yml @@ -0,0 +1,107 @@ +name: PR actions + +on: + issue_comment: + types: [created] + +env: + GH_TOKEN: ${{ github.token }} + COMMENT: ${{ github.event.comment.body }} + PR_NUM: ${{ github.event.issue.number }} + USER_EMAIL: 79236453+cloudwego@users.noreply.github.com + USER_NAME: cloudwegobot + +jobs: + pr-action: + name: Run PR action + runs-on: ubuntu-latest + + if: | + github.event.issue.pull_request && + startsWith(github.event.comment.body, '/fix:') + permissions: + contents: write + pull-requests: write + + steps: + - name: Extract action name + id: extract_action_name + run: | + PR_ACTION=$(echo $COMMENT | grep -oP '/fix:\K\w+') + echo "Action is $PR_ACTION" + ACTION_NAMES="all|refcache|filenames|format" + if [[ ! "$PR_ACTION" =~ ^($ACTION_NAMES)$ ]]; then + echo "Invalid action name: $PR_ACTION" + echo "Action name should be one of: $ACTION_NAMES" + exit 1 + fi + echo "PR_ACTION=$PR_ACTION" >> "$GITHUB_ENV" + + - name: Context info + run: | + echo $PR_NUM + echo $COMMENT + + - uses: actions/checkout@v4 + + - name: Write start comment + run: | + gh pr comment $PR_NUM -b "You triggered fix:${PR_ACTION} action run at $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - run: gh pr checkout $PR_NUM -b "pr-action-${RANDOM}" + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Create NPM cache-hash input file + run: | + mkdir -p tmp + jq '{devDependencies, dependencies, engines, gitHubActionCacheKey}' package.json > tmp/package-ci.json + + - uses: actions/setup-node@v4 + with: + node-version-file: .nvmrc + cache: npm + cache-dependency-path: tmp/package-ci.json + + - run: | + case $PR_ACTION in + all|refcache) + npm install --omit=optional + ;& + filenames|format) + npm run fix:$PR_ACTION + ;; + esac + git status + git branch -v + + - name: Commit and push changes, if any + run: | + git config --local user.email "$USER_EMAIL" + git config --local user.name "$USER_NAME" + if [[ $(git status --porcelain) ]]; then + git add -A + current_branch=$(git rev-parse --abbrev-ref HEAD) + echo current_branch=$current_branch + # gh pr checkout sets some git configs that we can use to make sure + # we push to the right repo & to the right branch + remote_repo=$(git config --get branch.${current_branch}.remote) + echo remote_repo=$remote_repo + remote_branch=$(git config --get branch.${current_branch}.merge) + echo remote_branch=$remote_branch + git commit -m "Results from /fix:${PR_ACTION}" + git push ${remote_repo} HEAD:${remote_branch} + else + echo "No changes to commit" + fi + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Report an error in the case of failure + if: ${{ failure() || cancelled() }} + run: | + gh pr comment $PR_NUM -b "fix:${PR_ACTION} run failed, please check $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID for details" + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index a28243342ad..0bb8ef2ebb2 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,5 @@ tech-doc-hugo # lock file package-lock.json + +tmp/ diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 00000000000..9a2a0e219c9 --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +v20 diff --git a/package.json b/package.json index cc3a02a4cbc..5eaba284c2d 100644 --- a/package.json +++ b/package.json @@ -5,11 +5,9 @@ "main": "none.js", "scripts": { "dev": "hugo server -D", - "postinstall": "simple-git-hooks", - "format": "node scripts/format/index.mjs" - }, - "simple-git-hooks": { - "pre-commit": "npm run format" + "format": "node scripts/format/index.mjs", + "fix:format": "npx prettier -w content", + "fix:refcache": "" }, "repository": { "type": "git", @@ -22,13 +20,18 @@ }, "homepage": "https://github.com/cloudwego/cloudwego.github.io#readme", "devDependencies": { - "autoprefixer": "^10.4.0", - "postcss": "^8.4.27", - "postcss-cli": "^10.1.0", "autocorrect-node": "^2.8.2", + "autoprefixer": "^10.4.0", "chalk": "^5.3.0", "jsonc-parser": "^3.2.0", "markdownlint": "^0.29.0", + "postcss": "^8.4.27", + "postcss-cli": "^10.1.0", + "prettier": "^3.3.2", "simple-git-hooks": "^2.9.0" - } + }, + "engines": { + "node": "20.x" + }, + "gitHubActionCacheKey": "2024-06-21 - change this key to force cache refresh" } From 800901d4f59d5ace1c58ab773c77e88681f0b791 Mon Sep 17 00:00:00 2001 From: rogerogers Date: Sat, 22 Jun 2024 13:25:34 +0800 Subject: [PATCH 2/2] chore: update commit username Signed-off-by: rogerogers --- .github/workflows/pr-actions.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pr-actions.yml b/.github/workflows/pr-actions.yml index fba430e4ef6..620518ad673 100644 --- a/.github/workflows/pr-actions.yml +++ b/.github/workflows/pr-actions.yml @@ -8,8 +8,8 @@ env: GH_TOKEN: ${{ github.token }} COMMENT: ${{ github.event.comment.body }} PR_NUM: ${{ github.event.issue.number }} - USER_EMAIL: 79236453+cloudwego@users.noreply.github.com - USER_NAME: cloudwegobot + USER_EMAIL: 90381261+alice-yyds@users.noreply.github.com + USER_NAME: alice-yyds jobs: pr-action: