Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make spec uploads persistent #8

Merged
merged 4 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()})
`
Loading