Closed6

GitHub Actionsでissue作成時に付与されているlabelを全て取得する

HidakaRintaroHidakaRintaro

やりたいこと

issueが作成されたら、titleとURL、特定のlabelをnotionに飛ばしたい

問題点

labelを取得できない or labelの処理が間違えている

HidakaRintaroHidakaRintaro
actions.yml
name: 'Create Notion Page in Database'
description: 'issueが作成されたらNotionのデータベースに新しい行を追加する'
inputs:
  title:
    description: 'Title'
    required: true
  labels:
    description: 'Labels'
    required: true
  issue-number:
    description: 'Issue number'
    required: true
runs:
  using: 'node16'
  main: 'dist/index.js'
workflow/on-issue.yml
name: Create GitHub Issue and Notion Database Page
on:
  issues:
    types:
      - opened
jobs:
  create-notion-page:
    runs-on: ubuntu-latest
    name: Create Notion Page
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Create Notion Page
        uses: ./
        env:
          NOTION_TOKEN: ${{ secrets.NOTION_TOKEN }}
          NOTION_DATABASE_ID: ${{ secrets.NOTION_DATABASE_ID }}
        with:
          title: ${{ github.event.issue.title }}
          labels: ${{ github.event.issue.label }}
          issue-number: ${{ github.event.issue.number }}
index.js
import { getInput, setFailed } from '@actions/core'

// 環境変数受け取ったりの処理

const LABELS = 'labels'

const fullLabels = Array.from(getInput(LABELS))
const labels = fullLabels.map((fullLabel) => fullLabel.name)
const checkLabels = [
  'backend',
  'frontend',
]

const resultLabel = labels.find((l) => checkLabels.find((cl) => cl == l))

// notionにpostする処理

HidakaRintaroHidakaRintaro

色々試したエラー集

長くなるのでトグル内に

labelsにしてみた

workflow/on-issue.yml
# 省略
        with:
          title: ${{ github.event.issue.title }}
-          labels: ${{ github.event.issue.label }}
+          labels: ${{ github.event.issue.labels }}
          issue-number: ${{ github.event.issue.number }}
Error: .github/workflows/on-issue.yml (Line: 20, Col: 19): A sequence was not expected
Error: The template is not valid. .github/workflows/on-issue.yml (Line: 20, Col: 19): A sequence was not expected

いっそgithub.event.issueを全て受け取ってjs側で処理しようとした結果

workflow/on-issue.yml
# 省略
        with:
          title: ${{ github.event.issue.title }}
-          labels: ${{ github.event.issue.label }}
+          labels: ${{ github.event.issue }}
          issue-number: ${{ github.event.issue.number }}
Error: .github/workflows/on-issue.yml (Line: 20, Col: 19): A mapping was not expected
Error: The template is not valid. .github/workflows/issue-created-notion-to-push.yml (Line: 20, Col: 19): A mapping was not expected
jsの変更(どうせここまで到達してないだろうからトグルに)
index.js
// 省略
const LABELS = 'labels'

- const fullLabels = Array.from(getInput(LABELS))
+ const fullLabels = Array.from(getInput(LABELS)).labels
const labels = fullLabels.map((fullLabel) => fullLabel.name)
const checkLabels = [
  'backend',
  'frontend',
]
// 省略

*使ってみた

workflow/on-issue.yml
# 省略
        with:
          title: ${{ github.event.issue.title }}
-          labels: ${{ github.event.issue.label }}
+          labels: ${{ github.event.issue.labels.* }}
          issue-number: ${{ github.event.issue.number }}
Error: .github/workflows/on-issue.yml (Line: 20, Col: 19): A sequence was not expected
Error: The template is not valid. .github/workflows/on-issue.yml (Line: 20, Col: 19): A sequence was not expected
HidakaRintaroHidakaRintaro

github.event.issue.labelだとjsでエラーが出る。
ただ、これwithのところにlabesが無いのはなんで?

Run ./
  with:
    title: github actions test26
    issue-number: 38
  env:
    NOTION_TOKEN: ***
    NOTION_DATABASE_ID: ***
@notionhq/client info: request start { method: 'post', path: 'pages' }
@notionhq/client warn: request fail {
  code: 'validation_error',
  message: 'body failed validation. Fix one:\n' +
    'body.properties.Type.select.id should be defined, instead was `undefined`.\n' +
    'body.properties.Type.select.name should be defined, instead was `undefined`.'
}
@notionhq/client debug: failed response body {
  body: '{"object":"error","status":400,"code":"validation_error","message":"body failed validation. Fix one:\\nbody.properties.Type.select.id should be defined, instead was `undefined`.\\nbody.properties.Type.select.name should be defined, instead was `undefined`."}'
}
Error: body failed validation. Fix one:
body.properties.Type.select.id should be defined, instead was `undefined`.
body.properties.Type.select.name should be defined, instead was `undefined`.
HidakaRintaroHidakaRintaro

解決した

toJson()を使えばどうやら解決した
なんでかは知らない。
JS側はArray.fromJSON.parseにしないといけない。

workflow/on-issue.yml
# 省略
        with:
          title: ${{ github.event.issue.title }}
-          labels: ${{ github.event.issue.label }}
+          labels: ${{ toJson(github.event.issue.labels) }}
          issue-number: ${{ github.event.issue.number }}
index.js
// 省略
const LABELS = 'labels'

- const fullLabels = Array.from(getInput(LABELS))
+ const fullLabels = JSON.parse(getInput(LABELS))
const labels = fullLabels.map((fullLabel) => fullLabel.name)
const checkLabels = [
  'backend',
  'frontend',
]
// 省略
このスクラップは2022/06/04にクローズされました