-
Notifications
You must be signed in to change notification settings - Fork 1.2k
165 lines (155 loc) · 7.23 KB
/
workflow_run.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
name: Send Plagiarism Result On CI Complete
permissions:
actions: read
contents: read
issues: write
pull-requests: write
on:
workflow_run:
workflows: ["Plagiarism Checker"]
types:
- completed
jobs:
on_pr_finish:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: List available artifacts with detailed logs
uses: actions/github-script@v7
with:
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,
run_id: runId
});
console.log(`Artifacts found: ${artifacts.data.total_count}`);
for (const artifact of artifacts.data.artifacts) {
console.log(`Artifact name: ${artifact.name}, ID: ${artifact.id}, Size: ${artifact.size_in_bytes} bytes`);
}
- name: Download PR Number Artifact
uses: actions/github-script@v7
with:
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(`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
});
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 }}
script: |
const fs = require('fs');
const path = require('path');
const runId = ${{ github.event.workflow_run.id }};
const artifactName = 'plagiarism-report';
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.');
}
- name: Check if Plagiarism Report Exists
id: check-report
run: |
if unzip -l plagiarism-report.zip; then
echo "REPORT_EXISTS=true" >> $GITHUB_ENV
else
echo "REPORT_EXISTS=false" >> $GITHUB_ENV
fi
- name: Unzip Plagiarism Report Artifact
if: env.REPORT_EXISTS == 'true'
run: unzip -o plagiarism-report.zip -d ${{ github.workspace }}
- name: Fetch Pull Request Comments
id: fetch-comments
uses: actions/github-script@v7
with:
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 comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber
});
const hasPlagiarismComment = comments.data.some(comment => comment.body.includes('[Plagiarism Check Result]'));
console.log(`Has Plagiarism Comment: ${hasPlagiarismComment}`);
return hasPlagiarismComment;
- name: Post Markdown as Comment
if: env.REPORT_EXISTS == 'true' && steps.fetch-comments.outputs.result != 'true'
uses: actions/github-script@v7
with:
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');
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,
owner: context.repo.owner,
repo: context.repo.repo,
body: markdownContent
});
console.log(`Comment posted successfully: ${commentResponse.data.html_url}`);