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

feat(command): add gitea and forgejo upgrade provider #710

Draft
wants to merge 57 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
e79c45b
chore: :package: add jsr module metadatas
JOTSR Feb 29, 2024
4e104e5
refactor: :package: replace remote url imports by jsr or npm
JOTSR Feb 29, 2024
6daec80
fix: :wrench: prefix exports with `./`
JOTSR Feb 29, 2024
d6af963
fix: :package: remove `/mod` from `@std/path`
JOTSR Feb 29, 2024
564d1bf
refactor: :label: fix all slow types for jsr exports
JOTSR Feb 29, 2024
3fe231a
chore(upgrade): :arrow_up: deno/[email protected]
JOTSR Feb 29, 2024
eb72f6b
ci: add jsr publish workflow
JOTSR Feb 29, 2024
5cec103
style: run deno fmt on deno.jsonc
JOTSR Mar 8, 2024
ac01ba4
refactor: :truck: rename cliffy scope
JOTSR Mar 8, 2024
cc44644
refactor: :recycle: split cliffy in submodules with deno workspaces
JOTSR Mar 8, 2024
b7c5a7d
refactor: :label: simplify literals types to string
JOTSR Mar 8, 2024
aebf585
refactor: :recycle: replace `deps`/`dev_deps` by direct imports
JOTSR Mar 8, 2024
be0f0d2
ci: publish on release and dry run on main push
JOTSR Mar 8, 2024
587cfd1
fix: update missing import
JOTSR Mar 8, 2024
df78f58
refactor: dissociate examples imports
JOTSR Mar 13, 2024
f06d9c1
fix: :package: remove `examples` from workspace
JOTSR Mar 14, 2024
706e2c9
style: :art: apply `deno fmt` to harmonize sources
JOTSR Mar 14, 2024
3513bf4
fix cross package imports
c4spar Mar 17, 2024
2a8f296
fix: :construction_worker: `deno.jsonc` not loaded during tests in `s…
JOTSR Mar 18, 2024
1008ac2
Merge branch 'main' of github.com:JOTSR/deno-cliffy
JOTSR Mar 18, 2024
31c9d73
style: fix fmt
JOTSR Mar 18, 2024
766634a
Merge remote-tracking branch 'origin/main' into JOTSR-main
c4spar Mar 18, 2024
eb19021
Merge remote-tracking branch 'origin/main' into JOTSR-main
c4spar Mar 18, 2024
269648c
import ValidationError from @cliffy/flags
c4spar Mar 18, 2024
cd621ec
Merge remote-tracking branch 'origin/main' into JOTSR-main
c4spar Mar 18, 2024
0c34bbd
refactor: :recycle: don't force `deno.jsonc` prefers default options
JOTSR Mar 19, 2024
af3d77f
Merge branch 'main' of github.com:JOTSR/deno-cliffy
JOTSR Mar 19, 2024
3016d1f
remove examples/deno.json
c4spar Mar 19, 2024
820c260
Merge remote-tracking branch 'origin/main' into JOTSR-main
c4spar Mar 19, 2024
87a954b
fix imports
c4spar Mar 19, 2024
d7e3de6
fix test imports
c4spar Mar 19, 2024
0e6ce61
remove config option
c4spar Mar 19, 2024
1b69821
merge main branch
c4spar Apr 7, 2024
ef3046f
fmt
c4spar Apr 7, 2024
6c5bd08
fix
c4spar Apr 7, 2024
2d306d1
Merge remote-tracking branch 'origin/main' into JOTSR-main
c4spar Apr 7, 2024
bfe0dde
fix import
c4spar Apr 7, 2024
297574f
remove name and version from main deno.jsonc
c4spar Apr 9, 2024
9a7ebae
remove submodule exports from command/mod.ts
c4spar May 5, 2024
00397c2
add exports to ansi/deno.json
c4spar May 5, 2024
ee17e96
fix imports
c4spar May 5, 2024
7e4f76e
add workaround for workspace issue
c4spar May 5, 2024
c4171e5
fix examples
c4spar May 5, 2024
87b3a95
debug
c4spar May 5, 2024
ae2c9d8
debug
c4spar May 5, 2024
bd469ec
debug
c4spar May 5, 2024
df989de
debug
c4spar May 5, 2024
2e442fd
debug
c4spar May 5, 2024
682ccfd
debug
c4spar May 5, 2024
058d093
debug
c4spar May 5, 2024
463c41e
debug
c4spar May 5, 2024
e5d618e
adapt ansi exports
c4spar May 5, 2024
6ac036c
adapt publish workflow
c4spar May 5, 2024
1a0a3a4
cleanup
c4spar May 5, 2024
05e84b0
Merge branch 'main' of github.com:c4spar/deno-cliffy
JOTSR May 10, 2024
3d3a770
Merge branch 'main' of github.com:c4spar/deno-cliffy
JOTSR May 31, 2024
05581e7
feat(command): :sparkles: add `gitea` and `forgejo` upgrade provider
JOTSR May 31, 2024
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
187 changes: 187 additions & 0 deletions command/upgrade/provider/gitea.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
import { Provider, type Versions } from "../provider.ts";
import { bold, brightBlue } from "@std/fmt/colors";

export interface GiteaProviderOptions {
registryUrl: string;
repository: string;
branches?: boolean;
token?: string;
}

export interface GiteaVersions extends Versions {
tags: Array<string>;
branches: Array<string>;
}

export class GiteaProvider extends Provider {
name = "gitea";
private readonly registryUrl: string;
private readonly repositoryName: string;
private readonly listBranches?: boolean;
private readonly giteaToken?: string;

constructor({ registryUrl, repository, branches = true, token }: GiteaProviderOptions) {
super();
this.registryUrl = registryUrl
this.repositoryName = repository;
this.listBranches = branches;
this.giteaToken = token;
}

async getVersions(
_name: string,
): Promise<GiteaVersions> {
//!TODO
const [tags, branches] = await Promise.all([
this.gitFetch<GiteaRepoTag[]>("tags"),
this.gitFetch<GiteaRepoBranch[]>("branches"),
]);

const tagNames = tags.map(tag => tag.name).toReversed()

const branchNames = branches
.sort((a, b) =>
(a.protected === b.protected) ? 0 : (a.protected ? 1 : -1)
)
.map((tag) =>
tag.protected ? `${tag.name} (${bold("Protected")})` : tag.name
)
.reverse();

return {
versions: [
...tagNames,
...branchNames,
],
latest: tagNames[0],
tags: tagNames,
branches: branchNames,
};
}

getRepositoryUrl(_name: string): string {
return new URL(this.repositoryName, this.registryUrl).href;
}

getRegistryUrl(_name: string, version: string): string {
return new URL(`${this.repositoryName}/src/tag/${version}`, this.registryUrl).href;
}

async listVersions(name: string, currentVersion?: string): Promise<void> {
//!TODO
const { tags, branches } = await this.getVersions(name);
const showBranches: boolean = !!this.listBranches && branches.length > 0;
const indent = showBranches ? 2 : 0;
if (showBranches) {
console.log("\n" + " ".repeat(indent) + bold(brightBlue("Tags:\n")));
}
super.printVersions(tags, currentVersion, { indent });
if (showBranches) {
console.log("\n" + " ".repeat(indent) + bold(brightBlue("Branches:\n")));
super.printVersions(branches, currentVersion, { maxCols: 5, indent });
console.log();
}
}

private getApiUrl(endpoint: string): string {
return new URL(`api/v1/repos/${this.repositoryName}/${endpoint}`, this.registryUrl).href;
}

private async gitFetch<T>(endpoint: string): Promise<T> {
const headers = new Headers({ "Content-Type": "application/json" });
if (this.giteaToken) {
headers.set(
"Authorization",
this.giteaToken ? `token ${this.giteaToken}` : "",
);
}
const response = await fetch(
this.getApiUrl(endpoint),
{
method: "GET",
cache: "default",
headers,
},
);

if (!response.status) {
throw new Error(
"couldn't fetch versions - try again after sometime",
);
}

const data: GiteaResponse & T = await response.json();

//!TODO check error message
if (
typeof data === "object" && "message" in data &&
"documentation_url" in data
) {
throw new Error(data.message + " " + data.documentation_url);
}

return data;
}
}

interface GiteaResponse {
//!TODO check response format
message: string;
// deno-lint-ignore camelcase
documentation_url: string;
}

interface GiteaRepoTag {
//!TODO improve types
name: string
message: string
id: string //sha1 (commit.sha)
commit: {
url: string //url
sha: string //sha1
created: string //ISO
}
zipball_url: string //url
tarball_url: string //url
}

interface GiteaRepoBranch {
commit: {
author: {
email: string
name: string
username: string
committer: {
email: string
name: string
username: string
}
id: string //commit sha1
url: string //url
timestamp: string //ISO
message: string
added: string[] | null
modified: string[] | null
removed: string[] | null
verification: {
payload: string
reason: string
signature: string
verified: boolean
signer: {
email: string
name: string
username: string
} | null
}
}
}
effective_branch_protection_name: string
enable_status_check: boolean
name: string
protected: boolean
required_approvals: number
status_check_contexts: string[]
user_can_merge: boolean
user_can_push: boolean
}
Loading