Skip to content

Commit

Permalink
Merge pull request #61 from route06/feature
Browse files Browse the repository at this point in the history
ワークフロー get_next_scheduled_date の追加およびcreate_discussionの改善
  • Loading branch information
TomckySan authored Aug 1, 2024
2 parents 7600fba + 7df6cf7 commit ce13d99
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 47 deletions.
69 changes: 69 additions & 0 deletions .github/workflows/calc_next_date.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# @usage
#
# name: Calc next date
#
# on:
# workflow_dispatch:
#
# jobs:
# calc_next_weekly_date:
# uses: route06/actions/.github/workflows/calc_next_date.yml@v2
# with:
# interval: weekly
# target_day: wednesday
#
# log_result:
# runs-on: ubuntu-latest
# needs: [calc_next_weekly_date]
# steps:
# - name: Log the calculated next date
# run: |
# echo "The next weekly date is: ${{ needs.calc_next_weekly_date.outputs.next_date }}"

name: Calc next date

on:
workflow_call:
inputs:
interval:
description: daily, weeklyのいずれかを指定してください。
type: string
required: true
target_day:
description: intervalがweeklyの時は必須です。対象曜日を指定してください。
default: monday
type: string
time_zone:
description: タイムゾーンを指定してください。
default: "Asia/Tokyo"
type: string
outputs:
next_date:
description: yyyy/mm/dd形式で日付を返します。
value: ${{ jobs.calc_next_date.outputs.next_date }}

jobs:
calc_next_date:
runs-on: ubuntu-latest
timeout-minutes: 10
env:
TZ: ${{ inputs.time_zone }}
steps:
- name: Run script
id: calc_date
run: |
case "${{ inputs.interval }}" in
"daily")
next_date=$(date -d "tomorrow" +%Y/%m/%d)
;;
"weekly")
next_date=$(date -d "next ${{ inputs.target_day }}" +%Y/%m/%d)
;;
*)
echo "Invalid INTERVAL value"
exit 1
;;
esac
echo "next_date=$next_date" >> "$GITHUB_OUTPUT"
outputs:
next_date: ${{ steps.calc_date.outputs.next_date }}
Original file line number Diff line number Diff line change
@@ -1,80 +1,75 @@
# todo: 以下はStarter Workflow化する
# @usage
#
# name: Create GitHub discussion
#
# on:
# schedule:
# - cron: "0 0 * * mon"
# # 毎週水曜 13:00 JST に実行
# - cron: "0 4 * * wed"
#
# jobs:
# get_next_meeting_date:
# uses: route06/actions/.github/workflows/calc_next_date.yml@v2
# with:
# interval: weekly
# target_day: wednesday # NOTE: MTG開催曜日に合わせて変更してください
#
# create_discussion:
# uses: route06/actions/.github/workflows/create_discussion.yml@v2
# needs: get_next_meeting_date
# uses: route06/actions/.github/workflows/create_gh_discussion.yml@v2
# with:
# target_day: sunday
# title: ハドル
# discussion_category_slug: meeting-notes
# # NOTE: 作成するDiscussionのタイトルを指定
# title: ${{ needs.get_next_meeting_date.outputs.next_date }} Meeting Title
# # NOTE: category_slugについては補足参照
# category_slug: ideas
# # NOTE: 作成するDiscussionで使用するテンプレートファイルのパスを指定
# description_template_path: _templates/weekly_meeting_discussion/test.md
#
# 補足:
# discussion_category_slug は各カテゴリを選択した時の、URL 末尾の文字列です。以下は例です。
# category_slug は各カテゴリを選択した時の、URL 末尾の文字列です。以下は例です。
# カテゴリ `Q&A` の slug は `q-a`:
# https://github.com/<org>/<repo>/discussions/categories/q-a
# カテゴリ `Show and tell` の slug は `show-and-tell`:
# https://github.com/<org>/<repo>/discussions/categories/show-and-tell

name: Create discussion
name: Create GitHub discussion

on:
workflow_call:
inputs:
target_day:
description: MTG開催曜日に合わせて変更してください。
required: true
type: string
title:
description: 作成するDiscussionのタイトルを設定してください(yyyy/mm/dd が接頭辞として自動付与されます)。
required: true
type: string
discussion_category_slug:
description: 作成するDiscussionのカテゴリslugを設定してください。
default: general
type: string
description_template_path:
description: 作成するDiscussionの説明文に利用するテンプレートファイルのパスを設定してください。
required: true
type: string
inputs:
title:
description: 作成するDiscussionのタイトルを設定してください。
required: true
type: string
category_slug:
description: 作成するDiscussionのカテゴリslugを設定してください。
default: general
type: string
description_template_path:
description: 作成するDiscussionの説明文に利用するテンプレートファイルのパスを設定してください。
required: true
type: string

