Compare commits

...

32 Commits

Author SHA1 Message Date
cclecle
876e428003 remove dynamic link to doc in readme 2023-03-24 22:38:22 +00:00
cclecle
f32127abd7 revert to static readme file... :-/ 2023-03-24 22:12:08 +00:00
cclecle
b20e5bd868 fix 2023-03-24 21:46:51 +00:00
cclecle
f1b1901f8a format + dynamic readme 2023-03-24 21:37:55 +00:00
cclecle
f63a7b76cd format 2023-03-24 21:08:57 +00:00
cclecle
7ce1a04eb9 formating 2023-03-24 20:28:57 +00:00
cclecle
658eef2df6 update readme 2023-03-24 20:25:16 +00:00
cclecle
3d2b8d3762 adjust colours 2023-03-24 20:02:17 +00:00
cclecle
da9fa475e8 fix csv path 2023-03-24 19:53:01 +00:00
cclecle
93d2046c98 set badge colour 2023-03-24 19:51:24 +00:00
cclecle
fa7eaf0769 update badge_maintainability 2023-03-24 19:47:54 +00:00
cclecle
cad73c4711 add complexity check 2023-03-24 19:26:11 +00:00
cclecle
5621ba99f6 start implementation of complexity measurement 2023-03-24 01:09:10 +00:00
cclecle
c49468e294 fix decimal + colours 2023-03-24 00:19:30 +00:00
cclecle
bec5f9234d fix 2023-03-24 00:11:08 +00:00
cclecle
4e50b8d03f add badge colour
score all /10
2023-03-23 23:56:11 +00:00
cclecle
e74a53e3bf fix tostring() method from BigDecimal 2023-03-23 23:47:46 +00:00
cclecle
ef7469f35b aaaaaaa 2023-03-23 23:28:01 +00:00
cclecle
9e5f0de5c4 add badges in readme
fix float value for quality
2023-03-23 23:23:24 +00:00
cclecle
99f38741fd add code quality badge 2023-03-23 23:13:55 +00:00
cclecle
eee2bf551c bvlabnaal 2023-03-23 23:02:57 +00:00
cclecle
01ce809823 add missing import 2023-03-23 22:57:14 +00:00
cclecle
4ab70409a0 switch to BigDecimal grrrrr 2023-03-23 22:49:08 +00:00
cclecle
7b9752a17a cast to decimalformat 2023-03-23 22:47:52 +00:00
cclecle
879a7ca03d fix wrong method name :-/ 2023-03-23 22:42:07 +00:00
cclecle
bd3389aeb2 cast to double + fix complexity 2023-03-23 22:37:45 +00:00
cclecle
886a22bef2 add missing java lib in groovy 2023-03-23 22:23:57 +00:00
cclecle
e9355471ba cast float to strings 2023-03-23 22:19:48 +00:00
cclecle
3bbbe946a1 switch to readfile 2023-03-23 21:54:21 +00:00
cclecle
cc2f6353ac add coverage results publishing 2023-03-23 21:40:03 +00:00
cclecle
6e1bff135b add missing test 2023-03-23 01:12:51 +00:00
cclecle
f74633cee3 feat: remove coverage pragma because we need to be realistic
test: add missing unit test
2023-03-23 00:07:53 +00:00
9 changed files with 237 additions and 16 deletions

101
Jenkinsfile vendored
View File

