Repository: DontShaveTheYak/cf2tf (language: python)

Issue:
update username when using token



Discussion / hints:

Fix patch:
diff --git a/.github/workflows/pr_checks.yml b/.github/workflows/pr_checks.yml
new file mode 100644
index 0000000..8aad99f
--- /dev/null
+++ b/.github/workflows/pr_checks.yml
@@ -0,0 +1,27 @@
+name: PR
+
+on:
+  pull_request_target:
+    types: [opened, labeled, unlabeled, synchronize]
+
+jobs:
+  label:
+    name: Check Labels
+    runs-on: ubuntu-latest
+    steps:
+      - name: Has semver label
+        if: github.base_ref != 'master'
+        uses: jesusvasquez333/verify-pr-label-action@v1.4.0
+        with:
+          github-token: ${{ secrets.GITHUB_TOKEN }}
+          valid-labels: 'major, minor, patch'
+          pull-request-number: ${{ github.event.pull_request.number }}
+          disable-reviews: true
+
+      - name: Has change type label
+        uses: jesusvasquez333/verify-pr-label-action@v1.4.0
+        with:
+          github-token: ${{ secrets.GITHUB_TOKEN }}
+          valid-labels: 'feature, enhancement, fix, bugfix, bug, chore'
+          pull-request-number: ${{ github.event.pull_request.number }}
+          disable-reviews: true
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
new file mode 100644
index 0000000..64fe023
--- /dev/null
+++ b/.github/workflows/release.yml
@@ -0,0 +1,122 @@
+name: Release
+
+on:
+  pull_request:
+    types: closed
+
+jobs:
+  tag:
+    name: Create Tag
+    runs-on: ubuntu-latest
+    if: github.event.pull_request.merged && (github.base_ref == 'develop' || github.base_ref == 'master')
+    outputs:
+      tag: ${{ steps.bump.outputs.next_tag }}
+    steps:
+
+      - name: Checkout Code
+        uses: actions/checkout@v2.3.4
+        with:
+          fetch-depth: 0
+
+      - name: Setup Python 3.9
+        uses: actions/setup-python@v2.2.2
+        with:
+          python-version: '3.9'
+          architecture: x64
+
+      - name: Install dependencies
+        run: |
+          python -m pip install --upgrade pip
+          pip install -r .scripts/requirements.txt
+          pip install poetry==1.1.4
+
+      - name: Get Latest Tag
+        id: latest_tag
+        uses: "WyriHaximus/github-action-get-previous-tag@v1"
+
+      - name: Calculate base tag
+        id: calculate
+        run: python .scripts/bump.py ${{ steps.latest_tag.outputs.tag }} ${{ github.event.pull_request.number }} ${{ github.base_ref }}
+
+      - name: Bump version
+        id: bump
+        run: |
+          if [ "$BUMP_RULE" = "None" ]; then
+              echo "::set-output name=next_tag::$(echo $BASE_TAG)"
+          else
+              poetry version $BASE_TAG
+              poetry version $BUMP_RULE
+              NEXT_TAG=$(poetry version -s)
+              echo "::set-output name=next_tag::$(echo $NEXT_TAG)"
+          fi
+        env:
+          BASE_TAG: ${{ steps.calculate.outputs.base_tag }}
+          BUMP_RULE: ${{ steps.calculate.outputs.bump_rule }}
+
+      - name: Create Tag
+        id: create_tag
+        uses: K-Phoen/semver-release-action@master
+        env:
+          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+        with:
+          release_branch: ${{ github.base_ref }}
+          release_strategy: tag
+          tag: ${{ steps.bump.outputs.next_tag }}
+
+  publish:
+    name: Publish Package
+    runs-on: ubuntu-latest
+    needs: tag
+    steps:
+      - name: Checkout Code
+        uses: actions/checkout@v2.3.4
+
+      - name: Setup Python 3.9
+        uses: actions/setup-python@v2.2.2
+        with:
+          python-version: '3.9'
+          architecture: x64
+
+      - name: Setup Poetry
+        run: pip install poetry==1.1.4
+
+      - name: Publish to Test PyPI
+        if: github.base_ref == 'develop'
+        run: |
+          poetry version ${{needs.tag.outputs.tag}}
+          poetry build
+          poetry config repositories.testpypi https://test.pypi.org/legacy/
+          poetry publish -r testpypi --username '__token__' --password '${{ secrets.TEST_PYPI_TOKEN }}'
+
+      - name: Publish to PyPI
+        if: github.base_ref == 'master'
+        run: |
+          poetry version ${{needs.tag.outputs.tag}}
+          poetry build
+          poetry publish --username '__token__' --password '${{ secrets.PYPI_TOKEN }}'
+
+  release:
+    name: Create Release
+    runs-on: ubuntu-latest
+    needs: [tag, publish]
+    steps:
+      - name: Create Draft Release
+        uses: release-drafter/release-drafter@v5.15.0
+        if: github.base_ref == 'develop'
+        with:
+          tag: ${{needs.tag.outputs.tag}}
+          version: ${{needs.tag.outputs.tag}}
+          commitish: develop
+        env:
+          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+
+      - name: Create Release
+        uses: release-drafter/release-drafter@v5.15.0
+        if: github.base_ref == 'master'
+        with:
+          tag: ${{needs.tag.outputs.tag}}
+          version: ${{needs.tag.outputs.tag}}
+          publish: true
+          commitish: master
+        env:
+          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 78e655a..cb0a00a 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -8,6 +8,7 @@ repos:
       entry: nox
       language: python
       language_version: python3
