Skip to content

Instantly share code, notes, and snippets.

@dayhaysoos
Created October 5, 2021 13:24
Show Gist options
  • Select an option

  • Save dayhaysoos/7a5c3d20a5584f3db951b08d992ca745 to your computer and use it in GitHub Desktop.

Select an option

Save dayhaysoos/7a5c3d20a5584f3db951b08d992ca745 to your computer and use it in GitHub Desktop.
const { Octokit } = require('@octokit/rest')
const CLIENT_MAP = require('../utils/client-map')
exports.handler = async function (event, context) {
try {
const { httpMethod, body } = event
if (httpMethod !== 'POST') {
return {
statusCode: 422,
body: JSON.stringify({ message: 'Improper request method' }),
}
}
const data = JSON.parse(body)
const { code, content, sha } = data
const companyName = CLIENT_MAP.get(code)
if (!companyName) {
return {
statusCode: 404,
body: JSON.stringify({ message: 'No company found for that code' }),
}
}
const octokit = new Octokit({
auth: process.env.GITHUB_API_KEY,
})
const today = new Date()
const year = today.getFullYear()
const month = today.getMonth() + 1
const day = today.getDate()
const isDevelopment = process.env.NETLIFY_DEV === 'true'
const branch = isDevelopment ? 'local-development-working-branch' : 'master'
await octokit.repos.createOrUpdateFileContents({
owner: process.env.GITHUB_OWNER,
repo: 'blacktechpipeline.com',
path: `content/jobs/posts/${companyName}.md`,
message: `Manual update by ${companyName} on ${year}-${month}-${day}`,
branch,
sha,
content,
})
return {
statusCode: 201,
body: JSON.stringify({ message: 'Saved properly' }),
}
} catch (error) {
return {
statusCode: 422,
body: JSON.stringify({ message: error.message }),
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment