Skip to content

Commit

Permalink
feat: use ClientPayload
Browse files Browse the repository at this point in the history
BREAK CHANGE:
- deprecate out-file, content options
- add client-payload

Signed-off-by: Liu Bowen <mr_lbw@outlook.com>
  • Loading branch information
lbwa committed Aug 1, 2021
1 parent 647ab04 commit fb29b59
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 29 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/sync-data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
# Set always-auth in npmrc
always-auth: false # optional, default is false
# Version Spec of the version to use. Examples: 12.x, 10.15.1, >=10.15.0
node-version: 15.x
node-version: 16.x

# Retrieve event payload
# https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#fromjson
Expand All @@ -51,5 +51,5 @@ jobs:
# token field is optional
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
out-dir: 'docs'
out-file: ${{ github.event.client_payload.title }}
content: ${{ github.event.client_payload.post }}
# https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#fromjson
client-payload: ${{ fromJSON(github.event.client_payload) }}
7 changes: 2 additions & 5 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ inputs:
out-dir:
description: 'Where should our documentations be place in'
default: 'docs'
out-file:
description: 'As generated markdown file name'
required: true
content:
description: 'As generated markdown file content'
client-payload:
description: 'An request payload from Github Repository Dispatch Event'
required: true
runs:
using: 'node12'
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@actions/github": "^5.0.0",
"fs-extra": "^10.0.0",
"func-async": "^0.1.0",
"lodash": "^4.17.21",
"tslib": "^2.2.0",
"zx": "^2.0.0"
},
Expand All @@ -42,6 +43,7 @@
"@lbwa/prettier-config": "^0.1.0",
"@lbwa/tsconfig": "^0.1.0",
"@types/fs-extra": "^9.0.11",
"@types/lodash": "^4.14.170",
"@typescript-eslint/eslint-plugin": "^4.23.0",
"@typescript-eslint/parser": "^4.23.0",
"@vercel/ncc": "^0.28.5",
Expand Down
39 changes: 26 additions & 13 deletions serverless/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ type YuQueEvent struct {
Data yuque.DocDetailSerializer `json:"data"`
}

// should use `github.event.client_payload.post` to retrieve `Post` field in the repository action file(*.yml)
type GithubClientPayload struct {
Id yuque.YuQueId `json:"id"`
Title string `json:"title"`
Post string `json:"post"`
Path string `json:"path"`
}

// Inspired by https://github.com/google/go-github/blob/a19996a59629e9dc2b32dc2fb8628040e6e38459/github/repos_test.go#L2213
// github v3 rest api: https://docs.github.com/en/rest
// based on tencent cloud api gateway event, see https://github.com/tencentyun/scf-go-lib/blob/ccd4bf6de8cb891d5b58e49d6e03000337f9f817/events/apigw.go
Expand Down Expand Up @@ -93,19 +101,11 @@ func dispatchGithubAction(ctx context.Context, request events.APIGatewayRequest)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)

postBodyBytes, _ := json.Marshal(struct {
Id yuque.YuQueId `json:"id"`
Title string `json:"title"`
// should use `github.event.client_payload.post` to retrieve this payload in the action file(*.yml)
Post string `json:"post"`
Path string `json:"path"`
}{
Id: postMeta.Id,
Title: postMeta.Title,
Post: post,
Path: strings.Join(docPathParts, "/"),
})
clientPayload := json.RawMessage(postBodyBytes)
clientPayload, err := serializeClientPayload(postMeta.Id, postMeta.Title, post, strings.Join(docPathParts, "/"))
if err != nil {
sugar.Debug("Got error when marshal GithubClientPayload: ", err)
return "", err
}
// create a repository dispatch event
// https://docs.github.com/en/rest/reference/repos#create-a-repository-dispatch-event
repo, response, err := client.Repositories.Dispatch(
Expand Down Expand Up @@ -134,3 +134,16 @@ func dispatchGithubAction(ctx context.Context, request events.APIGatewayRequest)
sugar.Debug("Operation successfully: ", repo.HTMLURL)
return http.StatusText(http.StatusOK), nil
}

func serializeClientPayload(id yuque.YuQueId, title, post, filepath string) (msg json.RawMessage, err error) {
rawBytes, err := json.Marshal(GithubClientPayload{
Id: id,
Title: title,
Post: post,
Path: filepath,
})
if err != nil {
return []byte{}, err
}
return json.RawMessage(rawBytes), nil
}
34 changes: 26 additions & 8 deletions src/action.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,50 @@
import { getInput, info, startGroup, endGroup } from '@actions/core'
import { getInput, info, startGroup, endGroup, setFailed } from '@actions/core'
import * as github from '@actions/github'
import fs from 'fs-extra'
import { $ } from 'zx'
import isString from 'lodash/isString'

const enum Input {
TOKEN = 'token',
OUT_DIR = 'out-dir',
OUT_FILE = 'out-file',
CONTENT = 'content'
CLIENT_PAYLOAD = 'client-payload'
}

type ClientPayload = { id: number; title: string; post: string; path: string }

export async function main() {
const token = getInput(Input.TOKEN, { required: true })
const docsDir = getInput(Input.OUT_DIR)
const outFile = getInput(Input.OUT_FILE, { required: true })
const content = getInput(Input.CONTENT, { required: true })
const outDir = getInput(Input.OUT_DIR)
const rawClientPayload: ClientPayload | string = getInput(
Input.CLIENT_PAYLOAD,
{ required: true }
)
const clientPayload = isString(rawClientPayload)
? (JSON.parse(rawClientPayload) as ClientPayload)
: rawClientPayload
const outFile = clientPayload.title
const fileContent = clientPayload.post

if (!outFile || !fileContent) {
setFailed(
`Couldn't find available title or content, we got title ${outFile} and content ${fileContent.slice(
0,
10
)}`
)
}

// https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#github-context
// https://github.com/actions/checkout/blob/25a956c84d5dd820d28caab9f86b8d183aeeff3d/src/input-helper.ts#L22
const username = github.context.actor || github.context.repo.owner
const repoName = github.context.repo.repo
const remoteOrigin = `https://${username}:${token}@github.com/${username}/${repoName}.git`
const outFilePath =
(docsDir.endsWith('/') ? docsDir : `${docsDir}/`) +
(outDir.endsWith('/') ? outDir : `${outDir}/`) +
(/\.mdx?$/.test(outFile) ? outFile : `${outFile}.md`)

startGroup('Create local file')
await fs.outputFile(outFilePath, content)
await fs.outputFile(outFilePath, fileContent)
info(`New data is available in the ${username}/${repoName}/${outFilePath}`)
endGroup()

Expand Down

0 comments on commit fb29b59

Please sign in to comment.