Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for database sync #53

Merged
merged 1 commit into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
878 changes: 655 additions & 223 deletions dist/index.js

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"dependencies": {
"@actions/core": "^1.5.0",
"@actions/github": "^5.0.0",
"@notionhq/client": "^0.4.4",
"@notionhq/client": "^1.0.4",
"@types/node": "^15.12.3"
}
}
4 changes: 2 additions & 2 deletions src/__tests__/notion.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {NotionAdapter} from '../notion'
import {Notion} from '../notion'

describe('Notion Adapter should', () => {
it('be defined', () => {
expect(NotionAdapter).toBeDefined();
expect(Notion).toBeDefined();
})
})
233 changes: 233 additions & 0 deletions src/adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
import { Client } from '@notionhq/client';
import { IPageInput, Issue, IssueState } from './models';

class NotionClient {
protected client: Client;
constructor(apiKey: string) {
this.client = new Client({ auth: apiKey });
}
}

export class NotionAdapter extends NotionClient {
private ICON_URL = 'https://github.com/Souvikns/Notion-Board/blob/main/screenshots/iterative.png?raw=true';
constructor(apiKey: string, private databaseId: string) {
super(apiKey);
}

async isPageAvailable(ghId: number) {
const pages = await this.client.databases.query({
database_id: this.databaseId,
filter: {
property: 'ID',
number: {
equals: ghId
}
}
});

const notionPage = pages.results[0];
if (notionPage) return notionPage;

return undefined;
}

async updateCompletePage(issue: Issue, page_id: string) {
try {
let LabelList = issue.labels.map((el: any) => ({ name: el.name }));
await this.client.pages.update({
page_id: page_id,
properties: {
Name: {
title: [
{ text: { content: issue.title }, type: 'text' }
]
},
URL: {
url: issue.html_url
},
State: {
select: { name: issue.state }
},
ID: {
number: issue.id
},
Label: {
multi_select: LabelList
}
}
})
return {}
} catch (error) {
return {error};
}
}

async createPage(input: IPageInput): Promise<{ error?: Error | unknown; }> {
try {
await this.client.pages.create({
parent: {
database_id: this.databaseId
},
icon: {
external: {
url: this.ICON_URL
}
},
properties: {
Name: {
title: [
{ text: { content: input.title }, type: 'text' }
]
},
URL: {
url: input.url
},
ID: {
number: input.id
},
State: {
select: { name: input.state }
}
},
children: [
{
object: 'block',
type: 'paragraph',
paragraph: {
text: [
{
type: 'text',
text: {
content: input.body
}
},
]
}
}
]
} as any);
return {}
} catch (error) {
return { error };
}
}

async updatePage(id: number, title: string, body: string) {
try {
const pages = await this.client.databases.query({
database_id: this.databaseId,
filter: {
property: 'ID',
number: {
equals: id
}
}
});

const pageId = pages.results[0].id;

await this.client.pages.update({
page_id: pageId,
properties: {
Name: {
title: [
{
text: {
content: title,
},
type: 'text'
}
]
}
}
} as any);
} catch (error) {
throw error;
}
}

async setup(): Promise<{ error?: Error | unknown }> {
try {
await this.client.databases.update({
database_id: this.databaseId,
properties: {
URL: {
url: {}
},
ID: {
number: {}
},
State: {
select: {
options: [
{ name: 'open', color: 'green' },
{ name: 'closed', color: 'red' }
]
}
},
Label: {
multi_select: {}
}
}
})
return {}
} catch (error) {
return { error }
}
}

async updateState(state: IssueState, id: number) {

try {
const pages = await this.client.databases.query({
database_id: this.databaseId,
filter: {
property: 'ID',
number: {
equals: id
}
}
});

const pageId = pages.results[0].id;
await this.client.pages.update({
page_id: pageId,
properties: {
State: {
select: {
name: state
}
}
}
} as any);
} catch (error) {
throw error;
}
}

async updateLabel(id: number, labels: any) {
try {
const labelList = labels.map((el: any) => ({ name: el.name }));
const pages = await this.client.databases.query({
database_id: this.databaseId,
filter: {
property: 'ID',
number: {
equals: id
}
}
});
const pageId = pages.results[0].id;
await this.client.pages.update({
page_id: pageId,
properties: {
Label: {
multi_select: labelList
}
}
} as any)
} catch (error) {
throw error
}
}
}
Loading