-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
93 lines (85 loc) · 2.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const Airtable = require("airtable");
const { Octokit } = require("@octokit/core");
const { paginateRest } = require("@octokit/plugin-paginate-rest");
require("dotenv").config();
const base = new Airtable({ apiKey: process.env.AIRTABLE_API_KEY }).base(
process.env.AIRTABLE_BASE_ID
)(process.env.AIRTABLE_TABLE_ID);
const octokit = new (Octokit.plugin(paginateRest))({
auth: process.env.GH_API_TOKEN,
});
async function main() {
const issueNumberToRecord = {};
await base
.select({ view: "Ingest (do not edit)", fields: ["Number"] })
.eachPage((records, fetchNextPage) => {
records.forEach((record) => {
issueNumberToRecord[record.get("Number")] = record.getId();
});
fetchNextPage();
});
console.log(
`Fetched ${Object.keys(issueNumberToRecord).length} records from airtable.`
);
const githubIssues = {};
let dateAfter = new Date();
dateAfter.setDate(dateAfter.getDate() - 14);
for await (const response of octokit.paginate.iterator(
"GET /repos/{owner}/{repo}/issues",
{
owner: "ray-project",
repo: "ray",
since: dateAfter.toISOString(),
labels: ["bug"],
per_page: 100,
state: "all",
pull_request: false,
}
)) {
for (const issue of response.data) {
githubIssues[issue.number.toString()] = {
fields: {
Number: issue.number,
Title: issue.title,
Labels: issue.labels.map((label) => label.name),
Milestone: issue.milestone?.title,
Link: issue.html_url,
CreatedAt: issue.created_at,
UpdatedAt: issue.updated_at,
Body: issue.body,
State: issue.state,
Priority: issue.labels.filter((label) =>
label.name.startsWith("P")
)[0]?.name,
},
};
}
}
console.log(`Fetched ${Object.keys(githubIssues).length} issues from github`);
const airTableNumbers = new Set(Object.keys(issueNumberToRecord));
const recordToAdd = Object.entries(githubIssues)
.filter(([number, _]) => !airTableNumbers.has(number))
.map(([_, record]) => record);
const recordToUpdate = Object.entries(githubIssues)
.filter(([number, _]) => airTableNumbers.has(number))
.map(([_, record]) => record);
console.log(`Adding ${recordToAdd.length} records`);
for (let i = 0; i < recordToAdd.length; i += 10) {
const chunk = recordToAdd.slice(i, i + 10);
await base.create(chunk, {
typecast: true,
});
}
console.log(`Updating ${recordToUpdate.length} records`);
for (let i = 0; i < recordToUpdate.length; i += 10) {
const chunk = recordToUpdate.slice(i, i + 10).map((record) => ({
id: issueNumberToRecord[record.fields["Number"].toString()],
fields: record.fields,
}));
await base.replace(chunk, {
typecast: true,
});
}
console.log("Done!");
}
main().then(console.log).catch(console.error);