Skip to content

Commit

Permalink
Merge pull request #8 from openfisca/update
Browse files Browse the repository at this point in the history
Migrate to github ctions
  • Loading branch information
benjello authored Nov 14, 2024
2 parents 14b3e71 + 3e5d63e commit 878fe71
Show file tree
Hide file tree
Showing 42 changed files with 993 additions and 916 deletions.
25 changes: 25 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Hello hello !

Je suis le fan numéro un d'OpenFisca, mais je viens de rencontrer un problème.

### Qu'ai-je fait ?


### À quoi m'attendais-je ?


### Que s'est-il passé en réalité ?


### Voici des informations qui peuvent aider à reproduire le problème :


### Contexte

Je m'identifie plus en tant que :

- [ ] Contributeur·e : je contribue à OpenFisca Tunisia-Pension.
- [ ] Développeur·e : je crée des outils qui utilisent OpenFisca Tunisia-Pension.
- [ ] Économiste : je réalise des simulations avec des données.
- [ ] Mainteneur·e : j'intègre les contributions à OpenFisca Tunisia-Pension.
- [ ] Autre : _(ajoutez une description du contexte dans lequel vous utilisez OpenFisca)_.
31 changes: 31 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Merci de contribuer à OpenFisca ! Effacez cette ligne ainsi que, pour chaque ligne ci-dessous, les cas ne correspondant pas à votre contribution :)

* Évolution du système socio-fiscal. | Amélioration technique. | Correction d'un crash. | Changement mineur.
* Périodes concernées : toutes. | jusqu'au JJ/MM/AAAA. | à partir du JJ/MM/AAAA.
* Zones impactées : `chemin/vers/le/fichier/contenant/les/variables/impactées`.
* Détails :
- Description de la fonctionnalité ajoutée ou du nouveau comportement adopté.
- Cas dans lesquels une erreur était constatée.

- - - -

Ces changements (effacez les lignes ne correspondant pas à votre cas) :

