From 9f9044bb26ce7875529a6243a7bd5cd57aa2d849 Mon Sep 17 00:00:00 2001 From: Jeremy Ward Date: Wed, 17 Feb 2021 21:52:20 -0500 Subject: [PATCH 1/2] Add Medium Integration --- README.md | 33 ++++++++++++++++++--------------- src/main.ts | 2 ++ src/publish/medium.ts | 31 +++++++++++++++++++++++++++++++ src/publish/publisher.ts | 6 ++++-- 4 files changed, 55 insertions(+), 17 deletions(-) create mode 100644 src/publish/medium.ts diff --git a/README.md b/README.md index af67b51..3c2be6f 100644 --- a/README.md +++ b/README.md @@ -5,17 +5,20 @@ If a tree falls in the woods, does it make a sound? Publish your repo's markdown to multiple distributors with a simple `git push`, so that when your tree falls, it makes a sound. ## Current Integration -* [DEV](https://dev.to) + +- [DEV](https://dev.to) Create an Issue if you have another distributor that you'd like to add to this git action. ## Arguments -| Input | Description | Usage | -| :---: | :---: | :---: | -| `github-token` | GitHub Auth Token | *Required* | -| `content-dir` | Path from the root to your markdown files. Defaults to `./content/articles/` | Optional | -| `dev-to-token` | API token for dev.to. (https://dev.to/settings/account) | *Required* | +| Input | Description | Usage | +| :----------------: | :---------------------------------------------------------------------------------------------------------: | :--------: | +| `github-token` | GitHub Auth Token | _Required_ | +| `content-dir` | Path from the root to your markdown files. Defaults to `./content/articles/` | Optional | +| `dev-to-token` | API token for dev.to. (https://dev.to/settings/account) | _Required_ | +| `medium-token` | API token for medium.to. (https://medium.com/me/settings) | _Optional_ | +| `medium-author-id` | You can access your author id from the (User Endpoint)[https://github.com/Medium/medium-api-docs#31-users]. | _Optional_ | ## Example usage @@ -25,20 +28,20 @@ name: CrossPost on: push: paths: - - 'content/articles/*.md' + - 'content/articles/*.md' jobs: crosspost: runs-on: ubuntu-latest steps: - - name: Checkout Code - uses: actions/checkout@v2 - - - uses: basicBrogrammer/crosspost-markdown@v0.1.1 - with: - content-dir: 'content/articles/' - dev-to-token: ${{ secrets.DEV_TO }} - github-token: ${{ secrets.GITHUB_TOKEN }} + - name: Checkout Code + uses: actions/checkout@v2 + + - uses: basicBrogrammer/crosspost-markdown@v0.1.1 + with: + content-dir: 'content/articles/' + dev-to-token: ${{ secrets.DEV_TO }} + github-token: ${{ secrets.GITHUB_TOKEN }} ``` ## Contribute diff --git a/src/main.ts b/src/main.ts index f2feb85..c79b603 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,7 @@ import * as core from '@actions/core'; import {getFiles} from './get-files'; import DevTo from './publish/dev-to'; +import Medium from './publish/medium'; async function run(): Promise { try { @@ -9,6 +10,7 @@ async function run(): Promise { const files = await getFiles(); files.forEach((path: string) => { new DevTo(path, 'dev-to-token').publish(); + new Medium(path, 'medium-token').publish(); }); } catch (error) { core.setFailed(error.message); diff --git a/src/publish/medium.ts b/src/publish/medium.ts new file mode 100644 index 0000000..1829e20 --- /dev/null +++ b/src/publish/medium.ts @@ -0,0 +1,31 @@ +import fetch from 'node-fetch'; +import {Response} from 'node-fetch'; +import Publisher from './publisher'; +import * as core from '@actions/core'; + +class Medium extends Publisher { + _publish(): Promise { + const body = { + title: this.data.title, + contentFormat: 'markdown', + content: this.content, + tags: this.data.tags.split(',').map((tag: string) => tag.trim()), + publishStatus: 'public', + }; + + return fetch(`https://api.medium.com/v1/users/${this.authorId}/posts`, { + method: 'post', + body: JSON.stringify(body), + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${this.token}`, + }, + }); + } + + get authorId(): string { + return core.getInput('medium-author-id'); + } +} + +export default Medium; diff --git a/src/publish/publisher.ts b/src/publish/publisher.ts index 0c7bf52..a0dd6f7 100644 --- a/src/publish/publisher.ts +++ b/src/publish/publisher.ts @@ -7,7 +7,7 @@ export default class Publisher { token: string; markdown: string = 'not-configured'; data: any = null; - content: string | null = null; + content: string = 'no content'; constructor(path: string, tokenKey: string) { this.token = core.getInput(tokenKey); @@ -27,7 +27,9 @@ export default class Publisher { if (!this._isConfigured) return; if (this.data?.published) { - this._publish().then(this._logResponse); + this._publish() + .then(this._logResponse) + .catch((e) => console.log(e)); } else { console.log(`Article ${this.data?.title} NOT published. Skipping.`); } From f1f649d67c3100feb25b4ef83249daf64ad1acfe Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 19 Feb 2021 06:52:23 +0000 Subject: [PATCH 2/2] Bump @types/node from 14.14.22 to 14.14.30 Bumps [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) from 14.14.22 to 14.14.30. - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node) Signed-off-by: dependabot[bot] --- package-lock.json | 6 +++--- package.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4246bfc..007ba6b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1172,9 +1172,9 @@ "dev": true }, "@types/node": { - "version": "14.14.22", - "resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.22.tgz", - "integrity": "sha512-g+f/qj/cNcqKkc3tFqlXOYjrmZA+jNBiDzbP3kH+B+otKFqAdPgVTGP1IeKRdMml/aE69as5S4FqtxAbl+LaMw==" + "version": "14.14.30", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.30.tgz", + "integrity": "sha512-gUWhy8s45fQp4PqqKecsnOkdW0kt1IaKjgOIR3HPokkzTmQj9ji2wWFID5THu1MKrtO+d4s2lVrlEhXUsPXSvg==" }, "@types/node-fetch": { "version": "2.5.8", diff --git a/package.json b/package.json index 0c52ca3..5bbcd66 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ }, "devDependencies": { "@types/jest": "^26.0.20", - "@types/node": "^14.14.22", + "@types/node": "^14.14.30", "@typescript-eslint/parser": "^4.14.1", "@vercel/ncc": "^0.27.0", "eslint": "^7.18.0",