Skip to content

Commit

Permalink
Fix Plagiarism Workflow (#1881)
Browse files Browse the repository at this point in the history
  • Loading branch information
DevIos01 authored Jul 2, 2024
1 parent 810af25 commit 5b7d8a5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 68 deletions.
2 changes: 2 additions & 0 deletions .github/scripts/extract_percentages.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def write_to_markdown(file_path, lines):
with open(file_path, 'w') as md_file:
for line in lines:
md_file.write(line + '\n')
log(f"Markdown file written to {file_path}")

def main():
if len(sys.argv) != 2:
Expand All @@ -58,5 +59,6 @@ def main():
sys.exit(1)
else:
log("No high plagiarism percentages detected.")
log("Plagiarism report generation completed.")
if __name__ == "__main__":
main()
18 changes: 3 additions & 15 deletions .github/workflows/check_plagiarism.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,31 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v4
if: always()
with:
fetch-depth: 0

- name: Set up Python
if: always()
uses: actions/setup-python@v5
with:
python-version: '3.10'

- name: Install Compare50 && beautifulsoup4
if: always()
run: pip install compare50 beautifulsoup4

- name: Get list of changed files
if: always()
id: changed-files
run: |
echo "Pull Request Base SHA: ${{ github.event.pull_request.base.sha }}"
echo "Pull Request Head SHA: ${{ github.event.pull_request.head.sha }}"
echo "Running git diff..."
git diff --name-only --diff-filter=AM --find-renames --find-copies ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }}
echo "Filtering JS files in games/ directory..."
git diff --name-only --diff-filter=AM --find-renames --find-copies ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }} | grep 'games/.*\.js$' | xargs
js_files=$(git diff --name-only --diff-filter=AM --find-renames --find-copies ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }} | grep 'games/.*\.js$' | xargs)
echo "Found JS files: $js_files"
if [ -z "$js_files" ]; then
echo "No JavaScript files found in the changes."
else
echo "FILES=$js_files" >> $GITHUB_ENV
fi
echo "FILES=$js_files" >> $GITHUB_ENV
- name: Run Plagiarism Detection Script
if: env.FILES != ''
run: python .github/scripts/plagiarism_check.py "${{ env.FILES }}" games output_dir saved_dir

- name: Extract and Display Similarity Percentages
if: always()
run: python .github/scripts/extract_percentages.py saved_dir/
id: extract-percentages

Expand Down Expand Up @@ -79,4 +67,4 @@ jobs:

