Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CodeQL workflow for GitHub code scanning and fix few bugs it detected #1165

Merged
merged 4 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: "CodeQL"

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
schedule:
- cron: "31 5 * * 5"

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ python ]

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
queries: +security-and-quality
config-file: codeql.yml

- name: Autobuild
uses: github/codeql-action/autobuild@v2

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: "/language:${{ matrix.language }}"
4 changes: 4 additions & 0 deletions codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
paths-ignore:
- dandi/_version.py
- dandi/due.py
- versioneer.py
8 changes: 5 additions & 3 deletions dandi/cli/tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ def test_no_heavy_imports():
[
sys.executable,
"-c",
"import sys; "
"import dandi.cli.command; "
"print(','.join(set(m.split('.')[0] for m in sys.modules)));",
(
"import sys; "
"import dandi.cli.command; "
"print(','.join(set(m.split('.')[0] for m in sys.modules)));"
),
],
env=env,
stdout=PIPE,
Expand Down
6 changes: 4 additions & 2 deletions dandi/cli/tests/test_ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ def test_ls_path_url():
[
"-f",
"yaml",
"https://api.dandiarchive.org/api/dandisets/000027/versions/draft"
"/assets/?path=sub-RAT123/",
(
"https://api.dandiarchive.org/api/dandisets/000027/versions/draft"
"/assets/?path=sub-RAT123/"
),
],
)
assert r.exit_code == 0, r.output
Expand Down
2 changes: 1 addition & 1 deletion dandi/dandiapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ def __str__(self) -> str:

@classmethod
def from_base_data(
self,
cls,
client: "DandiAPIClient",
data: Dict[str, Any],
metadata: Optional[Dict[str, Any]] = None,
Expand Down
8 changes: 4 additions & 4 deletions dandi/support/pyout.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ def naturalsize(v):
return humanize.naturalsize(v)


def datefmt(v, fmt=u"%Y-%m-%d/%H:%M:%S"):
def datefmt(v, fmt="%Y-%m-%d/%H:%M:%S"):
if isinstance(v, datetime.datetime):
return v.strftime(fmt)
else:
time.strftime(fmt, time.localtime(v))
return time.strftime(fmt, time.localtime(v))


# def empty_for_none(v):
Expand Down Expand Up @@ -71,8 +71,8 @@ def counts(values):
color=dict(
interval=[
[0, 1024, "blue"],
[1024, 1024 ** 2, "green"],
[1024 ** 2, None, "red"],
[1024, 1024**2, "green"],
[1024**2, None, "red"],
]
),
aggregate=lambda x: naturalsize(sum(x)),
Expand Down
22 changes: 5 additions & 17 deletions dandi/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import re
import shutil
from subprocess import DEVNULL, check_output, run
import tempfile
from time import sleep
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterator, List, Optional, Union
from uuid import uuid4
Expand Down Expand Up @@ -246,30 +245,19 @@ def get_gitrepo_fixture(
url: str,
committish: Optional[str] = None,
scope: Scope = "session",
) -> Callable[[], Iterator[str]]:
) -> Callable[[pytest.TempPathFactory], str]:

if committish:
raise NotImplementedError()

@pytest.fixture(scope=scope)
def fixture() -> Iterator[str]:
def fixture(tmp_path_factory: pytest.TempPathFactory) -> str:
skipif.no_network()
skipif.no_git()

path = tempfile.mktemp() # not using pytest's tmpdir fixture to not
# collide in different scopes etc. But we
# would need to remove it ourselves
path = str(tmp_path_factory.mktemp("gitrepo"))
lgr.debug("Cloning %r into %r", url, path)
try:
runout = run(["git", "clone", "--depth=1", url, path])
if runout.returncode:
raise RuntimeError(f"Failed to clone {url} into {path}")
yield path
finally:
try:
shutil.rmtree(path)
except BaseException as exc:
lgr.warning("Failed to remove %s - using Windows?: %s", path, exc)
run(["git", "clone", "--depth=1", url, path], check=True)
return path

return fixture

Expand Down