+      types: ["python"]
       args: ["-rs", "black", "-p", "3.9"]
       require_serial: true
       additional_dependencies: []
@@ -16,6 +17,7 @@ repos:
       entry: nox
       language: python
       language_version: python3
+      types: ["python"]
       args: ["-rs", "lint", "-p", "3.9"]
       require_serial: true
       additional_dependencies: []
@@ -24,6 +26,16 @@ repos:
       entry: nox
       language: python
       language_version: python3
+      types: ["python"]
       args: ["-rs", "mypy", "-p", "3.9"]
       require_serial: true
+      additional_dependencies: []
+    - id: tests
+      name: tests
+      entry: nox
+      language: python
+      language_version: python3
+      types: ["python"]
+      args: ["-rs", "tests", "-p", "3.9"]
+      require_serial: true
       additional_dependencies: []
\ No newline at end of file
diff --git a/.scripts/bump.py b/.scripts/bump.py
new file mode 100644
index 0000000..ebd4b69
--- /dev/null
+++ b/.scripts/bump.py
@@ -0,0 +1,120 @@
+#!/usr/bin/python
+
+import sys
+from typing import Any, Dict
+
+import requests
+
+import semver
+
+# Should really get these from CICD :Rip:
+repo: str = "cf2tf"
+owner: str = "DontShaveTheYak"
+
+
+def do_action(action, version):
+    function = getattr(version, action)
+
+    new_version = function()
+
+    print(f"{version} {action} to {new_version}")
+    return new_version
+
+
+def get_response(url) -> Dict[str, Any]:
+    response = requests.get(url)
+    return response.json()
+
+
+def get_action(pull_request: str) -> str:
+    valid_labels = ["major", "minor", "patch"]
+    response = get_response(
+        f"https://api.github.com/repos/{owner}/{repo}/pulls/{pull_request}"
+    )
+
+    label = [
+        label["name"] for label in response["labels"] if label["name"] in valid_labels
+    ][0]
+    return label
+
+
+def set_output(name: str, value: str):
+    print(f"::set-output name={name}::{value}")
+
+
+def prerelease(rule: str) -> str:
+    return f"pre{rule}"
+
+
+latest_tag = sys.argv[1]
+pull_request = sys.argv[2]
+branch = sys.argv[3]
+
+
+action_methods = {"patch": "bump_patch", "minor": "bump_minor", "major": "bump_major"}
+
+if branch != "master":
+    action_name = get_action(pull_request)
+    action = action_methods[action_name]
+
+base_tag: str = ""
+bump_rule: str = ""
+
+print(f"Latest tag is {latest_tag}")
+
+response = get_response(f"https://api.github.com/repos/{owner}/{repo}/releases/latest")
+release_tag = str(semver.VersionInfo.parse(response["tag_name"]))
+
+print(f"Latest release is {release_tag}")
+
+if branch == "master":
+    print("This release is a final release!")
+    base_tag = latest_tag.split("-")[0]
+    bump_rule = "None"
+
+elif "-alpha" in latest_tag:
+    print("This is an existing prerelease.")
+
+    latest_ver = latest_tag.split("-")[0]
+
+    next_tag = semver.VersionInfo.parse(release_tag)
+
+    next_tag = do_action(action, next_tag)
+
+    latest_ver = semver.VersionInfo.parse(latest_ver)
+
+    compare = semver.compare(str(latest_ver), str(next_tag))
+
+    next_tag = str(next_tag)
+    latest_ver = str(latest_ver)
+
+    if compare == -1:
+        print(
+            f"Creating {next_tag} because its version is higher than latest tag: {latest_ver}"
+        )
+        base_tag = release_tag
+        bump_rule = prerelease(action_name)
+    elif compare == 1:
+        print(
+            f"Reusing latest tag ({latest_tag}) because next tag ({next_tag}) is lower."
+        )
+        base_tag = latest_tag
+        bump_rule = "prerelease"
+    else:
+        print(
+            f"Reusing latest tag ({latest_tag}) because its version is equal to next tag ({next_tag})"  # noqa: B950
+        )
+        base_tag = latest_tag
+        bump_rule = "prerelease"
+else:
+    print("This is a new prerelease.")
+    base_tag = release_tag
+    bump_rule = prerelease(action_name)
+
+
+print(f"Using base tag {base_tag} with bump rule {bump_rule}.")
+
+
+set_output("base_tag", base_tag)
+set_output("bump_rule", bump_rule)
+sys.exit(0)
diff --git a/.scripts/requirements.txt b/.scripts/requirements.txt
new file mode 100644
index 0000000..42c2860
--- /dev/null
+++ b/.scripts/requirements.txt
@@ -0,0 +1,2 @@
+requests==2.25.1
+semver==2.13.0
diff --git a/pyproject.toml b/pyproject.toml
index 6e5e652..7f46fbd 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,8 +1,11 @@
 [tool.poetry]
 name = "cf2tf"
