Skip to content

Instantly share code, notes, and snippets.

@maxwofford
Created July 1, 2024 15:42
Show Gist options
  • Save maxwofford/0745fd237e58c66c5adb77a254fe8aa9 to your computer and use it in GitHub Desktop.
Save maxwofford/0745fd237e58c66c5adb77a254fe8aa9 to your computer and use it in GitHub Desktop.
const commitFieldName = "TEMP: Git commits"
const slackChannel = "C06SBHMQU8G"
const Airtable = require('airtable');
Airtable.configure({
apiKey: process.env.AIRTABLE_API_KEY
})
const base = Airtable.base(process.env.AIRTABLE_BASE_ID);
// get all records from the base with a filter
const records = await base('Sessions').select({
filterByFormula: `{${commitFieldName}} = BLANK()`,
// maxRecords: 4
}).all();
console.log("Found", records.length, "record(s)")
const commitRegex = new RegExp('https://github\.com(?:/[^/]+)*/commit/[0-9a-f]{40}')
for (const record of records) {
const { id, fields } = record
const messageTS = fields['Message TS']
const slackMessages = await getSlackReplies(messageTS)
let commits = []
console.log("Checking", slackMessages.length, "messages")
slackMessages.forEach(msg => {
const txt = msg.text
if (!txt) return
// console.log(txt)
const matches = txt.match(commitRegex)
if (matches) {
commits = commits.concat(matches)
}
})
console.log(commits)
await base('Sessions').update(id, {
[commitFieldName]: commits.map(c => '- ' + c).join('\n')
})
console.log("Updated record", id, "with", commits.length, "commits")
await sleep(1000)
}
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function getSlackReplies(ts) {
const Slack = require('slack');
const slack = new Slack({ token: process.env.SLACK_TOKEN });
const replies = await slack.conversations.replies({
channel: slackChannel,
ts: ts
});
return replies.messages
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment