Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
fix

a

a

a
  • Loading branch information
Yang-33 committed Nov 22, 2024
1 parent d2b9c13 commit 2626e72
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 6 deletions.
34 changes: 28 additions & 6 deletions .github/workflows/update-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ jobs:
id: setup_node_id
with:
node-version: 18
- name: actions/setup-java@v3
uses: actions/setup-java@v4
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: 17
Expand All @@ -36,20 +35,43 @@ jobs:
- run: |
python3 generate-code.py
- run: |
echo "a" > a.txt
diff=$(git --no-pager diff --name-only)
echo "DIFF_IS_EMPTY=$([[ -z "$diff" ]] && echo 'true' || echo 'false')" >> $GITHUB_ENV
echo "DIFF_IS_EMPTY='false'" >> $GITHUB_ENV
echo "CURRENT_DATETIME=$(date +'%Y%m%d%H%M%S')" >> $GITHUB_ENV
- if: ${{ env.DIFF_IS_EMPTY != 'true' }}
run: |
# Determine Change Type via Submodule Script
CHANGE_TYPE=$(npx zx ./scripts/determin-change-type.mjs)
echo "Determined change type: $CHANGE_TYPE"
# Determine PR title and body
if [ "$CHANGE_TYPE" == "submodule-update" ]; then
# Fetch PR info from submodule
npx zx ./scripts/fetch-pr-info.mjs
PR_INFO=$(cat pr_info.json)
TITLE=$(echo "$PR_INFO" | jq -r '.title')
BODY=$(echo "$PR_INFO" | jq -r '.url')$'\n\n'$(echo "$PR_INFO" | jq -r '.body')
else
# Default PR title and body
TITLE="Code Update"
BODY="Reviewer: Please edit this description to include relevant information about the changes."
fi
BRANCH_NAME="update-diff-${{ env.CURRENT_DATETIME }}"
git config user.name github-actions
git config user.email github-actions@github.com
git checkout -b update-diff-${{ env.CURRENT_DATETIME }}
git checkout -b $BRANCH_NAME
#
git add a.txt
#
git add line-openapi
git add lib/**
git commit -m "Codes are generated by openapi"
git push origin update-diff-${{ env.CURRENT_DATETIME }}
gh pr create -B ${{ github.ref_name }} -t "Codes are generated by openapi" -b "" --label "line-openapi-update"
git push origin $BRANCH_NAME
gh pr create -B ${{ github.ref_name }} -H $BRANCH_NAME -t "$TITLE" -b "$BODY" # --label "line-openapi-update"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31 changes: 31 additions & 0 deletions scripts/determin-change-type.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env zx


// Call this script from the parent repository
// This script determines the type of change to the parent repository
// by checking the latest commit message and the status of the submodule.
// The possible change types are:
// - submodule-update: The submodule has been updated.
// - other: The parent repository has been updated, but the submodule has not been updated.

const submoduleDir = "line-openapi";

async function determineChangeType() {
// Get the latest commit message
const { stdout: lastCommit } = await $`git log -1 --pretty=%s`;

// Get the status of the submodule
const { stdout: submoduleStatus } = await $`git submodule status ${submoduleDir}`;
const hasUpdate = submoduleStatus.trim().startsWith('+');

// Determine the type of change
if (lastCommit.includes(submoduleDir)) {
console.log("submodule-update");
} else if (hasUpdate) {
console.log("submodule-update");
} else {
console.log("other");
}
}

await determineChangeType();
49 changes: 49 additions & 0 deletions scripts/get-pr-info.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env zx

import fs from "fs/promises";

const submoduleDir = "line-openapi";
const repo = "line/line-openapi";

// じっさいに、submoduleをつかったレポジトリで試す

async function generatePrInfo() {
// Submodule ディレクトリに移動
cd(submoduleDir);

// Submodule の最新コミットメッセージを取得
const { stdout: lastCommit } = await $`git log -1 --pretty=%s`;
console.log("Last commit in submodule:", lastCommit.trim());

// 最新コミットメッセージから PR 番号 (#数字) を抽出
const prNumberMatch = lastCommit.match(/#(\d+)/);
if (!prNumberMatch) {
console.error("No PR number found in submodule's latest commit. Exiting.");
process.exit(1);
}
const prNumber = prNumberMatch[1];
console.log("Detected PR number:", prNumber);

// PR URL を構築
const prUrl = `https://github.com/${repo}/pull/${prNumber}`;
console.log("Constructed PR URL:", prUrl);

// PR 情報を取得
const prInfo = JSON.parse(await $`gh pr view ${prNumber} --repo ${repo} --json title,body`);
const prTitle = prInfo.title;
const prBody = prInfo.body;

// 出力用 JSON を生成
const output = {
url: prUrl,
title: prTitle,
body: prBody,
};
await fs.writeFile("../pr_info.json", JSON.stringify(output, null, 2));
console.log("PR information saved to pr_info.json");

// 作業ディレクトリを元に戻す
cd("..");
}

await generatePrInfo();

0 comments on commit 2626e72

Please sign in to comment.