Skip to content

Commit 5b7d8a5

Browse files
authored
Fix Plagiarism Workflow (#1881)
1 parent 810af25 commit 5b7d8a5

File tree

3 files changed

+61
-68
lines changed

3 files changed

+61
-68
lines changed

.github/scripts/extract_percentages.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def write_to_markdown(file_path, lines):
3939
with open(file_path, 'w') as md_file:
4040
for line in lines:
4141
md_file.write(line + '\n')
42+
log(f"Markdown file written to {file_path}")
4243

4344
def main():
4445
if len(sys.argv) != 2:
@@ -58,5 +59,6 @@ def main():
5859
sys.exit(1)
5960
else:
6061
log("No high plagiarism percentages detected.")
62+
log("Plagiarism report generation completed.")
6163
if __name__ == "__main__":
6264
main()

.github/workflows/check_plagiarism.yml

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,31 @@ jobs:
1212
steps:
1313
- name: Checkout code
1414
uses: actions/checkout@v4
15-
if: always()
1615
with:
1716
fetch-depth: 0
1817

1918
- name: Set up Python
20-
if: always()
2119
uses: actions/setup-python@v5
2220
with:
2321
python-version: '3.10'
2422

2523
- name: Install Compare50 && beautifulsoup4
26-
if: always()
2724
run: pip install compare50 beautifulsoup4
2825

2926
- name: Get list of changed files
30-
if: always()
3127
id: changed-files
3228
run: |
3329
echo "Pull Request Base SHA: ${{ github.event.pull_request.base.sha }}"
3430
echo "Pull Request Head SHA: ${{ github.event.pull_request.head.sha }}"
35-
echo "Running git diff..."
36-
git diff --name-only --diff-filter=AM --find-renames --find-copies ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }}
37-
echo "Filtering JS files in games/ directory..."
31+
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
3832
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)
39-
echo "Found JS files: $js_files"
40-
if [ -z "$js_files" ]; then
41-
echo "No JavaScript files found in the changes."
42-
else
43-
echo "FILES=$js_files" >> $GITHUB_ENV
44-
fi
33+
echo "FILES=$js_files" >> $GITHUB_ENV
4534
4635
- name: Run Plagiarism Detection Script
4736
if: env.FILES != ''
4837
run: python .github/scripts/plagiarism_check.py "${{ env.FILES }}" games output_dir saved_dir
4938

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

@@ -79,4 +67,4 @@ jobs:
7967

8068
- name: Check for High Plagiarism Percentages
8169
if: always() && steps.extract-percentages.outcome == 'failure'
82-
run: echo "Plagiarism percentage over threshold detected."
70+
run: echo "Plagiarism percentage over threshold detected."

