Skip to content

Commit

Permalink
Merge pull request #52 from magitools/feature/github
Browse files Browse the repository at this point in the history
Feature/GitHub
  • Loading branch information
matfire authored Jan 26, 2024
2 parents 55df3c7 + e1fad8b commit 2705364
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 9 deletions.
108 changes: 108 additions & 0 deletions src/lib/articles/platforms/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { RegisterPlatform, type IBasePlatform, type IPlatformSetting } from './base';

@RegisterPlatform
export class GithubPlatform implements IBasePlatform<GithubPlatform> {
settings: Record<string, string> = {};
frontmatter: Record<string, any> = {};

async publish(content: string) {
const { github_token, github_repo, github_user, github_folder } = this.settings;
if (!github_repo || !github_token || !github_user) {
throw new Error('Required Settings not found');
}
try {
const title = this.frontmatter['title'].replaceAll(' ', '-').toLowerCase();
const commitMessage = this.settings['github_commit'].replace(/%([^%]+)%/g, (match, key) => {
return key in this.frontmatter ? this.frontmatter[key] : match;
});
const res = await fetch(
`https://api.github.com/repos/${github_user}/${github_repo}/contents/${
github_folder ? github_folder + '/' : ''
}${title}.md`,
{
method: 'put',
body: JSON.stringify({
content: btoa(content),
message: commitMessage || 'published with magiedit'
}),
headers: {
Authorization: `Bearer ${github_token}`,
Accept: 'application/vnd.github+json',
'Content-Type': 'application/json',
'X-GitHub-Api-Version': '2022-11-28'
}
}
);
if (!res.ok) {
throw new Error('request failed');
}
} catch (error) {
throw new Error(`${error}`);
}
}
setSettings(settings: Record<string, any>): GithubPlatform {
this.settings = settings;
return this;
}
getRequiredSettings(): IPlatformSetting[] {
return [
{
label: { htmlFor: 'github_token', value: 'Github Token' },
type: 'input',
name: 'github_token',
settings: {
type: 'text',
required: true
}
},
{
label: { htmlFor: 'github_user', value: 'Github User or Organization' },
type: 'input',
name: 'github_user',
settings: {
type: 'text',
required: true
}
},
{
label: { htmlFor: 'github_repo', value: 'Github Repository' },
type: 'input',
name: 'github_repo',
settings: {
type: 'text',
required: true
}
},
{
label: {
htmlFor: 'github_folder',
value: 'Github Folder Path (relative to the root of the repository)'
},
type: 'input',
name: 'github_folder',
settings: {
type: 'text'
}
},
{
label: {
htmlFor: 'github_commit',
value:
'Commit Message for file creation: you can use your frontmatter variables by writing %variable_name% (i.e. %title%)'
},
type: 'input',
name: 'github_commit',
settings: {
type: 'text'
}
}
];
}
setFrontmatter(data: Record<string, any>): GithubPlatform {
this.frontmatter = data;
return this;
}
getPlatformName(): string {
return 'Github';
}
}
3 changes: 2 additions & 1 deletion src/lib/articles/platforms/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DevPlatform } from './dev';
import { HashnodePlatform } from './hashnode';
import { GithubPlatform } from './github';

export { DevPlatform, HashnodePlatform };
export { DevPlatform, HashnodePlatform, GithubPlatform };
1 change: 0 additions & 1 deletion src/routes/(auth)/login/github/callback/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export const GET = async ({ url, cookies, locals }) => {
const state = url.searchParams.get('state');
const code = url.searchParams.get('code');
// validate state
console.log(storedState, state, code);
if (!storedState || !state || storedState !== state || !code) {
return new Response(null, {
status: 400
Expand Down
10 changes: 7 additions & 3 deletions src/routes/app/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { db } from '$lib/server/db';
import { userArticles } from '$lib/server/drizzle';
import { userArticles, userPublications } from '$lib/server/drizzle';
import { eq } from 'drizzle-orm';

export const load: PageServerLoad = async ({ locals }) => {
Expand All @@ -11,8 +11,12 @@ export const load: PageServerLoad = async ({ locals }) => {
.select()
.from(userArticles)
.where(eq(userArticles.author, session.user.userId));

const publications = await db
.select()
.from(userPublications)
.where(eq(userPublications.userId, session.user.userId));
return {
articles: articles || []
articles: articles || [],
publications: publications || []
};
};
2 changes: 0 additions & 2 deletions src/routes/app/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export const load: PageLoad = async ({ data }) => {
return {
articles: await Promise.all(
data.articles.map(async (e) => {
console.log(e);
const buffer = new Uint8Array(
atob(e.content)
.split('')
Expand All @@ -23,7 +22,6 @@ export const load: PageLoad = async ({ data }) => {
key,
buffer
);
console.log(decryptedContent);
return { content: new TextDecoder().decode(decryptedContent), id: e.id } as IArticle;
} catch (error) {
console.log(error.toString());
Expand Down
1 change: 0 additions & 1 deletion src/routes/app/publications/new/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export const actions: Actions = {
}
const formData = await request.formData();
const { publisher_id, publisher_name, ...args } = Object.fromEntries(formData);
console.log(publisher_id, publisher_name, args);
await db.insert(userPublications).values({
publisherName: publisher_id,
publisherData: args,
Expand Down
1 change: 0 additions & 1 deletion src/routes/app/publications/new/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
export let data;
let selectedPlatform: string | null = null;
console.log(data);
</script>

<form action="" method="post">
Expand Down

0 comments on commit 2705364

Please sign in to comment.