Skip to content

Commit

Permalink
allow eventName workflow_dispatch (#424)
Browse files Browse the repository at this point in the history
* allow eventName workflow_dispatch

* feat: add comment for event

* wip

* wip

* wip

* wip

* wip

* wip

* feat: add event name to comment

* chore: changeset
  • Loading branch information
andykenward authored Sep 21, 2024
1 parent 25b53b9 commit 18cd3d0
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-teachers-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'github-actions-cloudflare-pages': minor
---

support workflow_dispatch event. If the branch used for the action has an open pull request a comment be added.
2 changes: 1 addition & 1 deletion __tests__/common/github/comment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('addComment', () => {
query: MutationAddComment,
variables: {
subjectId: 'MDExOlB1bGxSZXF1ZXN0Mjc5MTQ3NDM3',
body: '## Cloudflare Pages Deployment\n**Environment:** production\n**Project:** cloudflare-pages-action\n**Built with commit:** mock-github-sha\n**Preview URL:** https://206e215c.cloudflare-pages-action-a5z.pages.dev\n**Branch Preview URL:** https://unknown-branch.cloudflare-pages-action-a5z.pages.dev\n\n### Wrangler Output\nsuccess'
body: '## Cloudflare Pages Deployment\n**Event Name:** pull_request\n**Environment:** production\n**Project:** cloudflare-pages-action\n**Built with commit:** mock-github-sha\n**Preview URL:** https://206e215c.cloudflare-pages-action-a5z.pages.dev\n**Branch Preview URL:** https://unknown-branch.cloudflare-pages-action-a5z.pages.dev\n\n### Wrangler Output\nsuccess'
}
},
{
Expand Down
43 changes: 23 additions & 20 deletions dist/deploy/index.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions dist/deploy/index.js.LEGAL.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions dist/deploy/index.js.map

Large diffs are not rendered by default.

35 changes: 30 additions & 5 deletions src/common/github/comment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {info} from '@actions/core'

import {graphql} from '@/gql/gql.js'

import type {PagesDeployment} from '@/common/cloudflare/types.js'
Expand All @@ -6,6 +8,7 @@ import {getCloudflareDeploymentAlias} from '@/common/cloudflare/deployment/get.j
import {raise} from '@/common/utils.js'

import {request} from './api/client.js'
import {paginate} from './api/paginate.js'
import {useContext, useContextEvent} from './context.js'

export const MutationAddComment = graphql(/* GraphQL */ `
Expand All @@ -20,19 +23,40 @@ export const MutationAddComment = graphql(/* GraphQL */ `
}
`)

const getNodeIdFromEvent = async () => {
const {eventName, payload} = useContextEvent()

if (eventName === 'workflow_dispatch') {
const {repo, branch} = useContext()
const pullRequestsOpen = await paginate('GET /repos/{owner}/{repo}/pulls', {
owner: repo.owner,
repo: repo.repo,
per_page: 100
})

const pullRequest = pullRequestsOpen.find(item => {
return item.head.ref === branch
})

return pullRequest?.node_id
}
if (eventName === 'pull_request' && payload.action !== 'closed') {
return payload.pull_request.node_id ?? raise('No pull request node id')
}
}

export const addComment = async (
deployment: PagesDeployment,
output: string
): Promise<string | undefined> => {
const {eventName, payload} = useContextEvent()
const {eventName} = useContextEvent()

if (eventName === 'pull_request' && payload.action !== 'closed') {
const prNodeId =
payload.pull_request.node_id ?? raise('No pull request node id')
const prNodeId = await getNodeIdFromEvent()

if (prNodeId) {
const {sha} = useContext()

const rawBody = `## Cloudflare Pages Deployment\n**Environment:** ${deployment.environment}\n**Project:** ${deployment.project_name}\n**Built with commit:** ${sha}\n**Preview URL:** ${deployment.url}\n**Branch Preview URL:** ${getCloudflareDeploymentAlias(deployment)}\n\n### Wrangler Output\n${output}`
const rawBody = `## Cloudflare Pages Deployment\n**Event Name:** ${eventName}\n**Environment:** ${deployment.environment}\n**Project:** ${deployment.project_name}\n**Built with commit:** ${sha}\n**Preview URL:** ${deployment.url}\n**Branch Preview URL:** ${getCloudflareDeploymentAlias(deployment)}\n\n### Wrangler Output\n${output}`

const comment = await request({
query: MutationAddComment,
Expand All @@ -43,4 +67,5 @@ export const addComment = async (
})
return comment.data.addComment?.commentEdge?.node?.id
}
info('addComment - No Pull Request could be found to post comment.')
}
8 changes: 6 additions & 2 deletions src/deploy/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ export async function run() {
const {eventName} = useContextEvent()

/**
* Only support eventName push & pull_request.
* Only support eventName push, pull_request & workflow_dispatch.
*/
if (eventName !== 'push' && eventName !== 'pull_request') {
if (
eventName !== 'push' &&
eventName !== 'pull_request' &&
eventName !== 'workflow_dispatch'
) {
setFailed(`GitHub Action event name '${eventName}' not supported.`)
return
}
Expand Down

0 comments on commit 18cd3d0

Please sign in to comment.