From 196ca48f153b8929a2fddae9c8ebfc71e9b88b53 Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Thu, 4 Jan 2024 13:42:50 +0100 Subject: [PATCH] refactor: extract github client into a function --- .github/workflows/main.yml | 2 +- package.json | 1 - src/index.js | 59 +++++++++++++++++++++++++------------- 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3e25f47..72ad516 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -60,7 +60,7 @@ jobs: # github-token: ${{ secrets.GITHUB_TOKEN }} - name: Release env: - CONVENTIONAL_GITHUB_RELEASER_TOKEN: ${{ secrets.GH_TOKEN }} + GH_TOKEN: ${{ secrets.GH_TOKEN }} NPM_TOKEN: ${{ secrets.NPM_TOKEN }} run: | git config --global user.email ${{ secrets.GIT_EMAIL }} diff --git a/package.json b/package.json index 52e8fbd..4c33201 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,6 @@ "@commitlint/config-conventional": "latest", "@ksmithut/prettier-standard": "latest", "ci-publish": "latest", - "conventional-github-releaser": "latest", "finepack": "latest", "git-authors-cli": "latest", "nano-staged": "latest", diff --git a/src/index.js b/src/index.js index eb91462..1644c8c 100644 --- a/src/index.js +++ b/src/index.js @@ -2,9 +2,10 @@ const { execSync } = require('child_process') -const latestTag = () => execSync('git tag --sort=-creatordate').toString().trim().split('\n')[0] +const latestTag = () => + execSync('git tag --sort=-creatordate').toString().trim().split('\n')[0] -const gitDetails = (opts) => { +const gitDetails = opts => { if (opts.owner && opts.repo) return opts const gitUrl = execSync('git remote get-url origin').toString().trim() const regex = /github\.com[:/](.*?)\/(.*?)\.git/ @@ -12,16 +13,44 @@ const gitDetails = (opts) => { return { owner, repo } } -module.exports = async ({ token, name, tagName, prerelease, draft, body, ...opts }) => { +const createGithubAPI = + token => + async (url, { headers, ...opts } = {}) => { + const response = await fetch(url, { + headers: { + Accept: 'application/vnd.github+json', + Authorization: `Bearer ${token}`, + ...headers + }, + ...opts + }) + + const payload = await response.json().catch(() => ({})) + + if (!response.ok) { + const error = new Error( + `${payload?.message} – See ${payload?.documentation_url}` + ) + error.name = 'GitHubError' + throw error + } + + return payload + } + +module.exports = async ({ + token, + name, + tagName, + prerelease, + draft, + body, + ...opts +}) => { const { owner, repo } = gitDetails(opts) - - const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/releases`, { + const githubAPI = createGithubAPI(token) + return githubAPI(`https://api.github.com/repos/${owner}/${repo}/releases`, { method: 'POST', - headers: { - Accept: 'application/vnd.github+json', - Authorization: `Bearer ${token}`, - 'Content-Type': 'application/json' - }, body: JSON.stringify({ name: name ?? undefined, tag_name: tagName ?? latestTag(), @@ -31,14 +60,4 @@ module.exports = async ({ token, name, tagName, prerelease, draft, body, ...opts body }) }) - - const payload = await response.json() - - if (!response.ok) { - const error = new Error(`${payload.message} – See ${payload.documentation_url}`) - error.name = 'GitHubError' - throw error - } - - return payload }