Skip to content

Commit

Permalink
Make template configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
andooown committed Aug 24, 2020
1 parent 3dac973 commit 179d0c1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
6 changes: 4 additions & 2 deletions auto-release-pr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
| パラメータ | Required | Default | |
|-|-|-|-|
| `githubToken` || | GitHub Token。PRの探索や更新に利用されます。 |
| `baseBranch` | | `release` | リリースPRのBase Branch。PRの探索や作成時に利用されます。 |
| `headBranch` | | `master` | リリースPRのHead Branch。PRの探索や作成時に利用されます。 |
| `baseBranch` | | `release` | リリースPRのBase Branch。PRの探索や作成時に利用されます。 |
| `headBranch` | | `master` | リリースPRのHead Branch。PRの探索や作成時に利用されます。 |
| `releasePRNumber` | | | リリースPRの番号。 `releasePRNumber` が指定されると `baseBranch`/`headBranch` は無視されます。 |
| `bodyTemplate` | | [`action.yml`](https://github.com/ratel-pay/github-actions/blob/master/auto-release-pr/action.yml) を参照 | PR本文の生成テンプレート。テンプレート内の `{summary}` が差分の箇条書きに置き換えられます。 |
| `commentTemplate` | | [`action.yml`](https://github.com/ratel-pay/github-actions/blob/master/auto-release-pr/action.yml) を参照 | 本文の更新差分のコメントのテンプレート。テンプレート内の `{diff}` が差分表示に置き換えられます。 |

## Usage

Expand Down
25 changes: 25 additions & 0 deletions auto-release-pr/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,31 @@ inputs:
releasePRNumber:
description: The number of release Pull Request.
required: false
bodyTemplate:
description: |
The template of generating release Pull Request.
`{summary}` will be replaced by generating body.
required: true
default: |
## Changes
{summary}
commentTemplate:
description: |
The template of generating comment.
`{diff}` will be replaced by diff.
required: true
default: |
PR body is updated!
<details><summary>diff</summary>
<p>
```diff
{diff}
```
</p>
</detail>
runs:
using: "docker"
image: "Dockerfile"
20 changes: 5 additions & 15 deletions auto-release-pr/release-pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,8 @@
HEAD_BRANCH: str = os.environ['INPUT_HEADBRANCH']
RELEASE_PR_NUMBER: Optional[str] = os.environ.get('INPUT_RELEASEPRNUMBER')


COMMENT_TEMPLATE = '''PR body is updated!
<details><summary>diff</summary>
<p>
```diff
{diff}
```
</p>
</detail>
'''
BODY_TEMPLATE: str = os.environ['INPUT_BODYTEMPLATE']
COMMENT_TEMPLATE: str = os.environ['INPUT_COMMENTTEMPLATE']


# release 向きの最新 PR を取得
Expand Down Expand Up @@ -52,7 +42,7 @@ def find_or_create_release_pr(repo: github.Repository.Repository, base: str, hea
draft=True)

# PR のコミットメッセージから含まれる PR を探して新しいの PR の body を作る
def make_new_body(pr: github.PullRequest.PullRequest) -> Optional[str]:
def make_new_body(pr: github.PullRequest.PullRequest, template: str) -> Optional[str]:
commit_messages = [cm.commit.message for cm in pr.get_commits()]
merge_commit_messages = [m for m in commit_messages if m.startswith("Merge pull request")]

Expand All @@ -67,7 +57,7 @@ def convert_to_body_line(message: str) -> str:

body_lines = '\n'.join(map(convert_to_body_line, merge_commit_messages))
if len(body_lines) > 0:
return '## Changes\n\n' + body_lines
return template.format(summary=body_lines)
else:
return None

Expand All @@ -77,7 +67,7 @@ def main():
release_pr = find_or_create_release_pr(repo, base=BASE_BRANCH, head=HEAD_BRANCH, number=RELEASE_PR_NUMBER)

# body を生成
new_body = make_new_body(release_pr)
new_body = make_new_body(release_pr, template=BODY_TEMPLATE)
if not new_body:
print("Failed to generate new PR body.")
sys.exit(1)
Expand Down

0 comments on commit 179d0c1

Please sign in to comment.