Skip to content

Commit cb7c034

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

File tree

6 files changed

+69
-1
lines changed

6 files changed

+69
-1
lines changed

.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.getParameter(f.getNumParameter() - 1) 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().getAbsolutePath().matches("%/tests/%") and
20+
not f.getFile().getAbsolutePath().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().getAbsolutePath().matches("%/tests/%") and
18+
not m.getFile().getAbsolutePath().matches("%/node_modules/%")
19+
select m, "This call uses .then(). Refactor to async/await."

.github/codeql/qlpack.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name: scality/cloudserver-async-migration
2+
version: 0.0.1
3+
dependencies:
4+
codeql/javascript-all: "*"

.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

0 commit comments

Comments
 (0)