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 dcb310fee1296ad85643c88a93b5d0ccd15cb5a5 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 19 Feb 2021 06:52:01 +0000 Subject: [PATCH 2/2] Bump @octokit/webhooks from 7.21.0 to 8.4.1 Bumps [@octokit/webhooks](https://github.com/octokit/webhooks.js) from 7.21.0 to 8.4.1. - [Release notes](https://github.com/octokit/webhooks.js/releases) - [Commits](https://github.com/octokit/webhooks.js/compare/v7.21.0...v8.4.1) Signed-off-by: dependabot[bot] --- package-lock.json | 12 +++++++++--- package.json | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4246bfc..1ef0012 100644 --- a/package-lock.json +++ b/package-lock.json @@ -979,15 +979,21 @@ } }, "@octokit/webhooks": { - "version": "7.21.0", - "resolved": "https://registry.npmjs.org/@octokit/webhooks/-/webhooks-7.21.0.tgz", - "integrity": "sha512-Mj7Pa6JZgSjfzQfYF3Bf5KpyhzEBv4kHbj2EjCB/vMQiZCiiW30j5rS6t/d0ZN0FBrlSOuJIT+YU8IJt30VyWA==", + "version": "8.4.1", + "resolved": "https://registry.npmjs.org/@octokit/webhooks/-/webhooks-8.4.1.tgz", + "integrity": "sha512-XV7VWuV13ltfuYREWX+3yrHFjBGalHrkzba5ht2q92s3RzFQH28NU+ZJADwbozFf5rY9fLtrVuhEsHPnaPxGrg==", "requires": { "@octokit/request-error": "^2.0.2", + "@octokit/webhooks-definitions": "3.61.0", "aggregate-error": "^3.1.0", "debug": "^4.0.0" } }, + "@octokit/webhooks-definitions": { + "version": "3.61.0", + "resolved": "https://registry.npmjs.org/@octokit/webhooks-definitions/-/webhooks-definitions-3.61.0.tgz", + "integrity": "sha512-Iu2rMpCU0y25T4rIMoAKyQRWf/YJZnJKbIzzrKQMQJRIiORe4AGev1GLgf5zUcnz9twQ23DYEFLpDLVHViwTDQ==" + }, "@sinonjs/commons": { "version": "1.8.1", "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-1.8.1.tgz", diff --git a/package.json b/package.json index 0c52ca3..37b898f 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "@actions/core": "^1.2.6", "@actions/github": "^4.0.0", "@github-docs/frontmatter": "^1.3.1", - "@octokit/webhooks": "^7.21.0", + "@octokit/webhooks": "^8.4.1", "@types/node-fetch": "^2.5.8", "node-fetch": "^2.6.1" },