-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(github): add github integration
- Loading branch information
1 parent
362aef2
commit 7bdfdcf
Showing
29 changed files
with
1,025 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
betamax==0.9.0 | ||
betamax-serializers==0.2.1 | ||
pytest==8.3.3 | ||
pytest-asyncio==0.24.0 | ||
pytest-cov==6.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# standard imports | ||
import os | ||
|
||
# lib imports | ||
from cryptography import x509 | ||
from cryptography.hazmat.backends import default_backend | ||
from cryptography.hazmat.primitives import hashes | ||
from cryptography.hazmat.primitives.asymmetric import rsa | ||
from cryptography.hazmat.primitives.serialization import Encoding, PrivateFormat, NoEncryption | ||
from datetime import datetime, timedelta, UTC | ||
|
||
# local imports | ||
from src.common import common | ||
|
||
CERT_FILE = os.path.join(common.data_dir, "cert.pem") | ||
KEY_FILE = os.path.join(common.data_dir, "key.pem") | ||
|
||
|
||
def check_expiration(cert_path: str) -> int: | ||
with open(cert_path, "rb") as cert_file: | ||
cert_data = cert_file.read() | ||
cert = x509.load_pem_x509_certificate(cert_data, default_backend()) | ||
expiry_date = cert.not_valid_after_utc | ||
return (expiry_date - datetime.now(UTC)).days | ||
|
||
|
||
def generate_certificate(): | ||
private_key = rsa.generate_private_key( | ||
public_exponent=65537, | ||
key_size=4096, | ||
) | ||
subject = issuer = x509.Name([ | ||
x509.NameAttribute(x509.NameOID.COMMON_NAME, u"localhost"), | ||
]) | ||
cert = x509.CertificateBuilder().subject_name( | ||
subject | ||
).issuer_name( | ||
issuer | ||
).public_key( | ||
private_key.public_key() | ||
).serial_number( | ||
x509.random_serial_number() | ||
).not_valid_before( | ||
datetime.now(UTC) | ||
).not_valid_after( | ||
datetime.now(UTC) + timedelta(days=365) | ||
).sign(private_key, hashes.SHA256()) | ||
|
||
with open(KEY_FILE, "wb") as f: | ||
f.write(private_key.private_bytes( | ||
encoding=Encoding.PEM, | ||
format=PrivateFormat.TraditionalOpenSSL, | ||
encryption_algorithm=NoEncryption(), | ||
)) | ||
|
||
with open(CERT_FILE, "wb") as f: | ||
f.write(cert.public_bytes(Encoding.PEM)) | ||
|
||
|
||
def initialize_certificate() -> tuple[str, str]: | ||
print("Initializing SSL certificate") | ||
if os.path.exists(CERT_FILE) and os.path.exists(KEY_FILE): | ||
cert_expires_in = check_expiration(CERT_FILE) | ||
print(f"Certificate expires in {cert_expires_in} days.") | ||
if cert_expires_in >= 90: | ||
return CERT_FILE, KEY_FILE | ||
print("Generating new certificate") | ||
generate_certificate() | ||
return CERT_FILE, KEY_FILE | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# standard imports | ||
import shelve | ||
import threading | ||
|
||
|
||
class Database: | ||
def __init__(self, db_path): | ||
self.db_path = db_path | ||
self.lock = threading.Lock() | ||
|
||
def __enter__(self): | ||
self.lock.acquire() | ||
self.db = shelve.open(self.db_path, writeback=True) | ||
return self.db | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
self.sync() | ||
self.db.close() | ||
self.lock.release() | ||
|
||
def sync(self): | ||
self.db.sync() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DISCORD_BOT = None | ||
REDDIT_BOT = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# standard imports | ||
import os | ||
from typing import Union | ||
|
||
# lib imports | ||
import requests | ||
|
||
|
||
tier_map = { | ||
't4-sponsors': 15, | ||
't3-sponsors': 10, | ||
't2-sponsors': 5, | ||
't1-sponsors': 3, | ||
} | ||
|
||
|
||
def get_github_sponsors() -> Union[dict, False]: | ||
""" | ||
Get list of GitHub sponsors. | ||
Returns | ||
------- | ||
Union[dict, False] | ||
JSON response containing the list of sponsors. False if an error occurred. | ||
""" | ||
token = os.getenv("GITHUB_TOKEN") | ||
org_name = os.getenv("GITHUB_ORG_NAME", "LizardByte") | ||
|
||
graphql_url = "https://api.github.com/graphql" | ||
headers = { | ||
"Authorization": f"Bearer {token}", | ||
"Content-Type": "application/json" | ||
} | ||
query = """ | ||
query { | ||
organization(login: "%s") { | ||
sponsorshipsAsMaintainer(first: 100) { | ||
edges { | ||
node { | ||
sponsorEntity { | ||
... on User { | ||
login | ||
name | ||
avatarUrl | ||
url | ||
} | ||
... on Organization { | ||
login | ||
name | ||
avatarUrl | ||
url | ||
} | ||
} | ||
tier { | ||
name | ||
monthlyPriceInDollars | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
""" % org_name | ||
|
||
response = requests.post(graphql_url, json={'query': query}, headers=headers) | ||
data = response.json() | ||
|
||
if 'errors' in data: | ||
print(data['errors']) | ||
print('::error::An error occurred while fetching sponsors.') | ||
return False | ||
|
||
return data | ||
Oops, something went wrong.