Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

スクリプト内のif文を外に出す #1817

Draft
wants to merge 2 commits into
base: use_ts_13
Choose a base branch
from
Draft
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
28 changes: 16 additions & 12 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,26 @@ runs:
- name: Set env
id: set-env
shell: bash
run: |
echo "HEAD_REF=${{github.event.pull_request.head.ref || github.ref_name}}" >>"${GITHUB_ENV}"

delimiter="$(openssl rand -hex 8)"
echo "PR_DESCRIPTION_PREFIX<<${delimiter}" >> "$GITHUB_ENV"
echo "${{inputs.pr-description-prefix}}" >> "$GITHUB_ENV"
echo "${delimiter}" >> "$GITHUB_ENV"

echo "PR_NUMBER=${{github.event.pull_request.number}}" >> "$GITHUB_ENV"
echo "PR_TITLE_PREFIX=${{inputs.pr-title-prefix}}" >> "$GITHUB_ENV"
echo "BRANCH_NAME_PREFIX=${{inputs.branch-name-prefix}}" >> "$GITHUB_ENV"
env:
HEAD_REF: ${{github.event.pull_request.head.ref || github.ref_name}}
PR_DESCRIPTION_PREFIX: ${{inputs.pr-description-prefix}}
PR_NUMBER: ${{github.event.pull_request.number}}
PR_TITLE_PREFIX: ${{inputs.pr-title-prefix}}
BRANCH_NAME_PREFIX: ${{inputs.branch-name-prefix}}
run: ${{ github.action_path }}/src/set_env.sh
- name: Set env (HEAD_NAME)
if: env.HEAD_REF != ''
shell: bash
run: echo "HEAD_NAME=${HEAD_NAME}-${HEAD_REF}" >> "$GITHUB_ENV"
- name: Set Git command
if: steps.diff.outputs.result != '' && ((github.event_name == 'pull_request' && github.event.action != 'closed') || github.event_name == 'push' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' || github.event_name == 'repository_dispatch') && inputs.no-verify == 'true'
working-directory: ${{inputs.working-directory}}
run: ${{ github.action_path }}/src/set_git_command.sh
shell: bash
# 差分があったときは、コミットを作りpushする
- name: Push
env:
TOKEN: ${{inputs.github-token}}
NO_VERIFY: ${{inputs.no-verify}}
if: steps.diff.outputs.result != '' && ((github.event_name == 'pull_request' && github.event.action != 'closed') || github.event_name == 'push' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' || github.event_name == 'repository_dispatch')
working-directory: ${{inputs.working-directory}}
run: ${{ github.action_path }}/src/push.sh
Expand Down
8 changes: 2 additions & 6 deletions dist/close_pull_request.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions dist/create_pull_request.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions dist/generate_title_description.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions dist/get_pull_requests.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions src/close_pull_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@ export async function script(
github: InstanceType<typeof GitHub>,
context: Context,
) {
const HEAD_REF = process.env.HEAD_REF;
let headName = process.env.BRANCH_NAME_PREFIX;

if (HEAD_REF !== "") {
headName += "-" + HEAD_REF;
}

for (const pull of await getPullRequests(github, context)) {
const HEAD_NAME = process.env.HEAD_NAME;

// 修正PRをcloseする (修正PRのstateをclosedに更新する)
const pullsUpdateParams: RestEndpointMethodTypes["pulls"]["update"]["parameters"] =
{
Expand All @@ -31,7 +26,7 @@ export async function script(
{
owner: context.repo.owner,
repo: context.repo.repo,
ref: "heads/" + headName,
ref: "heads/" + HEAD_NAME,
};
console.log("call git.deleteRef:", gitDeleteRefParams);
await github.rest.git.deleteRef(gitDeleteRefParams);
Expand Down
9 changes: 2 additions & 7 deletions src/create_pull_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@ export async function script(
context: Context,
): Promise<number> {
const HEAD_REF = process.env.HEAD_REF || "";
let head = process.env.BRANCH_NAME_PREFIX;

if (HEAD_REF !== "") {
head += "-" + HEAD_REF;
}

const headWithRepo = context.repo.owner + ":" + head;
const HEAD_NAME = process.env.HEAD_NAME;
const headWithRepo = context.repo.owner + ":" + HEAD_NAME;
const { title, body } = generateTitleDescription();
const pullsCreateParams: RestEndpointMethodTypes["pulls"]["create"]["parameters"] =
{
Expand Down
9 changes: 2 additions & 7 deletions src/generate_title_description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ export function generateTitleDescription(): {
const escapedHeadRef = HEAD_REF.replace(/#/g, "");
const PR_NUMBER = process.env.PR_NUMBER;
const PR_TITLE_PREFIX = process.env.PR_TITLE_PREFIX || "";
let head = process.env.BRANCH_NAME_PREFIX || "";

if (HEAD_REF !== "") {
head += "-" + HEAD_REF;
}

const escapedHead = head.replace(/#/g, "");
const HEAD_NAME = process.env.HEAD_NAME || "";
const escapedHead = HEAD_NAME.replace(/#/g, "");
let title = PR_TITLE_PREFIX;
let body = process.env.PR_DESCRIPTION_PREFIX || "";

Expand Down
9 changes: 2 additions & 7 deletions src/get_pull_requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,8 @@ export async function getPullRequests(
): Promise<
PaginatingEndpoints["GET /repos/{owner}/{repo}/pulls"]["response"]["data"]
> {
const HEAD_REF = process.env.HEAD_REF;
let head = context.repo.owner + ":" + process.env.BRANCH_NAME_PREFIX;

if (HEAD_REF !== "") {
head += "-" + HEAD_REF;
}

const HEAD_NAME = process.env.HEAD_NAME;
const head = context.repo.owner + ":" + HEAD_NAME;
const pullsListParams: RestEndpointMethodTypes["pulls"]["list"]["parameters"] =
{
owner: context.repo.owner,
Expand Down
12 changes: 0 additions & 12 deletions src/push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,11 @@
git config user.name "github-actions[bot]"
EMAIL="41898282+github-actions[bot]@users.noreply.github.com"
git config user.email "${EMAIL}"
GIT_COMMIT_COMMAND="git commit"

if [ "$NO_VERIFY" = "true" ]; then
GIT_COMMIT_COMMAND="$GIT_COMMIT_COMMAND --no-verify"
fi

GIT_COMMIT_COMMAND="$GIT_COMMIT_COMMAND -m \"${PR_TITLE_PREFIX}\""
eval "$GIT_COMMIT_COMMAND"
REPO_URL="https://"
REPO_URL+="${GITHUB_ACTOR}:${TOKEN}@github.com/"
REPO_URL+="${GITHUB_REPOSITORY}.git"
GITHUB_HEAD="HEAD:refs/heads/${BRANCH_NAME_PREFIX}-${HEAD_REF}"
GIT_PUSH_COMMAND="git push"

if [ "$NO_VERIFY" = "true" ]; then
GIT_PUSH_COMMAND="$GIT_PUSH_COMMAND --no-verify"
fi

GIT_PUSH_COMMAND="$GIT_PUSH_COMMAND -f \"${REPO_URL}\" \"${GITHUB_HEAD}\""
eval "$GIT_PUSH_COMMAND"
17 changes: 17 additions & 0 deletions src/set_env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

delimiter="$(openssl rand -hex 8)"
{
echo "HEAD_REF=$HEAD_REF"

echo "PR_DESCRIPTION_PREFIX<<${delimiter}"
echo "${PR_DESCRIPTION_PREFIX}"
echo "${delimiter}"

echo "PR_NUMBER=$PR_NUMBER"
echo "PR_TITLE_PREFIX=$PR_TITLE_PREFIX"
echo "BRANCH_NAME_PREFIX=$BRANCH_NAME_PREFIX"
echo "HEAD_NAME=$BRANCH_NAME_PREFIX"
echo "GIT_COMMIT_COMMAND=git commit"
echo "GIT_PUSH_COMMAND=git push"
} >>"$GITHUB_ENV"
6 changes: 6 additions & 0 deletions src/set_git_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

{
echo "GIT_COMMIT_COMMAND=$GIT_COMMIT_COMMAND --no-verify"
echo "GIT_PUSH_COMMAND=$GIT_PUSH_COMMAND --no-verify"
} >>"$GITHUB_ENV"