-
Notifications
You must be signed in to change notification settings - Fork 75
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: publish blogs automatically from github to hashnode #305
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3f7dc49
init commit
bnchi 32cc3f5
lint fixes
bnchi 80b56a5
change functions defintions to camel case
bnchi b60f761
fix error logs for hashnode
bnchi 03a3419
a few typo fixes
bnchi 56115e6
add workflow for publishing articles
bnchi 0f70a07
refactoring
bnchi 358fc87
change
bnchi 3357462
test blog
bnchi 9782ed9
Merge pull request #1 from bnchi/article/test
bnchi c573625
revert
bnchi 30823f8
update
bnchi 6fad65b
changes
bnchi 42c63b6
change yaml
bnchi 9184231
remove dotenv
bnchi 7d685b8
drop dotenv
bnchi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: Articles Publisher | ||
|
||
on: | ||
pull_request: | ||
types: [closed] | ||
|
||
env: | ||
HASHNODE_API_TOKEN: ${{ secrets.HASHNODE_API_TOKEN }} | ||
HASHNODE_PUBLICATION_ID: ${{ secrets.HASHNODE_PUBLICATION_ID }} | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
BRANCH_NAME: "develop" | ||
|
||
jobs: | ||
article-publisher: | ||
if: github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'develop' | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: set up Node.js | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: "20" | ||
|
||
- name: install deps and build | ||
run: | | ||
cd ./publisher | ||
npm install | ||
npm run build | ||
|
||
- name: run publisher | ||
run: | | ||
node ./publish_articles.js | ||
|
||
- name: (AUTO COMMIT) Commit and Push changes | ||
uses: mikeal/publish-to-github-action@master |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"name": "my-website", | ||
"workspaces": [ | ||
"publisher" | ||
], | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const publisher = require("publisher") | ||
const path = require("path") | ||
|
||
function main() { | ||
let posts_directory = path.join(__dirname, "blog") | ||
publisher.init(posts_directory) | ||
} | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "publisher", | ||
"version": "1.0.0", | ||
"main": "dist/main.js", | ||
"scripts": { | ||
"dev": "ts-node main.ts", | ||
"build": "tsc", | ||
"start": "node dist/main.js" | ||
}, | ||
"dependencies": { | ||
"axios": "^1.7.2", | ||
"gray-matter": "^4.0.3", | ||
"slugify": "^1.6.6", | ||
"ts-node": "^10.9.2", | ||
"typescript": "^5.5.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
export interface SEO { | ||
title: string | null | ||
description: string | null | ||
} | ||
|
||
export interface Article { | ||
title: string | ||
subtitle: string | null | ||
cover: string | null | ||
tags: string[] | ||
slug: string | ||
content: string | ||
canonicalUrl: string | null | ||
seo: SEO | null | ||
} | ||
|
||
export class ArticleBuilder { | ||
private article: Partial<Article> | ||
|
||
constructor(title: string, slug: string, content: string) { | ||
this.article = { | ||
title, | ||
slug, | ||
content, | ||
tags: [], | ||
subtitle: null, | ||
cover: null, | ||
canonicalUrl: null, | ||
seo: null, | ||
} | ||
} | ||
|
||
withSubtitle(subtitle: string | null): ArticleBuilder { | ||
this.article.subtitle = subtitle | ||
return this | ||
} | ||
|
||
withCover(cover: string | null): ArticleBuilder { | ||
this.article.cover = cover | ||
return this | ||
} | ||
|
||
withTags(tags: string[]): ArticleBuilder { | ||
this.article.tags = tags | ||
return this | ||
} | ||
|
||
withCanonicalUrl(canonicalUrl: string | null): ArticleBuilder { | ||
this.article.canonicalUrl = canonicalUrl | ||
return this | ||
} | ||
|
||
withSEO(seo: SEO | null): ArticleBuilder { | ||
this.article.seo = seo | ||
return this | ||
} | ||
|
||
build(): Article { | ||
if (!this.article.title || !this.article.slug || !this.article.content) { | ||
throw new Error("Title, slug, and content are required.") | ||
} | ||
return this.article as Article | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import HashnodePublisher from "../integrations/hashnode_publisher" | ||
import Publisher from "./publisher" | ||
|
||
export interface Config { | ||
posts_directory: string | ||
publishers: Array<Publisher> | ||
} | ||
|
||
export function buildConfig(posts_directory: string): Config { | ||
let config: Config = { | ||
posts_directory, | ||
publishers: [], | ||
} | ||
|
||
let hashnode_token = process.env.HASHNODE_API_TOKEN | ||
let hashnode_publication_id = process.env.HASHNODE_PUBLICATION_ID | ||
let hashnode_enabled = hashnode_token !== undefined && hashnode_publication_id !== undefined | ||
|
||
if (hashnode_enabled) { | ||
config.publishers.push(new HashnodePublisher(hashnode_token as string, hashnode_publication_id as string)) | ||
} | ||
|
||
return config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {Article} from "./article" | ||
|
||
export default interface Publisher { | ||
publishDraft(article: Article): Promise<void> | ||
publish(article: Article): Promise<string> | ||
edit(id: string, article: Article): Promise<void> | ||
getContentId(matter_data: {[key: string]: any}): string | ||
addContentId(matter_data: {[key: string]: any}, id: string): {[key: string]: any} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Build should have happened in the previous build. I think we can just wait for it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I don't quite get which build do you mean, if you mean build-and-deploy in the CI workflow here's why I didn't go with that approach :
I actually don't want this job to depend on build-and-deploy because build-and-deploy runs on every push to any branch.
Blog publishing will only work for merged PRs, in case you want to do editing or other peers want to double check the blog before publishing it.
So in this workflow we only download the dependencies necessary for publishing blogs and run the script to do it, we don't really need to wait on build-and-deploy.