forked from UofT-DSI/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
135 lines (111 loc) · 4.96 KB
/
sql_assignment_runner.yml
File metadata and controls
135 lines (111 loc) · 4.96 KB
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
name: SQL Unit Tests
on:
workflow_dispatch: # Manual trigger from GitHub UI
inputs:
branch:
description: 'Branch to use to run assignment for, i.e. assignment-one, assignment-2'
required: true
default: assignment-one
type: string
pr_number:
description: 'Pull request number. Extract a number from the end of pull request url, i.e. https://github.com/khsergvl/sql/pull/17'
required: true
type: string
default: '1'
permissions:
contents: read
pull-requests: write
jobs:
run-assignment-queries:
# uncomment once a change to PR trigger will be implemented
# if: startsWith(github.head_ref, 'assignment-')
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ inputs.branch }}
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install dependencies
run: |
pip install -r ./.github/scripts/requirements.txt
- name: Get changed files
id: changes
run: |
git fetch
git diff --name-only origin/main > changed_files.txt
echo "Changed files:"
cat changed_files.txt
# The goal here to search for a file modified during PR that is match to a pattern
# and give priority in next order 2, 1 as it quite often happens when students submit
# PR without following assignments order and git flow. It will to run latest / greatest
assignment_changed=""
# Priority: assignment2 > assignment1
if grep -qE '(^|/)assignment2\.sql$' changed_files.txt; then
assignment_changed=$(grep -E '(^|/)assignment2\.sql$' changed_files.txt | head -n1)
elif grep -qE '(^|/)assignment1\.sql$' changed_files.txt; then
assignment_changed=$(grep -E '(^|/)assignment1\.sql$' changed_files.txt | head -n1)
fi
if [ -n "$assignment_changed" ]; then
echo "assignment_changed=$assignment_changed" >> "$GITHUB_OUTPUT"
fi
- name: Run tests
id: pytest
run: |
pytest tests/test_assignment.py --file_path="${{steps.changes.outputs.assignment_changed}}" --tb=short --disable-warnings \
--junitxml=pytest-report.xml || true
- name: Post test results to PR
uses: actions/github-script@v7
with:
script: |
function jsonToMarkdownTable(data) {
if (!Array.isArray(data) || data.length === 0) {
return 'No data';
}
const headers = Object.keys(data[0]);
// Header row
const headerRow = `| ${headers.join(' | ')} |`;
// Separator row
const separatorRow = `| ${headers.map(() => '---').join(' | ')} |`;
// Data rows
const rows = data.map(row =>
`| ${headers.map(h => formatValue(row[h])).join(' | ')} |`
);
return [headerRow, separatorRow, ...rows].join('\n');
}
function formatValue(value) {
if (value === null || value === undefined) return '';
if (typeof value === 'object') return `\`${JSON.stringify(value)}\``;
return String(value);
}
const fs = require('fs')
const file_read_result = fs.readFileSync('test-results.json', 'utf8')
const results = JSON.parse(file_read_result)
let body = `### 🧪 SQL Queries Run Results (up to 3 rows)\n\n`
body += `<details> <summary>Click to expand/collapse assignment queries execution results</summary>\n\n`
for (const result of results) {
const isArrayResult = Array.isArray(result.result);
if (result.error) {
body += `❌ Query ${result.number}: \n\n *${result.query}*, \n\n **Error**: \n${result.error}\n\n`
} else if (isArrayResult && result.result.length === 0) {
body += `⚪ Query ${result.number}: \n\n *${result.query}*, \n\n No rows returned \n\n`
} else if (isArrayResult && result.result.length > 0) {
body += `✅ Query ${result.number}: \n\n *${result.query}*, \n\n **Results**: \n`
const table = jsonToMarkdownTable(result.result)
body += `${table} \n\n`
}
body += `-------------------------------------------------------- \n`;
}
body += `</details>`
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: ${{ inputs.pr_number }},
body
})