From 8d9d85823954a9974e31683ebce7cbcc4f2c3ab3 Mon Sep 17 00:00:00 2001 From: Jiaqi Liu Date: Mon, 22 Apr 2024 12:31:04 +0800 Subject: [PATCH] Support auto-versioning (#7) --- .github/workflows/ci-cd.yml | 39 ++++++++++++++++++- .readthedocs.yaml | 2 +- README.md | 4 ++ docs/source/conf.py | 1 + docs/source/faq.rst | 5 +-- screwdriver_cd_python_sdk/events.py | 12 ++++-- screwdriver_cd_python_sdk/pipeline.py | 6 ++- .../screwdriver_initializer.py | 25 +++++++----- screwdriver_cd_python_sdk/secrets.py | 23 +++++++++-- setup.cfg | 3 ++ setup.py | 7 ++-- 11 files changed, 100 insertions(+), 27 deletions(-) create mode 100644 setup.cfg diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index dec5a5c..f6684f7 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -21,20 +21,55 @@ name: CI/CD - master jobs: + yml-md-style-and-link-checks: + uses: QubitPi/hashicorp-aws/.github/workflows/yml-md-style-and-link-checks.yml@master + + tests: + name: Run all tests + needs: yml-md-style-and-link-checks + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set up Python 3.10 + uses: actions/setup-python@v4 + with: + python-version: "3.10" + - name: Run tests + run: | + python setup.py pep8 + python setup.py isort + - name: Test dist build + run: python setup.py sdist + + push-release-tag: + needs: tests + if: github.ref == 'refs/heads/master' + uses: QubitPi/hashicorp-aws/.github/workflows/version-bump.yml@master + with: + user: QubitPi + email: jack20220723@gmail.com + release: name: Publish Python SDK To PyPI + if: github.ref == 'refs/heads/master' runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 + with: + fetch-depth: 0 - name: Set up Python 3.10 uses: actions/setup-python@v4 with: python-version: "3.10" + - name: Bump version + run: | + git pull + VERSION=$(git describe) + echo "INFO Last tag: $VERSION" + cat setup.py | sed -i "s/ version=\"1.0.0\",/ version=\"$NEW_VERSION\",/" - name: Package up SDK - if: github.ref == 'refs/heads/master' run: python setup.py sdist - name: Publish a Python distribution to PyPI - if: github.ref == 'refs/heads/master' uses: pypa/gh-action-pypi-publish@release/v1 with: user: __token__ diff --git a/.readthedocs.yaml b/.readthedocs.yaml index 0fbf526..4c9aae1 100644 --- a/.readthedocs.yaml +++ b/.readthedocs.yaml @@ -23,4 +23,4 @@ sphinx: python: install: - - requirements: docs/source/requirements.txt + - requirements: docs/source/requirements.txt diff --git a/README.md b/README.md index 47a1288..d0fceec 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ Screwdriver CD Python SDK ========================= ![Python Version][Python Version Badge] +[![Read the Docs][Read the Docs badge]][Read the Docs URL] [![PyPI][PyPI project badge]][PyPI project url] [![GitHub Workflow Status][GitHub Workflow Status badge]][GitHub Workflow Status URL] [![Apache License badge]][Apache License URL] @@ -39,3 +40,6 @@ The use and distribution terms for Screwdriver CD Python SDK are covered by the [Python Version Badge]: https://img.shields.io/badge/Python-3.10-brightgreen?style=for-the-badge&logo=python&logoColor=white [PyPI project badge]: https://img.shields.io/pypi/v/screwdriver-cd-python-sdk?logo=pypi&logoColor=white&style=for-the-badge [PyPI project url]: https://pypi.org/project/screwdriver-cd-python-sdk/ + +[Read the Docs badge]: https://img.shields.io/readthedocs/screwdriver-cd-python-sdk?style=for-the-badge&logo=readthedocs&logoColor=white&label=Read%20the%20Docs&labelColor=8CA1AF +[Read the Docs URL]: https://screwdriver-cd-python-sdk.readthedocs.io/en/latest/ diff --git a/docs/source/conf.py b/docs/source/conf.py index 9ac6ab4..d583cc5 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -12,6 +12,7 @@ # import os import sys + sys.path.insert(0, os.path.abspath('../../')) diff --git a/docs/source/faq.rst b/docs/source/faq.rst index ff8a038..12b1d0d 100644 --- a/docs/source/faq.rst +++ b/docs/source/faq.rst @@ -8,9 +8,8 @@ Frequently Asked Questions Python Sphinx Autodoc Is Not Rendering on readthedocs ===================================================== -The project's dependencies are not specified on RTD, but instead have installed the dependencies locally. We can verify -this in the `build log `_. Visit the project's Builds, click a build, -and click "view raw":: +The project's dependencies are not specified on RTD, but instead have installed the dependencies locally. Visit the +project's Builds, click a build, and click "view raw":: WARNING: autodoc: failed to import module 'rnn' from module 'screwdriver_cd_python_sdk'; the following exception was raised: No module named 'matplotlib' diff --git a/screwdriver_cd_python_sdk/events.py b/screwdriver_cd_python_sdk/events.py index c1365d3..35e3507 100644 --- a/screwdriver_cd_python_sdk/events.py +++ b/screwdriver_cd_python_sdk/events.py @@ -12,15 +12,19 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os +import sys + import requests -import sys, os + def start_build(pipeline_id: int, screwdriver_api_url: str, token: str) -> object: """ Creates and starts a pipeline build. :param pipeline_id: The ID of the pipeline to trigger the build - :param screwdriver_api_url: The URL of the Screwdriver API server. For example: http://192.168.7.2:9001 or https://mysd.com + :param screwdriver_api_url: The URL of the Screwdriver API server. For example: http://192.168.7.2:9001 or + https://mysd.com :param token: The Screwdriver API token :return: The exact same response body as the "POST /v4/events" Swagger API in JSON @@ -35,13 +39,15 @@ def start_build(pipeline_id: int, screwdriver_api_url: str, token: str) -> objec token ) + def create_and_start_event(screwdriver_api_url: str, body: object, token: str) -> object: """ Creates and starts a specific event. If an error occurs, this function returns nothing but throws the causing error. - :param screwdriver_api_url: The URL of the Screwdriver API server. For example: http://192.168.7.2:9001 or https://mysd.com + :param screwdriver_api_url: The URL of the Screwdriver API server. For example: http://192.168.7.2:9001 or + https://mysd.com :param body: The exact same body as the one used in "POST /v4/events" Swagger API :param token: The Screwdriver API token diff --git a/screwdriver_cd_python_sdk/pipeline.py b/screwdriver_cd_python_sdk/pipeline.py index 55c89ec..f7caf23 100644 --- a/screwdriver_cd_python_sdk/pipeline.py +++ b/screwdriver_cd_python_sdk/pipeline.py @@ -13,8 +13,11 @@ # limitations under the License. import logging +import os +import sys + import requests -import sys, os + def search_pipelines_by_name(name: str, screwdriver_api_url: str, token: str) -> list[object]: """ @@ -89,6 +92,7 @@ def search_pipelines_by_name(name: str, screwdriver_api_url: str, token: str) -> return response.json() + def create_pipeline(checkout_url: str, screwdriver_api_url: str, token: str, source_directory: object = None) -> object: """ Creates a new Screwdriver pipeline for a particular repo and an optional source directory. diff --git a/screwdriver_cd_python_sdk/screwdriver_initializer.py b/screwdriver_cd_python_sdk/screwdriver_initializer.py index 1c3264e..26ca8b1 100644 --- a/screwdriver_cd_python_sdk/screwdriver_initializer.py +++ b/screwdriver_cd_python_sdk/screwdriver_initializer.py @@ -12,14 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -import logging -import os -import json import csv -from screwdriver_cd_python_sdk.pipeline import create_pipeline -from screwdriver_cd_python_sdk.pipeline import search_pipelines_by_name -from screwdriver_cd_python_sdk.secrets import create_or_update_secret +import json +import logging + from screwdriver_cd_python_sdk.events import start_build +from screwdriver_cd_python_sdk.pipeline import (create_pipeline, + search_pipelines_by_name) +from screwdriver_cd_python_sdk.secrets import create_or_update_secret + def initialize(pipelines_config_path: str, screwdriver_api_url: str, token: str) -> None: with open(pipelines_config_path, 'r') as file: @@ -28,9 +29,9 @@ def initialize(pipelines_config_path: str, screwdriver_api_url: str, token: str) for pipeline in pipelines: git_url = pipeline["git"] - repo_name = git_url[git_url.find(":")+1 : git_url.find(".git")] + repo_name = git_url[git_url.find(":") + 1:git_url.find(".git")] - pipeline_id=None + pipeline_id = None for match in search_pipelines_by_name(name=repo_name, screwdriver_api_url=screwdriver_api_url, token=token): if match["name"] == repo_name: pipeline_id = match["id"] @@ -40,7 +41,11 @@ def initialize(pipelines_config_path: str, screwdriver_api_url: str, token: str) if pipeline_id is None: logging.debug("Creating {}...".format(repo_name)) - pipeline_id = create_pipeline(checkout_url=pipeline["git"], screwdriver_api_url=screwdriver_api_url, token=token)["id"] + pipeline_id = create_pipeline( + checkout_url=pipeline["git"], + screwdriver_api_url=screwdriver_api_url, + token=token + )["id"] if "awsCredentialFile" in pipeline: with open(pipeline["awsCredentialFile"], 'r') as file: @@ -86,4 +91,4 @@ def initialize(pipelines_config_path: str, screwdriver_api_url: str, token: str) def _file_content(file_path: str) -> str: with open(file_path, 'r') as file: - return file.read().rstrip('\n') # https://stackoverflow.com/a/70233945 + return file.read().rstrip('\n') # https://stackoverflow.com/a/70233945 diff --git a/screwdriver_cd_python_sdk/secrets.py b/screwdriver_cd_python_sdk/secrets.py index 606e0e7..1cf1bfd 100644 --- a/screwdriver_cd_python_sdk/secrets.py +++ b/screwdriver_cd_python_sdk/secrets.py @@ -13,11 +13,14 @@ # limitations under the License. import logging +import os +import sys + import requests -import sys, os logging.basicConfig(level=logging.DEBUG) + def _headers(token: str) -> object: return { 'accept': 'application/json', @@ -26,7 +29,13 @@ def _headers(token: str) -> object: } -def create_or_update_secret(secret_name: str, secret_value: str, pipeline_id: int, screwdriver_api_url: str, token: str) -> None: +def create_or_update_secret( + secret_name: str, + secret_value: str, + pipeline_id: int, + screwdriver_api_url: str, + token: str +) -> None: """ "allowInPR" is set to be false by default @@ -53,7 +62,11 @@ def create_or_update_secret(secret_name: str, secret_value: str, pipeline_id: in 'allowInPR': False, } - if requests.put('{}/v4/secrets/{}'.format(screwdriver_api_url, secrete["id"]), headers=_headers(token), json=json_data).status_code != 200: + if requests.put( + '{}/v4/secrets/{}'.format(screwdriver_api_url, secrete["id"]), + headers=_headers(token), + json=json_data + ).status_code != 200: sys.exit(os.EX_CONFIG) else: logging.debug("Creating secret '{}'".format(secret_name)) @@ -65,5 +78,7 @@ def create_or_update_secret(secret_name: str, secret_value: str, pipeline_id: in 'allowInPR': False, } - if requests.post('{}/v4/secrets'.format(screwdriver_api_url), headers=_headers(token), json=json_data).status_code != 201: + if requests.post( + '{}/v4/secrets'.format(screwdriver_api_url), headers=_headers(token), json=json_data + ).status_code != 201: sys.exit(os.EX_CONFIG) diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..67bc0f8 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,3 @@ +[pep8] +max-line-length = 120 +exclude = ./.eggs diff --git a/setup.py b/setup.py index 15e84d5..af0fa94 100644 --- a/setup.py +++ b/setup.py @@ -1,9 +1,9 @@ -from setuptools import setup, find_packages +from setuptools import find_packages, setup setup( name="screwdriver-cd-python-sdk", version="1.0.0", - description="Screwdriver CD Python Software Development Kit (SDK) used to write Python automation scripts that create and manage resources in Screwdriver", + description="Screwdriver CD Python Software Development Kit (SDK) for managing resources in Screwdriver", url="https://github.com/QubitPi/screwdriver-cd-python-sdk", author="Jiaqi liu", author_email="jack20220723@gmail.com", @@ -14,5 +14,6 @@ ], zip_safe=False, - include_package_data=True + include_package_data=True, + setup_requires=["setuptools-pep8", "isort"] )