@@ -6,7 +6,13 @@
// You should have received a copy of the license along with this
// work. If not, see <https://creativecommons.org/licenses/by-nc-sa/4.0/>.
import groovy.xml.XmlUtil
import static javax.xml.xpath.XPathConstants.*
import javax.xml.xpath.*
import groovy.xml.DOMBuilder
import groovy.xml.dom.DOMCategory
import java.math.RoundingMode
import java.math.BigDecimal
// configurable settings:
// use to send email if workflow problem
@@ -28,6 +34,11 @@ def _MkDocsWebURL = "dabauto--mkdocs-web.dmz.chacha.home/mkdocs-web/"
def _MkDocsWebCredentials = "2c5b684e-3787-4b37-8aca-b3dd4a383fe2"
def _PypiCredentials = "Pypi"
def badge_coverage = addEmbeddableBadgeConfiguration(id: "coverage", subject: "coverage")
def badge_maintainability = addEmbeddableBadgeConfiguration(id: "maintainability", subject: "maintainability")
def badge_quality = addEmbeddableBadgeConfiguration(id: "quality", subject: "quality score")
// commands Helper: /!\ Made for GITEA /!\
String determineRepoUserName() {
return scm.getUserRemoteConfigs()[0].getUrl().tokenize('/')[3].split("\\.")[0]
@@ -63,6 +74,46 @@ String ExtractBaseVersion(inVersion) {
return matcher[0][1]
}
int GetCoverageValue(String CoverageFilePath,String XPath)
{
//File file = new File(CoverageFilePath)
//coverageReportRaw = file.getText('UTF-8')
coverageReportRaw = readFile(CoverageFilePath)
coverageReport = DOMBuilder.parse(new StringReader(coverageReportRaw), false, false)
coverageReportRoot = coverageReport.documentElement
def xpath = XPathFactory.newInstance().newXPath()
res = xpath.evaluate(XPath,coverageReportRoot,NUMBER)
return res
}
String getColorScale(BigDecimal value)
{
if( value >9) { return "Goldenrod"}
else if( value >6) { return "seagreen"}
else if( value >4) { return "orange"}
else if( value >2) { return "darkred"}
else { return "dimgrey"}
}
String getColorScale_reversed(BigDecimal value)
{
if( value >9) { return "dimgrey"}
else if( value >6) { return "darkred"}
else if( value >4) { return "orange"}
else if( value >2) { return "seagreen"}
else { return "Goldenrod"}
}
int GetCoverageValue_lines_valid(String CoverageFilePath) { return GetCoverageValue(CoverageFilePath,"/coverage/@lines-valid") }
int GetCoverageValue_lines_covered(String CoverageFilePath) { return GetCoverageValue(CoverageFilePath,"/coverage/@lines-covered") }
int GetCoverageValue_line_rate(String CoverageFilePath) { return GetCoverageValue(CoverageFilePath,"/coverage/@line-rate") }
int GetCoverageValue_branches_valid(String CoverageFilePath) { return GetCoverageValue(CoverageFilePath,"/coverage/@branches-valid") }
int GetCoverageValue_branches_covered(String CoverageFilePath) { return GetCoverageValue(CoverageFilePath,"/coverage/@branches-covered") }
int GetCoverageValue_branch_rate(String CoverageFilePath) { return GetCoverageValue(CoverageFilePath,"/coverage/@branch-rate") }
int GetCoverageValue_complexity(String CoverageFilePath) { return GetCoverageValue(CoverageFilePath,"/coverage/@complexity") }
pipeline {
// for Docker based build (preferable)
@@ -324,7 +375,7 @@ pipeline {
def wheelPath = findFiles(glob: "**/dist/*.whl")[0]
echo "wheel artifact path: $wheelPath"
// install the package, with *test* optionnal packages, as user
sh(". ~/TEST_ENV/bin/activate && pip install --find-links dist/ ${PY_PROJECT_NAME} .[test,coverage-check,quality-check,type-check,doc-gen]")
sh(". ~/TEST_ENV/bin/activate && pip install --find-links dist/ ${PY_PROJECT_NAME} .[test,coverage-check,quality-check,type-check,doc-gen,complexity-check]")
}
}
}
@@ -334,6 +385,14 @@ pipeline {
steps {
dir("gitrepo") {
sh(". ~/TEST_ENV/bin/activate && python -m helpers --type-check --quality-check")
script {
def jsonObj = readJSON file: "helpers-results/quality_check/metrics.json"
quality_score = new BigDecimal(jsonObj["GlobalScore"])
sz_quality_score = quality_score.setScale(2, RoundingMode.HALF_EVEN).toString()
badge_quality.setStatus(sz_quality_score)
badge_quality.setColor(getColorScale(quality_score))
}
sh(". ~/TEST_ENV/bin/activate && python -m helpers --complexity-check")
}
}
post {
@@ -388,6 +447,14 @@ pipeline {
style: 'stackedArea',
keepRecords: true,
numBuilds: ''])
plot([ csvFileName: 'plot-4ceb9ee2-ca78-11ed-afa1-0242ac120002.csv',
csvSeries: [[ file: 'gitrepo/helpers-results/complexity_check/MI.csv', inclusionFlag: 'INCLUDE_BY_STRING',exclusionValues: 'MeanMaintainability', url: '']],
group: 'metrics',
title: 'maintainability',
style: 'stackedArea',
keepRecords: true,
numBuilds: ''])
}
}
@@ -400,6 +467,36 @@ pipeline {
println unit_test_full_name__html
unit_test_full_name__xml=findFiles(glob: "helpers-results/unit_test_full/*.xml")[0].getName()
println unit_test_full_name__xml
coverage_report_path = "helpers-results/unit_test_coverage/test_coverage.xml"
println GetCoverageValue_lines_valid(coverage_report_path)
println GetCoverageValue_lines_covered(coverage_report_path)
println GetCoverageValue_line_rate(coverage_report_path)
println GetCoverageValue_branches_valid(coverage_report_path)
println GetCoverageValue_branches_covered(coverage_report_path)
println GetCoverageValue_branch_rate(coverage_report_path)
println GetCoverageValue_complexity(coverage_report_path)
full_rate = new BigDecimal( 10*(GetCoverageValue_line_rate(coverage_report_path) + GetCoverageValue_branch_rate(coverage_report_path)) / 2 )
sz_full_rate = full_rate.setScale(2, RoundingMode.HALF_EVEN).toString()
badge_coverage.setStatus(sz_full_rate)
badge_coverage.setColor(getColorScale(full_rate))
//complexity = new BigDecimal( 10*GetCoverageValue_complexity(coverage_report_path))
//sz_complexity = complexity.setScale(2, RoundingMode.HALF_EVEN).toString()
//badge_complexity.setStatus(sz_complexity)
//badge_quality.setColor(getColorScale_reversed(complexity))
//badge_maintainability
records = readCSV file: 'helpers-results/complexity_check/MI.csv'
maintainability = records[1][1]
badge_maintainability.setStatus(maintainability)
if ( maintainability == 'D') { badge_maintainability.setColor( "dimgrey")}
else if( maintainability == 'C') { badge_maintainability.setColor( "darkred")}
else if( maintainability == 'B') { badge_maintainability.setColor( "orange")}
else if( maintainability == 'A') { badge_maintainability.setColor( "seagreen")}
else if( maintainability == 'A+') { badge_maintainability.setColor( "Goldenrod")}
}
}
}

