Skip to content

Commit

Permalink
feat(workflows): add build and publish jobs (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
fynnfluegge authored Aug 25, 2023
1 parent 6ac3610 commit cf7f766
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 132 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Build

on:
push:
branches: [ main ]

jobs:
test:
name: Run unit tests
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.9

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install poetry
poetry install
- name: Run unittest
run: poetry run pytest
32 changes: 32 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Publish

on:
release:
types: [published]

jobs:
publish:
name: Publish to PyPI
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.9

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install poetry
poetry install
- name: Publish to PyPI
env:
POETRY_PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
run: |
poetry config pypi-token.pypi $POETRY_PYPI_TOKEN
poetry publish --build
3 changes: 2 additions & 1 deletion doc_comments_ai/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ def __init__(self, model: str = "gpt-3.5-turbo"):
self.llm = ChatOpenAI(temperature=0.9, max_tokens=2048, model=model)
self.template = (
"I have this {language} method:\n{code}\nAdd a doc comment to the method. "
"Return the method with the doc comment embedded in a markdown code block. "
"Return the method with the doc comment as a markdown code block. "
"{inline_comments}"
"Don't include any explanations in your response."
)
self.prompt = PromptTemplate(
template=self.template,
Expand Down
34 changes: 22 additions & 12 deletions doc_comments_ai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,39 @@ def extract_content_from_markdown_code_block(markdown_code_block, language) -> s
str: The extracted content.
"""
pattern = f"```{language}\n(.*?)```"
pattern = r"```(?:[a-zA-Z0-9]+)?\n(.*?)```"
match = re.search(pattern, markdown_code_block, re.DOTALL)

if match:
# TODO fix this
# sometimes the doc comment has ``` block itself, which will break
# the regex pattern. In this case, we need to extract the all
# subsequent ``` blocks and append them to the first one
subsequent_matches = re.findall("```\n(.*?)```", markdown_code_block, re.DOTALL)
if subsequent_matches:
# join all subsequent code blocks
subsequent_code = "\n".join(subsequent_matches).strip()
# append the last block
last_match = re.findall("```(.*?)```", markdown_code_block, re.DOTALL)
if last_match:
last_code_block = last_match[-1].strip()
subsequent_code += "\n" + last_code_block
# return the first code block + subsequent code blocks
return match.group(1).strip() + "\n" + subsequent_code
# subsequent_matches = re.findall("```\n(.*?)```", markdown_code_block, re.DOTALL)
# if subsequent_matches:
# # join all subsequent code blocks
# subsequent_code = "\n".join(subsequent_matches).strip()
# # append the last block
# last_match = re.findall("```(.*?)```", markdown_code_block, re.DOTALL)
# if last_match:
# last_code_block = last_match[-1].strip()
# subsequent_code += "\n" + last_code_block
# # return the first code block + subsequent code blocks
# return match.group(1).strip() + "\n" + subsequent_code

return match.group(1).strip()
else:
return markdown_code_block.strip()


def get_bold_text(text):
"""
Returns the specified text formatted in bold.
Parameters:
- text (str): The text to be formatted.
Returns:
str: The input text formatted in bold.
"""
return f"\033[01m{text}\033[0m"
260 changes: 144 additions & 116 deletions poetry.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[tool.poetry]
name = "doc-comments-ai"
version = "0.1.0"
version = "0.1.2"
description = ""
authors = ["fynnfluegge <[email protected]>"]
readme = "README.md"
packages = [{ include = "doc_comments_ai" }]

[tool.poetry.dependencies]
python = "^3.11"
python = "^3.9"
tree-sitter-languages = "^1.7.0"
tree-sitter = "^0.20.1"
python-dotenv = "^1.0.0"
Expand All @@ -18,7 +18,7 @@ yaspin = "^3.0.0"
pytest = "^7.4.0"

[tool.poetry.scripts]
dccai = "doc_comments_ai.__main__:main"
aicomment = "doc_comments_ai.__main__:main"

[build-system]
requires = ["poetry-core"]
Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
from tests.fixtures.code_fixture_js import javascript_code_fixture
from tests.fixtures.code_fixture_ts import typescript_code_fixture
from tests.fixtures.code_fixture_rs import rust_code_fixture
from tests.fixtures.response_fixtures import (
response_fixture,
response_fixture_language_enclosed,
response_fixture_with_text,
)
62 changes: 62 additions & 0 deletions tests/fixtures/response_fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import pytest


@pytest.fixture
def response_fixture():
return """
```
def get_bold_text(text):
\"\"\"
Returns the provided text formatted in bold.
Args:
text (str): The text to be formatted.
Returns:
str: The text formatted in bold.
\"\"\"
return f"\033[01m{text}\033[0m"
```
"""


@pytest.fixture
def response_fixture_language_enclosed():
return """
```python
def get_bold_text(text):
\"\"\"
Returns the provided text formatted in bold.
Args:
text (str): The text to be formatted.
Returns:
str: The text formatted in bold.
\"\"\"
return f"\033[01m{text}\033[0m"
```
"""


@pytest.fixture
def response_fixture_with_text():
return """
```
def get_bold_text(text):
\"\"\"
Returns the provided text formatted in bold.
Args:
text (str): The text to be formatted.
Returns:
str: The text formatted in bold.
\"\"\"
return f"\033[01m{text}\033[0m"
```
This a text
"""
68 changes: 68 additions & 0 deletions tests/response_parser_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import pytest
from doc_comments_ai import utils


@pytest.mark.usefixtures("response_fixture")
def test_response_parser(response_fixture):
markdown_code_block = utils.extract_content_from_markdown_code_block(
response_fixture, "python"
)
assert (
markdown_code_block
== """def get_bold_text(text):
\"\"\"
Returns the provided text formatted in bold.
Args:
text (str): The text to be formatted.
Returns:
str: The text formatted in bold.
\"\"\"
return f"\033[01m{text}\033[0m\""""
)


@pytest.mark.usefixtures("response_fixture_language_enclosed")
def test_response_parser_language_enclosed(response_fixture_language_enclosed):
markdown_code_block = utils.extract_content_from_markdown_code_block(
response_fixture_language_enclosed, "python"
)
assert (
markdown_code_block
== """def get_bold_text(text):
\"\"\"
Returns the provided text formatted in bold.
Args:
text (str): The text to be formatted.
Returns:
str: The text formatted in bold.
\"\"\"
return f"\033[01m{text}\033[0m\""""
)


@pytest.mark.usefixtures("response_fixture_with_text")
def test_response_parser_with_text(response_fixture_with_text):
markdown_code_block = utils.extract_content_from_markdown_code_block(
response_fixture_with_text, "python"
)
assert (
markdown_code_block
== """def get_bold_text(text):
\"\"\"
Returns the provided text formatted in bold.
Args:
text (str): The text to be formatted.
Returns:
str: The text formatted in bold.
\"\"\"
return f"\033[01m{text}\033[0m\""""
)

0 comments on commit cf7f766

Please sign in to comment.