diff --git a/Dockerfile b/Dockerfile index 6f5d5ef..d941c16 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM python:3.7-alpine -RUN apk update && apk --no-cache add git +RUN apk update && apk --no-cache add git gcc libc-dev libffi-dev COPY ./requirements.txt /app/requirements.txt diff --git a/README.md b/README.md index 5597b8b..cee53e7 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ In markdown, write code block as follows: And, you can refer specific lines as ```python:tests/src/sample.py [4-5] - + ``` ```` @@ -60,7 +60,7 @@ jobs: with: persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token fetch-depth: 0 # otherwise, you will failed to push refs to dest repo - ref: refs/heads/${{ github.head_ref }} + ref: ${{ github.head_ref }} - uses: tokusumi/markdown-embed-code@main with: @@ -80,3 +80,4 @@ jobs: | no_change (Optional) | Issue comment at no changed (default: "No changes on README!" ) | | output (Optional) | Output markdown file path. If none, override target file. (default: "") | | silent (Optional) | No issue comment in silent mode (default: false) | +| server (Optional) | GitHub server URL (default: https://github.com) | diff --git a/action.yaml b/action.yaml index 0b5d1bf..249b55f 100644 --- a/action.yaml +++ b/action.yaml @@ -16,7 +16,7 @@ inputs: no_change: description: Issue comment at no changed required: false - default: "No changes on README!" + default: "No changes on README!" output: description: Output markdown file path. If none, override target file. required: false @@ -24,7 +24,11 @@ inputs: silent: description: "No issue comment in silent mode" required: false - default: false + default: "false" + server: + description: Github server URL + required: false + default: ${{ github.server_url || 'https://github.com' }} runs: using: docker image: Dockerfile diff --git a/markdown_embed_code/__main__.py b/markdown_embed_code/__main__.py index 25a3819..cb682c8 100644 --- a/markdown_embed_code/__main__.py +++ b/markdown_embed_code/__main__.py @@ -5,6 +5,7 @@ from github import Github from pydantic import BaseModel, BaseSettings, SecretStr +from urllib.parse import urlparse from markdown_embed_code import get_code_emb @@ -16,8 +17,10 @@ class Settings(BaseSettings): input_output: Path = Path("") input_silent: bool = False input_token: SecretStr + input_server: str github_actor: str github_repository: str + github_event_name: str github_event_path: Path @@ -26,6 +29,7 @@ class PartialGitHubEventInputs(BaseModel): class PartialGitHubEvent(BaseModel): + ref: Optional[str] = None number: Optional[int] = None inputs: Optional[PartialGitHubEventInputs] = None @@ -36,29 +40,39 @@ class PartialGitHubEvent(BaseModel): ["git", "config", "--local", "user.email", "github-actions@github.com"], check=True ) - -g = Github(settings.input_token.get_secret_value()) +g = Github(base_url=f"{settings.input_server}/api/v3", login_or_token=settings.input_token.get_secret_value()) repo = g.get_repo(settings.github_repository) if not settings.github_event_path.is_file(): sys.exit(1) contents = settings.github_event_path.read_text() event = PartialGitHubEvent.parse_raw(contents) -if event.number is not None: - number = event.number -elif event.inputs and event.inputs.number: - number = event.inputs.number -else: - sys.exit(1) -pr = repo.get_pull(number) -if pr.merged: - # ignore at merged + +ref = None + +if settings.github_event_name == 'pull_request': + if event.number is not None: + number = event.number + elif event.inputs and event.inputs.number: + number = event.inputs.number + else: + sys.exit(1) + + pr = repo.get_pull(number) + if pr.merged: + # ignore at merged + sys.exit(0) + ref = pr.head.ref +elif settings.github_event_name == 'push': + ref = event.ref + +if not ref: + print('unknown ref', ref) sys.exit(0) if not settings.input_output.is_dir(): output_path = settings.input_output else: output_path = settings.input_markdown - with open(settings.input_markdown, "r") as f: doc = f.read() @@ -81,7 +95,7 @@ class PartialGitHubEvent(BaseModel): subprocess.run(["git", "add", output_path], check=True) subprocess.run(["git", "commit", "-m", settings.input_message], check=True) -remote_repo = f"https://{settings.github_actor}:{settings.input_token.get_secret_value()}@github.com/{settings.github_repository}.git" -proc = subprocess.run(["git", "push", remote_repo, f"HEAD:{pr.head.ref}"], check=False) +remote_repo = f"https://{settings.github_actor}:{settings.input_token.get_secret_value()}@{urlparse(settings.input_server).hostname}/{settings.github_repository}.git" +proc = subprocess.run(["git", "push", remote_repo, f"HEAD:{ref}"], check=False) if proc.returncode != 0: sys.exit(1)