GitHub ProjectのCardのDraftからIssueへの変換時にGitHub Actionsを実行したい

2022.09.17

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、CX事業本部 IoT事業部の若槻です。

GitHub Projectでボード(またはテーブル)に新規アイテム(Card)を追加すると、そのアイテムはDraft(Draft issueとも呼ぶ)として作成されます。

Draftは「思いつきのアイデアなのでIssueにするほどではないけど、ボードには追加しておきたい」というジャストアイデアな内容を残しておくのに便利です。そしてDraftはいつでもIssueに変換することができます。

今回は、GitHub ProjectのCardのDraftからIssueへの変換時にGitHub Actions Workflowを実行したいケースがあったので、方法を確認してみました。

試してみた

該当のイベントを探す(project_card)

まずWorkflowの実行トリガーとなるイベントを探してみます。公式ドキュメントの下記ページにアクセスします。

ここではイベント一覧が確認できます。この中だとproject_cardイベントがそれっぽいですね。クリックします。

project_cardの詳細に飛びます。Activity typeはconvertedを使えば良さそうですね。

project_cardイベントを試してみる(できなかった)

Repositoryに紐付けたProjectで試してみます。

同じRepositoryに次のWorkflowをファイルで作成します。

.github/workflows/script.yml

name: on card converted to issue
on:
  project_card:
    types: 
      - converted

jobs:
  some_task:
    runs-on: ubuntu-latest
    steps:
      - run: echo '${{ toJSON(github.event) }}'

では動作を確認してみます。

ProjectにDraftを作成。

DraftからIssueに変換。

Issueに変換されました。

しかしGitHub Actionsの履歴を見てもWorkflowが発火していない。(1つ表示されている実行はyml作成時にエラーとなったもの)

ここでドキュメントを読み直すと、なんとproject_cardイベントはprojects(classic)のみにしか対応していないようです。

Note: This event only occurs for projects (classic).

このprojects (classic)は今までclassicのprojectを作成したことのないRepositoryでは現在新規で作成することはできないようです。

You can only create a new classic project board for an organization, user, or repository that already has at least one classic project board. If you're unable to create a classic project board, create a project board instead.

よってproject_cardイベントの利用は避けたほうが良さそうですね。

issueイベントを試してみる(できた)

ドキュメントのイベント一覧を見直してみると、他に該当しそうなイベントはissuesです。このうち今回使えそうなactivity typeはopenedです。(先程のconvertedに対応するものは残念ながら無さそう)

次のようなIssueのオープン時に発火するWorkflowを作成します。

name: on card converted to issue
on:
  issues:
    types: 
      - opened

jobs:
  some_task:
    runs-on: ubuntu-latest
    steps:
      - run: echo '${{ toJSON(github.event) }}'

DraftをIssueに変換します。Issueの作成先は前述のWorkflowが作成されたRepositoryと同じにします。

すると今度はちゃんとWorkflowが実行されました。

ジョブ実行がちゃんと成功しています。

ただしecho '${{ toJSON(github.event) }}'の出力結果を見ても、Project上でDraftから変換されたissueであることを示す情報はありませんでした。

github.event

{
  "action": "opened",
  "issue": {
    "active_lock_reason": null,
    "assignee": null,
    "assignees": [],
    "author_association": "OWNER",
    "body": null,
    "closed_at": null,
    "comments": 0,
    "comments_url": "https://api.github.com/repos/cm-rwakatsuki/test/issues/5/comments",
    "created_at": "2022-09-17T15:08:37Z",
    "events_url": "https://api.github.com/repos/cm-rwakatsuki/test/issues/5/events",
    "html_url": "https://github.com/cm-rwakatsuki/test/issues/5",
    "id": 1376797175,
    "labels": [],
    "labels_url": "https://api.github.com/repos/cm-rwakatsuki/test/issues/5/labels{/name}",
    "locked": false,
    "milestone": null,
    "node_id": "I_kwDOIA6Uf85SEEH3",
    "number": 5,
    "performed_via_github_app": null,
    "reactions": {
      "+1": 0,
      "-1": 0,
      "confused": 0,
      "eyes": 0,
      "heart": 0,
      "hooray": 0,
      "laugh": 0,
      "rocket": 0,
      "total_count": 0,
      "url": "https://api.github.com/repos/cm-rwakatsuki/test/issues/5/reactions"
    },
    "repository_url": "https://api.github.com/repos/cm-rwakatsuki/test",
    "state": "open",
    "state_reason": null,
    "timeline_url": "https://api.github.com/repos/cm-rwakatsuki/test/issues/5/timeline",
    "title": "APIを追加する",
    "updated_at": "2022-09-17T15:08:37Z",
    "url": "https://api.github.com/repos/cm-rwakatsuki/test/issues/5",
    "user": {
      "avatar_url": "https://avatars.githubusercontent.com/u/57384023?v=4",
      "events_url": "https://api.github.com/users/cm-rwakatsuki/events{/privacy}",
      "followers_url": "https://api.github.com/users/cm-rwakatsuki/followers",
      "following_url": "https://api.github.com/users/cm-rwakatsuki/following{/other_user}",
      "gists_url": "https://api.github.com/users/cm-rwakatsuki/gists{/gist_id}",
      "gravatar_id": "",
      "html_url": "https://github.com/cm-rwakatsuki",
      "id": 57384023,
      "login": "cm-rwakatsuki",
      "node_id": "MDQ6VXNlcjU3Mzg0MDIz",
      "organizations_url": "https://api.github.com/users/cm-rwakatsuki/orgs",
      "received_events_url": "https://api.github.com/users/cm-rwakatsuki/received_events",
      "repos_url": "https://api.github.com/users/cm-rwakatsuki/repos",
      "site_admin": false,
      "starred_url": "https://api.github.com/users/cm-rwakatsuki/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/cm-rwakatsuki/subscriptions",
      "type": "User",
      "url": "https://api.github.com/users/cm-rwakatsuki"
    }
  },
  "repository": {
    "allow_forking": true,
    "archive_url": "https://api.github.com/repos/cm-rwakatsuki/test/{archive_format}{/ref}",
    "archived": false,
    "assignees_url": "https://api.github.com/repos/cm-rwakatsuki/test/assignees{/user}",
    "blobs_url": "https://api.github.com/repos/cm-rwakatsuki/test/git/blobs{/sha}",
    "branches_url": "https://api.github.com/repos/cm-rwakatsuki/test/branches{/branch}",
    "clone_url": "https://github.com/cm-rwakatsuki/test.git",
    "collaborators_url": "https://api.github.com/repos/cm-rwakatsuki/test/collaborators{/collaborator}",
    "comments_url": "https://api.github.com/repos/cm-rwakatsuki/test/comments{/number}",
    "commits_url": "https://api.github.com/repos/cm-rwakatsuki/test/commits{/sha}",
    "compare_url": "https://api.github.com/repos/cm-rwakatsuki/test/compare/{base}...{head}",
    "contents_url": "https://api.github.com/repos/cm-rwakatsuki/test/contents/{+path}",
    "contributors_url": "https://api.github.com/repos/cm-rwakatsuki/test/contributors",
    "created_at": "2022-09-17T14:03:52Z",
    "default_branch": "main",
    "deployments_url": "https://api.github.com/repos/cm-rwakatsuki/test/deployments",
    "description": null,
    "disabled": false,
    "downloads_url": "https://api.github.com/repos/cm-rwakatsuki/test/downloads",
    "events_url": "https://api.github.com/repos/cm-rwakatsuki/test/events",
    "fork": false,
    "forks": 0,
    "forks_count": 0,
    "forks_url": "https://api.github.com/repos/cm-rwakatsuki/test/forks",
    "full_name": "cm-rwakatsuki/test",
    "git_commits_url": "https://api.github.com/repos/cm-rwakatsuki/test/git/commits{/sha}",
    "git_refs_url": "https://api.github.com/repos/cm-rwakatsuki/test/git/refs{/sha}",
    "git_tags_url": "https://api.github.com/repos/cm-rwakatsuki/test/git/tags{/sha}",
    "git_url": "git://github.com/cm-rwakatsuki/test.git",
    "has_downloads": true,
    "has_issues": true,
    "has_pages": false,
    "has_projects": true,
    "has_wiki": true,
    "homepage": null,
    "hooks_url": "https://api.github.com/repos/cm-rwakatsuki/test/hooks",
    "html_url": "https://github.com/cm-rwakatsuki/test",
    "id": 537826431,
    "is_template": false,
    "issue_comment_url": "https://api.github.com/repos/cm-rwakatsuki/test/issues/comments{/number}",
    "issue_events_url": "https://api.github.com/repos/cm-rwakatsuki/test/issues/events{/number}",
    "issues_url": "https://api.github.com/repos/cm-rwakatsuki/test/issues{/number}",
    "keys_url": "https://api.github.com/repos/cm-rwakatsuki/test/keys{/key_id}",
    "labels_url": "https://api.github.com/repos/cm-rwakatsuki/test/labels{/name}",
    "language": null,
    "languages_url": "https://api.github.com/repos/cm-rwakatsuki/test/languages",
    "license": null,
    "merges_url": "https://api.github.com/repos/cm-rwakatsuki/test/merges",
    "milestones_url": "https://api.github.com/repos/cm-rwakatsuki/test/milestones{/number}",
    "mirror_url": null,
    "name": "test",
    "node_id": "R_kgDOIA6Ufw",
    "notifications_url": "https://api.github.com/repos/cm-rwakatsuki/test/notifications{?since,all,participating}",
    "open_issues": 5,
    "open_issues_count": 5,
    "owner": {
      "avatar_url": "https://avatars.githubusercontent.com/u/57384023?v=4",
      "events_url": "https://api.github.com/users/cm-rwakatsuki/events{/privacy}",
      "followers_url": "https://api.github.com/users/cm-rwakatsuki/followers",
      "following_url": "https://api.github.com/users/cm-rwakatsuki/following{/other_user}",
      "gists_url": "https://api.github.com/users/cm-rwakatsuki/gists{/gist_id}",
      "gravatar_id": "",
      "html_url": "https://github.com/cm-rwakatsuki",
      "id": 57384023,
      "login": "cm-rwakatsuki",
      "node_id": "MDQ6VXNlcjU3Mzg0MDIz",
      "organizations_url": "https://api.github.com/users/cm-rwakatsuki/orgs",
      "received_events_url": "https://api.github.com/users/cm-rwakatsuki/received_events",
      "repos_url": "https://api.github.com/users/cm-rwakatsuki/repos",
      "site_admin": false,
      "starred_url": "https://api.github.com/users/cm-rwakatsuki/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/cm-rwakatsuki/subscriptions",
      "type": "User",
      "url": "https://api.github.com/users/cm-rwakatsuki"
    },
    "private": true,
    "pulls_url": "https://api.github.com/repos/cm-rwakatsuki/test/pulls{/number}",
    "pushed_at": "2022-09-17T15:08:23Z",
    "releases_url": "https://api.github.com/repos/cm-rwakatsuki/test/releases{/id}",
    "size": 0,
    "ssh_url": "git@github.com:cm-rwakatsuki/test.git",
    "stargazers_count": 0,
    "stargazers_url": "https://api.github.com/repos/cm-rwakatsuki/test/stargazers",
    "statuses_url": "https://api.github.com/repos/cm-rwakatsuki/test/statuses/{sha}",
    "subscribers_url": "https://api.github.com/repos/cm-rwakatsuki/test/subscribers",
    "subscription_url": "https://api.github.com/repos/cm-rwakatsuki/test/subscription",
    "svn_url": "https://github.com/cm-rwakatsuki/test",
    "tags_url": "https://api.github.com/repos/cm-rwakatsuki/test/tags",
    "teams_url": "https://api.github.com/repos/cm-rwakatsuki/test/teams",
    "topics": [],
    "trees_url": "https://api.github.com/repos/cm-rwakatsuki/test/git/trees{/sha}",
    "updated_at": "2022-09-17T14:03:52Z",
    "url": "https://api.github.com/repos/cm-rwakatsuki/test",
    "visibility": "private",
    "watchers": 0,
    "watchers_count": 0,
    "web_commit_signoff_required": false
  },
  "sender": {
    "avatar_url": "https://avatars.githubusercontent.com/u/57384023?v=4",
    "events_url": "https://api.github.com/users/cm-rwakatsuki/events{/privacy}",
    "followers_url": "https://api.github.com/users/cm-rwakatsuki/followers",
    "following_url": "https://api.github.com/users/cm-rwakatsuki/following{/other_user}",
    "gists_url": "https://api.github.com/users/cm-rwakatsuki/gists{/gist_id}",
    "gravatar_id": "",
    "html_url": "https://github.com/cm-rwakatsuki",
    "id": 57384023,
    "login": "cm-rwakatsuki",
    "node_id": "MDQ6VXNlcjU3Mzg0MDIz",
    "organizations_url": "https://api.github.com/users/cm-rwakatsuki/orgs",
    "received_events_url": "https://api.github.com/users/cm-rwakatsuki/received_events",
    "repos_url": "https://api.github.com/users/cm-rwakatsuki/repos",
    "site_admin": false,
    "starred_url": "https://api.github.com/users/cm-rwakatsuki/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/cm-rwakatsuki/subscriptions",
    "type": "User",
    "url": "https://api.github.com/users/cm-rwakatsuki"
  }
}

なので、Draftからの変換による作成と、直接作成されたIssueとで異なる処理をWorkflowで実行したい場合は、Repositoryを分けるなどの運用の工夫が必要そうです。

おわりに

GitHub ProjectのCardのDraftからIssueへの変換時にGitHub Actionsを実行する方法を確認してみました。

今回「DraftからIssueへの変換時のみ」という条件ではGitHub ActionsのWorkflowをトリガーさせることができず、通常のIssue作成時にも実行されることを許容する必要があることが分かりました。(大抵の場合はそれで問題ないと思いますが)

実は、GitHub ProjectにはProjectごとにBuilt-in workflows機能があるのですが、こちらではまだあまり器用なことはできません。

今後このBuilt-in workflows機能が充実していくか、GitHub Actionsが(Classicではない)Projectに対応することに期待しましょう。

以上