Skip to content

Commit f675f8b

Browse files
authored
Merge pull request #363 from online-judge-tools/fix/362
Fix the bug that GitHub Pages was not updated when `main` branch is the default
2 parents 04a7c7c + 0253f5f commit f675f8b

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

onlinejudge_verify/main.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
# pylint: enable=unused-import,ungrouped-imports
1212

1313
import argparse
14+
import json
1415
import glob
1516
import math
1617
import os
1718
import pathlib
19+
import urllib.request
1820
import subprocess
1921
import sys
2022
import textwrap
@@ -157,12 +159,33 @@ def push_documents_to_gh_pages(*, src_dir: pathlib.Path, dst_branch: str = 'gh-p
157159

158160
def subcommand_docs(*, jobs: int = 1) -> None:
159161
if 'GITHUB_ACTION' in os.environ and 'GITHUB_TOKEN' in os.environ:
160-
if os.environ['GITHUB_REF'] == 'refs/heads/master':
161-
logger.info('generate documents...')
162-
onlinejudge_verify.documentation.main.main(jobs=jobs)
162+
# check it is kicked by "push" event
163+
if os.environ['GITHUB_EVENT_NAME'] != 'push':
164+
logger.info('This execution is not kicked from "push" event. Updating GitHub Pages is skipped.')
165+
return
166+
167+
# check it is on the default branch.
168+
try:
169+
# /repos/{owner}/{repo} endpoint. See https://docs.github.com/en/free-pro-team@latest/rest/reference/repos#get-a-repository
170+
req = urllib.request.Request(os.environ['GITHUB_API_URL'] + '/repos/' + os.environ['GITHUB_REPOSITORY'])
171+
req.add_header('authorization', 'Bearer ' + os.environ['GITHUB_TOKEN'])
172+
with urllib.request.urlopen(req) as fh:
173+
repos = json.loads(fh.read())
174+
default_branch = repos['default_branch']
175+
except Exception as e:
176+
logger.exception('failed to get the default branch: %s', e)
177+
logger.info('Updating GitHub Pages is skipped.')
178+
return
179+
if os.environ['GITHUB_REF'] == 'refs/heads/{}'.format(default_branch):
180+
logger.info('This execution is not on the default branch (the default is "refs/heads/%s" but the actual is "%s"). Updating GitHub Pages is skipped.', default_branch, os.environ['GITHUB_REF'])
181+
return
182+
183+
# updating the GitHub Pages
184+
logger.info('generate documents...')
185+
onlinejudge_verify.documentation.main.main(jobs=jobs)
163186

164-
logger.info('upload documents...')
165-
push_documents_to_gh_pages(src_dir=pathlib.Path('.verify-helper/markdown'))
187+
logger.info('upload documents...')
188+
push_documents_to_gh_pages(src_dir=pathlib.Path('.verify-helper/markdown'))
166189

167190
else:
168191
logger.info('generate documents...')

0 commit comments

Comments
 (0)