Skip to content

Commit 5bc536d

Browse files
authored
Merge pull request #53 from endlessm/push-ypsvmqqkxruv
Move GitHub Pages configuration check to Python code
2 parents ebfbcae + d08129f commit 5bc536d

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

action.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,6 @@ inputs:
1010
runs:
1111
using: composite
1212
steps:
13-
- name: Check that GitHub Pages is correctly configured
14-
env:
15-
GH_TOKEN: ${{ github.token }}
16-
shell: bash
17-
run: |
18-
if ! gh api "repos/${{ github.repository }}/pages" | jq --exit-status '.build_type == "workflow"'
19-
then
20-
echo -n "Check that Pages is enabled, with the source set to GitHub Actions, in the " >> "$GITHUB_STEP_SUMMARY"
21-
echo "[repository settings](https://github.com/${{ github.repository }}/settings/pages)." >> "$GITHUB_STEP_SUMMARY"
22-
exit 1
23-
fi
24-
2513
- uses: astral-sh/setup-uv@v6
2614
with:
2715
cache-dependency-glob: |

godoctopus.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import requests
1616
import requests_cache
1717

18+
API = "https://api.github.com"
19+
1820

1921
class ConfigurationError(Exception):
2022
pass
@@ -112,13 +114,13 @@ def _paginate(
112114
params = None
113115

114116
def get_default_repo_details(self) -> dict[str, Any]:
115-
response = self.session.get(f"https://api.github.com/repos/{self.default_repo}")
117+
response = self.session.get(f"{API}/repos/{self.default_repo}")
116118
response.raise_for_status()
117119
return response.json()
118120

119121
def find_workflow(self) -> dict[str, Any]:
120122
for workflow in self._paginate(
121-
f"https://api.github.com/repos/{self.default_repo}/actions/workflows",
123+
f"{API}/repos/{self.default_repo}/actions/workflows",
122124
item_key="workflows",
123125
):
124126
if workflow["name"] == self.workflow_name:
@@ -133,7 +135,7 @@ def list_branches(self, repo: str) -> list[dict]:
133135
try:
134136
return list(
135137
self._paginate(
136-
f"https://api.github.com/repos/{repo}/branches",
138+
f"{API}/repos/{repo}/branches",
137139
)
138140
)
139141
except requests.HTTPError as error:
@@ -155,7 +157,7 @@ def list_pull_requests(self) -> dict[str, list[dict]]:
155157
branch_prs: dict[str, list[dict]] = {}
156158

157159
for pr in self._paginate(
158-
f"https://api.github.com/repos/{self.default_repo}/pulls",
160+
f"{API}/repos/{self.default_repo}/pulls",
159161
params={"state": "all"},
160162
):
161163
branch_prs.setdefault(pr["head"]["label"], []).append(pr)
@@ -178,7 +180,7 @@ def find_artifact(self, artifacts_url: str) -> dict[str, Any] | None:
178180
def find_latest_artifacts(self, workflow_id: int) -> dict[str, Fork]:
179181
artifacts: dict[str, Fork] = {}
180182
for run in self._paginate(
181-
f"https://api.github.com/repos/{self.default_repo}/actions/workflows/{workflow_id}/runs",
183+
f"{API}/repos/{self.default_repo}/actions/workflows/{workflow_id}/runs",
182184
params={"status": "success"},
183185
item_key="workflow_runs",
184186
):
@@ -237,9 +239,7 @@ def get_latest_built_release(self) -> Release | None:
237239
# https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#get-the-latest-release
238240
# does not allow you to fetch the latest prerelease, and currently all
239241
# releases in Threadbare are prereleases.
240-
for release in self._paginate(
241-
f"https://api.github.com/repos/{self.default_repo}/releases"
242-
):
242+
for release in self._paginate(f"{API}/repos/{self.default_repo}/releases"):
243243
if release["draft"]:
244244
continue
245245

@@ -401,6 +401,29 @@ def run(self) -> None:
401401
logging.info("Site assembled at %s", tmpdir)
402402

403403

404+
def check_pages_configuration(session: requests.Session, repo: str) -> None:
405+
logging.debug("Checking GitHub Pages configuration")
406+
407+
get_response = session.get(f"{API}/repos/{repo}/pages")
408+
match get_response.status_code:
409+
case 404:
410+
logging.debug("GitHub Pages configuration not found")
411+
case 200:
412+
data = get_response.json()
413+
if data["build_type"] == "workflow":
414+
logging.debug(
415+
"GitHub Pages is configured correctly for this repository"
416+
)
417+
return
418+
case _:
419+
get_response.raise_for_status()
420+
421+
raise ConfigurationError(
422+
"GitHub Pages must be enabled, with the source set to GitHub Actions, in the repository settings.",
423+
f"Go to https://github.com/{ repo }/settings/pages to fix this.",
424+
)
425+
426+
404427
def setup_logging() -> None:
405428
log_format = "+ %(asctime)s %(levelname)s %(name)s: %(message)s"
406429
date_format = "%H:%M:%S"
@@ -425,10 +448,13 @@ def main() -> None:
425448
session = make_session(api_token)
426449

427450
try:
451+
check_pages_configuration(session, repo)
452+
428453
amalgamate_pages = AmalgamatePages(session, repo, workflow_name, artifact_name)
429454
amalgamate_pages.run()
430455
except ConfigurationError as e:
431-
print(f"::error::{e.args[0]}")
456+
for message in e.args:
457+
print(f"::error::{message}")
432458
raise
433459

434460
session.cache.delete(older_than=dt.timedelta(days=7))

0 commit comments

Comments
 (0)