View File

@@ -1,5 +1,10 @@
![](https://chacha.ddns.net/jenkins/buildStatus/icon?subject=status&status=active&color=seagreen)
![](https://chacha.ddns.net/jenkins/buildStatus/icon?subject=doc&status=MkDocs&color=blue)
![](https://chacha.ddns.net/jenkins/buildStatus/icon?subject=jenkins-unittest&job={{repository}}-{{branch}})
![](https://chacha.ddns.net/jenkins/buildStatus/icon?subject=licence&status=CC%20BY-NC-SA%204.0&color=blue)
![](https://chacha.ddns.net/jenkins/buildStatus/icon?job={{repository}}-{{branch}}&build=0&config=coverage)
![](https://chacha.ddns.net/jenkins/buildStatus/icon?job={{repository}}-{{branch}}&build=0&config=maintainability)
![](https://chacha.ddns.net/jenkins/buildStatus/icon?job={{repository}}-{{branch}}&build=0&config=quality)
![](https://chacha.ddns.net/jenkins/buildStatus/icon?subject=licence&status=CC%20BY-NC-SA%204.0&color=teal)
![](docs-static/Library.jpg)
@@ -9,8 +14,7 @@ _A tiny library to help versioning management of git python projects_
Because a good developer is a lazy developer and version management in CI/CD can be very time consuming.
Checkout [Latest Documentation](https://chacha.ddns.net/mkdocs-web/chacha/pygitversionhelper/{{branch}}/latest/).
Checkout [Latest Documentation](https://chacha.ddns.net/mkdocs-web/chacha/pygitversionhelper/master/latest/).
## Features
- list tags
@@ -19,7 +23,8 @@ Checkout [Latest Documentation](https://chacha.ddns.net/mkdocs-web/chacha/pygitv
- get current version (bumped)
- convert / switch from SemVer to PEP440 (both ways)
- automatic version format detection (SemVer by default)
- get commit message history
## Options
- restrict to same branch
- both SemVer and PEP440 support

View File

@@ -23,6 +23,7 @@ if __package__ == "helpers":
from .unit_test import unit_test
from .doc_gen import doc_gen
from .changelog_gen import changelog_gen
from .complexity_check import complexity_check
else:
# when calling the __main__.py file (from IDE)
from helpers.types_check import types_check
@@ -30,6 +31,7 @@ else:
from helpers.unit_test import unit_test
from helpers.doc_gen import doc_gen
from helpers.changelog_gen import changelog_gen
from helpers.complexity_check import complexity_check
logging.getLogger().setLevel(logging.INFO)
@@ -59,6 +61,8 @@ if __name__ == "__main__":
parser.add_argument("-clg", "--changelog-gen", dest="changeloggen", action="store_true", help="enable changelog generation")
parser.add_argument("-cpc", "--complexity-check", dest="complexitycheck", action="store_true", help="enable complexity check")
args = parser.parse_args()
##################################
@@ -73,6 +77,7 @@ if __name__ == "__main__":
# args.docgen = True
# args.docgenpdf = True
# args.changeloggen = True
# args.complexitycheck = True
helpers = []
if args.typecheck == True:
@@ -102,6 +107,9 @@ if __name__ == "__main__":
if args.changeloggen == True:
helpers.append(changelog_gen)
if args.complexitycheck == True:
helpers.append(complexity_check)
for helper in helpers:
helper.set_context(project_rootdir_path, pyproject)
helper.reset_result_dir()

View File

@@ -0,0 +1,70 @@
# pyChaChaDummyProject (c) by chacha
#
# pyChaChaDummyProject is licensed under a
# Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Unported License.
#
# You should have received a copy of the license along with this
# work. If not, see <https://creativecommons.org/licenses/by-nc-sa/4.0/>.
from __future__ import annotations
from typing import TYPE_CHECKING
# from pathlib import Path
# import os
import statistics
import csv
from json import loads as JSON_LOADS
from radon.complexity import cc_rank, SCORE
from radon.cli import Config
from radon.cli.harvest import CCHarvester, HCHarvester, MIHarvester
from .helper_base import helper_withresults_base
from pprint import pprint
class complexity_check(helper_withresults_base):
@classmethod
def do_job(cls):
config = Config(
exclude="__init__\.py",
ignore=None,
order=SCORE,
show_closures=False,
no_assert=True,
min="A",
max="F",
multi=False,
)
h = MIHarvester([str(_) for _ in sorted((cls.project_rootdir_path / "src").rglob("*.py"))], config).as_json()
res = JSON_LOADS(h)
with open(cls.get_result_dir() / "MI.json", "w", newline="") as oFile:
oFile.write(h)
mean = statistics.mean(_["mi"] for _ in res.values())
if mean >= 65:
rank = "A+"
elif mean >= 20:
rank = "A"
elif mean >= 10:
rank = "B"
else:
rank = "C"
RES_MI = {"MeanMaintainability": mean, "MaintainabilityIndex": rank}
with open(cls.get_result_dir() / "MI.csv", "w", newline="") as oFile:
writer = csv.DictWriter(oFile, fieldnames=RES_MI.keys())
writer.writeheader()
writer.writerow(RES_MI)
config = Config(exclude=None, ignore=None, order=SCORE, show_closures=False, no_assert=True, min="A", max="F", multi=False)
h = CCHarvester([str(_) for _ in sorted((cls.project_rootdir_path / "src").rglob("*.py"))], config).as_json()
with open(cls.get_result_dir() / "CC.json", "w", newline="") as oFile:
oFile.write(h)
config = Config(exclude=None, ignore=None, order=SCORE, show_closures=False, no_assert=True, min="A", max="F", by_function=None)
h = HCHarvester([str(_) for _ in sorted((cls.project_rootdir_path / "src").rglob("*.py"))], config).as_json()
with open(cls.get_result_dir() / "HC.json", "w", newline="") as oFile:
oFile.write(h)

View File

@@ -31,8 +31,6 @@ class doc_gen(helper_withresults_base):
@classmethod
def do_job(cls):
print(cls.project_rootdir_path)
print()
# create doc root dir
doc_path = cls.project_rootdir_path / "docs"

View File

@@ -60,6 +60,7 @@ class quality_check(helper_withresults_base):
with redirect_stdout(StdOutput):
pylint_Run(
[
"--load-plugins=pylint.extensions.mccabe",
"--output-format=json,parseable",
"--disable=invalid-name",
"--ignore=_version.py",

View File

@@ -12,12 +12,10 @@ build-backend = "setuptools.build_meta"
[tool.setuptools_scm]
version_scheme= "post-release"
# tag_regex="^(?:v)?(?P<version>\\d+\\.\\d+\\.\\d+)([\\.\\-\\+])?(?:.*)?"
[project]
name = "pygitversionhelper"
description = "pygitversionhelper"
readme = "README.md"
requires-python = ">=3.9"
keywords = ["chacha","chacha","template","pygitversionhelper"]
license = { file = "LICENSE.md" }
@@ -57,6 +55,7 @@ Tracker = "https://chacha.ddns.net/gitea/chacha/pygitversionhelper/issue
[project.optional-dependencies]
test = ["junitparser>=2.8","junit2html>=30.1","xmlrunner>=1.7","mypy>=0.99" ]
coverage-check = ["coverage>=7.0"]
complexity-check = ["radon>=5.1"]
quality-check = ["pylint>=2.15","pylint-json2html>=0.4","pandas>=1.5"]
type-check = ["mypy[reports]>=0.99" ]
doc-gen = ["mkdocs>=1.4.0", "mkdocs-material>=8.5", "mkdocs-localsearch>=0.9.0", "mkdocstrings[python]>=0.19", "mkdocs-with-pdf>=0.9.3","pyyaml>=6.0","pymdown-extensions>=9","mkdocs-markdownextradata-plugin","mkdocs-mermaid2-plugin"]

View File

@@ -58,9 +58,9 @@ def _exec(cmd: str, root: str | os.PathLike | None = None, raw: bool = False) ->
)
if re.search("not a git repository", p.stderr):
raise gitversionhelper.repository.notAGitRepository()
if re.search("fatal:", p.stderr): # pragma: nocover
if re.search("fatal:", p.stderr):
raise gitversionhelper.unknownGITFatalError(p.stderr)
if int(p.returncode) < 0: # pragma: nocover
if int(p.returncode) < 0:
raise gitversionhelper.unknownGITError(p.stderr)
if raw:
@@ -145,7 +145,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
def getMessagesSinceTag(cls, tag: str, **kwargs) -> str:
"""
retrieve a commits message history from repository
from LastCommit to the given tag
from Latest commit to the given tag
Keyword Arguments:
merged_output: output one single merged string
same_branch(bool): force searching only in the same branch
@@ -278,7 +278,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
@classmethod
def getLastTag(cls, **kwargs) -> str | None:
"""
retrieve the last tag from a repository
retrieve the Latest tag from a repository
Keyword Arguments:
same_branch(bool): force searching only in the same branch
Returns:
@@ -293,14 +293,14 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
if len(res) == 0:
raise cls.tagNotFound("no tag found in commit history")
if len(res) != 1: # pragma: nocover
if len(res) != 1:
raise cls.moreThanOneTag("multiple tags on same commit is unsupported")
return res[0]
@classmethod
def getDistanceFromTag(cls, tag: str = None, **kwargs) -> int:
"""
retrieve the distance between HEAD and tag in the repository
retrieve the distance between Latest commit and tag in the repository
Arguments:
tag: reference tag, if None the most recent one will be used
Keyword Arguments:

View File

@@ -298,6 +298,8 @@ class Test_gitversionhelper(unittest.TestCase):
self.assertEqual(_v.pre_count, 0)
self.assertEqual(_v.post_count, 2)
self.assertEqual(_v.doFormatVersion(), "2.1.2+post.2")
self.assertEqual(_v.doFormatVersion(version_std="SemVer"), "2.1.2+post.2")
self.assertEqual(_v.doFormatVersion(version_std="PEP440"), "2.1.2.post2")
_v = _v.bump(bump_type="patch")
self.assertEqual(_v.raw, "2.1.3")
@@ -544,6 +546,15 @@ class Test_gitversionhelper(unittest.TestCase):
self.assertEqual(_v.minor, 1)
self.assertEqual(_v.patch, 0)
def test_nominal__version___getCurrentFormatedVersion(self):
with open("demofile.txt", "w+t") as tmpFile:
tmpFile.write("testvalue")
os.system("git add .")
os.system('git commit -m "first commit"')
os.system(f"git tag 0.2.0")
self.assertEqual(pygitversionhelper.gitversionhelper.version.getCurrentFormatedVersion(), "0.2.0")
def test_nominal__version___AUTO_bump_commits(self):
with open("demofile.txt", "w+t") as tmpFile:
tmpFile.write("testvalue")
@@ -678,6 +689,12 @@ class Test_gitversionhelper(unittest.TestCase):
formated_output=True,
),
)
self.assertEqual(
"0.1.1",
pygitversionhelper.gitversionhelper.version.getLastVersion(
formated_output=True,
),
)
def test_nominal__git__emptyrepo(self):
_v = pygitversionhelper.gitversionhelper.version.getCurrentVersion()
@@ -1253,6 +1270,10 @@ class Test_gitversionhelper(unittest.TestCase):
with self.assertRaises(pygitversionhelper.gitversionhelper.commit.commitNotFound):
pygitversionhelper.gitversionhelper.commit.getLast(same_branch=True)
def test_defect__commit_getMessage_notfound(self):
with self.assertRaises(pygitversionhelper.gitversionhelper.commit.commitNotFound):
pygitversionhelper.gitversionhelper.commit.getMessage("")
def test_nominal__commit_getFromTag(self):
with open("demofile.txt", "w+t") as tmpFile:
tmpFile.write("testvalue")
@@ -1371,6 +1392,28 @@ class Test_gitversionhelper(unittest.TestCase):
res = pygitversionhelper.gitversionhelper.commit.getMessagesSinceTag("0.1.1", merged_output=True)
self.assertEqual(os.linesep.join([commit_message4, commit_message3, commit_message2]), res)
cmd = "git checkout -b dev".split()
subprocess.run(cmd, text=True, check=True)
commit_message5 = "5.1 update this" + os.linesep + "5.1 fix that" + os.linesep + "5.1 test"
commit_message5 = commit_message5.replace("\r\n", "\n").replace("\n", "\r\n")
with open("demofile.txt", "w+t") as tmpFile:
tmpFile.write("testvalue5")
os.system("git add .")
cmd = "git commit -m".split()
cmd.append(commit_message5)
subprocess.run(cmd, text=True, check=True)
cmd = "git switch master".split()
subprocess.run(cmd, text=True, check=True)
res = pygitversionhelper.gitversionhelper.commit.getMessagesSinceTag("0.1.1", merged_output=True, same_branch=True)
self.assertEqual(os.linesep.join([commit_message4, commit_message3, commit_message2]), res)
res = pygitversionhelper.gitversionhelper.commit.getMessagesSinceTag("0.1.1", merged_output=True)
self.assertEqual(os.linesep.join([commit_message5, commit_message4, commit_message3, commit_message2]), res)
def tearDown(self):
os.chdir("/")