- Modifient l'API publique d'OpenFisca Tunisia-Pension (par exemple renommage ou suppression de variables).
- Ajoutent une fonctionnalité (par exemple ajout d'une variable).
- Corrigent ou améliorent un calcul déjà existant.
- Modifient des éléments non fonctionnels de ce dépôt (par exemple modification du README).

- - - -

Quelques conseils à prendre en compte :

- [ ] Jetez un coup d'œil au [guide de contribution](https://github.com/openfisca/openfisca-tunisia-pension-pension/blob/master/CONTRIBUTING.md).
- [ ] Regardez s'il n'y a pas une [proposition introduisant ces mêmes changements](https://github.com/openfisca/openfisca-tunisia-pension/pulls).
- [ ] Documentez votre contribution avec des références législatives.
- [ ] Mettez à jour ou ajoutez des tests correspondant à votre contribution.
- [ ] Augmentez le [numéro de version](https://speakerdeck.com/mattisg/git-session-2-strategies?slide=81) dans [`pyproject.toml`](https://github.com/openfisca/openfisca-tunisia-pension/blob/master/pyproject.toml).
- [ ] Mettez à jour le [`CHANGELOG.md`](https://github.com/openfisca/openfisca-tunisia-pension/blob/master/CHANGELOG.md).
- [ ] Assurez-vous de bien décrire votre contribution, comme indiqué ci-dessus

Et surtout, n'hésitez pas à demander de l'aide ! :)
9 changes: 9 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: 2
updates:
- package-ecosystem: pip
directory: "/"
schedule:
interval: monthly
labels:
- kind:dependencies
open-pull-requests-limit: 2
14 changes: 14 additions & 0 deletions .github/get_minimal_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import re
import tomli
# This script prints the minimal version of Openfisca-Core to ensure their compatibility during CI testing
with open('./pyproject.toml', 'rb') as file:
config = tomli.load(file)
deps = config['project']['dependencies']
for dep in deps:
version = re.search(r'openfisca-core\[([^\]]+)\]\s*>=\s*([\d\.]*)', dep)
if version:
try:
print(f'openfisca-core[{version[1]}]=={version[2]}') # noqa: T201 <- This is to avoid flake8 print detection.
except Exception as e:
print(f'Error processing "{dep}": {e}') # noqa: T201 <- This is to avoid flake8 print detection.
exit(1)
51 changes: 51 additions & 0 deletions .github/get_pypi_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import argparse
import requests
import logging


logging.basicConfig(level=logging.INFO)


def get_info(package_name: str = '') -> dict:
'''
Get minimal informations needed by .conda/meta.yaml from PyPi JSON API.
::package_name:: Name of package to get infos from.
::return:: A dict with last_version, url and sha256
'''
if package_name == '':
raise ValueError('Package name not provided.')
resp = requests.get(f'https://pypi.org/pypi/{package_name}/json').json()
version = resp['info']['version']
for v in resp['releases'][version]:
if v['packagetype'] == 'sdist': # for .tag.gz
return {
'last_version': version,
'url': v['url'],
'sha256': v['digests']['sha256']
}


def replace_in_file(filepath: str, info: dict):
'''
::filepath:: Path to meta.yaml, with filename
::info:: Dict with information to populate
'''
with open(filepath, 'rt') as fin:
meta = fin.read()
# Replace with info from PyPi
meta = meta.replace('PYPI_VERSION', info['last_version'])
meta = meta.replace('PYPI_URL', info['url'])
meta = meta.replace('PYPI_SHA256', info['sha256'])
with open(filepath, 'wt') as fout:
fout.write(meta)
logging.info(f'File {filepath} has been updated with informations from PyPi.')


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--package', type=str, default='', required=True, help='The name of the package')
parser.add_argument('-f', '--filename', type=str, default='.conda/meta.yaml', help='Path to meta.yaml, with filename')
args = parser.parse_args()
info = get_info(args.package)
logging.info(f'Information of the last published PyPi package : {info}')
replace_in_file(args.filename, info)
12 changes: 12 additions & 0 deletions .github/has-functional-changes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#! /usr/bin/env bash

IGNORE_DIFF_ON="README.md CONTRIBUTING.md Makefile .gitignore .github/*"

last_tagged_commit=`git describe --tags --abbrev=0 --first-parent` # --first-parent ensures we don't follow tags not published in master through an unlikely intermediary merge commit

if git diff-index --name-only --exit-code $last_tagged_commit -- . `echo " $IGNORE_DIFF_ON" | sed 's/ / :(exclude)/g'` # Check if any file that has not be listed in IGNORE_DIFF_ON has changed since the last tag was published.
then
echo "No functional changes detected."
exit 1
else echo "The functional files above were changed."
fi
45 changes: 45 additions & 0 deletions .github/is-version-number-acceptable.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#! /usr/bin/env bash

if [[ ${GITHUB_REF#refs/heads/} == master ]]
then
echo "No need for a version check on master."
exit 0
fi

if ! $(dirname "$BASH_SOURCE")/has-functional-changes.sh
then
echo "No need for a version update."
exit 0
fi

current_version=$(python `dirname "$BASH_SOURCE"`/pyproject_version.py --only_package_version True) # parsing with tomllib is complicated, see https://github.com/python-poetry/poetry/issues/273
if [ $? -eq 0 ]; then
echo "Package version in pyproject.toml : $current_version"
else
echo "ERROR getting current version: $current_version"
exit 3
fi

if [[ -z $current_version ]]
then
echo "Error getting current version"
exit 1
fi

if git rev-parse --verify --quiet $current_version
then
echo "Version $current_version already exists in commit:"
git --no-pager log -1 $current_version
echo
echo "Update the version number in pyproject.toml before merging this branch into master."
echo "Look at the CONTRIBUTING.md file to learn how the version number should be updated."
exit 2
fi

if ! $(dirname "$BASH_SOURCE")/has-functional-changes.sh | grep --quiet CHANGELOG.md
then
echo "CHANGELOG.md has not been modified, while functional changes were made."
echo "Explain what you changed before merging this branch into master."
echo "Look at the CONTRIBUTING.md file to learn how to write the changelog."
exit 2
fi
11 changes: 11 additions & 0 deletions .github/lint-changed-python-files.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#! /usr/bin/env bash

last_tagged_commit=`git describe --tags --abbrev=0 --first-parent` # --first-parent ensures we don't follow tags not published in master through an unlikely intermediary merge commit

if ! changes=$(git diff-index --name-only --diff-filter=ACMR --exit-code $last_tagged_commit -- "*.py")
then
echo "Linting the following Python files:"
echo $changes
flake8 $changes
else echo "No changed Python files to lint"
fi
11 changes: 11 additions & 0 deletions .github/lint-changed-yaml-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#! /usr/bin/env bash

last_tagged_commit=`git describe --tags --abbrev=0 --first-parent` # --first-parent ensures we don't follow tags not published in master through an unlikely intermediary merge commit

if ! changes=$(git diff-index --name-only --diff-filter=ACMR --exit-code $last_tagged_commit -- "tests/*.yaml")
then
echo "Linting the following changed YAML tests:"
echo $changes
yamllint $changes
else echo "No changed YAML tests to lint"
fi
5 changes: 5 additions & 0 deletions .github/publish-git-tag.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /usr/bin/env bash

current_version=$(grep '^version =' pyproject.toml | cut -d '"' -f 2) # parsing with tomllib is complicated, see https://github.com/python-poetry/poetry/issues/273
git tag $current_version
git push --tags # update the repository version
82 changes: 82 additions & 0 deletions .github/pyproject_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Read package version in pyproject.toml and replace it in .conda/recipe.yaml

import argparse
import logging
import re

logging.basicConfig(level=logging.INFO, format='%(message)s')
PACKAGE_VERSION = 'X.X.X'
CORE_VERSION = '>=43,<44'
NUMPY_VERSION = '>=1.24.3,<2'


def get_versions():
'''
Read package version and deps in pyproject.toml
'''
openfisca_core_api = None
openfisca_tunisia_pension = None
with open('./pyproject.toml', 'r') as file:
content = file.read()
# Extract the version of openfisca_tunisia-pension
version_match = re.search(r'^version\s*=\s*"([\d.]*)"', content, re.MULTILINE)
if version_match:
openfisca_tunisia_pension = version_match.group(1)
else:
raise Exception('Package version not found in pyproject.toml')
# Extract dependencies
version = re.search(r'openfisca-core\[web-api\]\s*(>=\s*[\d\.]*,\s*<\d*)"', content, re.MULTILINE)
if version:
openfisca_core_api = version.group(1)
version = re.search(r'numpy\s*(>=\s*[\d\.]*,\s*<\d*)"', content, re.MULTILINE)
if version:
numpy = version.group(1)
if not openfisca_core_api or not numpy:
raise Exception('Dependencies not found in pyproject.toml')
return {
'openfisca_tunisia_pension': openfisca_tunisia_pension,
'openfisca_core_api': openfisca_core_api.replace(' ', ''),
'numpy': numpy.replace(' ', ''),
}


def replace_in_file(filepath: str, info: dict):
'''
::filepath:: Path to meta.yaml, with filename
::info:: Dict with information to populate
'''
with open(filepath, 'rt') as fin:
meta = fin.read()
# Replace with info from pyproject.toml
if PACKAGE_VERSION not in meta:
raise Exception(f'{PACKAGE_VERSION=} not found in {filepath}')
meta = meta.replace(PACKAGE_VERSION, info['openfisca_tunisia-pension'])
if CORE_VERSION not in meta:
raise Exception(f'{CORE_VERSION=} not found in {filepath}')
meta = meta.replace(CORE_VERSION, info['openfisca_core_api'])
if NUMPY_VERSION not in meta:
raise Exception(f'{NUMPY_VERSION=} not found in {filepath}')
meta = meta.replace(NUMPY_VERSION, info['numpy'])
with open(filepath, 'wt') as fout:
fout.write(meta)
logging.info(f'File {filepath} has been updated with informations from pyproject.toml.')


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-r', '--replace', type=bool, default=False, required=False, help='replace in file')
parser.add_argument('-f', '--filename', type=str, default='.conda/recipe.yaml', help='Path to recipe.yaml, with filename')
parser.add_argument('-o', '--only_package_version', type=bool, default=False, help='Only display current package version')
args = parser.parse_args()
info = get_versions()
file = args.filename
if args.only_package_version:
print(f'{info["openfisca_tunisia-pension"]}') # noqa: T201
exit()
logging.info('Versions :')
print(info) # noqa: T201
if args.replace:
logging.info(f'Replace in {file}')
replace_in_file(file, info)
else:
logging.info('Dry mode, no replace made')
22 changes: 22 additions & 0 deletions .github/split_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import sys
from glob import glob


def split_tests(number_of_files, CI_NODE_TOTAL, CI_NODE_INDEX, test_files_list):
test_files_sublist = []

for file_index in range(number_of_files):
file_number = file_index % CI_NODE_TOTAL
if file_number == CI_NODE_INDEX:
test_files_sublist.append(test_files_list[file_index])

tests_to_run_string = ' '.join(test_files_sublist)

return tests_to_run_string


if __name__ == '__main__':
CI_NODE_TOTAL, CI_NODE_INDEX = int(sys.argv[1]), int(sys.argv[2])
test_files_list = glob('tests/**/*.yaml', recursive=True) + glob('tests/**/*.yml', recursive=True)
number_of_files = len(test_files_list)
sys.stdout.write(split_tests(number_of_files, CI_NODE_TOTAL, CI_NODE_INDEX, test_files_list))
14 changes: 14 additions & 0 deletions .github/test-api.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#! /usr/bin/env bash

PORT=5000
ENDPOINT=spec

openfisca serve --country-package openfisca_tunisia-pension --port $PORT --workers 1 &
server_pid=$!

curl --retry-connrefused --retry 10 --retry-delay 5 --fail http://127.0.0.1:$PORT/$ENDPOINT | python -m json.tool > /dev/null
result=$?

kill $server_pid

exit $?
Loading

0 comments on commit 878fe71

Please sign in to comment.