Skip to content

Commit c057495

Browse files
committed
👷 init the CodeQL for async/await
Issue: CLDSRV-860
1 parent 8612e12 commit c057495

6 files changed

Lines changed: 77 additions & 1 deletion

File tree

.github/codeql/async-migration.ql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @name Callback-style function (async migration)
3+
* @description These functions use callback parameters. They should be refactored to use async/await.
4+
* @kind problem
5+
* @problem.severity recommendation
6+
* @id js/callback-style-function
7+
* @tags maintainability
8+
* async-migration
9+
*/
10+
11+
import javascript
12+
13+
from Function f, Parameter p
14+
where
15+
p = f.getLastParameter() and
16+
p.getName().regexpMatch("(?i)^(cb|callback|next|done)$") and
17+
not f.isAsync() and
18+
// Exclude test files and node_modules
19+
not f.getFile().getPath().matches("%/tests/%") and
20+
not f.getFile().getPath().matches("%/node_modules/%")
21+
select f, "This function uses a callback parameter ('" + p.getName() + "'). Refactor to async/await."

.github/codeql/async-suite.qls

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- description: Scality Cloudserver Async Migration Suite
2+
- queries: .
3+
- qlpack: codeql/javascript-queries
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @name Promise .then() usage (async migration)
3+
* @description These calls use .then() instead of async/await. They should be refactored to use async/await.
4+
* @kind problem
5+
* @problem.severity recommendation
6+
* @id js/promise-then-usage
7+
* @tags maintainability
8+
* async-migration
9+
*/
10+
11+
import javascript
12+
13+
from MethodCallExpr m
14+
where
15+
m.getMethodName() = "then" and
16+
// Exclude test files and node_modules
17+
not m.getFile().getPath().matches("%/tests/%") and
18+
not m.getFile().getPath().matches("%/node_modules/%")
19+
select m, "This call uses .then(). Refactor to async/await."

.github/scripts/count-async-functions.mjs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ console.log('');
8484
console.log(`Migration (trend): ${asyncFunctions}/${asyncFunctions + callbackFunctions} (${migrationPercent}%)`);
8585

8686
if (process.env.GITHUB_STEP_SUMMARY) {
87-
const { appendFileSync } = await import('node:fs');
87+
const { appendFileSync, writeFileSync } = await import('node:fs');
8888
appendFileSync(process.env.GITHUB_STEP_SUMMARY, [
8989
'## Async/Await Migration Progress',
9090
'',
@@ -97,4 +97,24 @@ if (process.env.GITHUB_STEP_SUMMARY) {
9797
`| Migration trend (async / (async + callback)) | ${asyncFunctions}/${asyncFunctions + callbackFunctions} (${migrationPercent}%) |`,
9898
'',
9999
].join('\n'));
100+
101+
// Output benchmark JSON for visualization
102+
const benchmarkData = [
103+
{
104+
name: 'Async Migration Progress',
105+
unit: '%',
106+
value: parseFloat(migrationPercent),
107+
},
108+
{
109+
name: 'Async Functions Percentage',
110+
unit: '%',
111+
value: parseFloat(asyncFunctionPercent),
112+
},
113+
{
114+
name: 'Total callback functions',
115+
unit: 'count',
116+
value: callbackFunctions,
117+
}
118+
];
119+
writeFileSync('async-migration-benchmark.json', JSON.stringify(benchmarkData, null, 2));
100120
}

.github/workflows/codeql.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
uses: github/codeql-action/init@v3
2121
with:
2222
languages: javascript, python, ruby
23+
queries: security-extended,./.github/codeql/async-suite.qls
2324

2425
- name: Build and analyze
2526
uses: github/codeql-action/analyze@v3

.github/workflows/tests.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ jobs:
122122
run: yarn install --frozen-lockfile --network-concurrency 1
123123
- name: Count async/await migration progress
124124
run: yarn run count-async
125+
- name: Store benchmark results
126+
uses: benchmark-action/github-action-benchmark@v1
127+
with:
128+
name: Async Migration Benchmark
129+
tool: 'customGeneric'
130+
output-file-path: async-migration-benchmark.json
131+
github-token: ${{ secrets.GITHUB_TOKEN }}
132+
auto-push: true
133+
# Show alert if percentage drops
134+
comment-on-alert: true
135+
alert-threshold: '110%'
136+
fail-on-alert: false
125137

126138
unit-tests:
127139
runs-on: ubuntu-24.04

0 commit comments

Comments
 (0)