Skip to content
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
25 changes: 17 additions & 8 deletions functions/pull-request/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { simpleGit } from 'simple-git'
import envs from '../../lib/envs'
import { logger, middify } from '../../lib/lambda-common'
import type { Summary } from '../../lib/types'
import { getS3JSON } from '../../lib/utils'
import { getS3JSON, getS3Text } from '../../lib/utils'
import { createPullRequestDescription } from './pr-description'

const GIT_HUB_CREDENTIALS_SSM_PARAMETER = '/episoder/gitHubUserCredentials'
Expand All @@ -30,6 +30,7 @@ const gitHubUserCredentialsPromise = ssmClient.send(

interface PullRequestEvent {
transcriptKey: string
vttKey: string
summary: Summary
}

Expand All @@ -47,7 +48,9 @@ interface PullRequestEvent {
*/
export const handleEvent = middify(async (event: PullRequestEvent) => {
const transcript = await getS3JSON(s3Client, BUCKET_NAME, event.transcriptKey)
const id: string = path.basename(event.transcriptKey).split('.')[0]
const vttContent = await getS3Text(s3Client, BUCKET_NAME, event.vttKey)
// Extract episode number from filename, stripping _caption suffix (e.g., "150_caption.json" -> "150")
const id: string = path.basename(event.transcriptKey).replace('_caption.json', '')

const tmpDir = await mkdtemp(path.join(tmpdir(), 'pr-'))
logger.info('Using temporary directory', { tmpDir })
Expand Down Expand Up @@ -83,14 +86,20 @@ export const handleEvent = middify(async (event: PullRequestEvent) => {
logger.info('Checking out new branch', { branchName })
await git.cwd(path.resolve(tmpDir, repoName)).checkoutBranch(branchName, 'HEAD')

logger.info('Adding transcript', { branchName })
const newFilePath = path.join(tmpDir, repoName, 'src', '_transcripts', `${id}.json`)
await mkdir(path.dirname(newFilePath), { recursive: true })
await writeFile(newFilePath, JSON.stringify(transcript, null, ' '))
await git.add(newFilePath)
logger.info('Adding transcript and captions', { branchName })
const transcriptsDir = path.join(tmpDir, repoName, 'src', '_transcripts')
await mkdir(transcriptsDir, { recursive: true })

const jsonFilePath = path.join(transcriptsDir, `${id}.json`)
await writeFile(jsonFilePath, JSON.stringify(transcript, null, ' '))

const vttFilePath = path.join(transcriptsDir, `${id}.vtt`)
await writeFile(vttFilePath, vttContent)

await git.add([jsonFilePath, vttFilePath])

logger.info('Committing and pushing')
const title = `add episode ${id} transcript`
const title = `add episode ${id} transcript and captions`
await git.commit(`chore: ${title}`)
await git.push('origin', branchName, ['--set-upstream'])

Expand Down
15 changes: 14 additions & 1 deletion lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ import { logger } from './lambda-common'
* @returns The retrieved JSON as an object
*/
export async function getS3JSON<T = unknown>(s3Client: S3Client, bucket: string, key: string): Promise<T> {
const text = await getS3Text(s3Client, bucket, key)
return JSON.parse(text)
}

/**
* Convenience function for retrieving text content from S3
*
* @param s3Client An AWS SDK v3 S3 Client
* @param bucket The S3 bucket name
* @param key The S3 object key
* @returns The retrieved content as a string
*/
export async function getS3Text(s3Client: S3Client, bucket: string, key: string): Promise<string> {
logger.info('Getting object', { bucket, key })
const response = await s3Client.send(
new GetObjectCommand({
Expand All @@ -24,5 +37,5 @@ export async function getS3JSON<T = unknown>(s3Client: S3Client, bucket: string,
chunks.push(chunk as Uint8Array)
}

return JSON.parse(Buffer.concat(chunks).toString('utf-8'))
return Buffer.concat(chunks).toString('utf-8')
}
Loading