GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Move logs, SARIF, database bundle actions uploads to post: hooks by angelapwen · Pull Request #1159 · github/codeql-action Conversation
|
|
I'm correctly hitting the case where the database isn't finalized now, and just need to implement the zipping of the partial database files. I see documentation for the CLI command but am still not exactly sure which files I'm looking to zip. I'll take a look at the CLI source code but would appreciate any pointers! Also, I see I have a CI failure on debug artifact which looks legitimate, as that's the code I'm changing. It only fails on ubuntu, as for some reason the step to download the artifact doesn't begin on the Mac runners. It looks like this is because the step to download the artifacts is executed before the I see the PR check comes from
analyze step. Not entirely sure how to make sure it executes only after the post: hooks as I guess the entire point is that the post: hooks happen after everything else. 🤔
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I think perhaps splitting the PR check file into 2 jobs, one for running everything before checking artifact downloads, and one for just checking artifact downloads with a |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
| let suffix = ""; | ||
| const matrix = getRequiredInput("matrix"); | ||
| if (matrix !== undefined && matrix !== "null") { | ||
| for (const entry of Object.entries(JSON.parse(matrix)).sort()) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
What happens if matrix is not valid json? How is the error handled?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Hm — I actually didn't write this (just moved it to another section of the codebase) but that's a good question. I had thought that it was forced to be JSON from where it was declared in inputs:
Lines 14 to 15 in 19d025e
| matrix: | |
| default: ${{ toJson(matrix) }} |
and
https://github.com/github/codeql-action/blob/main/analyze/action.yml#L67-L68
but it seems like that's the default input and the user could theoretically have passed non-valid JSON.
I am adding a catch block with an error message indicating that there was an error parsing the matrix input and the debug artifact will be uploaded without the matrix input in its name. It seems like the only thing this block of code does is add a suffix to the artifact name, for every value in the matrix input, so it should be able to still upload the artifact correctly (just without the appropriate suffix(es).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
| } | ||
| } | ||
|
|
||
| async function uploadLogsDebugArtifact(config: Config) { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Same comment here about making this module as small as possible, and passing in actionsUtil.uploadDebugArtifacts as a function parameter.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Co-authored-by: Andrew Eisenberg <[email protected]>
Co-authored-by: Andrew Eisenberg <[email protected]>
|
I've made most of the requested changes, and will work on the refactoring for unit tests. I also made the change to zip up the database bundle and it seems to be working as expected on a failed run. The and then the zip file is uploaded as part of the artifacts. I can't attach it here due to file size limits but here is the directory structure of the partial database bundle file when unzipped: |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I added |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Good work! The overall logic looks sensible -- suggestions mostly about clarity and readability.
Let me know when you've added some tests and would like another look.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| import * as core from "@actions/core"; | ||
| import * as toolrunner from "@actions/exec/lib/toolrunner"; | ||
| import * as safeWhich from "@chrisgavin/safe-which"; | ||
| import AdmZip from "adm-zip"; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
We already use the zlib library; I think we could reuse that in this situation rather than adding a dependency. One difference is that zlib produces gzip.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Ah interesting. I will give this a try!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Ah, I remembered that I had some trouble with zlib when I tried it out earlier because it seems to only compress streams of data, and it makes preserving the directory structure difficult. I looked around 👀 and see another package that can create zipped directories using zlib for compression: https://github.com/archiverjs/node-archiver
But as this would also add a new dependency I'm not sure if it's worthwhile 🤔
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Let's stick with what you've got for now. There is an astounding number of ways to do this in the npm ecosystem, and I don't have the expertise/familiarity to know whether any one is best, so I won't hold this up. adm-zip is widely used and has no other dependencies, which is nice. (In the VS Code extension I see we use zip-a-folder.)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Quickly closing and re-opening to trigger new PR checks from actions/toolkit#1160 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
That looks good. The other option I considered was turning this workflow off on PRs, and only running it on push events. Let's keep this for now, and if the "failing" check confuses other contributors we can adjust it. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
ea909e8 to
ff7a29d
Compare
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Some minor suggestions while you polish up the tests.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
| import * as core from "@actions/core"; | ||
| import * as toolrunner from "@actions/exec/lib/toolrunner"; | ||
| import * as safeWhich from "@chrisgavin/safe-which"; | ||
| import AdmZip from "adm-zip"; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Let's stick with what you've got for now. There is an astounding number of ways to do this in the npm ecosystem, and I don't have the expertise/familiarity to know whether any one is best, so I won't hold this up. adm-zip is widely used and has no other dependencies, which is nice. (In the VS Code extension I see we use zip-a-folder.)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
| import * as debugArtifacts from "./debug-artifacts"; | ||
| import { getActionsLogger } from "./logging"; | ||
|
|
||
| async function run(uploadSarifDebugArtifact: Function) { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
For easier testing, you can move this function to a different file and test that file instead. This way, you avoid invoking runWrapper by default.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I've done this, though I didn't know what to call the other file (not sure what patterns we want to follow). I called it analyze-action-post-helper but would like to hear what you think!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| /** | ||
| * If a database has not been finalized, we cannot run the `codeql database bundle` | ||
| * command in the CLI because it will return an error. Instead we directly zip |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
That's unfortunate and avoids a good use case for codeql database bundle. Do you know why it throws? I wonder if we could add a --force option (or something similar) to allow the command to succeed even if the database is malformed.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I did a little digging into the CLI source code, but I thought it'd be too complex to somehow catch the error thrown in the action and then do something different. A --force option would be interesting though and isn't something I'd considered.
We'd like to get this change in this week but I can write up a follow-up issue on the --force option on the CLI side for our backlog.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Right...there's no need to get this done now.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Co-authored-by: Aditya Sharad <[email protected]>
Co-authored-by: Andrew Eisenberg <[email protected]>
There was an error while loading. Please reload this page.
|
The remaining unit tests are up, requesting final review now 😄 After this is merged, I will add the "Download and check debug artifacts after failure in analyze" PR check/job to the required PR checks for each branch. I'll also write up an issue on improving the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| test("post: analyze action with debug mode off", async (t) => { | ||
| return await util.withTmpDir(async (tmpDir) => { | ||
| process.env["RUNNER_TEMP"] = tmpDir; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You may want to save the old value and restore it at the end of this test, so it doesn't affect the environment for later tests. Or turn it into a stub.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Hm — I see it used this way without restoring in several other existing tests like
codeql-action/src/config-utils.test.ts
Line 626 in a6d0901
| process.env["RUNNER_TEMP"] = tmpDir; |
Is there a reason this one would act differently from other tests?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Since it's used elsewhere in this way, it's probably fine. I would recommend in the future that we do what Aditya suggests in all the tests. The danger if we don't do this is there may be hard to spot dependencies between tests that use this environment variable.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I see we also have this helper function that is called quite a lot
codeql-action/src/testing-utils.ts
Line 102 in a6d0901
| export function setupActionsVars(tempDir: string, toolsDir: string) { |
although we don't need the other two environment variables here.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Hm, does this line
codeql-action/src/testing-utils.ts
Line 96 in cade2b5
| process.env = t.context.env; |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Yes...it does. I also see sinon.restore();, which makes my comment about using a sandbox irrelevant.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
| } as unknown as configUtils.Config); | ||
|
|
||
| const requiredInputStub = sinon.stub(actionsUtil, "getRequiredInput"); | ||
| requiredInputStub.withArgs("output").returns("fake-output-dir"); |
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
On a related note, I see that in some places we use ${STUB}.restore() to clean up at the end of a test, but in others we don't. Is this necessary or best practice?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Same as above, I think this line
codeql-action/src/testing-utils.ts
Line 93 in cade2b5
| sinon.restore(); |
sinon stubs after each test?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Actually...we should do that everywhere. The vscode extension uses a different (and better) idiom, which we should move to in the action.
Instead of creating stubs directly using sinon.stub, use a sandbox:
let sandbox: sinon.SinonSandbox;
beforeEach(() => {
sandbox = sinon.createSandbox();
});
afterEach(() => {
sandbox.restore();
});
And create stubs using: sandbox.stub(). This ensures all stubs are removed after every test runs. In the action, we are not doing this. So far, it hasn't caused any problems, but it might later.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
For now it looks like as long as we call
codeql-action/src/testing-utils.ts
Line 49 in cade2b5
| export function setupTests(test: TestFn<any>) { |
in each test file it should call sinon.restore() and also restore the environment variables, right?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Yes....I missed that. So, you can ignore my comment.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Ok, great! This was educational 🧑🎓
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| test("post: analyze action with debug mode off", async (t) => { | ||
| return await util.withTmpDir(async (tmpDir) => { | ||
| process.env["RUNNER_TEMP"] = tmpDir; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Since it's used elsewhere in this way, it's probably fine. I would recommend in the future that we do what Aditya suggests in all the tests. The danger if we don't do this is there may be hard to spot dependencies between tests that use this environment variable.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| /** | ||
| * If a database has not been finalized, we cannot run the `codeql database bundle` | ||
| * command in the CLI because it will return an error. Instead we directly zip |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Right...there's no need to get this done now.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.