feat: cli commit upload
This commit is contained in:
parent
1b18ac3f3a
commit
106b3c0545
@ -1 +0,0 @@
|
|||||||
POSTGRES_URL=postgres://postgres:postgres@localhost:5432/postgres
|
|
@ -15,7 +15,8 @@
|
|||||||
"@scalar/hono-api-reference": "^0.5.149",
|
"@scalar/hono-api-reference": "^0.5.149",
|
||||||
"hono": "^4.6.3",
|
"hono": "^4.6.3",
|
||||||
"semver": "^7.6.3",
|
"semver": "^7.6.3",
|
||||||
"svix": "^1.36.0"
|
"svix": "^1.36.0",
|
||||||
|
"ts-pattern": "^5.5.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/bun": "latest",
|
"@types/bun": "latest",
|
||||||
|
@ -1,66 +0,0 @@
|
|||||||
import { changelog, changelog_version, db } from '@boring.tools/database'
|
|
||||||
import { VersionByIdParams, VersionOutput } from '@boring.tools/schema'
|
|
||||||
import { createRoute } from '@hono/zod-openapi'
|
|
||||||
import { and, eq } from 'drizzle-orm'
|
|
||||||
|
|
||||||
export const byId = createRoute({
|
|
||||||
method: 'get',
|
|
||||||
path: '/:id',
|
|
||||||
request: {
|
|
||||||
params: VersionByIdParams,
|
|
||||||
},
|
|
||||||
responses: {
|
|
||||||
200: {
|
|
||||||
content: {
|
|
||||||
'application/json': {
|
|
||||||
schema: VersionOutput,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
description: 'Return version by id',
|
|
||||||
},
|
|
||||||
400: {
|
|
||||||
description: 'Bad Request',
|
|
||||||
},
|
|
||||||
500: {
|
|
||||||
description: 'Internal Server Error',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
export const byIdFunc = async ({
|
|
||||||
userId,
|
|
||||||
id,
|
|
||||||
}: {
|
|
||||||
userId: string
|
|
||||||
id: string
|
|
||||||
}) => {
|
|
||||||
const versionResult = await db.query.changelog_version.findFirst({
|
|
||||||
where: eq(changelog_version.id, id),
|
|
||||||
with: {
|
|
||||||
commits: true,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!versionResult) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!versionResult.changelogId) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
const changelogResult = await db.query.changelog.findMany({
|
|
||||||
where: and(eq(changelog.userId, userId)),
|
|
||||||
columns: {
|
|
||||||
id: true,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
const changelogIds = changelogResult.map((cl) => cl.id)
|
|
||||||
|
|
||||||
if (!changelogIds.includes(versionResult.changelogId)) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
return versionResult
|
|
||||||
}
|
|
@ -32,7 +32,7 @@ export const route = createRoute({
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
export const regsiterCommitCreate = (api: typeof changelogCommitApi) => {
|
export const registerCommitCreate = (api: typeof changelogCommitApi) => {
|
||||||
return api.openapi(route, async (c) => {
|
return api.openapi(route, async (c) => {
|
||||||
const userId = verifyAuthentication(c)
|
const userId = verifyAuthentication(c)
|
||||||
|
|
||||||
@ -49,7 +49,14 @@ export const regsiterCommitCreate = (api: typeof changelogCommitApi) => {
|
|||||||
throw new HTTPException(404, { message: 'Not Found' })
|
throw new HTTPException(404, { message: 'Not Found' })
|
||||||
}
|
}
|
||||||
|
|
||||||
const [result] = await db.insert(changelog_commit).values(data).returning()
|
const mappedData = data.map((entry) => ({
|
||||||
|
...entry,
|
||||||
|
createdAt: new Date(entry.author.date),
|
||||||
|
}))
|
||||||
|
const [result] = await db
|
||||||
|
.insert(changelog_commit)
|
||||||
|
.values(mappedData)
|
||||||
|
.returning()
|
||||||
|
|
||||||
if (!result) {
|
if (!result) {
|
||||||
throw new HTTPException(404, { message: 'Not Found' })
|
throw new HTTPException(404, { message: 'Not Found' })
|
||||||
|
@ -1,63 +0,0 @@
|
|||||||
import { changelog, changelog_version, db } from '@boring.tools/database'
|
|
||||||
import { GeneralOutput } from '@boring.tools/schema'
|
|
||||||
import { createRoute } from '@hono/zod-openapi'
|
|
||||||
import { and, eq } from 'drizzle-orm'
|
|
||||||
import { HTTPException } from 'hono/http-exception'
|
|
||||||
|
|
||||||
export const remove = createRoute({
|
|
||||||
method: 'delete',
|
|
||||||
path: '/:id',
|
|
||||||
responses: {
|
|
||||||
200: {
|
|
||||||
content: {
|
|
||||||
'application/json': {
|
|
||||||
schema: GeneralOutput,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
description: 'Removes a version by id',
|
|
||||||
},
|
|
||||||
404: {
|
|
||||||
content: {
|
|
||||||
'application/json': {
|
|
||||||
schema: GeneralOutput,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
description: 'Version not found',
|
|
||||||
},
|
|
||||||
500: {
|
|
||||||
description: 'Internal Server Error',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
export const removeFunc = async ({
|
|
||||||
userId,
|
|
||||||
id,
|
|
||||||
}: {
|
|
||||||
userId: string
|
|
||||||
id: string
|
|
||||||
}) => {
|
|
||||||
const changelogResult = await db.query.changelog.findMany({
|
|
||||||
where: and(eq(changelog.userId, userId)),
|
|
||||||
with: {
|
|
||||||
versions: {
|
|
||||||
where: eq(changelog_version.id, id),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
const findChangelog = changelogResult.find((change) =>
|
|
||||||
change.versions.find((ver) => ver.id === id),
|
|
||||||
)
|
|
||||||
|
|
||||||
if (!findChangelog?.versions.length) {
|
|
||||||
throw new HTTPException(404, {
|
|
||||||
message: 'Version not found',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return db
|
|
||||||
.delete(changelog_version)
|
|
||||||
.where(and(eq(changelog_version.id, id)))
|
|
||||||
.returning()
|
|
||||||
}
|
|
@ -1,7 +1,8 @@
|
|||||||
import { OpenAPIHono } from '@hono/zod-openapi'
|
import { OpenAPIHono } from '@hono/zod-openapi'
|
||||||
import type { Variables } from '../..'
|
import type { Variables } from '../..'
|
||||||
import type { ContextModule } from '../../utils/sentry'
|
import type { ContextModule } from '../../utils/sentry'
|
||||||
import { regsiterCommitCreate } from './create'
|
import { registerCommitCreate } from './create'
|
||||||
|
import { registerCommitList } from './list'
|
||||||
|
|
||||||
export const changelogCommitApi = new OpenAPIHono<{ Variables: Variables }>()
|
export const changelogCommitApi = new OpenAPIHono<{ Variables: Variables }>()
|
||||||
|
|
||||||
@ -10,113 +11,5 @@ const module: ContextModule = {
|
|||||||
sub_module: 'version',
|
sub_module: 'version',
|
||||||
}
|
}
|
||||||
|
|
||||||
regsiterCommitCreate(changelogCommitApi)
|
registerCommitCreate(changelogCommitApi)
|
||||||
|
registerCommitList(changelogCommitApi)
|
||||||
// app.openapi(create, async (c) => {
|
|
||||||
// const userId = verifyAuthentication(c)
|
|
||||||
// try {
|
|
||||||
// const payload = await c.req.json()
|
|
||||||
// const result = await createFunc({ userId, payload })
|
|
||||||
|
|
||||||
// if (!result) {
|
|
||||||
// return c.json({ message: 'Version not created' }, 400)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return c.json(result, 201)
|
|
||||||
// } catch (error) {
|
|
||||||
// return captureSentry({
|
|
||||||
// c,
|
|
||||||
// error,
|
|
||||||
// module,
|
|
||||||
// user: {
|
|
||||||
// id: userId,
|
|
||||||
// },
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
// })
|
|
||||||
|
|
||||||
/* app.openapi(byId, async (c) => {
|
|
||||||
const userId = verifyAuthentication(c)
|
|
||||||
try {
|
|
||||||
const id = c.req.param('id')
|
|
||||||
const result = await byIdFunc({ userId, id })
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return c.json({ message: 'Version not found' }, 404)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure all required properties are present and non-null
|
|
||||||
return c.json(
|
|
||||||
{
|
|
||||||
...result,
|
|
||||||
changelogId: result.changelogId || '',
|
|
||||||
version: result.version || '',
|
|
||||||
status: result.status || 'draft',
|
|
||||||
releasedAt: result.releasedAt,
|
|
||||||
commits: result.commits || [],
|
|
||||||
markdown: result.markdown || '',
|
|
||||||
},
|
|
||||||
200,
|
|
||||||
)
|
|
||||||
} catch (error) {
|
|
||||||
return captureSentry({
|
|
||||||
c,
|
|
||||||
error,
|
|
||||||
module,
|
|
||||||
user: {
|
|
||||||
id: userId,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
app.openapi(update, async (c) => {
|
|
||||||
const userId = verifyAuthentication(c)
|
|
||||||
try {
|
|
||||||
const id = c.req.param('id')
|
|
||||||
|
|
||||||
if (!id) {
|
|
||||||
return c.json({ message: 'Version not found' }, 404)
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await updateFunc({
|
|
||||||
userId,
|
|
||||||
payload: await c.req.json(),
|
|
||||||
id,
|
|
||||||
})
|
|
||||||
|
|
||||||
return c.json(result)
|
|
||||||
} catch (error) {
|
|
||||||
return captureSentry({
|
|
||||||
c,
|
|
||||||
error,
|
|
||||||
module,
|
|
||||||
user: {
|
|
||||||
id: userId,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
app.openapi(remove, async (c) => {
|
|
||||||
const userId = verifyAuthentication(c)
|
|
||||||
try {
|
|
||||||
const id = c.req.param('id')
|
|
||||||
const result = await removeFunc({ userId, id })
|
|
||||||
|
|
||||||
if (result.length === 0) {
|
|
||||||
return c.json({ message: 'Version not found' }, 404)
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.json({ message: 'Version removed' })
|
|
||||||
} catch (error) {
|
|
||||||
return captureSentry({
|
|
||||||
c,
|
|
||||||
error,
|
|
||||||
module,
|
|
||||||
user: {
|
|
||||||
id: userId,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}) */
|
|
||||||
|
84
apps/api/src/changelog/commit/list.ts
Normal file
84
apps/api/src/changelog/commit/list.ts
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
import { changelog, changelog_commit, db } from '@boring.tools/database'
|
||||||
|
import { CommitListOutput, CommitListParams } from '@boring.tools/schema'
|
||||||
|
import { createRoute } from '@hono/zod-openapi'
|
||||||
|
import { and, eq, isNotNull, isNull } from 'drizzle-orm'
|
||||||
|
import { HTTPException } from 'hono/http-exception'
|
||||||
|
import { P, match } from 'ts-pattern'
|
||||||
|
import type { changelogCommitApi } from '.'
|
||||||
|
import { verifyAuthentication } from '../../utils/authentication'
|
||||||
|
|
||||||
|
const route = createRoute({
|
||||||
|
method: 'get',
|
||||||
|
path: '/',
|
||||||
|
request: {
|
||||||
|
query: CommitListParams,
|
||||||
|
},
|
||||||
|
responses: {
|
||||||
|
200: {
|
||||||
|
content: {
|
||||||
|
'application/json': {
|
||||||
|
schema: CommitListOutput,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
description: 'Return version by id',
|
||||||
|
},
|
||||||
|
400: {
|
||||||
|
description: 'Bad Request',
|
||||||
|
},
|
||||||
|
500: {
|
||||||
|
description: 'Internal Server Error',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
export const registerCommitList = (api: typeof changelogCommitApi) => {
|
||||||
|
return api.openapi(route, async (c) => {
|
||||||
|
const userId = verifyAuthentication(c)
|
||||||
|
const { changelogId, limit, offset, hasVersion } = c.req.valid('query')
|
||||||
|
const result = await db.query.changelog.findFirst({
|
||||||
|
where: and(eq(changelog.userId, userId), eq(changelog.id, changelogId)),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
throw new HTTPException(404, { message: 'Changelog not found' })
|
||||||
|
}
|
||||||
|
|
||||||
|
const where = match({ changelogId, hasVersion })
|
||||||
|
.with(
|
||||||
|
{
|
||||||
|
changelogId: P.select('changelogId'),
|
||||||
|
hasVersion: P.when((hasVersion) => hasVersion === true),
|
||||||
|
},
|
||||||
|
({ changelogId }) =>
|
||||||
|
and(
|
||||||
|
eq(changelog_commit.changelogId, changelogId),
|
||||||
|
isNotNull(changelog_commit.versionId),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.with(
|
||||||
|
{
|
||||||
|
changelogId: P.select('changelogId'),
|
||||||
|
hasVersion: P.when((hasVersion) => hasVersion === false),
|
||||||
|
},
|
||||||
|
({ changelogId }) =>
|
||||||
|
and(
|
||||||
|
eq(changelog_commit.changelogId, changelogId),
|
||||||
|
isNull(changelog_commit.versionId),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.otherwise(() => eq(changelog_commit.changelogId, changelogId))
|
||||||
|
|
||||||
|
const commits = await db.query.changelog_commit.findMany({
|
||||||
|
where,
|
||||||
|
limit: Number(limit) ?? 10,
|
||||||
|
offset: Number(offset) ?? 0,
|
||||||
|
orderBy: (_, { desc }) => [desc(changelog_commit.createdAt)],
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!commits) {
|
||||||
|
throw new HTTPException(404, { message: 'Not Found' })
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.json(commits, 200)
|
||||||
|
})
|
||||||
|
}
|
@ -1,72 +0,0 @@
|
|||||||
import { changelog, changelog_version, db } from '@boring.tools/database'
|
|
||||||
import { VersionUpdateInput, VersionUpdateOutput } from '@boring.tools/schema'
|
|
||||||
import { createRoute, type z } from '@hono/zod-openapi'
|
|
||||||
import { and, eq } from 'drizzle-orm'
|
|
||||||
import { HTTPException } from 'hono/http-exception'
|
|
||||||
|
|
||||||
export const update = createRoute({
|
|
||||||
method: 'put',
|
|
||||||
path: '/:id',
|
|
||||||
request: {
|
|
||||||
body: {
|
|
||||||
content: {
|
|
||||||
'application/json': { schema: VersionUpdateInput },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
responses: {
|
|
||||||
200: {
|
|
||||||
content: {
|
|
||||||
'application/json': { schema: VersionUpdateOutput },
|
|
||||||
},
|
|
||||||
description: 'Return updated version',
|
|
||||||
},
|
|
||||||
400: {
|
|
||||||
description: 'Bad Request',
|
|
||||||
},
|
|
||||||
500: {
|
|
||||||
description: 'Internal Server Error',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
export const updateFunc = async ({
|
|
||||||
userId,
|
|
||||||
payload,
|
|
||||||
id,
|
|
||||||
}: {
|
|
||||||
userId: string
|
|
||||||
payload: z.infer<typeof VersionUpdateInput>
|
|
||||||
id: string
|
|
||||||
}) => {
|
|
||||||
const changelogResult = await db.query.changelog.findMany({
|
|
||||||
where: and(eq(changelog.userId, userId)),
|
|
||||||
with: {
|
|
||||||
versions: {
|
|
||||||
where: eq(changelog_version.id, id),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
const findChangelog = changelogResult.find((change) =>
|
|
||||||
change.versions.find((ver) => ver.id === id),
|
|
||||||
)
|
|
||||||
|
|
||||||
if (!findChangelog?.versions.length) {
|
|
||||||
throw new HTTPException(404, {
|
|
||||||
message: 'Version not found',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const [versionUpdateResult] = await db
|
|
||||||
.update(changelog_version)
|
|
||||||
.set({
|
|
||||||
status: payload.status,
|
|
||||||
markdown: payload.markdown,
|
|
||||||
releasedAt: payload.releasedAt ? new Date(payload.releasedAt) : null,
|
|
||||||
})
|
|
||||||
.where(and(eq(changelog_version.id, id)))
|
|
||||||
.returning()
|
|
||||||
|
|
||||||
return versionUpdateResult
|
|
||||||
}
|
|
@ -3,17 +3,20 @@ import type { Variables } from '..'
|
|||||||
import { verifyAuthentication } from '../utils/authentication'
|
import { verifyAuthentication } from '../utils/authentication'
|
||||||
import { type ContextModule, captureSentry } from '../utils/sentry'
|
import { type ContextModule, captureSentry } from '../utils/sentry'
|
||||||
import ById from './byId'
|
import ById from './byId'
|
||||||
|
import { changelogCommitApi } from './commit'
|
||||||
import Create from './create'
|
import Create from './create'
|
||||||
import Delete from './delete'
|
import Delete from './delete'
|
||||||
import List from './list'
|
import List from './list'
|
||||||
import Update from './update'
|
import Update from './update'
|
||||||
|
import version from './version'
|
||||||
|
|
||||||
const app = new OpenAPIHono<{ Variables: Variables }>()
|
const app = new OpenAPIHono<{ Variables: Variables }>()
|
||||||
|
|
||||||
const module: ContextModule = {
|
const module: ContextModule = {
|
||||||
name: 'changelog',
|
name: 'changelog',
|
||||||
}
|
}
|
||||||
|
app.route('/commit', changelogCommitApi)
|
||||||
|
app.route('/version', version)
|
||||||
app.openapi(ById.route, async (c) => {
|
app.openapi(ById.route, async (c) => {
|
||||||
const userId = verifyAuthentication(c)
|
const userId = verifyAuthentication(c)
|
||||||
try {
|
try {
|
||||||
|
@ -21,7 +21,7 @@ export type Variables = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const app = new OpenAPIHono<{ Variables: Variables }>({
|
export const app = new OpenAPIHono<{ Variables: Variables }>({
|
||||||
defaultHook: handleZodError,
|
// defaultHook: handleZodError,
|
||||||
})
|
})
|
||||||
|
|
||||||
// app.use(
|
// app.use(
|
||||||
@ -36,8 +36,6 @@ app.use('/v1/*', authentication)
|
|||||||
|
|
||||||
app.route('/v1/user', user)
|
app.route('/v1/user', user)
|
||||||
app.route('/v1/changelog', changelog)
|
app.route('/v1/changelog', changelog)
|
||||||
app.route('/v1/changelog/version', version)
|
|
||||||
app.route('/v1/changelog/commit', changelogCommitApi)
|
|
||||||
app.route('/v1/page', pageApi)
|
app.route('/v1/page', pageApi)
|
||||||
|
|
||||||
app.doc('/openapi.json', {
|
app.doc('/openapi.json', {
|
||||||
|
@ -20,6 +20,8 @@ const generatedToken = async (c: Context, next: Next) => {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
console.log(accessTokenResult)
|
||||||
|
|
||||||
if (!accessTokenResult) {
|
if (!accessTokenResult) {
|
||||||
throw new HTTPException(401, { message: 'Unauthorized' })
|
throw new HTTPException(401, { message: 'Unauthorized' })
|
||||||
}
|
}
|
||||||
|
@ -106,6 +106,7 @@ export function handleZodError(
|
|||||||
},
|
},
|
||||||
c: Context,
|
c: Context,
|
||||||
) {
|
) {
|
||||||
|
console.log('asdasasdas')
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
const error = SchemaError.fromZod(result.error, c)
|
const error = SchemaError.fromZod(result.error, c)
|
||||||
return c.json<z.infer<ReturnType<typeof createErrorSchema>>>(
|
return c.json<z.infer<ReturnType<typeof createErrorSchema>>>(
|
||||||
|
@ -2,69 +2,11 @@
|
|||||||
import { parseArgs } from 'node:util'
|
import { parseArgs } from 'node:util'
|
||||||
import semver from 'semver'
|
import semver from 'semver'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import { git_log } from './utils/git_log'
|
import { upload_commits } from './upload_commits'
|
||||||
//import { pushCommits } from './pushCommits'
|
//import { pushCommits } from './pushCommits'
|
||||||
|
import { args } from './utils/arguments'
|
||||||
|
import { git_log } from './utils/git_log'
|
||||||
|
|
||||||
const ENV_VERSION = Bun.env.CHANGELOG_VERSION
|
if (args.success) {
|
||||||
const ENV_ID = Bun.env.CHANGELOG_ID
|
upload_commits(args.data)
|
||||||
const ENV_TOKEN = Bun.env.CHANGELOG_TOKEN
|
|
||||||
|
|
||||||
const schema = z.object({
|
|
||||||
title: z.string().optional(),
|
|
||||||
version: z.string().optional(),
|
|
||||||
token: z.string(),
|
|
||||||
changelogId: z.string(),
|
|
||||||
})
|
|
||||||
export type Arguments = z.infer<typeof schema>
|
|
||||||
|
|
||||||
const { values } = parseArgs({
|
|
||||||
args: Bun.argv,
|
|
||||||
options: {
|
|
||||||
title: {
|
|
||||||
type: 'string',
|
|
||||||
},
|
|
||||||
version: {
|
|
||||||
type: 'string',
|
|
||||||
},
|
|
||||||
token: {
|
|
||||||
type: 'string',
|
|
||||||
},
|
|
||||||
changelogId: {
|
|
||||||
type: 'string',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
strict: true,
|
|
||||||
allowPositionals: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
const mappedArguments = {
|
|
||||||
...values,
|
|
||||||
version: values.version || ENV_VERSION,
|
|
||||||
token: values.token || ENV_TOKEN,
|
|
||||||
changelogId: values.changelogId || ENV_ID,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const args = schema.safeParse(mappedArguments)
|
|
||||||
|
|
||||||
/*if (!args.success) {
|
|
||||||
console.error(
|
|
||||||
`@changelog/cli: Missing arguemnts: ${args.error.errors
|
|
||||||
.map((error) => error.path[0])
|
|
||||||
.join(', ')}`,
|
|
||||||
)
|
|
||||||
process.exit(1)
|
|
||||||
} */
|
|
||||||
|
|
||||||
// const version = semver.coerce(result.data.version);
|
|
||||||
|
|
||||||
// // Check for correct semver
|
|
||||||
// if (!version) {
|
|
||||||
// console.error(
|
|
||||||
// "@changelog/cli: Invalid version. Please provide a valid semver version."
|
|
||||||
// );
|
|
||||||
// process.exit(1);
|
|
||||||
// }
|
|
||||||
|
|
||||||
//pushCommits(args.data)
|
|
||||||
|
|
||||||
git_log('')
|
|
||||||
|
49
apps/cli/src/upload_commits.ts
Normal file
49
apps/cli/src/upload_commits.ts
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import type { Arguments } from './utils/arguments'
|
||||||
|
import { fetchAPI } from './utils/fetch_api'
|
||||||
|
import { git_log } from './utils/git_log'
|
||||||
|
|
||||||
|
const getLastCommitHash = async (args: Arguments) => {
|
||||||
|
const result = await fetchAPI(
|
||||||
|
`/v1/changelog/commit?changelogId=${args.changelogId}&limit=1`,
|
||||||
|
{},
|
||||||
|
args.token,
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(result)) {
|
||||||
|
if (result.length === 0) {
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
return result[0].commit
|
||||||
|
}
|
||||||
|
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
|
||||||
|
export const upload_commits = async (arguemnts: Arguments) => {
|
||||||
|
const hash = await getLastCommitHash(arguemnts)
|
||||||
|
const commits = await git_log(hash)
|
||||||
|
|
||||||
|
if (commits.length === 0) {
|
||||||
|
console.info('No new commits found')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
console.info(`Pushing ${commits.length} commits`)
|
||||||
|
const mappedCommits = commits.map((commit) => ({
|
||||||
|
...commit,
|
||||||
|
changelogId: arguemnts.changelogId,
|
||||||
|
}))
|
||||||
|
|
||||||
|
await fetchAPI(
|
||||||
|
'/v1/changelog/commit',
|
||||||
|
{
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify(mappedCommits),
|
||||||
|
},
|
||||||
|
arguemnts.token,
|
||||||
|
)
|
||||||
|
}
|
42
apps/cli/src/utils/arguments.ts
Normal file
42
apps/cli/src/utils/arguments.ts
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import { parseArgs } from 'node:util'
|
||||||
|
import { z } from 'zod'
|
||||||
|
|
||||||
|
const ENV_ID = Bun.env.CHANGELOG_ID
|
||||||
|
const ENV_TOKEN = Bun.env.AUTH_TOKEN
|
||||||
|
|
||||||
|
const schema = z.object({
|
||||||
|
token: z.string(),
|
||||||
|
changelogId: z.string(),
|
||||||
|
})
|
||||||
|
export type Arguments = z.infer<typeof schema>
|
||||||
|
|
||||||
|
const { values } = parseArgs({
|
||||||
|
args: Bun.argv,
|
||||||
|
options: {
|
||||||
|
token: {
|
||||||
|
type: 'string',
|
||||||
|
},
|
||||||
|
changelogId: {
|
||||||
|
type: 'string',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
strict: true,
|
||||||
|
allowPositionals: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
const mappedArguments = {
|
||||||
|
...values,
|
||||||
|
token: values.token || ENV_TOKEN,
|
||||||
|
changelogId: values.changelogId || ENV_ID,
|
||||||
|
}
|
||||||
|
|
||||||
|
export const args = schema.safeParse(mappedArguments)
|
||||||
|
|
||||||
|
if (!args.success) {
|
||||||
|
console.error(
|
||||||
|
`@changelog/cli: Missing arguemnts: ${args.error.errors
|
||||||
|
.map((error) => error.path[0])
|
||||||
|
.join(', ')}`,
|
||||||
|
)
|
||||||
|
process.exit(1)
|
||||||
|
}
|
27
apps/cli/src/utils/fetch_api.ts
Normal file
27
apps/cli/src/utils/fetch_api.ts
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
const getAPIUrl = () => {
|
||||||
|
if (Bun.env.NODE_ENV === 'development') {
|
||||||
|
return 'http://localhost:3000'
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'https://api.boring.tools'
|
||||||
|
}
|
||||||
|
|
||||||
|
export const fetchAPI = async (
|
||||||
|
url: string,
|
||||||
|
options: RequestInit,
|
||||||
|
token: string,
|
||||||
|
) => {
|
||||||
|
const response = await fetch(`${getAPIUrl()}${url}`, {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
},
|
||||||
|
...options,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! status: ${response.status}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.json()
|
||||||
|
}
|
@ -1,21 +1,19 @@
|
|||||||
const GITFORMAT = `--pretty=format:{%n "commit": "%h",%n "parent": "%p",%n "refs": "%D",%n "subject": "%s",%n "notes": "%N",%n "body": "%b",%n "author": { "name": "%aN", "email": "%aE", "date": "%ad" },%n "commiter": { "name": "%cN", "email": "%cE", "date": "%cd" }%n},`
|
const GITFORMAT =
|
||||||
|
'--pretty=format:{"commit": "%h", "parent": "%p", "refs": "%D", "subject": "%s", "author": { "name": "%aN", "email": "%aE", "date": "%ad" }, "commiter": { "name": "%cN", "email": "%cE", "date": "%cd" }},'
|
||||||
export const git_log = async (
|
export const git_log = async (
|
||||||
from: string | undefined,
|
from: string | undefined,
|
||||||
to = 'HEAD',
|
to = 'HEAD',
|
||||||
): Promise<boolean> => {
|
): Promise<Array<Record<string, unknown>>> => {
|
||||||
// https://git-scm.com/docs/pretty-formats
|
// https://git-scm.com/docs/pretty-formats
|
||||||
const r = await Bun.spawn([
|
const process = Bun.spawn([
|
||||||
'git',
|
'git',
|
||||||
'--no-pager',
|
|
||||||
'log',
|
'log',
|
||||||
`${from ? `${from}...` : ''}${to}`,
|
`${from ? `${from}...` : ''}${to}`,
|
||||||
GITFORMAT,
|
GITFORMAT,
|
||||||
//'--name-status',
|
|
||||||
'--date=iso',
|
'--date=iso',
|
||||||
])
|
])
|
||||||
const text = await new Response(r.stdout).text()
|
|
||||||
// console.log(JSON.parse('[' + text.slice(0, -1) + ']'))
|
const output = await new Response(process.stdout).text()
|
||||||
// const str = JSON.stringify(`[${text.slice(0, -1)}]`)
|
const jsonString = `[${output.slice(0, -1)}]`
|
||||||
// console.log(JSON.parse(str))
|
return JSON.parse(jsonString)
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user