Skip to content

Commit

Permalink
Merge pull request #8 from grid-x/fix/make-spec-uploads-persistent
Browse files Browse the repository at this point in the history
fix: Make spec uploads persistent
  • Loading branch information
wwerner authored Sep 26, 2024
2 parents 88d253b + 6c224a7 commit 8c25417
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 29 deletions.
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 24 additions & 12 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

55 changes: 40 additions & 15 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import Axios from 'axios'
import FormData from 'form-data'
import fs from 'fs'

type DiscourseUploadResult = {
url: string
short_url: string
short_path: string
original_filename: string
}

/**
* The main function for the action.
* @returns {Promise<void>} Resolves when the action is complete.
Expand All @@ -22,7 +29,9 @@ export async function run(
}
const postUrl = `https://${discourseUrl}/posts/${discoursePostId}.json`

const upload = async (specPath: string): Promise<string | void> => {
const upload = async (
specPath: string
): Promise<DiscourseUploadResult | void> => {
// ref: https://docs.discourse.org/#tag/Uploads/operation/createUpload
const http = Axios.create({
baseURL: `https://${discourseUrl}`,
Expand Down Expand Up @@ -54,7 +63,12 @@ export async function run(
})
.then(({ data }) => {
core.debug(JSON.stringify(data, null, 2))
return data.url
return {
url: data.url,
short_url: data.short_url,
short_path: data.short_path,
original_filename: data.original_filename
}
})
.catch(e => {
console.error(
Expand All @@ -65,13 +79,15 @@ export async function run(
})
}

const updatePost = async (specUrl: string): Promise<void> => {
const updatePost = async (
uploadResult: DiscourseUploadResult
): Promise<void> => {
// ref: https://docs.discourse.org/#tag/Posts/operation/updatePost
core.info(`Updating ${postUrl}`)

const payload = {
post: {
raw: postBody(specUrl, commit),
raw: postBody(uploadResult, commit),
edit_reason: `Uploaded spec at ${commit}`
}
}
Expand All @@ -93,23 +109,32 @@ export async function run(
})
}

const postBody = (
uploadResult: DiscourseUploadResult,
commit: string
): string => `API Documentation/Specification \`${uploadResult.original_filename}\`
\`\`\`apidoc
https://${discourseUrl}/${uploadResult.short_path}
\`\`\`
[${uploadResult.original_filename}|attachment](${uploadResult.short_url})
*last updated*: ${new Date().toISOString()} (sha ${commit.trim()})
`

// Debug logs are only output if the `ACTIONS_STEP_DEBUG` secret is true
core.debug(`Uploading ${specFile} to post #${discoursePostId}`)

// Log the current timestamp, wait, then log the new timestamp
await upload(specFile).then(async specPath =>
// we can coerce string | void into string as void happens only with client side aborted requests
updatePost(specPath as string)
)
await upload(specFile).then(async uploadResult => {
if (!uploadResult) {
throw new Error('Upload failed. Aborting post update.')
}
return updatePost(uploadResult)
})
} catch (error) {
// Fail the workflow run if an error occurs
if (error instanceof Error) core.setFailed(error.message)
}
}

const postBody = (specUrl: string, commit: string): string => `\`\`\`apidoc
${specUrl}
\`\`\`
*last updated*: ${new Date().toISOString()} (sha ${commit.trim()})
`

0 comments on commit 8c25417

Please sign in to comment.