Skip to content

Commit 7c92dfb

Browse files
committed
Added guard against empty rows
1 parent c808259 commit 7c92dfb

File tree

2 files changed

+131
-117
lines changed

2 files changed

+131
-117
lines changed

dist/index.js

Lines changed: 65 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16775,79 +16775,86 @@ async function main() {
1677516775
.filter((block) => block.value.id !== databaseId)
1677616776
.map((block) => block.value);
1677716777

16778-
const categories = category_schema_entry[1].options
16779-
.map((option) => ({
16780-
color: option.color,
16781-
value: option.value
16782-
}))
16783-
.sort((categoryA, categoryB) =>
16784-
categoryA.value > categoryB.value ? 1 : -1
16785-
);
16778+
if (rows.length === 0) return core.warn('No database rows detected');
16779+
else {
16780+
const categories = category_schema_entry[1].options
16781+
.map((option) => ({
16782+
color: option.color,
16783+
value: option.value
16784+
}))
16785+
.sort((categoryA, categoryB) =>
16786+
categoryA.value > categoryB.value ? 1 : -1
16787+
);
1678616788

16787-
const categories_map = new Map();
16789+
const categories_map = new Map();
1678816790

16789-
categories.forEach((category) => {
16790-
categories_map.set(category.value, {
16791-
items: [],
16792-
...category
16791+
categories.forEach((category) => {
16792+
categories_map.set(category.value, {
16793+
items: [],
16794+
...category
16795+
});
1679316796
});
16794-
});
1679516797

16796-
rows.forEach((row) => {
16797-
const category = row.properties[category_schema_entry[0]][0][0];
16798-
if (!category) throw new Error('Each row must have a category value');
16799-
const category_value = categories_map.get(category);
16800-
category_value.items.push(row.properties.title[0][0]);
16801-
});
16798+
rows.forEach((row) => {
16799+
const category = row.properties[category_schema_entry[0]][0][0];
16800+
if (!category) throw new Error('Each row must have a category value');
16801+
const category_value = categories_map.get(category);
16802+
category_value.items.push(row.properties.title[0][0]);
16803+
});
1680216804

16803-
const newLines = [];
16805+
const newLines = [];
1680416806

16805-
for (const [category, category_info] of categories_map) {
16806-
const content = [
16807-
`<img height="20px" src="https://img.shields.io/badge/${category}-${category_info.color}"/>`,
16808-
'</br>'
16809-
];
16810-
category_info.items.forEach((item) =>
16811-
content.push(
16812-
`<img src="https://img.shields.io/badge/-${item}-black?style=flat-square&amp;logo=${item}" alt="${item}">`
16813-
)
16807+
for (const [category, category_info] of categories_map) {
16808+
const content = [
16809+
`<div><img height="20px" src="https://img.shields.io/badge/${category}-${category_info.color}"/></div>`
16810+
];
16811+
category_info.items.forEach((item) =>
16812+
content.push(
16813+
`<img src="https://img.shields.io/badge/-${item}-black?style=flat-square&amp;logo=${item}" alt="${item}">`
16814+
)
16815+
);
16816+
newLines.push(...content, '<hr>');
16817+
}
16818+
16819+
const README_PATH = `${process.env.GITHUB_WORKSPACE}/README.md`;
16820+
core.info(`Reading from ${README_PATH}`);
16821+
16822+
const readmeLines = fs.readFileSync(README_PATH, 'utf-8').split('\n');
16823+
let startIdx = readmeLines.findIndex(
16824+
(content) => content.trim() === '<!--START_SECTION:learn-->'
1681416825
);
16815-
newLines.push(...content, '</br>');
16816-
}
1681716826

16818-
const README_PATH = `${process.env.GITHUB_WORKSPACE}/README.md`;
16819-
core.info(`Reading from ${README_PATH}`);
16827+
if (startIdx === -1) {
16828+
return core.setFailed(
16829+
`Couldn't find the <!--START_SECTION:learn--> comment. Exiting!`
16830+
);
16831+
}
1682016832

16821-
const readmeLines = fs.readFileSync(README_PATH, 'utf-8').split('\n');
16822-
let startIdx = readmeLines.findIndex(
16823-
(content) => content.trim() === '<!--START_SECTION:notion_learn-->'
16824-
);
16833+
if (endIdx === -1) {
16834+
return core.setFailed(
16835+
`Couldn't find the <!--END_SECTION:learn--> comment. Exiting!`
16836+
);
16837+
}
1682516838

16826-
if (startIdx === -1) {
16827-
return core.setFailed(
16828-
`Couldn't find the <!--START_SECTION:notion_learn--> comment. Exiting!`
16839+
const endIdx = readmeLines.findIndex(
16840+
(content) => content.trim() === '<!--END_SECTION:learn-->'
1682916841
);
16830-
}
16831-
16832-
const endIdx = readmeLines.findIndex(
16833-
(content) => content.trim() === '<!--END_SECTION:notion_learn-->'
16834-
);
1683516842

16836-
const finalLines = [
16837-
...readmeLines.slice(0, startIdx + 1),
16838-
...newLines,
16839-
...readmeLines.slice(endIdx)
16840-
];
16843+
const finalLines = [
16844+
...readmeLines.slice(0, startIdx + 1),
16845+
...newLines,
16846+
...readmeLines.slice(endIdx)
16847+
];
1684116848

16842-
core.info(`Writing to ${README_PATH}`);
16849+
core.info(`Writing to ${README_PATH}`);
1684316850

16844-
fs.writeFileSync(README_PATH, finalLines.join('\n'));
16851+
fs.writeFileSync(README_PATH, finalLines.join('\n'));
1684516852

16846-
try {
16847-
await commitFile();
16848-
} catch (err) {
16849-
tools.log.debug('Something went wrong');
16850-
return core.setFailed(err.message);
16853+
try {
16854+
await commitFile();
16855+
} catch (err) {
16856+
return core.setFailed(err.message);
16857+
}
1685116858
}
1685216859
} catch (error) {
1685316860
return core.setFailed(error.message);

src/index.js

Lines changed: 66 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -91,79 +91,86 @@ async function main() {
9191
.filter((block) => block.value.id !== databaseId)
9292
.map((block) => block.value);
9393

94-
const categories = category_schema_entry[1].options
95-
.map((option) => ({
96-
color: option.color,
97-
value: option.value
98-
}))
99-
.sort((categoryA, categoryB) =>
100-
categoryA.value > categoryB.value ? 1 : -1
101-
);
102-
103-
const categories_map = new Map();
94+
if (rows.length === 0) return core.warn('No database rows detected');
95+
else {
96+
const categories = category_schema_entry[1].options
97+
.map((option) => ({
98+
color: option.color,
99+
value: option.value
100+
}))
101+
.sort((categoryA, categoryB) =>
102+
categoryA.value > categoryB.value ? 1 : -1
103+
);
104+
105+
const categories_map = new Map();
106+
107+
categories.forEach((category) => {
108+
categories_map.set(category.value, {
109+
items: [],
110+
...category
111+
});
112+
});
104113

105-
categories.forEach((category) => {
106-
categories_map.set(category.value, {
107-
items: [],
108-
...category
114+
rows.forEach((row) => {
115+
const category = row.properties[category_schema_entry[0]][0][0];
116+
if (!category) throw new Error('Each row must have a category value');
117+
const category_value = categories_map.get(category);
118+
category_value.items.push(row.properties.title[0][0]);
109119
});
110-
});
111120

112-
rows.forEach((row) => {
113-
const category = row.properties[category_schema_entry[0]][0][0];
114-
if (!category) throw new Error('Each row must have a category value');
115-
const category_value = categories_map.get(category);
116-
category_value.items.push(row.properties.title[0][0]);
117-
});
121+
const newLines = [];
122+
123+
for (const [category, category_info] of categories_map) {
124+
const content = [
125+
`<div><img height="20px" src="https://img.shields.io/badge/${category}-${category_info.color}"/></div>`
126+
];
127+
category_info.items.forEach((item) =>
128+
content.push(
129+
`<img src="https://img.shields.io/badge/-${item}-black?style=flat-square&amp;logo=${item}" alt="${item}">`
130+
)
131+
);
132+
newLines.push(...content, '<hr>');
133+
}
118134

119-
const newLines = [];
135+
const README_PATH = `${process.env.GITHUB_WORKSPACE}/README.md`;
136+
core.info(`Reading from ${README_PATH}`);
120137

121-
for (const [category, category_info] of categories_map) {
122-
const content = [
123-
`<img height="20px" src="https://img.shields.io/badge/${category}-${category_info.color}"/>`,
124-
'</br>'
125-
];
126-
category_info.items.forEach((item) =>
127-
content.push(
128-
`<img src="https://img.shields.io/badge/-${item}-black?style=flat-square&amp;logo=${item}" alt="${item}">`
129-
)
138+
const readmeLines = fs.readFileSync(README_PATH, 'utf-8').split('\n');
139+
let startIdx = readmeLines.findIndex(
140+
(content) => content.trim() === '<!--START_SECTION:learn-->'
130141
);
131-
newLines.push(...content, '</br>');
132-
}
133142

134-
const README_PATH = `${process.env.GITHUB_WORKSPACE}/README.md`;
135-
core.info(`Reading from ${README_PATH}`);
143+
if (startIdx === -1) {
144+
return core.setFailed(
145+
`Couldn't find the <!--START_SECTION:learn--> comment. Exiting!`
146+
);
147+
}
136148

137-
const readmeLines = fs.readFileSync(README_PATH, 'utf-8').split('\n');
138-
let startIdx = readmeLines.findIndex(
139-
(content) => content.trim() === '<!--START_SECTION:notion_learn-->'
140-
);
149+
if (endIdx === -1) {
150+
return core.setFailed(
151+
`Couldn't find the <!--END_SECTION:learn--> comment. Exiting!`
152+
);
153+
}
141154

142-
if (startIdx === -1) {
143-
return core.setFailed(
144-
`Couldn't find the <!--START_SECTION:notion_learn--> comment. Exiting!`
155+
const endIdx = readmeLines.findIndex(
156+
(content) => content.trim() === '<!--END_SECTION:learn-->'
145157
);
146-
}
147158

148-
const endIdx = readmeLines.findIndex(
149-
(content) => content.trim() === '<!--END_SECTION:notion_learn-->'
150-
);
151-
152-
const finalLines = [
153-
...readmeLines.slice(0, startIdx + 1),
154-
...newLines,
155-
...readmeLines.slice(endIdx)
156-
];
159+
const finalLines = [
160+
...readmeLines.slice(0, startIdx + 1),
161+
...newLines,
162+
...readmeLines.slice(endIdx)
163+
];
157164

158-
core.info(`Writing to ${README_PATH}`);
165+
core.info(`Writing to ${README_PATH}`);
159166

160-
fs.writeFileSync(README_PATH, finalLines.join('\n'));
167+
fs.writeFileSync(README_PATH, finalLines.join('\n'));
161168

162-
try {
163-
await commitFile();
164-
} catch (err) {
165-
tools.log.debug('Something went wrong');
166-
return core.setFailed(err.message);
169+
try {
170+
await commitFile();
171+
} catch (err) {
172+
return core.setFailed(err.message);
173+
}
167174
}
168175
} catch (error) {
169176
return core.setFailed(error.message);

0 commit comments

Comments
 (0)