Skip to content

Commit 08bdac3

Browse files
Merge pull request #3 from henriettelienrebnor/write_to_file
Test write to file
2 parents c94fbf2 + c6cd1a3 commit 08bdac3

File tree

9 files changed

+120
-16
lines changed

9 files changed

+120
-16
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ jobs:
55
comment:
66
runs-on: ubuntu-latest
77
permissions:
8-
contents: read
8+
contents: write
99
pull-requests: write
1010
name: Hermegås
1111
steps:
1212
- name: goosy
13-
uses: henriettelienrebnor/Fagdag@more_test
13+
uses: henriettelienrebnor/Fagdag@write_to_file
1414
env:
1515
GITHUB_TOKEN: ${{ secrets.github_token }}
1616
PR_NUMBER: ${{ github.event.number }}

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
FROM python:3.12.5-alpine3.20
22

33
RUN pip install --no-cache pyyaml PyGithub
4+
RUN pip install pyfiglet
45

56
WORKDIR /usr/src
67

poetry.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
_ _ _ _ _ _
2+
/ \ _ __ ___ | | __| | ___(_) | ___ _ __ | |_
3+
/ _ \ | '_ \ / _ \| |/ _` | / __| | |/ _ \ '_ \| __|
4+
/ ___ \| | | | | (_) | | (_| | \__ \ | | __/ | | | |_
5+
/_/ \_\_| |_| \___/|_|\__,_| |___/_|_|\___|_| |_|\__|
6+
7+
_ _____ _ __
8+
_ __ ___ _ __ __| | |_ _| |__ ___ / _|_ __ ___ __ _
9+
| '_ \ / _ \| '_ \ / _` | _____ | | | '_ \ / _ \ | |_| '__/ _ \ / _` |
10+
| |_) | (_) | | | | (_| | |_____| | | | | | | __/ | _| | | (_) | (_| |
11+
| .__/ \___/|_| |_|\__,_| |_| |_| |_|\___| |_| |_| \___/ \__, |
12+
|_| |___/
13+
_ _ _ _ _
14+
(_)_ _ _ __ ___ _ __ ___ (_)_ __ | |_ ___ | |_| |__ ___
15+
| | | | | '_ ` _ \| '_ \/ __| | | '_ \| __/ _ \ | __| '_ \ / _ \
16+
| | |_| | | | | | | |_) \__ \ | | | | | || (_) | | |_| | | | __/
17+
_/ |\__,_|_| |_| |_| .__/|___/ |_|_| |_|\__\___/ \__|_| |_|\___|
18+
|__/ |_|
19+
_ ____ _ _ _
20+
_ __ ___ _ __ __| | / ___| _ __ | | __ _ ___| |__ | |
21+
| '_ \ / _ \| '_ \ / _` | _____ \___ \| '_ \| |/ _` / __| '_ \| |
22+
| |_) | (_) | | | | (_| | |_____| ___) | |_) | | (_| \__ \ | | |_|
23+
| .__/ \___/|_| |_|\__,_| |____/| .__/|_|\__,_|___/_| |_(_)
24+
|_| |_|
25+
____ _ _ _
26+
/ ___|(_) | ___ _ __ ___ ___ __ _ __ _ __ _(_)_ __
27+
\___ \| | |/ _ \ '_ \ / __/ _ \ / _` |/ _` |/ _` | | '_ \
28+
___) | | | __/ | | | (_| __/ | (_| | (_| | (_| | | | | |_
29+
|____/|_|_|\___|_| |_|\___\___| \__,_|\__, |\__,_|_|_| |_(_)
30+
|___/
1.74 KB
Binary file not shown.

src/github_utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from github import PullRequest, InputGitAuthor, Repository
2+
3+
def get_latest_commit_message(pull_request : PullRequest) -> list[str]:
4+
return [c.commit.message for c in pull_request.get_commits()][-1]
5+
6+
def commit_and_push(repo: Repository, target_branch:str, file_path: str) -> None:
7+
author = InputGitAuthor(
8+
"GitHub Action",
9+
"action@github.com")
10+
commit_message = f"Add haiku art"
11+
with open(file_path) as f: new_file_content = f.read()
12+
remote_file = repo.get_contents(file_path, ref=target_branch)
13+
14+
repo.update_file(
15+
remote_file.path,
16+
commit_message,
17+
new_file_content,
18+
remote_file.sha,
19+
branch = target_branch,
20+
author = author)

src/haiku_checker.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import re
2+
3+
def is_haiku(text):
4+
"""Check if a given text follows the 5-7-5 haiku pattern."""
5+
lines = text.strip().split("-")
6+
if len(lines) != 3:
7+
return False
8+
9+
syllable_counts = [count_syllables_in_line(line) for line in lines]
10+
for count in syllable_counts:
11+
print(count)
12+
return syllable_counts == [5, 7, 5]
13+
14+
def count_syllables_in_line(line):
15+
"""Count total syllables in a line."""
16+
words = line.strip().split(" ") # Extract words
17+
return sum(count_syllables(word) for word in words)
18+
19+
def count_syllables(word):
20+
"""Count vowel groups in a word. Remove silent e unless word ends with le"""
21+
word = word.lower()
22+
if word.endswith("e") and not word.endswith("le"):
23+
word = word[:-1]
24+
25+
# Count vowel groups
26+
syllables = re.findall(r'[aeiouy]+', word)
27+
return max(1, len(syllables))
28+

src/haiku_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import unittest
2+
from haiku_checker import count_syllables, is_haiku
3+
4+
class TestHaikuChecker(unittest.TestCase):
5+
6+
def test_syllables(self):
7+
self.assertEqual(count_syllables("silence"), 2)
8+
self.assertEqual(count_syllables("beautiful"), 3)
9+
self.assertEqual(count_syllables("rhythm"), 1)
10+
self.assertEqual(count_syllables("table"), 2)
11+
self.assertEqual(count_syllables("sky"), 1)
12+
self.assertEqual(count_syllables("hmm"), 1)
13+
self.assertEqual(count_syllables("queue"), 1)
14+
15+
def test_is_haiku(self):
16+
haiku_valid = "An old silent pond - The frog jumps into the pond - Splash! Silence again."
17+
haiku_invalid = "An old silent pond - The frog jumps into the pond - Splash! Water everywhere."
18+
19+
self.assertTrue(is_haiku(haiku_valid))
20+
self.assertFalse(is_haiku(haiku_invalid))
21+
22+
if __name__ == '__main__':
23+
unittest.main()

src/main.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
import os
22
from github import Github, Auth
3-
#from dotenv import load_dotenv
3+
import pyfiglet
4+
from haiku_checker import is_haiku
5+
from github_utils import commit_and_push, get_latest_commit_message
46

57
if __name__ == '__main__':
68

7-
#load_dotenv()
89
acces_token = os.environ.get('GITHUB_TOKEN')
910
repo_uri = os.environ.get('GITHUB_REPOSITORY')
1011
pr_number = int(os.environ.get('PR_NUMBER'))
1112

1213
if(repo_uri is None or acces_token is None):
1314
raise Exception('Could not find repository')
15+
1416
token = Auth.Token(acces_token)
15-
1617
github = Github(auth=token)
18+
1719
repo = github.get_repo(repo_uri)
18-
1920
pull_request = repo.get_pull(pr_number)
21+
branch = pull_request.head.ref
22+
23+
file_name = "poetry.md"
24+
file_path = file_name.replace("/github/workspace/", "")
2025

21-
def get_latest_message():
22-
commits_messages = [c.commit.message for c in pull_request.get_commits()]
23-
if (len(commits_messages) > 0) :
24-
return commits_messages[-1]
25-
else:
26-
return ""
27-
28-
latest = get_latest_message()
29-
if latest:
30-
pull_request.create_issue_comment(f"💅{latest}💅")
26+
commit_message = get_latest_commit_message(pull_request)
27+
if(is_haiku(commit_message)):
28+
haiku_ascii_art = pyfiglet.figlet_format(commit_message)
29+
with open(file_path, "w") as file:
30+
file.write(haiku_ascii_art)
31+
commit_and_push(repo, branch, file_path)
32+

src/testing.py

Whitespace-only changes.

0 commit comments

Comments
 (0)