- name: Check for High Plagiarism Percentages
if: always() && steps.extract-percentages.outcome == 'failure'
run: echo "Plagiarism percentage over threshold detected."
run: echo "Plagiarism percentage over threshold detected."
109 changes: 56 additions & 53 deletions .github/workflows/workflow_run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ jobs:
- name: List available artifacts with detailed logs
uses: actions/github-script@v7
with:
github-token: ${{secrets.GITHUB_TOKEN}}
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const runId = ${{ github.event.workflow_run.id }};
console.log(`Fetching artifacts for workflow run ID: ${runId}`);
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
Expand All @@ -40,68 +39,76 @@ jobs:
- name: Download PR Number Artifact
uses: actions/github-script@v7
with:
github-token: ${{secrets.GITHUB_TOKEN}}
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const path = require('path');
const runId = ${{ github.event.workflow_run.id }};
const artifactName = 'pr-number';
console.log(`Downloading artifact ${artifactName} from workflow run ID: ${runId}`);
const artifactData = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: (await github.rest.actions.listWorkflowRunArtifacts({
console.log(`Checking for artifact ${artifactName} from workflow run ID: ${runId}`);
try {
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: runId
})).data.artifacts.find(a => a.name === artifactName).id,
archive_format: 'zip',
});
const artifactPath = path.join(process.env.GITHUB_WORKSPACE, `${artifactName}.zip`);
fs.writeFileSync(artifactPath, Buffer.from(artifactData.data));
console.log(`Artifact ${artifactName} downloaded to ${artifactPath}`);
require('child_process').execSync(`unzip -o ${artifactPath} -d ${process.env.GITHUB_WORKSPACE}`);
const prNumber = fs.readFileSync(path.join(process.env.GITHUB_WORKSPACE, 'pr_number.txt'), 'utf8').trim();
console.log(`PR Number: ${prNumber}`);
});
const artifact = artifacts.data.artifacts.find(a => a.name === artifactName);
if (!artifact) {
console.log(`Artifact '${artifactName}' not found, skipping download.`);
return;
}
const artifactData = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id,
archive_format: 'zip',
});
const artifactPath = path.join(process.env.GITHUB_WORKSPACE, `${artifactName}.zip`);
fs.writeFileSync(artifactPath, Buffer.from(artifactData.data));
console.log(`Artifact ${artifactName} downloaded to ${artifactPath}`);
require('child_process').execSync(`unzip -o ${artifactPath} -d ${process.env.GITHUB_WORKSPACE}`);
const prNumber = fs.readFileSync(path.join(process.env.GITHUB_WORKSPACE, 'pr_number.txt'), 'utf8').trim();
console.log(`PR Number: ${prNumber}`);
} catch (error) {
console.log(`Error occurred: ${error.message}`);
console.log('Continuing workflow execution despite the error.');
}
- name: Download Plagiarism Report Artifact from Another Workflow Run
uses: actions/github-script@v7
with:
github-token: ${{secrets.GITHUB_TOKEN}}
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const path = require('path');
const runId = ${{ github.event.workflow_run.id }};
const artifactName = 'plagiarism-report';
console.log(`Fetching artifacts for workflow run ID: ${runId}`);
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: runId,
});
const artifact = artifacts.data.artifacts.find(a => a.name === artifactName);
if (!artifact) {
throw new Error(`Artifact with name ${artifactName} not found`);
console.log(`Downloading artifact '${artifactName}' from workflow run ID: ${runId}`);
try {
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: runId
});
const artifact = artifacts.data.artifacts.find(a => a.name === artifactName);
if (!artifact) {
console.log(`Artifact '${artifactName}' not found, skipping download.`);
return;
}
const artifactData = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id,
archive_format: 'zip',
});
const artifactPath = path.join(process.env.GITHUB_WORKSPACE, `${artifactName}.zip`);
fs.writeFileSync(artifactPath, Buffer.from(artifactData.data));
console.log(`Artifact ${artifactName} downloaded to ${artifactPath}`);
require('child_process').execSync(`unzip -o ${artifactPath} -d ${process.env.GITHUB_WORKSPACE}`);
} catch (error) {
console.log(`Error occurred: ${error.message}`);
console.log('Continuing workflow execution despite the error.');
}
console.log(`Downloading artifact ${artifactName} from workflow run ID: ${runId}`);
const artifactData = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id,
archive_format: 'zip',
});
const artifactPath = path.join(process.env.GITHUB_WORKSPACE, `${artifactName}.zip`);
fs.writeFileSync(artifactPath, Buffer.from(artifactData.data));
console.log(`Artifact ${artifactName} downloaded to ${artifactPath}`);
require('child_process').execSync(`unzip -o ${artifactPath} -d ${process.env.GITHUB_WORKSPACE}`);
- name: Check if Plagiarism Report Exists
id: check-report
Expand All @@ -110,6 +117,7 @@ jobs:
echo "REPORT_EXISTS=true" >> $GITHUB_ENV
else
echo "REPORT_EXISTS=false" >> $GITHUB_ENV
fi
- name: Unzip Plagiarism Report Artifact
if: env.REPORT_EXISTS == 'true'
Expand All @@ -119,7 +127,7 @@ jobs:
id: fetch-comments
uses: actions/github-script@v7
with:
github-token: ${{secrets.GITHUB_TOKEN}}
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const path = require('path');
Expand All @@ -137,21 +145,16 @@ jobs:
if: env.REPORT_EXISTS == 'true' && steps.fetch-comments.outputs.result != 'true'
uses: actions/github-script@v7
with:
github-token: ${{secrets.GITHUB_TOKEN}}
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const path = require('path');
const prNumber = fs.readFileSync(path.join(process.env.GITHUB_WORKSPACE, 'pr_number.txt'), 'utf8').trim();
const markdownPath = path.join(process.env.GITHUB_WORKSPACE, 'plagiarism-report.md');
console.log(`Reading the Markdown report from: ${markdownPath}`);
let markdownContent = fs.readFileSync(markdownPath, 'utf8');
markdownContent = '[Plagiarism Check Result]\n\n' + markdownContent;
console.log("Fetching associated pull request...");
console.log(`Found associated pull request: #${prNumber}`);
console.log("Posting the Markdown content as a comment...");
const commentResponse = await github.rest.issues.createComment({
issue_number: prNumber,
Expand Down

0 comments on commit 5b7d8a5

Please sign in to comment.