From 26d10bbb8e240c75da37e7d91f38e28788c00fa6 Mon Sep 17 00:00:00 2001
From: Edward Kirk <ek599@cam.ac.uk>
Date: Mon, 22 Jul 2024 16:24:31 +0100
Subject: [PATCH] wip: add tooling configuration

---
 .flake8                 |  6 +++++
 .gitlab-ci.yml          | 14 ++++++++++
 .pre-commit-config.yaml | 59 +++++++++++++++++++++++++++++++++++++++++
 CHANGELOG.md            |  8 ++++++
 pyproject.toml          |  2 +-
 sprint_stats.py         | 37 +++++++++++++++++---------
 tox.ini                 | 52 ++++++++++++++++++++++++++++++++++++
 7 files changed, 164 insertions(+), 14 deletions(-)
 create mode 100644 .flake8
 create mode 100644 .gitlab-ci.yml
 create mode 100644 .pre-commit-config.yaml
 create mode 100644 CHANGELOG.md
 create mode 100644 tox.ini

diff --git a/.flake8 b/.flake8
new file mode 100644
index 0000000..3ea3d9d
--- /dev/null
+++ b/.flake8
@@ -0,0 +1,6 @@
+[flake8]
+max-line-length=99
+exclude = .venv,venv,.tox,*/migrations/*,*/frontend/*,build/*
+# Ignore E203, see explanation here:
+# https://black.readthedocs.io/en/stable/guides/using_black_with_other_tools.html#id1
+extend-ignore = E203
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 0000000..300dbf4
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,14 @@
+include:
+  - project: 'uis/devops/continuous-delivery/ci-templates'
+    file: '/auto-devops/common-pipeline.yml'
+    ref: v4.0.0
+  - project: "uis/devops/continuous-delivery/ci-templates"
+    file: "/auto-devops/artifact-registry.yml"
+    ref: v4.0.0
+
+python:tox:
+  variables:
+    TOX_ADDITIONAL_REQUIREMENTS: "poetry>=1.7.1"
+
+variables:
+  DAST_DISABLED: true
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
new file mode 100644
index 0000000..4580f88
--- /dev/null
+++ b/.pre-commit-config.yaml
@@ -0,0 +1,59 @@
+# See https://pre-commit.com/hooks.html for more hooks
+repos:
+  - repo: https://github.com/pre-commit/pre-commit-hooks
+    rev: v4.6.0
+    hooks:
+      - id: trailing-whitespace
+      - id: end-of-file-fixer
+      - id: check-yaml
+        args:
+          - --unsafe
+      - id: check-json
+      - id: check-toml
+      - id: check-xml
+      - id: check-added-large-files
+      - id: check-executables-have-shebangs
+      - id: check-merge-conflict
+      - id: check-symlinks
+      - id: detect-private-key
+      - id: mixed-line-ending
+      - id: pretty-format-json
+        args:
+          - --autofix
+          - --no-sort-keys
+      - id: debug-statements
+
+  - repo: https://github.com/python-poetry/poetry
+    rev: 1.8.0
+    hooks:
+      - id: poetry-check
+
+  - repo: https://github.com/psf/black
+    rev: 24.4.2
+    hooks:
+      - id: black
+
+  - repo: https://github.com/timothycrosley/isort
+    rev: 5.13.2
+    hooks:
+      - id: isort
+
+  - repo: https://github.com/pycqa/flake8
+    rev: 7.1.0
+    hooks:
+      - id: flake8
+
+  - repo: https://github.com/pre-commit/mirrors-mypy
+    rev: v1.11.0
+    hooks:
+      - id: mypy
+        additional_dependencies: ["types-decorator"]
+
+  - repo: local
+    hooks:
+      - id: trivy
+        name: trivy
+        language: docker_image
+        pass_filenames: false
+        entry: aquasec/trivy:latest
+        args: ["--cache-dir", "/tmp/.trivy-cache", "--skip-dirs", "tests", "config", ".", "--exit-code", "1"]
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..a1f3785
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,8 @@
+# Change Log
+
+
+## [0.0.1]
+
+### Added
+
+- POC/rough code for Wilson team
diff --git a/pyproject.toml b/pyproject.toml
index 7ca8fb3..d5df84b 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
 [tool.poetry]
 name = "gitlab-stats"
-version = "0.1.0"
+version = "0.0.1"
 description = ""
 authors = ["Edward Kirk <ek599@cam.ac.uk>"]
 readme = "README.md"
diff --git a/sprint_stats.py b/sprint_stats.py
index 16f4198..ad43903 100644
--- a/sprint_stats.py
+++ b/sprint_stats.py
@@ -1,9 +1,11 @@
-import gitlab
 import os
-import pprint
+
+import gitlab
 
 # private token or personal token authentication (self-hosted GitLab instance)
-gl = gitlab.Gitlab(url='https://gitlab.developers.cam.ac.uk/', private_token=os.environ["GITLAB_TOKEN"])
+gl = gitlab.Gitlab(
+    url="https://gitlab.developers.cam.ac.uk/", private_token=os.environ["GITLAB_TOKEN"]
+)
 
 # Test auth
 gl.auth()
@@ -14,32 +16,41 @@ iam_iterations = []
 iterations = devops_group.iterations.list(iterator=True, cadence=14)
 for iteration in iterations:
     iam_iterations.append(iteration)
- 
+
 for iteration in iam_iterations:
-    
+
     iteration_id = iteration.id
 
-    issues = gl.issues.list(iterator=True, state="closed", labels=["team::Identity"], iteration_id=iteration_id, order="start_date", scope="all")
- 
+    issues = gl.issues.list(
+        iterator=True,
+        state="closed",
+        labels=["team::Identity"],
+        iteration_id=iteration_id,
+        order="start_date",
+        scope="all",
+    )
+
     total_weight = 0
     total_time_days = 0
     ticket_count = len(issues)
-    
+
     for issue in issues:
         try:
             if issue.weight:
                 total_weight += issue.weight
         except AttributeError:
             pass
-        
-        #issue.time_stats["time_estimate"] == seconds w/ 8 hours == 1 day
+
+        # issue.time_stats["time_estimate"] == seconds w/ 8 hours == 1 day
         total_time_days += issue.time_stats["time_estimate"] / 60 / 60 / 8
-        
+
     if ticket_count > 0:
         print(f"iteration:{iteration.start_date}  id:{iteration.id}")
-        print(f"estimate days: {total_time_days} weight: {total_weight} ticket count {ticket_count}")
+        print(
+            f"estimate days: {total_time_days} weight: {total_weight} ticket count {ticket_count}"
+        )
         print("")
 
 
 # FIXME
-# Calculate mean for last quarter of work performed 
+# Calculate mean for last quarter of work performed
diff --git a/tox.ini b/tox.ini
new file mode 100644
index 0000000..0a7485b
--- /dev/null
+++ b/tox.ini
@@ -0,0 +1,52 @@
+# Tox runner configuration
+#
+# The following optional environment variables can change behaviour. See the
+# comments where they are used for more information.
+#
+# - TOXINI_ARTEFACT_DIR
+# - TOXINI_FLAKE8_VERSION
+# - TOXINI_WORK_DIR
+#
+[tox]
+# Envs which should be run by default.
+envlist=py3
+# Allow overriding toxworkdir via environment variable
+toxworkdir={env:TOXINI_WORK_DIR:{toxinidir}/.tox}
+# We do not actually ship a setup.py file which is used by our deployment.
+# Django projects instead prefer to add the project directories to the Python
+# path. (Or, with the current working directory being the project.) The
+# skipsdist option tells tox it is OK to not run "setup.py install" for our
+# project.
+skipsdist=True
+
+# The "_vars" section is ignored by tox but we place some useful shared
+# variables in it to avoid needless repetition.
+[_vars]
+# Where to write build artefacts. We default to the "build" directory in the
+# tox.ini file's directory. Override with the TOXINI_ARTEFACT_DIR environment
+# variable.
+build_root={env:TOXINI_ARTEFACT_DIR:{toxinidir}/build}
+
+[testenv]
+# Which environment variables should be passed into the environment.
+passenv=
+    EXTERNAL_SETTING_*
+#   Allow people to override the coverage report location should they so wish.
+    COVERAGE_FILE
+# How to run the test suite. Note that arguments passed to tox are passed on to
+# the test command.
+allowlist_externals=
+    poetry
+commands_pre=
+    poetry install --no-root --sync
+commands=
+# Unlike many other libraries, we are running tests provided in the 'example'
+# directory, which is very explicitly not a package. Therefore, we pass the test
+# file directly as an argument to pytest.
+    coverage run --source={toxinidir} -m pytest --junitxml={[_vars]build_root}/junit.xml {posargs}
+    coverage html --directory {[_vars]build_root}/htmlcov/
+    coverage report
+    coverage xml -o {env:COVERAGE_XML_FILE:{[_vars]build_root}/coverage.xml}
+
+[testenv:py3]
+basepython=python3
-- 
GitLab