Skip to content

Commit

Permalink
Fix outputs.result not considering result of first two steps (#2)
Browse files Browse the repository at this point in the history
Also add tests for the action to ensure things are working as they should.
  • Loading branch information
harigopal authored Nov 27, 2023
1 parent 270bff8 commit e1006be
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 10 deletions.
105 changes: 105 additions & 0 deletions .github/workflows/test-action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
name: Test the check-repo action

on:
push:
branches:
- main
pull_request:
branches:
- "*"

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Action should fail for non GitHub repo URLs
id: reject-invalid-url
uses: ./
with:
globs: README.md
submissionJsonPath: test/invalid-repo.json
testMode: "true"
- name: Report result of previous test
run: |
if [ "${{ steps.reject-invalid-url.outputs.result }}" != "failure" ]; then
echo "Action did not fail for an invalid URL"
exit 1
fi
- name: Action should fail for URL that is points to a resource inside a repo
id: reject-deep-github-url
uses: ./
with:
globs: README.md
submissionJsonPath: test/deep-link.json
testMode: "true"
- name: Report result of previous test
run: |
if [ "${{ steps.reject-deep-github-url.outputs.result }}" != "failure" ]; then
echo "Action did not fail for a folder / file URL within a repo"
exit 1
fi
- name: Action should fail for GitHub repo URL that is inaccessible
id: reject-inaccessible-github-url
uses: ./
with:
repoPath: clone-for-inaccessible-repo-url
globs: README.md
submissionJsonPath: test/non-existent-repo.json
testMode: "true"
- name: Report result of previous test
run: |
if [ "${{ steps.reject-inaccessible-github-url.outputs.result }}" != "failure" ]; then
echo "Action did not fail for an inaccessible GitHub repo URL"
exit 1
fi
- name: Action should fail for a GitHub repo where a file is missing
id: reject-github-repo-with-missing-file
uses: ./
with:
repoPath: clone-for-file-missing
globs: |-
README.md
test/this-does-not-exist.json
submissionJsonPath: test/valid-repo.json
testMode: "true"
- name: Report result of previous test
run: |
if [ "${{ steps.reject-github-repo-with-missing-file.outputs.result }}" != "failure" ]; then
echo "Action did not fail for a GitHub repo with a missing file"
exit 1
fi
- name: Action should fail for a GitHub repo where a folder is missing
id: reject-github-repo-with-missing-folder
uses: ./
with:
repoPath: clone-for-folder-missing
globs: |-
README.md
non-existent-folder/*
submissionJsonPath: test/valid-repo.json
testMode: "true"
- name: Report result of previous test
run: |
if [ "${{ steps.reject-github-repo-with-missing-folder.outputs.result }}" != "failure" ]; then
echo "Action did not fail for a GitHub repo with a missing folder"
exit 1
fi
- name: Action should succeed for a GitHub repo where no files or folders from globs are missing
id: accept-github-repo-with-matching-globs
uses: ./
with:
repoPath: clone-for-happy-path
globs: |-
action.yml
README.md
test/*
submissionJsonPath: test/valid-repo.json
testMode: "true"
- name: Report result of previous test
run: |
if [ "${{ steps.accept-github-repo-with-matching-globs.outputs.result }}" != "success" ]; then
echo "Action did not succeed for a valid GitHub repo with matching files and folders"
exit 1
fi
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ When a student submission contains a URL to a GitHub repository, the `check_repo

To fetch the repo URL, the action makes the assumption that the first question in the target checklist is asking for the GitHub repo URL. The following steps are performed:

1. The action will consider the first item in the submission checklist as the GitHub repo URL to be checked.
1. The action will validate the repo URL (that it is, indeed, a GitHub repo URL), and then clones it. By default, it clones to the root path, but this can be customized using the `repoPath` input.
2. It then checks for the presence of files and folders specified using the `globs` input array.

Expand Down
33 changes: 23 additions & 10 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ inputs:
description: Set to true for debugging
required: false
default: "false"
submissionJsonPath:
description: Path to the submission.json file for testing
required: false
default: ""
outputs:
result:
description: Returns 'success' if all checks pass.
Expand All @@ -20,15 +24,17 @@ runs:
using: composite
steps:
- name: Check out the parent repository with student submission data
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Extract the student submission repo from URL
uses: actions/github-script@v6
uses: actions/github-script@v7
id: get-repo-name
continue-on-error: true
with:
result-encoding: string
script: |
const submission = require('./submission.json');
const submissionJsonPathOverride = "${{ inputs.submissionJsonPath }}";
const submissionJsonPath = submissionJsonPathOverride === "" ? "submission.json" : submissionJsonPathOverride;
const submission = require(process.env.GITHUB_WORKSPACE + "/" + submissionJsonPath);
const gitURL = submission.checklist[0].result;
const regex = /(?:git@|https:\/\/)github.com[:\/]([a-zA-Z0-9\-_.]+\/[a-zA-Z0-9\-_.\/]+)$/g;
const matches = regex.exec(gitURL);
Expand Down Expand Up @@ -63,13 +69,13 @@ runs:
```
- name: Check out the repository with submitted code
if: ${{ steps.get-repo-name.outputs.result != 'failure' }}
uses: actions/checkout@v2
uses: actions/checkout@v4
continue-on-error: true
id: checkout-repo
with:
repository: ${{ steps.get-repo-name.outputs.result }}
path: ${{ inputs.repoPath }}
- name: Report invalid repository URL in submission
- name: Report inability to clone the repository URL in submission
if: ${{ steps.get-repo-name.outputs.result != 'failure' && steps.checkout-repo.outcome != 'success' && inputs.testMode == 'false' }}
uses: pupilfirst/grade-action@v1
with:
Expand All @@ -91,7 +97,7 @@ runs:
- name: Verify structure of the repo
if: steps.checkout-repo.outcome == 'success'
id: verify-structure
uses: actions/github-script@v6
uses: actions/github-script@v7
with:
globs: ${{ inputs.globs }}
repoPath: ${{ inputs.repoPath }}
Expand All @@ -117,7 +123,7 @@ runs:
}));
return missingPaths.flat().map(path => "- " + path).join("\n");
- name: Report invalid repository URL in submission
- name: Report the absence of required files or folders in submitted GitHub repo
if: ${{ steps.verify-structure.outputs.result != '' && inputs.testMode == 'false' }}
uses: pupilfirst/grade-action@v1
with:
Expand All @@ -132,9 +138,16 @@ runs:
```
- name: Provide success or failure response from composite action
id: final-step
uses: actions/github-script@v6
uses: actions/github-script@v7
with:
verifyStructureOutput: ${{ steps.verify-structure.outputs.result }}
result-encoding: string
script: |
return core.getInput("verifyStructureOutput") == "" ? "success" : "failure";
if ("${{ steps.get-repo-name.outputs.result }}" === "failure") {
return "failure";
}
if ("${{ steps.checkout-repo.outcome }}" !== "success") {
return "failure";
}
return "${{ steps.verify-structure.outputs.result }}" === "" ? "success" : "failure";
7 changes: 7 additions & 0 deletions test/deep-link.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"checklist": [
{
"result": "https://github.com/pupilfirst/check-repo-action/blob/main/README.md"
}
]
}
7 changes: 7 additions & 0 deletions test/invalid-repo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"checklist": [
{
"result": "https://github.com"
}
]
}
7 changes: 7 additions & 0 deletions test/non-existent-repo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"checklist": [
{
"result": "https://github.com/pupilfirst/does-not-exist"
}
]
}
7 changes: 7 additions & 0 deletions test/valid-repo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"checklist": [
{
"result": "https://github.com/pupilfirst/check-repo-action"
}
]
}

0 comments on commit e1006be

Please sign in to comment.