Skip to content
This repository has been archived by the owner on Oct 17, 2023. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
setcy committed Sep 25, 2023
1 parent 7d33329 commit f9c1d0e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 11 deletions.
30 changes: 30 additions & 0 deletions .github/scripts/get_root_directories.js
Original file line number Diff line number Diff line change
@@ -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);
});
24 changes: 13 additions & 11 deletions .github/workflows/check-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# backend_2023_freshman_task

2023 杭助后端招新小任务提交仓库

- fork本仓库
- git clone 你 fork 出来的仓库
- 在仓库根目录下新建一个和你 github 用户名同名的文件夹
- 在此文件夹内,放入你的代码
- 提交 Pull request
- github action通过后,即为提交成功

0 comments on commit f9c1d0e

Please sign in to comment.