-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from tranzystorekk/codeberg-endpoints
Add Codeberg endpoints
- Loading branch information
Showing
7 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
badgers-web/src/app/codeberg/closed-issues/[owner]/[repo]/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { NextRequest } from "next/server" | ||
|
||
import Badge from '@/utils/Badge' | ||
import Codeberg from '@/utils/Codeberg' | ||
|
||
interface Params { | ||
params: { | ||
owner: string | ||
repo: string | ||
} | ||
} | ||
|
||
export async function GET(request: NextRequest, { params: { owner, repo } }: Params) { | ||
const closedIssuesCount = await Codeberg.getClient().getIssuesCount({ owner, repo }, { type: 'issues', state: 'closed' }) | ||
|
||
return await Badge.generate(request, 'closed issues', closedIssuesCount?.toString() ?? 'None') | ||
} | ||
|
||
export const runtime = 'edge' |
19 changes: 19 additions & 0 deletions
19
badgers-web/src/app/codeberg/issues/[owner]/[repo]/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { NextRequest } from "next/server" | ||
|
||
import Badge from '@/utils/Badge' | ||
import Codeberg from '@/utils/Codeberg' | ||
|
||
interface Params { | ||
params: { | ||
owner: string | ||
repo: string | ||
} | ||
} | ||
|
||
export async function GET(request: NextRequest, { params: { owner, repo } }: Params) { | ||
const issuesCount = await Codeberg.getClient().getIssuesCount({ owner, repo }, { type: 'issues', state: 'all' }) | ||
|
||
return await Badge.generate(request, 'issues', issuesCount?.toString() ?? 'None') | ||
} | ||
|
||
export const runtime = 'edge' |
19 changes: 19 additions & 0 deletions
19
badgers-web/src/app/codeberg/open-issues/[owner]/[repo]/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { NextRequest } from "next/server" | ||
|
||
import Badge from '@/utils/Badge' | ||
import Codeberg from '@/utils/Codeberg' | ||
|
||
interface Params { | ||
params: { | ||
owner: string | ||
repo: string | ||
} | ||
} | ||
|
||
export async function GET(request: NextRequest, { params: { owner, repo } }: Params) { | ||
const openIssuesCount = await Codeberg.getClient().getIssuesCount({ owner, repo }, { type: 'issues', state: 'open' }) | ||
|
||
return await Badge.generate(request, 'open issues', openIssuesCount?.toString() ?? 'None') | ||
} | ||
|
||
export const runtime = 'edge' |
24 changes: 24 additions & 0 deletions
24
badgers-web/src/app/codeberg/release/[owner]/[repo]/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { NextRequest } from 'next/server' | ||
|
||
import Badge from '@/utils/Badge' | ||
import Codeberg from '@/utils/Codeberg' | ||
|
||
interface Params { | ||
params: { | ||
owner: string | ||
repo: string | ||
} | ||
} | ||
|
||
export async function GET(request: NextRequest, { params: { owner, repo } }: Params) { | ||
const release = await Codeberg.getClient().getLatestRelease({ owner, repo }) | ||
const shortestName = [release?.tag_name, release?.name] | ||
.filter(Boolean) | ||
.reduce((a, b) => a!.length < b!.length ? a : b) | ||
|
||
return await Badge.generate(request, 'release', shortestName ?? 'None', { | ||
color: !!shortestName ? 'blue' : 'yellow' | ||
}) | ||
} | ||
|
||
export const runtime = 'edge' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const API_BASE = 'https://codeberg.org/api/v1'; | ||
|
||
type ProjectInfo = { | ||
owner: string | ||
repo: string | ||
} | ||
|
||
type Repository = { | ||
id: number | ||
default_branch: string | ||
name: string | ||
forks_count: number | ||
stars_count: number | ||
} | ||
|
||
type Release = { | ||
name: string | ||
tag_name: string | ||
} | ||
|
||
class CodebergClient { | ||
token: string | ||
|
||
constructor(token: string) { | ||
this.token = token | ||
} | ||
|
||
buildUrl(path: string, query: Record<string, any> = {}): string { | ||
const queryArgs = { | ||
...query, | ||
token: this.token, | ||
} | ||
const queryString = Object | ||
.entries(queryArgs) | ||
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`) | ||
.join('&') | ||
|
||
return `${API_BASE}/${path}?${queryString}` | ||
} | ||
|
||
async getRepository({ owner, repo }: ProjectInfo): Promise<Repository | null> { | ||
const repoId = `${owner}/${repo}` | ||
const url = this.buildUrl(`repos/${repoId}`) | ||
const resp = await fetch(url) | ||
|
||
if (resp.status !== 200) return null | ||
return await resp.json() as Repository | ||
} | ||
|
||
async getIssuesCount({ owner, repo }: ProjectInfo, query: Record<string, any> = {}): Promise<Number | null> { | ||
const repoId = `${owner}/${repo}` | ||
const url = this.buildUrl(`repos/${repoId}/issues`, query) | ||
const resp = await fetch(url) | ||
|
||
if (resp.status !== 200) return null | ||
const count = resp.headers.get('x-total-count') | ||
return Number(count) | ||
} | ||
|
||
async getLatestRelease({ owner, repo }: ProjectInfo): Promise<Release | null> { | ||
const repoId = `${owner}/${repo}` | ||
const url = this.buildUrl(`repos/${repoId}/releases/latest`) | ||
const resp = await fetch(url) | ||
|
||
if (resp.status !== 200) return null | ||
return await resp.json() as Release | ||
} | ||
} | ||
|
||
export default class Codeberg { | ||
static getClient(): CodebergClient { | ||
return new CodebergClient(process.env.CODEBERG_TOKEN as string) | ||
} | ||
} |
a186a1f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
spacebadgers – ./
spacebadgers-splittydev-s-team.vercel.app
badgers-ebon.vercel.app
spacebadgers-git-main-splittydev-s-team.vercel.app
badgers.space
www.badgers.space