Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Byte-compiled / optimized / DLL files
.DS_Store
__pycache__/
*.py[cod]
*$py.class
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ ADD requirements.txt /requirements.txt
RUN pip install -r requirements.txt

# Copy code.
ADD main.py /main.py
ADD dark.py /dark.py

CMD ["python", "/main.py"]
CMD ["python", "/dark.py"]
79 changes: 79 additions & 0 deletions dark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#dark.py
"""
GitHub Action Code to update README file with provided images randomly.
"""

import os
import re
import sys
import base64
import requests
import random
from github import Github, GithubException

START_COMMENT = '<!--START_SECTION:update_image_dark-->'
END_COMMENT = '<!--END_SECTION:update_image_dark-->'
IMAGE_REPL = f"{START_COMMENT}[\\s\\S]+{END_COMMENT}"

REPO = os.getenv("INPUT_README_REPOSITORY")
IMG_REPO = os.getenv("INPUT_IMG_REPOSITORY")
IMG_PATH = os.getenv("INPUT_IMG_PATH")
GHTOKEN = os.getenv("INPUT_GH_TOKEN")
COMMIT_MSG = os.getenv("INPUT_COMMIT_MESSAGE")
WIDTH = os.getenv("INPUT_WIDTH")
HEIGHT = os.getenv("INPUT_HEIGHT")
ALIGN = os.getenv("INPUT_ALIGN")
IMG_ALT = os.getenv("INPUT_IMG_ALT")

VALID_IMAGES_EXT = ['png', 'jpg', 'jpeg', 'gif', 'svg']


def verify_image_ext(image):
''' Validate image obtained '''
global VALID_IMAGES_EXT
if image.path.split('/')[-1].split('.')[-1].lower() not in VALID_IMAGES_EXT:
print(f"Please make sure image is one of following type {VALID_IMAGES_EXT}, error caused by image - {image.path}")
return False
return True

def get_image_tag(repo):
''' Get new image tag <img> to place in README '''
global IMG_PATH
images = repo.get_contents(IMG_PATH)
image = random.choice(images)
is_image = verify_image_ext(image)
if not is_image:
sys.exit(1)
img_src = image.download_url
img_tag = f"<img src=\"{img_src}#gh-dark-mode-only\" title=\"Hello! 😇💙\" height={HEIGHT} width={WIDTH} align={ALIGN} alt={IMG_ALT} />"
return img_tag

def decode_readme(data: str) -> str:
'''Decode the contents of old readme'''
decoded_bytes = base64.b64decode(data)
return str(decoded_bytes, 'utf-8')

def generate_new_readme(readme: str, image_tag: str) -> str:
'''Generate a new Readme.md'''
update_readme_with = f"{START_COMMENT}\n{image_tag}\n{END_COMMENT}"
return re.sub(IMAGE_REPL, update_readme_with, readme)

if __name__ == "__main__":
g = Github(GHTOKEN)
try:
readme_repo = g.get_repo(REPO)
img_repo = g.get_repo(IMG_REPO)
except GithubException:
print("Authentication Error. Try saving a GitHub Token in your Repo Secrets or Use the GitHub Actions Token, which is automatically used by the action.")
sys.exit(1)
image_tag = get_image_tag(img_repo)
readme_obj = readme_repo.get_readme()
readme_content = readme_obj.content
readme_content_decoded = decode_readme(readme_content)
new_readme = generate_new_readme(readme=readme_content_decoded, image_tag=image_tag)
if readme_content_decoded != new_readme:
readme_repo.update_file(path=readme_obj.path, message=COMMIT_MSG,
content=new_readme, sha=readme_obj.sha)
print("Success")
else:
print("No change")
4 changes: 4 additions & 0 deletions docker_entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

python light.py &
python dark.py
79 changes: 79 additions & 0 deletions light.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#light.py
"""
GitHub Action Code to update README file with provided images randomly.
"""

import os
import re
import sys
import base64
import requests
import random
from github import Github, GithubException

START_COMMENT = '<!--START_SECTION:update_image_light-->'
END_COMMENT = '<!--END_SECTION:update_image_light-->'
IMAGE_REPL = f"{START_COMMENT}[\\s\\S]+{END_COMMENT}"

REPO = os.getenv("INPUT_README_REPOSITORY")
IMG_REPO = os.getenv("INPUT_IMG_REPOSITORY")
IMG_PATH = os.getenv("INPUT_IMG_PATH")
GHTOKEN = os.getenv("INPUT_GH_TOKEN")
COMMIT_MSG = os.getenv("INPUT_COMMIT_MESSAGE")
WIDTH = os.getenv("INPUT_WIDTH")
HEIGHT = os.getenv("INPUT_HEIGHT")
ALIGN = os.getenv("INPUT_ALIGN")
IMG_ALT = os.getenv("INPUT_IMG_ALT")

VALID_IMAGES_EXT = ['png', 'jpg', 'jpeg', 'gif', 'svg']


def verify_image_ext(image):
''' Validate image obtained '''
global VALID_IMAGES_EXT
if image.path.split('/')[-1].split('.')[-1].lower() not in VALID_IMAGES_EXT:
print(f"Please make sure image is one of following type {VALID_IMAGES_EXT}, error caused by image - {image.path}")
return False
return True

def get_image_tag(repo):
''' Get new image tag <img> to place in README '''
global IMG_PATH
images = repo.get_contents(IMG_PATH)
image = random.choice(images)
is_image = verify_image_ext(image)
if not is_image:
sys.exit(1)
img_src = image.download_url
img_tag = f"<img src={img_src} height={HEIGHT} width={WIDTH} align={ALIGN} alt={IMG_ALT} />"
return img_tag

def decode_readme(data: str) -> str:
'''Decode the contents of old readme'''
decoded_bytes = base64.b64decode(data)
return str(decoded_bytes, 'utf-8')

def generate_new_readme(readme: str, image_tag: str) -> str:
'''Generate a new Readme.md'''
update_readme_with = f"{START_COMMENT}\n{image_tag}\n{END_COMMENT}"
return re.sub(IMAGE_REPL, update_readme_with, readme)

if __name__ == "__main__":
g = Github(GHTOKEN)
try:
readme_repo = g.get_repo(REPO)
img_repo = g.get_repo(IMG_REPO)
except GithubException:
print("Authentication Error. Try saving a GitHub Token in your Repo Secrets or Use the GitHub Actions Token, which is automatically used by the action.")
sys.exit(1)
image_tag = get_image_tag(img_repo)
readme_obj = readme_repo.get_readme()
readme_content = readme_obj.content
readme_content_decoded = decode_readme(readme_content)
new_readme = generate_new_readme(readme=readme_content_decoded, image_tag=image_tag)
if readme_content_decoded != new_readme:
readme_repo.update_file(path=readme_obj.path, message=COMMIT_MSG,
content=new_readme, sha=readme_obj.sha)
print("Success")
else:
print("No change")