diff --git a/.github/scripts/get_root_directories.js b/.github/scripts/get_root_directories.js new file mode 100644 index 0000000..12b10ed --- /dev/null +++ b/.github/scripts/get_root_directories.js @@ -0,0 +1,30 @@ +const axios = require('axios'); +const fs = require('fs'); + +const PR_NUMBER = process.env.GITHUB_EVENT_PATH ? JSON.parse(fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8')).number : null; +const REPO = process.env.GITHUB_REPOSITORY; +const TOKEN = process.env.GITHUB_TOKEN; + +console.log(`https://api.github.com/repos/${REPO}/pulls/${PR_NUMBER}/files`); + +axios.get(`https://api.github.com/repos/${REPO}/pulls/${PR_NUMBER}/files`, {}) + .then(response => { + const files = response.data; + const root_directories = new Set(); + + files.forEach(file => { + const root_directory = file.filename.split("/")[0]; + root_directories.add(root_directory); + }); + + if (root_directories.size > 1) { + throw new Error("Multiple root directories changed, action failed."); + } + + const root_dir = [...root_directories][0]; + console.log(`::set-output name=root_directory::${root_dir}`); + }) + .catch(error => { + console.error(error.message || 'Error occurred'); + process.exit(1); + }); diff --git a/.github/workflows/check-pr.yml b/.github/workflows/check-pr.yml index b7000c0..23dfe15 100644 --- a/.github/workflows/check-pr.yml +++ b/.github/workflows/check-pr.yml @@ -8,18 +8,20 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - - name: Check for new root directory + - name: Install Node.js + uses: actions/setup-node@v2 + with: + node-version: '14' + + - name: Install dependencies run: | - NEW_DIRS=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep '^[^/]\+/$') - TOTAL_CHANGES=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | wc -l) - DIR_COUNT=$(echo "$NEW_DIRS" | wc -l) - if [[ -z "$NEW_DIRS" || "$DIR_COUNT" -ne 1 || "$TOTAL_CHANGES" -ne 1 ]]; then - echo "PR must contain only one new root directory and no other changes!" - exit 1 - fi - echo "New directory: $NEW_DIRS" + npm install axios + + - name: Get root directory + run: node .github/scripts/get_root_directories.js + id: get-dir - name: Setup Go uses: actions/setup-go@v2 @@ -28,5 +30,5 @@ jobs: - name: Build Go Project in New Directory run: | - cd $NEW_DIRS + cd ${{ steps.get-dir.outputs.root_directory }} go build diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..dcccd85 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# backend_2023_freshman_task + +2023 杭助后端招新小任务提交仓库 + +- fork本仓库 +- git clone 你 fork 出来的仓库 +- 在仓库根目录下新建一个和你 github 用户名同名的文件夹 +- 在此文件夹内,放入你的代码 +- 提交 Pull request +- github action通过后,即为提交成功 \ No newline at end of file