jobs:
create_discussion:
runs-on: ubuntu-latest
timeout-minutes: 10
env:
TZ: "Asia/Tokyo"
steps:
- name: Checkout
uses: actions/checkout@v4

- name: "Get next meeting date"
id: get_date
run: |
target_date=$(date -d "next ${{ inputs.target_day }}" +%Y/%m/%d)
echo "target_date=$target_date" >> "$GITHUB_OUTPUT"
- name: Run script
env:
TARGET_DATE: ${{ steps.get_date.outputs.target_date }}
REPOSITORY: ${{ github.repository }}
DISCUSSION_CATEGORY_SLUG: ${{ inputs.discussion_category_slug }}
CATEGORY_SLUG: ${{ inputs.category_slug }}
TITLE: ${{ inputs.title }}
DESCRIPTION_TEMPLATE_PATH: ${{ inputs.description_template_path }}
uses: actions/github-script@v7
with:
script: |
const fs = require("fs");
/**
* @param {object} github
* @param {string} owner
* @param {string} repositoryName
* @returns
*/
async function featchRepoIdAndDiscussionCategoryId(github, owner, repositoryName, discussionCategorySlug) {
const result = await github.graphql(`query($owner:String!, $repositoryName:String!, $discussionCategorySlug:String!) {
repository(owner: $owner, name: $repositoryName) {
Expand All @@ -92,7 +87,7 @@ jobs:
return {repositoryId: result.repository.id, categoryId: result.repository.discussionCategory.id};
}
async function createWeeklyReportDiscussion(github, repositoryId, categoryId, title, description) {
async function createDiscussion(github, repositoryId, categoryId, title, description) {
const result = await github.graphql(`mutation($repositoryId:ID!, $categoryId:ID!, $title:String!, $body:String!) {
createDiscussion(input: {repositoryId: $repositoryId, categoryId: $categoryId, title: $title, body: $body}) {
discussion {
Expand All @@ -107,20 +102,35 @@ jobs:
});
}
const { REPOSITORY, DISCUSSION_CATEGORY_SLUG, TARGET_DATE, TITLE, DESCRIPTION_TEMPLATE_PATH } = process.env;
const { REPOSITORY, CATEGORY_SLUG, TITLE, DESCRIPTION_TEMPLATE_PATH } = process.env;
const [owner, repositoryName] = REPOSITORY.split('/');
const repositoryIdAndCategoryId = await featchRepoIdAndDiscussionCategoryId(
github,
owner,
repositoryName,
DISCUSSION_CATEGORY_SLUG,
CATEGORY_SLUG,
)
const discussionTitle = `${TARGET_DATE} ${TITLE}`;
const description = fs.readFileSync(DESCRIPTION_TEMPLATE_PATH, "utf8");
const discussionTitle = TITLE;
const description = (() => {
const DEFAULT_DESCRIPTION = '<!-- Write description here -->';
try {
const desc = fs.readFileSync(DESCRIPTION_TEMPLATE_PATH, "utf8");
return desc.trim() === '' ? DEFAULT_DESCRIPTION : desc;
} catch (error) {
if (error.code === 'ENOENT') {
console.error("Error reading description template file:", error);
return DEFAULT_DESCRIPTION;
}
else {
throw error;
}
}
})();
const discussionId = await createWeeklyReportDiscussion(
const discussionId = await createDiscussion(
github,
repositoryIdAndCategoryId.repositoryId,
repositoryIdAndCategoryId.categoryId,
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ ROUTE06内外から使われることを想定したGitHub ActionsのReusable Wo
<!-- アルファベット順にソートする -->

* [add_assignee_to_pr.yml](./.github/workflows/add_assignee_to_pr.yml)
* [calc_next_date.yml](./.github/workflows/calc_next_date.yml)
* [codeql.yml](./.github/workflows/codeql.yml)
* [create_discussion.yml](./.github/workflows/create_discussion.yml)
* [create_gh_discussion.yml](./.github/workflows/create_gh_discussion.yml)
* [get_last_discussion_url.yml](./.github/workflows/get_last_discussion_url.yml)
* [gh_discussion_comment_to_slack.yml](./.github/workflows/gh_discussion_comment_to_slack.yml)
* [notify_slack_on_ci_failed.yml](./.github/workflows/notify_slack_on_ci_failed.yml)
Expand Down

0 comments on commit ce13d99

Please sign in to comment.