From efeba054ee602134ef7ad54ad60ea8ab9ea14d14 Mon Sep 17 00:00:00 2001 From: AlaRduTP Date: Thu, 14 Mar 2024 09:21:17 +0800 Subject: [PATCH] feat: new workflows --- .github/workflows/PR.yml | 34 +++++++++------- .github/workflows/lab-autograding.yml | 56 +++++++++++++++++++++++++++ .github/workflows/lab1.yml | 26 ------------- .github/workflows/lab2.yml | 26 ------------- scripts/merge-all.sh | 29 ++++++++++++++ 5 files changed, 104 insertions(+), 67 deletions(-) create mode 100644 .github/workflows/lab-autograding.yml delete mode 100644 .github/workflows/lab1.yml delete mode 100644 .github/workflows/lab2.yml create mode 100755 scripts/merge-all.sh diff --git a/.github/workflows/PR.yml b/.github/workflows/PR.yml index 10f942ec..54b7d6b2 100644 --- a/.github/workflows/PR.yml +++ b/.github/workflows/PR.yml @@ -15,27 +15,31 @@ jobs: const { owner, repo, number: issue_number } = context.issue; const pr = await github.rest.pulls.get({ owner, repo, pull_number: issue_number }); const title = pr.data.title; - const labRegex = /\[LAB(\d+)\]/; - const titleRegex = /^\[LAB\d+\] [\da-zA-Z]+$/; - if (!titleRegex.test(title)) { - core.setFailed('PR title does not match the required format. Please use the format [LAB#] student#.'); + const titleRegex = /^\[LAB(\d+)\] [a-zA-Z]?\d+$/; + const match = title.match(titleRegex); + + let labNumberStr = undefined; + if (match) { + labNumberStr = match[1]; + } else { + core.setFailed('PR title does not match the required format. Please use the format: [LAB#] .'); } - if (pr.data.head.ref !== pr.data.base.ref) { - core.setFailed('The source branch and target branch must be the same.'); + const labelToAdd = `lab${labNumberStr}`; + if (labNumberStr) { + await github.rest.issues.addLabels({ owner, repo, issue_number, labels: [labelToAdd] }); } if (pr.data.base.ref === 'main') { core.setFailed('The target branch cannot be main.'); } - const match = title.match(labRegex); - if (match) { - const labelToAdd = 'lab' + match[1]; - await github.rest.issues.addLabels({ owner, repo, issue_number, labels: [labelToAdd] }); - } else { - core.setFailed('No match found in PR title. Please add a label in the format [LAB#] to the PR title.'); + if (labNumberStr < 3 && pr.data.head.ref !== pr.data.base.ref) { + core.setFailed('The source branch and target branch must be the same.'); + } + if (labNumberStr >= 3 && pr.data.head.ref !== labelToAdd) { + core.setFailed(`The source branch must be '${labelToAdd}'`); } checklist-check: runs-on: ubuntu-latest @@ -49,12 +53,12 @@ jobs: const pr = await github.rest.pulls.get({ owner, repo, pull_number: issue_number }); const body = pr.data.body; - const checkboxes = body.match(/\- \[[x ]\]/g); + const checkboxes = body.match(/^ ?(-|\*) \[[x ]\]/gmi); if (!checkboxes || checkboxes.length !== 5) { core.setFailed('The PR description must contain exactly 5 checkboxes.'); } - const unchecked = body.match(/\- \[ \]/g); + const unchecked = body.match(/^ ?(-|\*) \[ \]/gm); if (unchecked && unchecked.length > 0) { - core.setFailed(`There are ${unchecked.length} unchecked items in the PR description.`); + core.setFailed(`There are ${unchecked.length} unchecked item(s) in the PR description.`); } diff --git a/.github/workflows/lab-autograding.yml b/.github/workflows/lab-autograding.yml new file mode 100644 index 00000000..918d29b8 --- /dev/null +++ b/.github/workflows/lab-autograding.yml @@ -0,0 +1,56 @@ +name: Autograding + +on: + pull_request_target: + types: [labeled, synchronize, opened, reopened, ready_for_review] + +jobs: + build: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-22.04] + fail-fast: false + steps: + - uses: actions/checkout@v1 + with: + fetch-depth: 1 + - uses: actions/setup-node@v4 + with: + node-version: latest + - name: Extract lab number and Check no changes other than specific files + uses: actions/github-script@v5 + id: lab + with: + result-encoding: string + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const { owner, repo, number: issue_number } = context.issue; + const pr = await github.rest.pulls.get({ owner, repo, pull_number: issue_number }); + const labels = pr.data.labels; + const lab = labels.find((label) => label.name.startsWith('lab')); + if (!lab) { + core.setFailed('No lab label found on the PR.'); + return { number: 0 }; + } + const labNumberMatch = lab.name.match(/lab(\d+)/); + if (!labNumberMatch) { + core.setFailed('Invalid lab label found on the PR.'); + return { number: 0 }; + } + const labNumber = labNumberMatch[1]; + console.log(`Lab number: ${labNumber}`) + + const files = await github.rest.pulls.listFiles({ owner, repo, pull_number: issue_number }); + const changedFiles = files.data.map((file) => file.filename); + const allowedFiles = [ + `lab${labNumber}/main_test.js`, + ]; + if (!changedFiles.every((file) => allowedFiles.includes(file))) { + core.setFailed('The PR contains changes to files other than the allowed files.'); + } + return labNumber; + - name: Grading + run: | + cd lab${{ steps.lab.outputs.result }} + ./validate.sh diff --git a/.github/workflows/lab1.yml b/.github/workflows/lab1.yml deleted file mode 100644 index 49a5d578..00000000 --- a/.github/workflows/lab1.yml +++ /dev/null @@ -1,26 +0,0 @@ -name: lab1 autograding - -on: - pull_request: - types: [labeled, synchronize, opened, reopened, ready_for_review] - -jobs: - build: - if: contains(github.event.pull_request.labels.*.name, 'lab1') - runs-on: ${{ matrix.os }} - strategy: - matrix: - os: [ubuntu-22.04] - fail-fast: false - steps: - - uses: actions/checkout@v1 - with: - fetch-depth: 1 - - name: dependency (ubuntu) - run: | - curl -fsSL https://deb.nodesource.com/setup_21.x | sudo -E bash - &&\ - sudo apt-get install -y nodejs - - name: grading - run: | - cd lab1 - ./validate.sh diff --git a/.github/workflows/lab2.yml b/.github/workflows/lab2.yml deleted file mode 100644 index 2cb76fa7..00000000 --- a/.github/workflows/lab2.yml +++ /dev/null @@ -1,26 +0,0 @@ -name: lab2 autograding - -on: - pull_request: - types: [labeled, synchronize, opened, reopened, ready_for_review] - -jobs: - build: - if: contains(github.event.pull_request.labels.*.name, 'lab2') - runs-on: ${{ matrix.os }} - strategy: - matrix: - os: [ubuntu-22.04] - fail-fast: false - steps: - - uses: actions/checkout@v1 - with: - fetch-depth: 1 - - name: dependency (ubuntu) - run: | - curl -fsSL https://deb.nodesource.com/setup_21.x | sudo -E bash - &&\ - sudo apt-get install -y nodejs - - name: grading - run: | - cd lab2 - ./validate.sh diff --git a/scripts/merge-all.sh b/scripts/merge-all.sh new file mode 100755 index 00000000..a3be7f90 --- /dev/null +++ b/scripts/merge-all.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +if [ $# -ne 1 ]; then + echo "./merge-all.sh " + exit 1 +fi + +git fetch origin + +for branch in $(git branch -r | grep -v HEAD); do + # Remove the "origin/" prefix + branch=${branch#origin/} + + if [[ "$branch" != "main" ]]; then + git checkout "$branch" + if [[ $? -ne 0 ]]; then + echo "Checkout failed for branch $branch" + exit 1 + fi + git merge --squash main + if [[ $? -ne 0 ]]; then + echo "Merge failed for branch $branch" + exit 1 + fi + git commit -m "$1" + fi +done + +git checkout main \ No newline at end of file