Skip to content

Commit 99fcf81

Browse files
committed
Added tests for action module
1 parent 5305d02 commit 99fcf81

File tree

4 files changed

+314
-99
lines changed

4 files changed

+314
-99
lines changed

src/action.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import * as core from '@actions/core';
2+
import { NotionEndpoints } from '@nishans/endpoints';
3+
import { ICollection, TCollectionBlock } from '@nishans/types';
4+
import fs from 'fs';
5+
import { ActionUtils } from './utils';
6+
7+
export async function action() {
8+
try {
9+
const NOTION_TOKEN_V2 = core.getInput('token_v2');
10+
const databaseId = core.getInput('database_id');
11+
12+
const collectionView = await ActionUtils.fetchData<TCollectionBlock>(
13+
databaseId,
14+
'block'
15+
);
16+
core.info('Fetched database');
17+
18+
const collection_id = collectionView.collection_id;
19+
const collection = await ActionUtils.fetchData<ICollection>(
20+
collection_id,
21+
'collection'
22+
);
23+
24+
core.info('Fetched collection');
25+
26+
const { recordMap } = await NotionEndpoints.Queries.queryCollection(
27+
{
28+
collectionId: collection_id,
29+
collectionViewId: '',
30+
query: {},
31+
loader: {
32+
type: 'table',
33+
loadContentCover: false,
34+
limit: 10000,
35+
userTimeZone: ''
36+
}
37+
},
38+
{
39+
token: NOTION_TOKEN_V2,
40+
user_id: ''
41+
}
42+
);
43+
44+
core.info('Fetched rows');
45+
const { schema } = collection;
46+
const [
47+
category_schema_entry,
48+
color_schema_entry
49+
] = ActionUtils.getSchemaEntries(schema);
50+
51+
const rows = ActionUtils.modifyRows(recordMap, databaseId);
52+
53+
if (rows.length === 0) return core.error('No database rows detected');
54+
else {
55+
const categories_map = ActionUtils.constructCategoriesMap(
56+
category_schema_entry[1]
57+
);
58+
rows.forEach((row) => {
59+
const category = row.properties[category_schema_entry[0]][0][0];
60+
if (!category) throw new Error('Each row must have a category value');
61+
const category_value = categories_map.get(category);
62+
category_value!.items.push(row.properties);
63+
});
64+
65+
const README_PATH = `${process.env.GITHUB_WORKSPACE}/README.md`;
66+
core.info(`Reading from ${README_PATH}`);
67+
68+
const readmeLines = fs.readFileSync(README_PATH, 'utf-8').split('\n');
69+
70+
const [startIdx, endIdx] = ActionUtils.checkForSections(readmeLines);
71+
const newLines = ActionUtils.constructNewContents(
72+
categories_map,
73+
color_schema_entry[0]
74+
);
75+
76+
const finalLines = [
77+
...readmeLines.slice(0, startIdx + 1),
78+
...newLines,
79+
...readmeLines.slice(endIdx)
80+
];
81+
82+
core.info(`Writing to ${README_PATH}`);
83+
84+
fs.writeFileSync(README_PATH, finalLines.join('\n'), 'utf-8');
85+
86+
try {
87+
await ActionUtils.commitFile();
88+
} catch (err) {
89+
core.setFailed(err.message);
90+
}
91+
}
92+
} catch (error) {
93+
core.setFailed(error.message);
94+
}
95+
}

src/index.ts

Lines changed: 2 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,3 @@
1-
import * as core from '@actions/core';
2-
import { NotionEndpoints } from '@nishans/endpoints';
3-
import { ICollection, TCollectionBlock } from '@nishans/types';
4-
import fs from 'fs';
5-
import { checkForSections } from './utils/checkForSections';
6-
import { commitFile } from './utils/commitFile';
7-
import { constructCategoriesMap } from './utils/constructCategoriesMap';
8-
import { constructNewContents } from './utils/constructNewContents';
9-
import { fetchData } from './utils/fetchData';
10-
import { getSchemaEntries } from './utils/getSchemaEntries';
11-
import { modifyRows } from './utils/modifyRows';
1+
import { action } from './action';
122

13-
async function main() {
14-
try {
15-
const NOTION_TOKEN_V2 = core.getInput('token_v2');
16-
const databaseId = core.getInput('database_id');
17-
18-
const collectionView = await fetchData<TCollectionBlock>(
19-
databaseId,
20-
'block'
21-
);
22-
core.info('Fetched database');
23-
24-
const collection_id = collectionView.collection_id;
25-
const collection = await fetchData<ICollection>(
26-
collection_id,
27-
'collection'
28-
);
29-
30-
core.info('Fetched collection');
31-
32-
const { recordMap } = await NotionEndpoints.Queries.queryCollection(
33-
{
34-
collectionId: collection_id,
35-
collectionViewId: '',
36-
query: {},
37-
loader: {
38-
type: 'table',
39-
loadContentCover: false,
40-
limit: 10000,
41-
userTimeZone: ''
42-
}
43-
},
44-
{
45-
token: NOTION_TOKEN_V2,
46-
user_id: ''
47-
}
48-
);
49-
50-
core.info('Fetched rows');
51-
const { schema } = collection;
52-
const [category_schema_entry, color_schema_entry] = getSchemaEntries(
53-
schema
54-
);
55-
56-
const rows = modifyRows(recordMap, databaseId);
57-
58-
if (rows.length === 0) return core.error('No database rows detected');
59-
else {
60-
const categories_map = constructCategoriesMap(category_schema_entry[1]);
61-
rows.forEach((row) => {
62-
const category = row.properties[category_schema_entry[0]][0][0];
63-
if (!category) throw new Error('Each row must have a category value');
64-
const category_value = categories_map.get(category);
65-
category_value!.items.push(row.properties);
66-
});
67-
68-
const README_PATH = `${process.env.GITHUB_WORKSPACE}/README.md`;
69-
core.info(`Reading from ${README_PATH}`);
70-
71-
const readmeLines = fs.readFileSync(README_PATH, 'utf-8').split('\n');
72-
73-
const [startIdx, endIdx] = checkForSections(readmeLines);
74-
const newLines = constructNewContents(
75-
categories_map,
76-
color_schema_entry[0]
77-
);
78-
79-
const finalLines = [
80-
...readmeLines.slice(0, startIdx + 1),
81-
...newLines,
82-
...readmeLines.slice(endIdx)
83-
];
84-
85-
core.info(`Writing to ${README_PATH}`);
86-
87-
fs.writeFileSync(README_PATH, finalLines.join('\n'), 'utf-8');
88-
89-
try {
90-
await commitFile();
91-
} catch (err) {
92-
core.setFailed(err.message);
93-
}
94-
}
95-
} catch (error) {
96-
core.setFailed(error.message);
97-
}
98-
}
99-
100-
main();
3+
action();

src/utils/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { checkForSections } from './checkForSections';
2+
import { commitFile } from './commitFile';
3+
import { constructCategoriesMap } from './constructCategoriesMap';
4+
import { constructNewContents } from './constructNewContents';
5+
import { fetchData } from './fetchData';
6+
import { getSchemaEntries } from './getSchemaEntries';
7+
import { modifyRows } from './modifyRows';
8+
9+
export const ActionUtils = {
10+
checkForSections,
11+
commitFile,
12+
constructCategoriesMap,
13+
constructNewContents,
14+
fetchData,
15+
getSchemaEntries,
16+
modifyRows
17+
};

0 commit comments

Comments
 (0)