.github/workflows/workflow_run.yml

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ jobs:
2222
- name: List available artifacts with detailed logs
2323
uses: actions/github-script@v7
2424
with:
25-
github-token: ${{secrets.GITHUB_TOKEN}}
25+
github-token: ${{ secrets.GITHUB_TOKEN }}
2626
script: |
2727
const runId = ${{ github.event.workflow_run.id }};
2828
console.log(`Fetching artifacts for workflow run ID: ${runId}`);
29-
3029
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
3130
owner: context.repo.owner,
3231
repo: context.repo.repo,
@@ -40,68 +39,76 @@ jobs:
4039
- name: Download PR Number Artifact
4140
uses: actions/github-script@v7
4241
with:
43-
github-token: ${{secrets.GITHUB_TOKEN}}
42+
github-token: ${{ secrets.GITHUB_TOKEN }}
4443
script: |
4544
const fs = require('fs');
4645
const path = require('path');
4746
const runId = ${{ github.event.workflow_run.id }};
4847
const artifactName = 'pr-number';
49-
50-
console.log(`Downloading artifact ${artifactName} from workflow run ID: ${runId}`);
51-
const artifactData = await github.rest.actions.downloadArtifact({
52-
owner: context.repo.owner,
53-
repo: context.repo.repo,
54-
artifact_id: (await github.rest.actions.listWorkflowRunArtifacts({
48+
console.log(`Checking for artifact ${artifactName} from workflow run ID: ${runId}`);
49+
try {
50+
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
5551
owner: context.repo.owner,
5652
repo: context.repo.repo,
5753
run_id: runId
58-
})).data.artifacts.find(a => a.name === artifactName).id,
59-
archive_format: 'zip',
60-
});
61-
62-
const artifactPath = path.join(process.env.GITHUB_WORKSPACE, `${artifactName}.zip`);
63-
fs.writeFileSync(artifactPath, Buffer.from(artifactData.data));
64-
65-
console.log(`Artifact ${artifactName} downloaded to ${artifactPath}`);
66-
require('child_process').execSync(`unzip -o ${artifactPath} -d ${process.env.GITHUB_WORKSPACE}`);
67-
const prNumber = fs.readFileSync(path.join(process.env.GITHUB_WORKSPACE, 'pr_number.txt'), 'utf8').trim();
68-
console.log(`PR Number: ${prNumber}`);
54+
});
55+
const artifact = artifacts.data.artifacts.find(a => a.name === artifactName);
56+
if (!artifact) {
57+
console.log(`Artifact '${artifactName}' not found, skipping download.`);
58+
return;
59+
}
60+
const artifactData = await github.rest.actions.downloadArtifact({
61+
owner: context.repo.owner,
62+
repo: context.repo.repo,
63+
artifact_id: artifact.id,
64+
archive_format: 'zip',
65+
});
66+
const artifactPath = path.join(process.env.GITHUB_WORKSPACE, `${artifactName}.zip`);
67+
fs.writeFileSync(artifactPath, Buffer.from(artifactData.data));
68+
console.log(`Artifact ${artifactName} downloaded to ${artifactPath}`);
69+
require('child_process').execSync(`unzip -o ${artifactPath} -d ${process.env.GITHUB_WORKSPACE}`);
70+
const prNumber = fs.readFileSync(path.join(process.env.GITHUB_WORKSPACE, 'pr_number.txt'), 'utf8').trim();
71+
console.log(`PR Number: ${prNumber}`);
72+
} catch (error) {
73+
console.log(`Error occurred: ${error.message}`);
74+
console.log('Continuing workflow execution despite the error.');
75+
}
6976
7077
- name: Download Plagiarism Report Artifact from Another Workflow Run
7178
uses: actions/github-script@v7
7279
with:
73-
github-token: ${{secrets.GITHUB_TOKEN}}
80+
github-token: ${{ secrets.GITHUB_TOKEN }}
7481
script: |
7582
const fs = require('fs');
7683
const path = require('path');
7784
const runId = ${{ github.event.workflow_run.id }};
7885
const artifactName = 'plagiarism-report';
79-
80-
console.log(`Fetching artifacts for workflow run ID: ${runId}`);
81-
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
82-
owner: context.repo.owner,
83-
repo: context.repo.repo,
84-
run_id: runId,
85-
});
86-
87-
const artifact = artifacts.data.artifacts.find(a => a.name === artifactName);
88-
if (!artifact) {
89-
throw new Error(`Artifact with name ${artifactName} not found`);
86+
console.log(`Downloading artifact '${artifactName}' from workflow run ID: ${runId}`);
87+
try {
88+
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
89+
owner: context.repo.owner,
90+
repo: context.repo.repo,
91+
run_id: runId
92+
});
93+
const artifact = artifacts.data.artifacts.find(a => a.name === artifactName);
94+
if (!artifact) {
95+
console.log(`Artifact '${artifactName}' not found, skipping download.`);
96+
return;
97+
}
98+
const artifactData = await github.rest.actions.downloadArtifact({
99+
owner: context.repo.owner,
100+
repo: context.repo.repo,
101+
artifact_id: artifact.id,
102+
archive_format: 'zip',
103+
});
104+
const artifactPath = path.join(process.env.GITHUB_WORKSPACE, `${artifactName}.zip`);
105+
fs.writeFileSync(artifactPath, Buffer.from(artifactData.data));
106+
console.log(`Artifact ${artifactName} downloaded to ${artifactPath}`);
107+
require('child_process').execSync(`unzip -o ${artifactPath} -d ${process.env.GITHUB_WORKSPACE}`);
108+
} catch (error) {
109+
console.log(`Error occurred: ${error.message}`);
110+
console.log('Continuing workflow execution despite the error.');
90111
}
91-
92-
console.log(`Downloading artifact ${artifactName} from workflow run ID: ${runId}`);
93-
const artifactData = await github.rest.actions.downloadArtifact({
94-
owner: context.repo.owner,
95-
repo: context.repo.repo,
96-
artifact_id: artifact.id,
97-
archive_format: 'zip',
98-
});
99-
100-
const artifactPath = path.join(process.env.GITHUB_WORKSPACE, `${artifactName}.zip`);
101-
fs.writeFileSync(artifactPath, Buffer.from(artifactData.data));
102-
103-
console.log(`Artifact ${artifactName} downloaded to ${artifactPath}`);
104-
require('child_process').execSync(`unzip -o ${artifactPath} -d ${process.env.GITHUB_WORKSPACE}`);
105112
106113
- name: Check if Plagiarism Report Exists
107114
id: check-report
@@ -110,6 +117,7 @@ jobs:
110117
echo "REPORT_EXISTS=true" >> $GITHUB_ENV
111118
else
112119
echo "REPORT_EXISTS=false" >> $GITHUB_ENV
120+
fi
113121
114122
- name: Unzip Plagiarism Report Artifact
115123
if: env.REPORT_EXISTS == 'true'
@@ -119,7 +127,7 @@ jobs:
119127
id: fetch-comments
120128
uses: actions/github-script@v7
121129
with:
122-
github-token: ${{secrets.GITHUB_TOKEN}}
130+
github-token: ${{ secrets.GITHUB_TOKEN }}
123131
script: |
124132
const fs = require('fs');
125133
const path = require('path');
@@ -137,21 +145,16 @@ jobs:
137145
if: env.REPORT_EXISTS == 'true' && steps.fetch-comments.outputs.result != 'true'
138146
uses: actions/github-script@v7
139147
with:
140-
github-token: ${{secrets.GITHUB_TOKEN}}
148+
github-token: ${{ secrets.GITHUB_TOKEN }}
141149
script: |
142150
const fs = require('fs');
143151
const path = require('path');
144-
145152
const prNumber = fs.readFileSync(path.join(process.env.GITHUB_WORKSPACE, 'pr_number.txt'), 'utf8').trim();
146153
const markdownPath = path.join(process.env.GITHUB_WORKSPACE, 'plagiarism-report.md');
147154
console.log(`Reading the Markdown report from: ${markdownPath}`);
148155
let markdownContent = fs.readFileSync(markdownPath, 'utf8');
149-
150-
markdownContent = '[Plagiarism Check Result]\n\n' + markdownContent;
151-
152156
console.log("Fetching associated pull request...");
153157
console.log(`Found associated pull request: #${prNumber}`);
154-
155158
console.log("Posting the Markdown content as a comment...");
156159
const commentResponse = await github.rest.issues.createComment({
157160
issue_number: prNumber,

0 commit comments

Comments
 (0)