From 0955d8fc972a3b388cfe0e7823270916912cec0e Mon Sep 17 00:00:00 2001 From: Paul STRETENOWICH <31796146+paulstretenowich@users.noreply.github.com> Date: Thu, 7 Nov 2024 17:16:55 -0500 Subject: [PATCH] Hot fix (#16) * Adding version + debug DB_ACTION_OUTPUT/WARNING + Switching to pdm backend * Testing adapted workflows https://www.thisdot.co/blog/tag-and-release-your-project-with-github-actions-workflows for releases with automated tagging * Changing input to be a text and not a drop down * Addressing PO's comments: adding a pattern filter for version name with a regex + adding a filter to consider only branch main and only release when the PR is merged and not declined --- .github/workflows/Tag-and-Release.yml | 63 +++++++++++++++++++ ...Update-Version-and-create-Realeases-PR.yml | 51 +++++++++++++++ .gitignore | 7 ++- pt_cli/__version__.py | 1 + pt_cli/cli.py | 4 +- pt_cli/connect.py | 1 + pt_cli/tools.py | 8 +++ pt_cli/version.py | 4 ++ pyproject.toml | 20 ++++-- 9 files changed, 152 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/Tag-and-Release.yml create mode 100644 .github/workflows/Update-Version-and-create-Realeases-PR.yml create mode 100644 pt_cli/__version__.py create mode 100644 pt_cli/version.py diff --git a/.github/workflows/Tag-and-Release.yml b/.github/workflows/Tag-and-Release.yml new file mode 100644 index 0000000..504b56f --- /dev/null +++ b/.github/workflows/Tag-and-Release.yml @@ -0,0 +1,63 @@ +name: Tag and Release Workflow + +on: + pull_request: + types: [closed] + branches: ['main'] + +jobs: + release: + runs-on: ${{ matrix.os }} + strategy: + matrix: + python-version: ["3.11", "3.12", "3.13"] + os: [ubuntu-latest, macos-latest] + + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. + permissions: + contents: read + packages: write + attestations: write + id-token: write + + if: startsWith(github.event.pull_request.title, 'Release:') && github.event.pull_request.merged == true + steps: + - name: Checkout code + uses: actions/checkout@v4 + - name: Setup Git + run: | + git config --local user.email "github-actions[bot]@users.noreply.github.com" + git config --local user.name "github-actions[bot]" + - name: Setup PDM + uses: pdm-project/setup-pdm@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Get tag + id: get_tag + run: | + git branch --show-current + git pull + pdm install --prod --no-lock --no-editable + echo "version=$(pdm show --version)" >> $GITHUB_OUTPUT + - name: Tag the commit + run: | + next_version=${{ steps.get_tag.outputs.version }} + git tag -a "$next_version" -m "Version $next_version" + git push --follow-tags + - name: Create changelog diff + id: changelog_diff + run: | + sed -n '/#### \[${{ steps.get_tag.outputs.version }}\]/,/^#### /p' CHANGELOG.md | sed '$d' > release_notes.md + - name: Create release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ steps.get_tag.outputs.version }} + release_name: Release ${{ steps.get_tag.outputs.version }} + body_path: ./release_notes.md + draft: false + prerelease: false + - name: Delete release_notes file + run: rm release_notes.md diff --git a/.github/workflows/Update-Version-and-create-Realeases-PR.yml b/.github/workflows/Update-Version-and-create-Realeases-PR.yml new file mode 100644 index 0000000..ec9bc92 --- /dev/null +++ b/.github/workflows/Update-Version-and-create-Realeases-PR.yml @@ -0,0 +1,51 @@ +name: Update version and create Release's PR Workflow + +on: + workflow_dispatch: + inputs: + version: + description: 'Version name' + required: true + type: string + pattern: '^[0-9]+\.[0-9]+\.[0-9]+$' + +jobs: + version: + runs-on: ubuntu-latest + + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. + permissions: + contents: read + packages: write + attestations: write + id-token: write + + steps: + - name: Checkout code + uses: actions/checkout@v4 + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: 18 + - name: Setup Git + run: | + git config --local user.email "github-actions[bot]@users.noreply.github.com" + git config --local user.name "github-actions[bot]" + - name: Update the version + id: update_version + run: | + echo "__version__ = '${{ inputs.version }}'" > pt_cli/__version__.py + echo "version=${{ inputs.version }}" >> $GITHUB_OUTPUT + - name: Update Changelog + run: | + npm install -g auto-changelog + auto-changelog -v ${{ steps.update_version.outputs.version }} + - name: Create pull request + id: create_pr + uses: peter-evans/create-pull-request@v7 + with: + token: ${{ secrets.GITHUB_TOKEN }} + branch: release/${{ steps.update_version.outputs.version }} + title: "Release: Candidate Version ${{ steps.update_version.outputs.version }} Pull Request" + body: "This pull request contains the updated __version__.py file with the new release version" + base: main diff --git a/.gitignore b/.gitignore index e2f6e1d..898f0b6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,14 @@ +# Virtual env venv +.venv +# Misc *.sublime-* - *.pyc __pycache__/ - .DS_Store +.pdm* +dist/ # Tests pair_file.csv diff --git a/pt_cli/__version__.py b/pt_cli/__version__.py new file mode 100644 index 0000000..566091e --- /dev/null +++ b/pt_cli/__version__.py @@ -0,0 +1 @@ +__version__ = '2.0.1-beta' \ No newline at end of file diff --git a/pt_cli/cli.py b/pt_cli/cli.py index 6023f90..541268f 100755 --- a/pt_cli/cli.py +++ b/pt_cli/cli.py @@ -26,6 +26,8 @@ Curate ) +from .__version__ import __version__ + logger = logging.getLogger(__name__) @@ -47,7 +49,7 @@ def get_main_parser(args=None): parser.add_argument('--loglevel', help='Set log level', choices=list(logging._levelToName.values()), default='INFO') parser.add_argument('--info', help='Get current client config', action='store_true') parser.add_argument('-q', '--quiet', help='Writes Warnings to a file instead of stdout', action='store_true') - # parser.add_argument('-v', '--verbose', help='Add more verbosity', action='store_true') + parser.add_argument('-v', '--version', help='Show version', action="version", version=f'{__version__}') return parser diff --git a/pt_cli/connect.py b/pt_cli/connect.py index 4a8b391..f1406e6 100755 --- a/pt_cli/connect.py +++ b/pt_cli/connect.py @@ -123,6 +123,7 @@ def maybe_json(self, data): f.write(f"\n{chr(10).join(loads.get('DB_ACTION_WARNING'))}") else: logger.warning(f"\n{chr(10).join(loads.get('DB_ACTION_WARNING'))}") + loads.pop("DB_ACTION_WARNING") self.data_type = 'json' return loads except json.decoder.JSONDecodeError: diff --git a/pt_cli/tools.py b/pt_cli/tools.py index e8d274b..20e61a9 100644 --- a/pt_cli/tools.py +++ b/pt_cli/tools.py @@ -258,6 +258,10 @@ def json_to_readset_file(self): readset_file = self.readset_file if not readset_file: raise EmptyGetError + readset_file = readset_file["DB_ACTION_OUTPUT"] + if not readset_file: + sys.stdout.write("Nothing returned.") + return with open(self.output_file, "w", encoding="utf-8") as out_readset_file: tsv_writer = csv.DictWriter(out_readset_file, delimiter='\t', fieldnames=self.READSET_HEADER) tsv_writer.writeheader() @@ -363,6 +367,10 @@ def json_to_pair_file(self): pair_file = self.pair_file if not pair_file: raise EmptyGetError + pair_file = pair_file["DB_ACTION_OUTPUT"] + if not pair_file: + sys.stdout.write("Nothing returned.") + return with open(self.output_file, "w", encoding="utf-8") as out_pair_file: tsv_writer = csv.DictWriter(out_pair_file, delimiter=',', fieldnames=self.PAIR_HEADER) # tsv_writer.writeheader() diff --git a/pt_cli/version.py b/pt_cli/version.py new file mode 100644 index 0000000..e74de6e --- /dev/null +++ b/pt_cli/version.py @@ -0,0 +1,4 @@ +from pdm.backend.hooks.version import SCMVersion + +def format_version(version: SCMVersion) -> str: + return str(version.version) diff --git a/pyproject.toml b/pyproject.toml index 2874c92..3f7f7fd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,10 +1,10 @@ [build-system] -requires = ["hatchling"] -build-backend = "hatchling.build" +requires = ["pdm-backend"] +build-backend = "pdm.backend" [project] name = "pt_cli" -version = "0.0.1" +dynamic = ["version"] authors = [ { name="P-O Quirion", email="po.quirion@mcgill.ca" }, { name="Paul Streteowich", email="paul.stretenowich@mcgill.ca" }, @@ -28,7 +28,19 @@ dependencies = [ [project.scripts] pt-cli = "pt_cli.cli:main" - [project.urls] "Homepage" = "https://github.com/C3G/pt_cli" "Bug Tracker" = "https://github.com/pypa/pt_cli/issues" + +[tool.pdm] +allow-direct-references = true + +[tool.pdm.version] +source = "file" +path = "pt_cli/__version__.py" +# source = "scm" +# version_format = "pt_cli.version:format_version" +# write_to = "pt_cli/__version__.py" +# write_template = "__version__ = '{}'" +# tag_filter = "main/*" +# tag_regex = "^main/.*([0-9].[0-9].[0-9])$" \ No newline at end of file