-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.ts
116 lines (100 loc) · 3.11 KB
/
action.ts
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import * as core from '@actions/core';
import { HttpClient } from '@actions/http-client';
import { IRequestOptions } from '@actions/http-client/interfaces';
import { ICollection, RecordMap, TCollectionBlock } from '@nishans/types';
import fs from 'fs';
import { ActionUtils } from './utils';
export async function action() {
try {
const NOTION_TOKEN_V2 = core.getInput('token_v2');
let id = core.getInput('database_id').replace(/-/g, '');
const databaseId = `${id.substr(0, 8)}-${id.substr(8, 4)}-${id.substr(
12,
4
)}-${id.substr(16, 4)}-${id.substr(20)}`;
const headers: IRequestOptions['headers'] = {
Accept: 'application/json',
'Content-Type': 'application/json',
cookie: `token_v2=${NOTION_TOKEN_V2}`
};
const http = new HttpClient(undefined, undefined, {
headers
});
const collectionView = await ActionUtils.fetchData<TCollectionBlock>(
databaseId,
'block',
http
);
core.info('Fetched database');
const collection_id = collectionView.collection_id;
const collection = await ActionUtils.fetchData<ICollection>(
collection_id,
'collection',
http
);
core.info('Fetched collection');
const response = await http.post(
`https://www.notion.so/api/v3/queryCollection`,
JSON.stringify({
collection: {
id: collection_id,
spaceId: collectionView.space_id
},
collectionView: {
id: collectionView.view_ids[0],
spaceId: collectionView.space_id
},
loader: {
type: 'reducer',
reducers: {
collection_group_results: {
type: 'results'
}
},
searchQuery: '',
userTimeZone: 'Asia/Dhaka'
}
})
);
const { recordMap } = JSON.parse(await response.readBody()) as {
recordMap: RecordMap;
};
core.info('Fetched rows');
const { schema } = collection;
const [
categorySchemaEntry,
colorSchemaEntry,
,
base64SchemaEntry
] = ActionUtils.getSchemaEntries(schema);
const rows = ActionUtils.modifyRows(recordMap, databaseId);
const categoriesMap = ActionUtils.constructCategoriesMap(
categorySchemaEntry[1]
);
ActionUtils.populateCategoriesMapItems(
rows,
categorySchemaEntry[0],
categoriesMap
);
const README_PATH = `${process.env.GITHUB_WORKSPACE}/README.md`;
core.info(`Reading from ${README_PATH}`);
const readmeLines = fs.readFileSync(README_PATH, 'utf-8').split('\n');
const [startIdx, endIdx] = ActionUtils.checkForSections(readmeLines);
const newLines = ActionUtils.constructNewContents(
categoriesMap,
colorSchemaEntry[0],
base64SchemaEntry[0]
);
const finalLines = [
...readmeLines.slice(0, startIdx + 1),
...newLines,
...readmeLines.slice(endIdx)
];
core.info(`Writing to ${README_PATH}`);
fs.writeFileSync(README_PATH, finalLines.join('\n'), 'utf-8');
await ActionUtils.commitFile();
} catch (err) {
core.error(err.message);
core.setFailed(err.message);
}
}