Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

```
````

Expand Down Expand Up @@ -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:
Expand All @@ -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) |
8 changes: 6 additions & 2 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ 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
default: ""
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
Expand Down
42 changes: 28 additions & 14 deletions markdown_embed_code/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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


Expand All @@ -26,6 +29,7 @@ class PartialGitHubEventInputs(BaseModel):


class PartialGitHubEvent(BaseModel):
ref: Optional[str] = None
number: Optional[int] = None
inputs: Optional[PartialGitHubEventInputs] = None

Expand All @@ -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()

Expand All @@ -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)