Skip to content
This repository was archived by the owner on Sep 1, 2022. It is now read-only.

Commit fa99433

Browse files
author
Sam Lanning
authored
Merge pull request #18 from github/query-pattern
Pass a query pattern
2 parents a86184a + d9c5e94 commit fa99433

File tree

6 files changed

+42
-24
lines changed

6 files changed

+42
-24
lines changed

README.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ including links to the lines of source code on GitHub when possible:
2828
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
2929
**Table of Contents**
3030

31-
- [Creating your own course](#creating-your-own-course)
32-
- [Creating the Query Checking Action](#creating-the-query-checking-action)
33-
- [Testing the action](#testing-the-action)
34-
- [Adding new queries & calculating the contents for the CSV files](#adding-new-queries--calculating-the-contents-for-the-csv-files)
35-
- [Publishing your action](#publishing-your-action)
36-
- [Contributing your GitHub Action to this repository](#contributing-your-github-action-to-this-repository)
37-
- [Creating the Learning Lab Course](#creating-the-learning-lab-course)
38-
- [Example Courses](#example-courses)
39-
- [Contributing](#contributing)
40-
- [Releasing new versions or updating dependencies](#releasing-new-versions-or-updating-dependencies)
41-
- [License](#license)
31+
- [Creating your own course](#creating-your-own-course)
32+
- [Creating the Query Checking Action](#creating-the-query-checking-action)
33+
- [Testing the action](#testing-the-action)
34+
- [Adding new queries & calculating the contents for the CSV files](#adding-new-queries--calculating-the-contents-for-the-csv-files)
35+
- [Publishing your action](#publishing-your-action)
36+
- [Contributing your GitHub Action to this repository](#contributing-your-github-action-to-this-repository)
37+
- [Creating the Learning Lab Course](#creating-the-learning-lab-course)
38+
- [Example Courses](#example-courses)
39+
- [Contributing](#contributing)
40+
- [Releasing new versions or updating dependencies](#releasing-new-versions-or-updating-dependencies)
41+
- [License](#license)
4242

4343
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
4444

@@ -207,6 +207,11 @@ run either of these scripts:
207207
affect this specific course,
208208
without publishing any new images.
209209

210+
Both scripts take as argument an **optional** regexp string.
211+
If this string is passed, only the queries
212+
with names matching the regexp will be run.
213+
Otherwise all queries are run.
214+
210215
**In GitHub Actions:**
211216

212217
If adding a course to this repository,
@@ -329,4 +334,3 @@ as per the [terms & conditions](https://securitylab.github.com/tools/codeql/lice
329334
> **To generate CodeQL databases for or during automated analysis,
330335
> continuous integration or continuous delivery,
331336
> whether as part of normal software engineering processes or otherwise.**
332-

codeql-learninglab-check/package/src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ const readFile = promisify(fs.readFile);
1818
const writeFile = promisify(fs.writeFile);
1919

2020
/**
21-
* Must be "true" if all queries should be run (and not just changed queries)
21+
* RUN_ALL must be "true" if all queries should be run (and not just changed queries)
22+
* If specific queries should be run, RUN_ALL cannot be true
2223
*/
2324
const RUN_ALL = process.env.RUN_ALL === 'true';
25+
const QUERY_PATTERN: RegExp | null = process.env.QUERY_PATTERN ? RegExp(process.env.QUERY_PATTERN) : null;
2426

2527
/**
2628
* Set to true to avoid using the GitHub API to post a comment
@@ -115,15 +117,15 @@ function isConfig(config: any): config is Config {
115117
}
116118

117119
/**
118-
* File paths changed by the user (if we're not just running all queries)
120+
* File paths changed by the user (if we're not just running all queries or a specific ones)
119121
*
120122
* This is used to reduce the number of queries we need to run to only those
121123
* that currently interest the user.
122124
*/
123125
const queriesChanged = new Set<string>();
124126
let unableToGetChangedQueries = false;
125127

126-
if (!RUN_ALL) {
128+
if (!RUN_ALL && !QUERY_PATTERN) {
127129

128130
/*
129131
* There are a few different ways in which we may determine which queries
@@ -274,7 +276,7 @@ function isConfig(config: any): config is Config {
274276
for (const query of Object.keys(config.expectedResults)) {
275277
const exists = await access(query, fs.constants.R_OK).then(() => true, () => false);
276278
// Run the query if either it's changed, or runAll is true
277-
if (exists && (RUN_ALL || unableToGetChangedQueries || queriesChanged.has(query))) {
279+
if (exists && (RUN_ALL || unableToGetChangedQueries || queriesChanged.has(query)) || (QUERY_PATTERN && QUERY_PATTERN.test(query))) {
278280
queriesToRun.push(query);
279281
}
280282
}
@@ -362,4 +364,4 @@ function isConfig(config: any): config is Config {
362364
})().catch(err => {
363365
console.error(err);
364366
process.exit(1);
365-
});
367+
});

codeql-learninglab-check/publish.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ set -x
55

66
docker login docker.pkg.github.com -u github-actions -p ${GITHUB_TOKEN}
77

8-
PREV_IMAGE_VERSION=v0.0.7
9-
IMAGE_VERSION=v0.0.8
8+
PREV_IMAGE_VERSION=v0.0.8
9+
IMAGE_VERSION=v0.0.9
1010
IMAGE_PATH=docker.pkg.github.com/github/codeql-learninglab-actions/codeql-learninglab-check
1111
IMAGE_TAG=${IMAGE_PATH}:${IMAGE_VERSION}
1212

@@ -22,4 +22,4 @@ else
2222
docker build -t $IMAGE_TAG .
2323

2424
docker push $IMAGE_TAG
25-
fi
25+
fi

scripts/test-course-actual.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
1616
docker pull $PARENT_TAG
1717

1818
# Run ./test-course-shared.sh
19-
$DIR/test-course-shared.sh
19+
$DIR/test-course-shared.sh $@

scripts/test-course-latest.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
1616
docker build -t $PARENT_TAG $DIR/../codeql-learninglab-check
1717

1818
# Run ./test-course-shared.sh
19-
$DIR/test-course-shared.sh
19+
$DIR/test-course-shared.sh $@

scripts/test-course-shared.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#! /bin/bash
22

3-
# Build the course image, and run all queries in
3+
# Build the course image, and run all or specific queries in
44
# the course to ensure the expected result
55
#
66
# Should be run with the cwd being the course folder
@@ -19,12 +19,24 @@ mkdir -p $TMP
1919
cp -R answers $TMP/answers
2020
echo "{}" > $TMP/event.json
2121

22+
# Get query argument
23+
RUN_ALL=true
24+
QUERY_PATTERN=""
25+
if [ "$1" != "" ]; then
26+
RUN_ALL=false
27+
QUERY_PATTERN=$1
28+
echo "Running specific queries $QUERY_PATTERN"
29+
else
30+
echo "Running all queries"
31+
fi
32+
2233
# Run docker image
2334
docker run -i \
2435
-e GITHUB_EVENT_NAME=push \
2536
-e GITHUB_EVENT_PATH=/opt/tmp/event.json \
2637
-e GITHUB_TOKEN=noop \
27-
-e RUN_ALL=true \
38+
-e RUN_ALL=$RUN_ALL \
39+
-e QUERY_PATTERN=$QUERY_PATTERN \
2840
-e SKIP_COMMENT=true \
2941
-v $TMP:/opt/tmp:ro \
3042
-w /opt/tmp/answers \

0 commit comments

Comments
 (0)