Skip to content

Commit a51c309

Browse files
authored
refactoring of badge creation (#1166)
1 parent 344ffe8 commit a51c309

3 files changed

Lines changed: 70 additions & 49 deletions

File tree

.github/workflows/welcome-new-member.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
2121
uuid=$(echo "${{github.head_ref}}" | cut -d - -f2 | awk '{$1=$1};1')
2222
echo "uuid=$uuid" >> $GITHUB_OUTPUT
23+
2324
- name: Create comment on success
2425
uses: peter-evans/create-or-update-comment@v5
2526
with:
@@ -30,7 +31,7 @@ jobs:
3031
**${{ steps.get-id.outputs.project_name }} is now part of the Qiskit Ecosystem**. Here are some follow up actions you can take to get the most out of the program:
3132
3233
- :speech_balloon: Join other Qiskit ecosystem developers in the Slack [channel `#qiskit-ecosystem`](https://qiskit.slack.com/archives/C04RHE56N93). If you are not in the Qiskit workspace yet, sign up at https://qisk.it/join-slack
33-
- :books: Use the [Qiskit theme](https://github.com/Qiskit/qiskit_sphinx_theme) for your documentation
34+
- :books: Use the [Qiskit Sphinx theme](https://github.com/Qiskit/qiskit_sphinx_theme) for your documentation
3435
- :purple_heart: Add the [Qiskit Ecosystem badge](https://qiskit.github.io/ecosystem/p/${{ steps.get-id.outputs.uuid }}/#badge) to the project's `README.md`
3536
- :ballot_box_with_check: Keep an eye on the [criteria compliance](https://qiskit.github.io/ecosystem/p/${{ steps.get-id.outputs.uuid }}/#checkups) to keep the project membership status.
3637
- :telephone_receiver: If you have questions or feedback, [join the Qiskit Community Call](http://evt.to/eooiimagw), [hosted by the Unitary Foundation](https://unitary.foundation/community/events/), or just open an issue on this repo.

ecosystem/badge.py

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
from dataclasses import dataclass
44
from typing import Optional
55

6+
from .error_handling import EcosystemError, logger
7+
from .request import URL, request_json
68
from .serializable import JsonSerializable
7-
from .request import URL
89

910

1011
@dataclass
@@ -15,7 +16,7 @@ class BadgeData(JsonSerializable):
1516
"""
1617

1718
# pylint: disable=invalid-name
18-
url: str | URL # the bitly link to the badge
19+
url: Optional[str | URL] = None # the bitly link to the badge
1920
style: Optional[str] = None # default: "flat"
2021
schemaVersion: Optional[int] = None
2122
label: Optional[str] = None
@@ -27,5 +28,61 @@ class BadgeData(JsonSerializable):
2728
logoSize: Optional[str] = None
2829

2930
def __post_init__(self):
30-
self.url = self.url if isinstance(self.url, URL) else URL(self.url)
31-
self.style = "flat" if self.style is None else str(self.style)
31+
if self.url:
32+
self.url = self.url if isinstance(self.url, URL) else URL(self.url)
33+
if self.style:
34+
if self.style not in [
35+
"flat",
36+
"flat-square",
37+
"plastic",
38+
"for-the-badge",
39+
"social",
40+
]:
41+
raise EcosystemError("Entry badge.style not valid")
42+
else:
43+
self.style = "flat"
44+
45+
def update_url(self, name, short_uuid):
46+
"""If not there yet, creates a new Bitly link for the badge"""
47+
short_url = f"https://qisk.it/e-{short_uuid}"
48+
49+
qisk_dot_it_link_check = request_json(
50+
short_url,
51+
parser=lambda x: {
52+
"exists": x.startswith("<svg xmlns") and x.endswith("</svg>")
53+
},
54+
)
55+
if qisk_dot_it_link_check["exists"]:
56+
self.url = short_url
57+
else:
58+
self.url = BadgeData.create_link(name, short_uuid)
59+
60+
@staticmethod
61+
def create_link(name, short_uuid):
62+
"""Creates short link in bitly.com"""
63+
long_url = (
64+
"https://img.shields.io/endpoint?url="
65+
f"https://qiskit.github.io/ecosystem/b/{short_uuid}"
66+
)
67+
keyword = f"e-{short_uuid}"
68+
data = {
69+
"long_url": long_url,
70+
"domain": "qisk.it",
71+
"keyword": keyword,
72+
"group_guid": "Bj9rgMHKfxH",
73+
"title": f'Qiskit ecosystem "{name}" badge',
74+
"tags": ["qiskit ecosystem badge", "permanent _do NOT remove_"],
75+
}
76+
try:
77+
response = request_json("https://api-ssl.bitly.com/v4/bitlinks", post=data)
78+
except EcosystemError as err:
79+
if "Bad Request (400)" in err.message:
80+
logger.info(
81+
"Bitly creation failed: %s -> %s ", f"qisk.it/{keyword}", long_url
82+
)
83+
return None # Sometimes, bitly errors 400 for some server-side reason
84+
raise err
85+
logger.info(
86+
"Bitly short link created: %s -> %s ", f"qisk.it/{keyword}", long_url
87+
)
88+
return response["link"]

ecosystem/member.py

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
from uuid import uuid4
55
from slugify import slugify
66

7-
from .error_handling import EcosystemError, logger
7+
from .error_handling import EcosystemError
88
from .julia import JuliaData
99
from .serializable import JsonSerializable, parse_datetime
1010
from .github import GitHubData
1111
from .pypi import PyPIData
1212
from .check import CheckData
1313
from .badge import BadgeData
14-
from .request import URL, request_json
14+
from .request import URL
1515
from .validation import validate_member
1616

1717

@@ -182,50 +182,13 @@ def update_github(self):
182182
self.github.update_json()
183183
self.github.update_owner_repo()
184184

185-
def _create_qisk_dot_it_link_for_badge(self):
186-
long_url = (
187-
"https://img.shields.io/endpoint?url="
188-
f"https://qiskit.github.io/ecosystem/b/{self.short_uuid}"
189-
)
190-
keyword = f"e-{self.short_uuid}"
191-
data = {
192-
"long_url": long_url,
193-
"domain": "qisk.it",
194-
"keyword": keyword,
195-
"group_guid": "Bj9rgMHKfxH",
196-
"title": f'Qiskit ecosystem "{self.name}" badge',
197-
"tags": ["qiskit ecosystem badge", "permanent _do NOT remove_"],
198-
}
199-
try:
200-
response = request_json("https://api-ssl.bitly.com/v4/bitlinks", post=data)
201-
except EcosystemError as err:
202-
if "Bad Request (400)" in err.message:
203-
return None # Sometimes, bitly errors 400 for some server-side reason
204-
raise err
205-
logger.info(
206-
"Bitly short link created: %s -> %s ", f"qisk.it/{keyword}", long_url
207-
)
208-
return response["link"]
209-
210-
def _qisk_dot_it_link_exists(self):
211-
qisk_dot_it_link_check = request_json(
212-
f"https://qisk.it/e-{self.short_uuid}",
213-
parser=lambda x: {"exists": "<title>Qiskit Ecosystem:" not in x},
214-
)
215-
return (
216-
f"https://qisk.it/e-{self.short_uuid}"
217-
if qisk_dot_it_link_check["exists"]
218-
else None
219-
)
220-
221185
def update_badge(self):
222186
"""If not there yet, creates a new Bitly link for the badge"""
223-
if self.badge is None:
224-
badge_url = (
225-
self._qisk_dot_it_link_exists()
226-
or self._create_qisk_dot_it_link_for_badge()
227-
)
228-
self.badge = BadgeData(url=badge_url) if badge_url else None
187+
if self.badge:
188+
self.badge.update_url(name=self.name, short_uuid=self.short_uuid)
189+
else:
190+
url = BadgeData.create_link(name=self.name, short_uuid=self.short_uuid)
191+
self.badge = BadgeData(url)
229192

230193
def update_data(self):
231194
"""Update all the member data in each existing section"""

0 commit comments

Comments
 (0)