Skip to content

Commit 9608e0c

Browse files
authored
Merge pull request #6 from relic-se/scripts
Git Integration in Build Scripts
2 parents 8cdc57d + b9686c7 commit 9608e0c

File tree

4 files changed

+18
-33
lines changed

4 files changed

+18
-33
lines changed

.github/workflows/build_readme.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ name: Validate Build
77
on: [pull_request, push]
88

99
jobs:
10-
validate-build:
10+
validate-readme-build:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: Set up requested Python version
@@ -29,7 +29,5 @@ jobs:
2929
pip install -r database/requirements.txt
3030
- name: Build README.md
3131
shell: bash
32-
env:
33-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3432
run: |
3533
python database/build.py

build/build.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
from pathlib import Path
1010
import re
1111
import shutil
12-
import subprocess
1312
import zipfile
1413

15-
import requests
14+
import pygit2
15+
from github import Github
1616
from circup.commands import main as circup_cli
1717

1818
ASSET_DIRS = (
@@ -26,15 +26,12 @@
2626
"metadata.json"
2727
)
2828

29-
def run(cmd):
30-
result = subprocess.run(cmd, shell=True, check=True, capture_output=True)
31-
return result.stdout.decode('utf-8').strip()
32-
33-
def get_latest_repository_release_assets(name:str|dict) -> list:
34-
request_url = "https://api.github.com/repos/{}/releases/latest".format(name)
35-
release_response = requests.get(request_url, allow_redirects=True)
36-
release_data = release_response.json()
37-
return release_data["assets"]
29+
def get_latest_repository_release_assets(name:str) -> list:
30+
gh = Github()
31+
repo = gh.get_repo(name)
32+
release = repo.get_latest_release()
33+
gh.close()
34+
return release.assets
3835

3936
def replace_tags(file:Path, data:dict) -> None:
4037
with open(file, "r") as f:
@@ -47,16 +44,15 @@ def replace_tags(file:Path, data:dict) -> None:
4744
def main():
4845

4946
# get github repository details
50-
git_remote = run("git config --get remote.origin.url")
47+
git_repo = pygit2.Repository(pygit2.discover_repository(os.getcwd()))
48+
49+
git_remote = git_repo.remotes["origin"].url
5150
git_remote = re.sub(r'^git@github\.com:', "https://github.com/", git_remote)
5251
git_remote = re.sub(r'\.git$', "", git_remote)
5352

5453
git_owner, git_name = re.findall(r'^https:\/\/github\.com\/([^\/]+)\/([^\/]+)$', git_remote)[0]
5554

56-
try:
57-
git_commit = run('git rev-parse --short HEAD')
58-
except subprocess.CalledProcessError:
59-
git_commit = "NO_COMMIT"
55+
git_commit = str(git_repo.revparse_single("HEAD").id)
6056

6157
# get the project root directory
6258
build_dir = Path(__file__).parent
@@ -100,7 +96,7 @@ def main():
10096

10197
try:
10298
for asset in get_latest_repository_release_assets("adafruit/Adafruit_CircuitPython_Bundle"):
103-
bundle_version = re.findall(r'^adafruit-circuitpython-bundle-(\d+.x)-mpy-\d{8}.zip$', asset["name"])
99+
bundle_version = re.findall(r'^adafruit-circuitpython-bundle-(\d+.x)-mpy-\d{8}.zip$', asset.name)
104100
if not len(bundle_version):
105101
continue
106102
bundle_version = bundle_version[0]

build/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
circup
2-
requests
2+
pygit2
3+
PyGithub

database/build.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,14 @@
22
# SPDX-FileCopyrightText: Copyright 2025 Cooper Dalrymple (@relic-se)
33
#
44
# SPDX-License-Identifier: MIT
5-
import argparse
65
import json
76
import os
87
from pathlib import Path
98
import re
109

11-
from github import Auth, Github
10+
from github import Github
1211
from mdutils.mdutils import MdUtils
1312

14-
if "GITHUB_TOKEN" in os.environ:
15-
ACCESS_TOKEN = os.environ["GITHUB_TOKEN"]
16-
else:
17-
parser = argparse.ArgumentParser()
18-
parser.add_argument("--token", type=str, required=True)
19-
ACCESS_TOKEN = parser.parse_args().token
20-
21-
2213
DATABASE_FILE = "applications.json"
2314
MARKDOWN_FILE = "README.md"
2415

@@ -36,8 +27,7 @@ def main():
3627

3728
# connect with GitHub API
3829
print("Connecting with GitHub Web API")
39-
auth = Auth.Token(ACCESS_TOKEN)
40-
gh = Github(auth=auth)
30+
gh = Github()
4131

4232
# setup README
4333
print("Beginning markdown file generation")

0 commit comments

Comments
 (0)