Commit d24fad54 authored by Vuillaume's avatar Vuillaume
Browse files

Auto-update codemeta in merge requests

parent 206a99b8
# Set up this script
# 1. Create a new personal access token (https://gitlab.in2p3.fr/-/profile/personal_access_tokens)
# with the following scopes:
# - read_repository
# - write_repository
# 2. Inside Settings -> CI / CD -> Variables, create the following variables:
#
# GITLAB_TOKEN Personal access token previously created.
# (masked)
# GITLAB_USERNAME Username associated with the personal access token.
# COMMIT_MESSAGE Commit message Automatic update of CodeMeta
stages:
- update_codemeta
update_codemeta:
stage: update_codemeta
image: gitlab-registry.in2p3.fr/escape2020/wp3/eossr:dev
before_script:
- apt-get install git -y
# Set the displayed user with the commits that are about to be made
- git config --global user.email "${GIT_USER_EMAIL:-$GITLAB_USER_EMAIL}"
- git config --global user.name "${GIT_USER_NAME:-$GITLAB_USER_NAME}"
- git fetch && git checkout "${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}"
script:
- pip install .
- python eossr/scripts/update_codemeta_eossr.py -c codemeta.json
- cat codemeta.json
# Add all generated files to Git
- git add codemeta.json
- |-
# Check if we have modifications to commit
CHANGES=$(git status --porcelain | wc -l)
if [ "$CHANGES" -gt "0" ]; then
# Show the status of files that are about to be created, updated or deleted
git status
# Commit all changes
git commit -m "Auto-update codemeta.json"
# Update the repository and make sure to skip the pipeline create for this commit
git push https://${GITLAB_USERNAME}:${GITLAB_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git "${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}" -o ci.skip
fi
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH'
......@@ -5,7 +5,9 @@ stages:
- zenodo
- deploy_pages
- pypi_publish
- update_codemeta
include: '.gitlab-ci-update-codemeta.yml'
.junit_template: &junit_definition
artifacts:
......
......@@ -46,8 +46,8 @@
"downloadUrl": "https://gitlab.in2p3.fr/escape2020/wp3/eossr/-/archive/v0.6/eossr-v0.6.zip",
"releaseNotes": "eossr v0.6 Minor bug fixes and documentation improvements. Full changelog: https://gitlab.in2p3.fr/escape2020/wp3/eossr/-/releases/v0.6",
"dateCreated": "2021-08-31",
"datePublished": "2022-04-21",
"dateModified": "2022-04-21",
"datePublished": "2022-04-22",
"dateModified": "2022-04-22",
"operatingSystem": "",
"maintainer": {
"@type": "Person",
......@@ -115,4 +115,4 @@
}
],
"funding": "824064"
}
}
\ No newline at end of file
import argparse
from pathlib import Path
from eossr import ROOT_DIR
from eossr.utils import update_codemeta
def build_argparser():
"""
Construct main argument parser for the ``codemet2zenodo`` script
:return:
argparser: `argparse.ArgumentParser`
"""
parser = argparse.ArgumentParser(description="Update Codemeta")
parser.add_argument(
'--codemeta_path',
'-c',
type=str,
dest='codemeta_path',
help='Path to codemeta.json',
default=Path(ROOT_DIR).joinpath('codemeta.json'),
required=False,
)
return parser
if __name__ == '__main__':
parser = build_argparser()
args = parser.parse_args()
html = update_codemeta(codemeta_path=args.codemeta_path)
......@@ -3,7 +3,6 @@ from pathlib import Path
from eossr import ROOT_DIR
from eossr import __version__ as eossr_version
from eossr.metadata import codemeta
from eossr.utils import update_codemeta
......@@ -28,22 +27,14 @@ def build_argparser():
return parser
def update_version(codemeta_path):
codemeta_handler = codemeta.Codemeta.from_file(codemeta_path)
metadata = codemeta_handler.metadata
metadata[
'downloadUrl'
] = f'https://gitlab.in2p3.fr/escape2020/wp3/eossr/-/archive/v{eossr_version}/eossr-v{eossr_version}.zip'
metadata['version'] = f"v{eossr_version}"
metadata['softwareVersion'] = f"v{eossr_version}"
codemeta_handler.write(path=codemeta_path, overwrite=True)
if __name__ == '__main__':
parser = build_argparser()
args = parser.parse_args()
html = update_codemeta(codemeta_path=args.codemeta_path)
update_version(args.codemeta_path)
download_url = f'https://gitlab.in2p3.fr/escape2020/wp3/eossr/-/archive/v{eossr_version}/eossr-v{eossr_version}.zip'
html = update_codemeta(
codemeta_path=args.codemeta_path,
readme_path=Path(__file__).parent.joinpath('../../README.md').resolve(),
version=eossr_version,
download_url=download_url,
overwrite=True,
)
......@@ -66,7 +66,7 @@ def test_zip_repository():
def test_readme_to_html():
eutils.readme_to_html()
eutils.markdown_to_html()
def test_update_codemeta():
......
......@@ -105,37 +105,61 @@ def zip_repository(repository_path, zip_filename=None, overwrite=True):
return zip_filename
def readme_to_html(readme_path=Path(ROOT_DIR).joinpath('README.md').as_posix()):
def markdown_to_html(filepath):
"""
Read README.md and return html transcript
:param readme_path: str
Read a markdown file and return html transcript
:param filepath: str or Path
:return: str
text in readme converted to html
"""
html = markdown(open(readme_path).read(), extensions=['fenced_code'])
html = markdown(open(filepath).read(), extensions=['fenced_code'])
html = html.replace('\n', '')
return html
def update_codemeta(codemeta_path=Path(ROOT_DIR).joinpath('codemeta.json').as_posix(), published=True, overwrite=True):
def update_codemeta(
codemeta_path=Path(ROOT_DIR).joinpath('codemeta.json').as_posix(),
version=None,
readme_path=None,
modification_date=True,
publication_date=True,
download_url=None,
overwrite=True,
):
"""
Update codemeta.json file for dates and description based on README.md
:param codemeta_path: Path
:param published: boolean
if True, datePublished is updated.
:param overwrite: boolean
overwrite codemeta file
:return:
:param codemeta_path: Path or str
path to the codemeta file to update
:param version: str or None
:param readme_path:
Path to readme in markdown. An html transcript of its content will be used to update the `description` value.
:param modification_date: bool
If True, updates the modification date with today's
:param publication_date: bool
If True, updates the publication date with today's
:param download_url: str
Download URL for the software
:param overwrite: bool
If True, overwrites the codemeta file
"""
with open(codemeta_path) as file:
metadata = json.load(file)
metadata['description'] = readme_to_html()
metadata['dateModified'] = f'{date.today()}'
if published:
if readme_path is not None:
metadata['description'] = markdown_to_html(readme_path)
if modification_date:
metadata['dateModified'] = f'{date.today()}'
if publication_date:
metadata['datePublished'] = f'{date.today()}'
if download_url is not None:
metadata['downloadUrl'] = download_url
if version is not None:
metadata['version'] = f"v{version}"
metadata['softwareVersion'] = f"v{version}"
if overwrite:
with open(codemeta_path, 'w') as file:
json.dump(metadata, file, indent=4)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment