From 2d6e1bd64ec85f3e967e1339029522ca0fda9a19 Mon Sep 17 00:00:00 2001 From: Sakshi Sharma Date: Thu, 22 Jun 2023 03:10:30 +0530 Subject: [PATCH 01/11] added files light and dark --- .DS_Store | Bin 0 -> 6148 bytes Dockerfile | 6 ++-- dark.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++ light.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 .DS_Store create mode 100644 dark.py create mode 100644 light.py diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..4d9b01f8d78f376c0d7474ac03bc65e7101f9639 GIT binary patch literal 6148 zcmeHKJ5Iwu5S=w91VNFK(p&&3EtQE(kCY2Q2ntBC9Es2%ioF1WOVLtLa0-Mt0piWh zDD2{dGD2uZ+I{Q!c*f74@e+|6+>HA~JtFF2FlO7BmI&KZo@G$s?m;I-s)IR>zKD&&bjm`-|se_y~ zogw!HU8lKk?|$0j{MqyCz2;BOQ8(W;tnx|f$RD|mO`3BBnA0j~>piu`HGIA;YKY?F z3YL2$`NMn1C72ik#(*)fRtC_sS-LxdHX8%RfHAOOfWHp~24kt{1;eKUQ`iCkbEt!0 z&3g%sDHKaZF9-|7NhnZ4oo+Fl{t`I}hrLvPFDT*ULS~#tGdta(xR4$CBDj+)1#LD4 zjDeJa4LR&_|36;6|4)PL$QUpN{uKi*uZGnCm!!S5b#dHl3yf0?7WVUkix4bAD~2z( c;ysKY&= 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_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") \ No newline at end of file diff --git a/light.py b/light.py new file mode 100644 index 0000000..a92bf09 --- /dev/null +++ b/light.py @@ -0,0 +1,79 @@ +#main.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 = '' +END_COMMENT = '' +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 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_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") \ No newline at end of file From f94120067f369233ec97430d0f132b3bc2e0fa35 Mon Sep 17 00:00:00 2001 From: Sakshi Sharma Date: Thu, 22 Jun 2023 03:15:47 +0530 Subject: [PATCH 02/11] Update Dockerfile --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7cfe860..0c828aa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,8 +5,8 @@ ADD requirements.txt /requirements.txt RUN pip install -r requirements.txt # Copy code. -ADD light.py /main.py -ADD dark.py /main.py +ADD light.py /light.py +ADD dark.py /dark.py CMD ["python", "/light.py"] CMD ["python", "/dark.py"] From 803c8bfa63a2c6d3465611942eb0414e05d0caa5 Mon Sep 17 00:00:00 2001 From: Sakshi Sharma Date: Thu, 22 Jun 2023 03:29:00 +0530 Subject: [PATCH 03/11] Update Dockerfile --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 0c828aa..91e55db 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,5 +8,6 @@ RUN pip install -r requirements.txt ADD light.py /light.py ADD dark.py /dark.py -CMD ["python", "/light.py"] CMD ["python", "/dark.py"] +CMD ["python", "/light.py"] + From 8548ada00bfdbe9ab5c0642ba6700ff4a9b5421b Mon Sep 17 00:00:00 2001 From: Sakshi Sharma Date: Thu, 22 Jun 2023 04:20:03 +0530 Subject: [PATCH 04/11] Update Dockerfile --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 91e55db..e743caa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,6 @@ RUN pip install -r requirements.txt ADD light.py /light.py ADD dark.py /dark.py -CMD ["python", "/dark.py"] CMD ["python", "/light.py"] +CMD ["python", "/dark.py"] From 205c3c93cf5a29530bede7d97be63e04c1ef6800 Mon Sep 17 00:00:00 2001 From: Sakshi Sharma Date: Thu, 22 Jun 2023 04:37:10 +0530 Subject: [PATCH 05/11] made changes to run docker --- .DS_Store | Bin 6148 -> 6148 bytes Dockerfile | 3 +-- docker_entrypoint.sh | 6 ++++++ 3 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 docker_entrypoint.sh diff --git a/.DS_Store b/.DS_Store index 4d9b01f8d78f376c0d7474ac03bc65e7101f9639..9ee4b1482ab95cc8f36d2073c8b5b90029417428 100644 GIT binary patch delta 107 zcmZoMXffEJ$`r?%BErDHz`~%%kj{|FP?DSP;*yk;p9B=+(CY7haYyl}BdUA~UipFy U!{Frn+ybB;pa~3{o0%F#0kW|g`Tzg` delta 107 zcmZoMXffEJ$`r@)!HR)_frUYjA)O(Up(Hoo#U&{xKM5$t!NAbae@XGEBdUA~UipFy V!{Frn+ybB;1_ri(&CN`Wq5ydg8KVFI diff --git a/Dockerfile b/Dockerfile index e743caa..2d39174 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,5 @@ RUN pip install -r requirements.txt ADD light.py /light.py ADD dark.py /dark.py -CMD ["python", "/light.py"] -CMD ["python", "/dark.py"] +ENTRYPOINT ["/docker_entrypoint.sh"] diff --git a/docker_entrypoint.sh b/docker_entrypoint.sh new file mode 100644 index 0000000..b8088f2 --- /dev/null +++ b/docker_entrypoint.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +set -e + +exec python light.py & +exec python dark.py \ No newline at end of file From cd5b0646aeee362af24b78c1380cccccb0d255fc Mon Sep 17 00:00:00 2001 From: Sakshi Sharma Date: Thu, 22 Jun 2023 04:39:35 +0530 Subject: [PATCH 06/11] made changes to run docker --- .DS_Store | Bin 6148 -> 6148 bytes Dockerfile | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/.DS_Store b/.DS_Store index 9ee4b1482ab95cc8f36d2073c8b5b90029417428..a8cf308e9d41232979061c336d34c7e35ec0ffda 100644 GIT binary patch delta 106 zcmZoMXffEJ#uQuJ!N9=4!l1{H&XCDalAG`1l9ZF51Qg?7U}zA$t9aBARXznS|Kp@3 ZKe~KDhGB4Wer^F!572~w&CN`Wq5y4w8Uz3U delta 106 zcmZoMXffEJ#uS?(!oa}5!l1{H&XCDalAG`1l9ZF51Qg@Y>hFJXNAaj5s(cDw`GO3? T;N<+=0-zqC2@IQ?nHog_t|l7w diff --git a/Dockerfile b/Dockerfile index 2d39174..39376f8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,5 +8,5 @@ RUN pip install -r requirements.txt ADD light.py /light.py ADD dark.py /dark.py -ENTRYPOINT ["/docker_entrypoint.sh"] +ENTRYPOINT ["docker_entrypoint.sh"] From 7f51d203267d9f67e1f3147a20325d8cb37a6fbf Mon Sep 17 00:00:00 2001 From: Sakshi Sharma Date: Thu, 22 Jun 2023 04:43:13 +0530 Subject: [PATCH 07/11] made changes to run docker --- .DS_Store | Bin 6148 -> 6148 bytes Dockerfile | 3 ++- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.DS_Store b/.DS_Store index a8cf308e9d41232979061c336d34c7e35ec0ffda..5af0b7bd3ee522eec6fcdfead0e9fe09faa85143 100644 GIT binary patch delta 106 zcmZoMXffEJ#uQtXz`(%3!l1{H&XCDalAG`1l9ZF51Qg={;>f#-M;%e+Q{eLTz4yY= Y Date: Thu, 22 Jun 2023 04:45:42 +0530 Subject: [PATCH 08/11] made changes to run docker --- .DS_Store | Bin 6148 -> 6148 bytes Dockerfile | 5 ++--- docker_entrypoint.sh | 6 ++---- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/.DS_Store b/.DS_Store index 5af0b7bd3ee522eec6fcdfead0e9fe09faa85143..90a4db3a592ea561660d10d651521854f81f970a 100644 GIT binary patch delta 106 zcmZoMXffEJ#uVG=$-uzC!l1{H&XCDalAG`1l9ZF51Qg={;$wFek2<2tr@-agg*%U+ Y%NJxA1}Ep|76A1CO}MbRnW<3}0Ay?$W&i*H delta 106 zcmZoMXffEJ#uQtXz`(%3!l1{H&XCDalAG`1l9ZF51Qg={;>f#-M;%e+Q{eLTz4yY= Y Date: Thu, 22 Jun 2023 04:54:52 +0530 Subject: [PATCH 09/11] Update Dockerfile --- Dockerfile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 042e4e9..3a1f66f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,8 +5,6 @@ ADD requirements.txt /requirements.txt RUN pip install -r requirements.txt # Copy code. -ADD light.py /light.py ADD dark.py /dark.py -CMD ["python", "/light.py"] -CMD ["python", "/dark.py"] \ No newline at end of file +CMD ["python", "/dark.py"] From 35686a75577155a492afa3a773894d8bb3964398 Mon Sep 17 00:00:00 2001 From: Sakshi Sharma Date: Fri, 23 Jun 2023 05:14:46 +0530 Subject: [PATCH 10/11] src to dark mode only --- .gitignore | 1 + dark.py | 4 ++-- light.py | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index b6e4761..169647b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ # Byte-compiled / optimized / DLL files +.DS_Store __pycache__/ *.py[cod] *$py.class diff --git a/dark.py b/dark.py index 21309b2..bdd56c3 100644 --- a/dark.py +++ b/dark.py @@ -1,4 +1,4 @@ -#main.py +#dark.py """ GitHub Action Code to update README file with provided images randomly. """ @@ -45,7 +45,7 @@ def get_image_tag(repo): if not is_image: sys.exit(1) img_src = image.download_url - img_tag = f"{IMG_ALT}" + img_tag = f"{IMG_ALT}" return img_tag def decode_readme(data: str) -> str: diff --git a/light.py b/light.py index a92bf09..a6cb50f 100644 --- a/light.py +++ b/light.py @@ -1,4 +1,4 @@ -#main.py +#light.py """ GitHub Action Code to update README file with provided images randomly. """ From f0c4be5b26dffa27154add0f3bb4091451e8382e Mon Sep 17 00:00:00 2001 From: Sakshi Sharma Date: Fri, 23 Jun 2023 05:31:30 +0530 Subject: [PATCH 11/11] updated dark mode --- dark.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dark.py b/dark.py index bdd56c3..e77dcf2 100644 --- a/dark.py +++ b/dark.py @@ -45,7 +45,7 @@ def get_image_tag(repo): if not is_image: sys.exit(1) img_src = image.download_url - img_tag = f"{IMG_ALT}" + img_tag = f"{IMG_ALT}" return img_tag def decode_readme(data: str) -> str: