Skip to content

Commit eb6884d

Browse files
committed
Added zoomed in snapshot for peek scripts
1 parent 4ec9d85 commit eb6884d

15 files changed

Lines changed: 159 additions & 28 deletions

.github/scripts/build_assets/SeleniumRunner.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,13 @@ def upload_icomoon(self, icomoon_json_path: str):
118118

119119
print("JSON file uploaded.")
120120

121-
def upload_svgs(self, svgs: List[str]):
121+
def upload_svgs(self, svgs: List[str], screenshot_folder: str=""):
122122
"""
123123
Upload the SVGs provided in folder_info
124124
:param svgs: a list of svg Paths that we'll upload to icomoon.
125+
:param screenshot_folder: the name of the screenshot_folder. If
126+
the value is provided, it means the user want to take a screenshot
127+
of each icon.
125128
"""
126129
try:
127130
print("Uploading SVGs...")
@@ -133,17 +136,20 @@ def upload_svgs(self, svgs: List[str]):
133136

134137
self.click_hamburger_input()
135138

136-
for svg in svgs:
139+
for i in range(len(svgs)):
137140
import_btn = self.driver.find_element_by_css_selector(
138141
"li.file input[type=file]"
139142
)
140-
import_btn.send_keys(svg)
141-
print(f"Uploaded {svg}")
143+
import_btn.send_keys(svgs[i])
144+
print(f"Uploaded {svgs[i]}")
142145
self.test_for_possible_alert(self.SHORT_WAIT_IN_SEC, "Dismiss")
143-
self.remove_color_from_icon()
146+
self.remove_color_from_icon(screenshot_folder, i)
144147

145148
# take a screenshot of the icons that were just added
146-
self.driver.save_screenshot("new_icons.png");
149+
new_icons_path = str(Path(screenshot_folder, "new_icons.png").resolve())
150+
self.driver.save_screenshot(new_icons_path);
151+
152+
# select all the svgs so that the newly added svg are part of the collection
147153
self.click_hamburger_input()
148154
select_all_button = WebDriverWait(self.driver, self.LONG_WAIT_IN_SEC).until(
149155
ec.element_to_be_clickable((By.XPATH, "//button[text()='Select All']"))
@@ -191,11 +197,15 @@ def test_for_possible_alert(self, wait_period: float, btn_text: str):
191197
)
192198
dismiss_btn.click()
193199
except SeleniumTimeoutException:
194-
pass
200+
pass # nothing found => everything is good
195201

196-
def remove_color_from_icon(self):
202+
def remove_color_from_icon(self, screenshot_folder: str, index: int):
197203
"""
198204
Remove the color from the most recent uploaded icon.
205+
:param screenshot_folder: the name of the screenshot_folder. If
206+
the value is provided, it means the user want to take a screenshot
207+
of each icon.
208+
:param index, index of the icon being uploaded. Used for naming the screenshot.
199209
:return: None.
200210
"""
201211
try:
@@ -216,8 +226,11 @@ def remove_color_from_icon(self):
216226
remove_color_btn = self.driver \
217227
.find_element_by_css_selector("div.overlayWindow i.icon-droplet-cross")
218228
remove_color_btn.click()
219-
except SeleniumTimeoutException:
220-
pass
229+
230+
# take a screenshot before closing the pop up
231+
if screenshot_folder:
232+
screenshot_path = str(Path(screenshot_folder, f"screenshot_{index}.png").resolve())
233+
self.driver.save_screenshot(screenshot_path)
221234
except Exception as e:
222235
self.close()
223236
raise e

.github/scripts/build_assets/filehandler.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,18 @@ def rename_extracted_files(extract_path: str):
145145
os.replace(dict_["old"], dict_["new"])
146146

147147
print("Files renamed")
148+
149+
150+
def create_screenshot_folder(dir, screenshot_name: str="screenshots"):
151+
"""
152+
Create a screenshots folder in the dir.
153+
:param dir, the dir where we want to create the folder.
154+
:param screenshot_name, the name of the screenshot folder.
155+
:raise Exception if the dir provided is not a directory.
156+
:return the string name of the screenshot folder.
157+
"""
158+
folder = Path(dir).resolve()
159+
if not folder.is_dir():
160+
raise Exception(f"This is not a dir: {str(folder)}. \ndir must be a valid directory")
161+
folder.mkdir(screenshot_name, exist_ok=True)
162+
return str(folder)

.github/scripts/build_assets/util.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
from pathlib import Path
21
from argparse import ArgumentParser
32
from build_assets.PathResolverAction import PathResolverAction
43

5-
def get_commandline_args():
4+
def get_commandline_args(peek_mode=False):
65
parser = ArgumentParser(description="Upload svgs to Icomoon to create icon files.")
76

87
parser.add_argument("--headless",
@@ -26,8 +25,11 @@ def get_commandline_args():
2625
action=PathResolverAction)
2726

2827
parser.add_argument("download_path",
29-
help="The path where you'd like to download the Icomoon files to",
28+
help="The download destination of the Icomoon files",
3029
action=PathResolverAction)
3130

31+
if peek_mode:
32+
parser.add_argument("--pr_title",
33+
help="The title of the PR that we are peeking at")
3234

33-
return parser.parse_args()
35+
return parser.parse_args()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List
2+
3+
4+
def generate_screenshot_markdown(img_urls: List[str]):
5+
"""
6+
Generate the markdown for the screenshots using the
7+
img_urls then print it to the console.
8+
:param img_urls are valid image links.
9+
"""
10+
template = "![Detailed Screenshot]({})"
11+
return [template.format(img_url) for img_url in img_urls]
12+
13+
14+
if __name__ == "__main__":
15+
markdown = generate_screenshot_markdown()
16+
print("\n\n".join(markdown)) # format it before printing

.github/scripts/icomoon_peek.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from os import sep
2+
from typing import List
3+
import re
14
from selenium.common.exceptions import TimeoutException
25

36
# pycharm complains that build_assets is an unresolved ref
@@ -7,27 +10,50 @@
710

811

912
def main():
10-
args = util.get_commandline_args()
13+
args = util.get_commandline_args(True)
1114
new_icons = filehandler.find_new_icons(args.devicon_json_path, args.icomoon_json_path)
1215
if len(new_icons) == 0:
1316
print("No files need to be uploaded. Ending script...")
1417
return
1518

19+
# get only the icon object that has the name matching the pr title
20+
filtered_icons = find_object_added_in_this_pr(new_icons, args.pr_title)
21+
1622
# print list of new icons
1723
print("List of new icons:", *new_icons, sep = "\n")
24+
print("Icons being uploaded:", *filtered_icons, sep = "\n")
1825

1926
runner = None
2027
try:
2128
runner = SeleniumRunner(args.download_path, args.geckodriver_path, args.headless)
2229
svgs = filehandler.get_svgs_paths(new_icons, args.icons_folder_path)
23-
runner.upload_svgs(svgs)
30+
screenshot_folder = filehandler.create_screenshot_folder("./")
31+
runner.upload_svgs(svgs, screenshot_folder)
2432
print("Task completed.")
2533
except TimeoutException as e:
34+
print("Selenium Time Out Error: ", e.stacktrace, sep='\n')
35+
except Exception as e:
2636
print(e)
27-
print(e.stacktrace)
2837
finally:
2938
runner.close()
3039

3140

41+
def find_object_added_in_this_pr(icons: List[dict], pr_title: str):
42+
"""
43+
Find the icon name from the PR title.
44+
:param icons, a list of the font objects found in the devicon.json.
45+
:pr_title, the title of the PR that this workflow was called on.
46+
:return a list containing the dictionary with the "name"
47+
entry's value matching the name in the pr_title.
48+
If none can be found, return an empty list.
49+
"""
50+
try:
51+
pattern = re.compile(r"(?<=^new icon: )\w+ (?=\(.+\))", re.I)
52+
icon_name = pattern.findall(pr_title)[0].lower().strip() # should only have one match
53+
return [icon for icon in icons if icon["name"] == icon_name]
54+
except IndexError: # there are no match in the findall()
55+
return []
56+
57+
3258
if __name__ == "__main__":
3359
main()

.github/workflows/build_icons.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ jobs:
1919
pip install -r ./.github/scripts/requirements.txt
2020
npm install
2121
- name: Executing build and create fonts via icomoon
22-
run: npm run build
22+
run: >
23+
python ./.github/scripts/icomoon_build.py
24+
./.github/scripts/build_assets/geckodriver-v0.27.0-win64/geckodriver.exe ./icomoon.json
25+
./devicon.json ./icons ./ --headless
2326
- name: Upload geckodriver.log for debugging purposes
2427
uses: actions/upload-artifact@v2
2528
if: ${{failure()}}

.github/workflows/peek_icons.yml

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,41 @@ jobs:
2121
python -m pip install --upgrade pip
2222
pip install -r ./.github/scripts/requirements.txt
2323
- name: Run icomoon_peek.py
24-
run: npm run peek
24+
env:
25+
PR_TITLE: ${{ github.event.pull_request.title }}
26+
run: >
27+
python ./.github/scripts/icomoon_peek.py
28+
./.github/scripts/build_assets/geckodriver-v0.27.0-win64/geckodriver.exe ./icomoon.json
29+
./devicon.json ./icons ./ --headless --pr_title $PR_TITLE
2530
- name: Upload geckodriver.log for debugging purposes
2631
uses: actions/upload-artifact@v2
2732
if: ${{failure()}}
2833
with:
2934
name: geckodriver-log
3035
path: ./geckodriver.log
3136
- name: Upload screenshot of the newly made icons
32-
id: imgur_step
33-
uses: devicons/public-upload-to-imgur@v1
37+
id: new_icons_overview_step
38+
uses: devicons/public-upload-to-imgur@v1.1
39+
if: ${{success()}}
40+
with:
41+
img_path: ./screenshots/new_icons.png
42+
client_id: ${{secrets.IMGUR_CLIENT_ID}}
43+
- name: Upload zoomed in screenshot of the newly made icons
44+
id: new_icons_detailed_step
45+
uses: devicons/public-upload-to-imgur@v1.1
3446
if: ${{success()}}
3547
with:
36-
img_path: ./new_icons.png
48+
img_path: ./screenshots/screenshot_*.png
3749
client_id: ${{secrets.IMGUR_CLIENT_ID}}
50+
- name: Generate the markdowns for the screenshot
51+
run: |
52+
echo "DETAILED_IMGS_MARKDOWN<<EOF" >> $GITHUB_ENV
53+
python ./.github/scripts/generate_screenshot_markdown.py >> $GITHUB_ENV
54+
echo "EOF" >> $GITHUB_ENV
3855
- name: Comment on the PR about the result
3956
uses: github-actions-up-and-running/pr-comment@v1.0.1
4057
env:
41-
IMG_URL: ${{ steps.imgur_step.outputs.imgur_url }}
58+
OVERVIEW_IMG_URL: ${{ steps.imgur_step.outputs.imgur_url }}
4259
MESSAGE: |
4360
Hi!
4461
@@ -47,7 +64,10 @@ jobs:
4764
4865
![Peeked Icons (top left)]({0})
4966
50-
Note: If the image doesn't show up, it's probably because it has been autodeleted by Imgur after 6 months due to our API choice.
67+
Here are the zoomed-in screenshots of the added icons:
68+
{1}
69+
70+
Note: If the images doesn't show up, it's probably because it has been autodeleted by Imgur after 6 months due to our API choice.
5171
5272
The maintainers will now take a look at it and decide whether to merge your PR.
5373
@@ -56,4 +76,4 @@ jobs:
5676
Peek Bot
5777
with:
5878
repo-token: ${{ secrets.GITHUB_TOKEN }}
59-
message: ${{format(env.MESSAGE, env.IMG_URL, env.IMG_URL)}}
79+
message: ${{format(env.MESSAGE, env.IMG_URL, env.DETAILED_IMGS_MARKDOWN)}}

devicon.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,6 +2173,40 @@
21732173
"color": "#a62c46",
21742174
"aliases": []
21752175
},
2176+
{
2177+
"name": "testingicon",
2178+
"tags": [
2179+
"framework"
2180+
],
2181+
"versions": {
2182+
"svg": [
2183+
"original",
2184+
"plain"
2185+
],
2186+
"font": [
2187+
"plain"
2188+
]
2189+
},
2190+
"color": "#61dafb",
2191+
"aliases": [ ]
2192+
},
2193+
{
2194+
"name": "testingicon2",
2195+
"tags": [
2196+
"framework"
2197+
],
2198+
"versions": {
2199+
"svg": [
2200+
"original",
2201+
"plain"
2202+
],
2203+
"font": [
2204+
"plain"
2205+
]
2206+
},
2207+
"color": "#61dafb",
2208+
"aliases": [ ]
2209+
},
21762210
{
21772211
"name": "react",
21782212
"tags": [
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)