-version = "0.1.0"
+version = "0.0.0"
 description = "Convert Cloudformation Templates to Terraform."
+readme = "README.md"
 authors = ["Levi Blaney <shadycuz@gmail.com>"]
+repository = "https://github.com/DontShaveTheYak/cf2tf"
+keywords = ["cloudformation", "terraform", "cf2tf", "convert", "cloud", "conversion"]
 
 [tool.poetry.scripts]
 cf2tf = "cf2tf.app:cli"


Test patch:
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
new file mode 100644
index 0000000..024b72a
--- /dev/null
+++ b/.github/workflows/test.yml
@@ -0,0 +1,73 @@
+name: Tests
+
+on:
+  push:
+    branches:
+      - 'master'
+  pull_request:
+    paths-ignore:
+      - '.github/workflows/release.yml'
+      - '.github/workflows/pr_checks.yml'
+
+jobs:
+  single:
+    runs-on: ubuntu-latest
+    name: Python 3.9
+    steps:
+      - name: Checkout Code
+        uses: actions/checkout@v2.3.4
+
+      - name: Setup Latest Python
+        uses: actions/setup-python@v2.2.2
+        with:
+          python-version: 3.9
+          architecture: x64
+
+      - name: Setup Poetry
+        run: |
+          pip install poetry==1.1.13
+          poetry install
+
+      - name: Setup requirements
+        run: pip install nox nox_poetry coverage[toml]
+
+      - name: Run Tests
+        run: nox -s tests -p 3.9
+
+      - name: Combine Coverage reports
+        run: nox -s coverage -p 3.9
+
+      - name: Convert Coverage
+        run: coverage xml --fail-under=0
+
+      - name: Upload coverage to Codecov
+        uses: codecov/codecov-action@v2.0.3
+        with:
+          flags: unit
+          fail_ci_if_error: true
+
+  multi:
+    needs: single
+    runs-on: ubuntu-latest
+    strategy:
+      matrix:
+        python-version: ['3.7', '3.8', '3.10'] # 3.9 is done in the job above
+    name: Python ${{ matrix.python-version }}
+    steps:
+      - name: Checkout Code
+        uses: actions/checkout@v2.3.4
+
+      - name: Setup Python ${{ matrix.python-version }}
+        uses: actions/setup-python@v2.2.2
+        with:
+          python-version: ${{ matrix.python-version }}
+          architecture: x64
+
+      - name: Setup Nox
+        run: pip install nox nox_poetry
+
+      - name: Setup Poetry
+        run: pip install poetry==1.1.13
+
+      - name: Run Tests
+        run: nox -s tests -p ${{ matrix.python-version }}