Skip to content
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

refactor: extract github client into a function #1

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
59 changes: 39 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,55 @@

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/
const [, owner, repo] = gitUrl.match(regex)
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(),
Expand All @@ -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
}