How to automatically synchronize the Issue blog to the github page

I asked chatgpt this question:

Question 1

Generate a github workflows configuration document. When there is a new issue, the content of the issue will be automatically synchronized to the new Markdown file in the content/post folder of the blog branch of the GitHub project. The file name is “{YYYYMMDD}-{Title}.md”, where {YYYYMMDD} is the date of the issue, and “{Title}” is the title of the issue. add at the beginning of the file

---
title:
data:
lastmod:
draft: false
tags:
---

It returned me a GitHub Workflows config document

name: Sync Issue to Blog

on:
  issues:
    types: [opened, edited]

jobs:
  sync-issue-to-blog:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v2

      -name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: "latest"

      - name: Create Blog Post
        id: create_post
        run: |
          issue_title=$(echo "${<!-- -->{ github.event.issue.title }}" | sed 's/[[:space:]]/-/g')
          issue_date=$(echo "${<!-- -->{ github.event.issue.created_at }}" | cut -c1-10 | sed 's/-//g')
          post_path="content/post/${issue_date}-${issue_title}.md"
          post_date=$(echo "${<!-- -->{ github.event.issue.created_at }}" | cut -c1-10)
          post_title=$(echo "${<!-- -->{ github.event.issue.title }}" | sed 's/"/\"/g')
          post_body=$(echo "${<!-- -->{ github.event.issue.body }}" | sed 's/"/\"/g')
          echo "---" > "${post_path}"
          echo "title: "${post_title}"" >> "${post_path}"
          echo "date: "${post_date}"" >> "${post_path}"
          echo "lastmod: "${post_date}"" >> "${post_path}"
          echo "draft: false" >> "${post_path}"
          echo "tags:" >> "${post_path}"
          echo "---" >> "${post_path}"
          echo "${post_body}" >> "${post_path}"

      - name: Commit and Push Changes
        run: |
          git config --global user.name "${<!-- -->{ github.actor }}"
          git config --global user.email "${<!-- -->{ github.actor }}@users.noreply.github.com"
          git add .
          git commit -m "Sync issue #${<!-- -->{ github.event.issue.number }} to blog"
          git push origin blog
        env:
          GH_TOKEN: ${<!-- -->{<!-- --> secrets.GH_TOKEN }}

The Workflows will be run when new issues are created or edited in the project. It first checks out the repository and installs the latest version of Hugo. Next, it will create a new Markdown file, which contains the title and body of the issue, and save it to the content/post directory of the project’s blog branch, and the file name format is {YYYYMMDD}-{Title}.md . Finally, it commits the changes to the blog branch, authenticating with the GitHub API token. Note that in order for Workflows to work correctly, you need to store the GitHub API token in your project’s secrets and name it GH_TOKEN.

Question 2

Later, I improved the answer given by gpt and asked new bing again.

Write the github action configuration file: when the open/edit event of the issue is triggered, use the GitHub CLI client to get the detailed information of the issue.

Define the id of the issue as ISSUE_ID. Traverse all markdown files under content/posts/, read the yaml part of the markdown file header, if there is issue_id=

I

S

S

u

E.

I

D.

, then define the variable

P

o

S

T

P

A

T

h

=

Should

m

a

r

k

d

o

w

no

file path, otherwise define variable

P

o

S

T

P

A

T

h

=

c

o

no

t

e

no

t

/

p

o

the s

t

/

{ISSUE_ID}, then define the variable POST_PATH=the markdown file path, otherwise define the variable POST_PATH=”content/post/

ISSUEI?D, then define the variable POSTP?ATH=the markdown file path, otherwise define the variable POSTP?ATH=”content/post/{issue_date}-${issue_title}.md”.

Set the environment variable BLOG_TITLE to the title of the issue,

Set the environment variable BLOG_DATE to the creation time of the issue,

The format is 2020-09-15T11:30:03+00:00,

Set the environment variable BLOG_DESCRIPTION to blank,

Set the environment variable BLOG_TAGS to the tag list of issue, the format is [“tag1”, “tag2”],

Set the environment variable BLOG_CONTENT to the content of the issue, based on markdown format.

Finally, use the envsubst command to update the content of template/template.md with environment variables, and save the updated file in the ${POST_PATH} path.

# This is a basic workflow to help you get started with Actions

name: Blog Post Generator

# Controls when the action will run.
on:
  # Triggers the workflow on issue open or edit events
  issues:
    types: [opened, edited]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This job generates a blog post from an issue
  generate-post:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2
      
      # Installs GitHub CLI on the runner (you may need to adjust this according to your OS)
      - name: Install GitHub CLI
        run: |
          sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-key C99B11DEB97541F0
          sudo apt-add-repository https://cli.github.com/packages
          sudo apt update
          sudo apt install gh
      
      # Authenticates to GitHub using a personal access token (you need to store it as a secret in your repository settings)
      - name: Authenticate to GitHub
        env:
          GITHUB_TOKEN: ${<!-- -->{<!-- --> secrets.GITHUB_TOKEN }}
        run: |
          gh auth login --with-token < $GITHUB_TOKEN
      
      # Runs a bash script to set environment variables and update template file using GitHub CLI and envsubst commands
      -name: Run bash script
        env:
          ISSUE_ID: ${<!-- -->{<!-- --> github.event.issue.number }}
        run: |
          # Get the issue data from GitHub CLI using gh api command (you may need to specify owner and repo flags)
          issue_data=$(gh api issues/${ISSUE_ID})
          # Set BLOG_TITLE to the issue's title
          BLOG_TITLE=$(echo "$issue_data" | jq -r '.title')
          # Set BLOG_DATE to the issue's creation time, formatted as 2020-09-15T11:30:03 + 00:00
          BLOG_DATE=$(echo "$issue_data" | jq -r '.created_at')
          # Set BLOG_DESCRIPTION to blank
          BLOG_DESCRIPTION=""
          # Set BLOG_TAGS to the issue's label list, formatted as ["tag1","tag2"]
          BLOG_TAGS=$(echo "$issue_data" | jq -r '[.labels[].name]')
          # Set BLOG_CONTENT to the issue's content, based on markdown format
          BLOG_CONTENT=$(echo "$issue_data" | jq -r '.body')
          # Export these variables so they can be used by envsubst command later
          export BLOG_TITLE BLOG_DATE BLOG_DESCRIPTION BLOG_TAGS BLOG_CONTENT
          # Loop through all markdown files under content/posts/
          for file in content/posts/*.md; do
            # Read the yaml part at the top of each file using yq command (you may need to install yq first)
            yaml_part=$(yq e '.' $file)
            # Check if there is an issue_id field that matches ISSUE_ID
            if [[ $(echo "$yaml_part" | yq e '.issue_id' -) == "$ISSUE_ID" ]]; then
              # If yes, set POST_PATH to that file path and break out of loop
              POST_PATH=$file
              break
            else
              # If no, set POST_PATH using issue date and title (you may need to adjust this according to your desired format)
              POST_PATH="content/post/${BLOG_DATE}-${BLOG_TITLE}.md"
            the fi
          done
          # Use these variables to update template/template.md's content using envsubst command (you may need to adjust this according to your template format)
          envsubst < template/template.md > ${POST_PATH}
      # Commits and pushes the updated file to your repository (you may need a personal access token for this step)
      - name: Commit and push changes
      run: |
      git config --global user.name 'GitHub Action'
      git config --global user.email '[email protected]'

git add ${<!-- -->POST_PATH}
git commit -m "Update blog post from issue $ISSUE_ID"
git push origin main

Nudge

The github cli client is installed in the container of github workflow by default, so no additional installation is required, just pass in the environment variable ${GITHUB_TOKEN} when calling.

So removed the steps Install GitHub CLI and Authenticate to GitHub.

Trigger site update action

Add workflow_run configuration under on in gh-page.yml.

on:
 ?…
  workflow_run:
    workflows:
      - "issue-to-post"
    types:
      - completed

Test success!
image

Reference: https://docs.github.com/en/actions/using-workflows/using-github-cli-in-workflows

Welcome to participate in this blog discussion at ISSUE: m2kar/m2kar.github.io#14