cjstuff21-Passed-Quiz #19
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Grant Repository Access From Quiz Results | |
| on: | |
| workflow_dispatch: | |
| push: | |
| paths: | |
| - "quiz-results/*.json" | |
| permissions: | |
| contents: write # Only needed for committing updates | |
| jobs: | |
| process_quiz: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Find latest quiz results JSON file | |
| id: findfile | |
| run: | | |
| LATEST_FILE=$(ls -t quiz-results/*.json | head -n 1) | |
| echo "file=$LATEST_FILE" >> $GITHUB_OUTPUT | |
| echo "Processing JSON file: $LATEST_FILE" | |
| - name: Parse JSON values | |
| id: parsejson | |
| run: | | |
| FILE="${{ steps.findfile.outputs.file }}" | |
| USERNAME=$(jq -r '.github_username' "$FILE") | |
| STUDENTID=$(jq -r '.studentID' "$FILE") | |
| NAME=$(jq -r '.name' "$FILE") | |
| TIMESTAMP=$(jq -r '.timestamp' "$FILE") | |
| echo "USERNAME=$USERNAME" >> $GITHUB_ENV | |
| echo "STUDENTID=$STUDENTID" >> $GITHUB_ENV | |
| echo "NAME=$NAME" >> $GITHUB_ENV | |
| echo "TIMESTAMP=$TIMESTAMP" >> $GITHUB_ENV | |
| - name: Validate studentID and username | |
| run: | | |
| if [[ "$STUDENTID" != s* ]]; then | |
| echo "Invalid student ID — must begin with 's'. Aborting." | |
| exit 1 | |
| fi | |
| if ! [[ "$USERNAME" =~ ^[a-zA-Z0-9-]+$ ]]; then | |
| echo "Invalid GitHub username. Aborting." | |
| exit 1 | |
| fi | |
| - name: Grant repository access to student | |
| run: | | |
| curl -X PUT \ | |
| -H "Authorization: token ${{ secrets.PDE_ADMIN_PAT }}" \ | |
| -H "Accept: application/vnd.github.v3+json" \ | |
| https://api.github.com/repos/patrickandersondeakin/test_pde/collaborators/$USERNAME \ | |
| -d '{"permission": "push"}' | |
| - name: Update STUDENT_ACCESS.md dashboard | |
| run: | | |
| DASH_FILE="STUDENT_ACCESS.md" | |
| if [ ! -f "$DASH_FILE" ]; then | |
| echo "# Student Access Dashboard" > "$DASH_FILE" | |
| echo "| Timestamp | Name | GitHub Username | StudentID |" >> "$DASH_FILE" | |
| echo "|-----------|------|------------------|-----------|" >> "$DASH_FILE" | |
| fi | |
| echo "| $TIMESTAMP | $NAME | $USERNAME | $STUDENTID |" >> "$DASH_FILE" | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git add "$DASH_FILE" | |
| git commit -m "Update STUDENT_ACCESS.md with $USERNAME" | |
| git push | |