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

Gitea support #8131

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix some context parser tests
  • Loading branch information
anbraten committed Jun 4, 2022
commit de295669343edf6c74e5b52f683246598bc41b1e
2 changes: 1 addition & 1 deletion components/server/src/dev/dev-data.ts
Original file line number Diff line number Diff line change
@@ -77,7 +77,7 @@ export namespace DevData {
export function createGiteaTestToken(): Token {
return {
...getTokenFromEnv("GITPOD_TEST_TOKEN_GITEA"),
scopes: []
scopes: [],
};
}

45 changes: 29 additions & 16 deletions components/server/src/gitea/api.ts
Original file line number Diff line number Diff line change
@@ -4,28 +4,39 @@
* See License-AGPL.txt in the project root for license information.
*/

import { giteaApi, Api, Commit as ICommit, Repository as IRepository, ContentsResponse as IContentsResponse, Branch as IBranch, Tag as ITag, PullRequest as IPullRequest, Issue as IIssue, User as IUser } from "gitea-js"
import fetch from 'cross-fetch'
import {
giteaApi,
Api,
Commit as ICommit,
Repository as IRepository,
ContentsResponse as IContentsResponse,
Branch as IBranch,
Tag as ITag,
PullRequest as IPullRequest,
Issue as IIssue,
User as IUser,
} from "gitea-js";
import fetch from "cross-fetch";

import { User } from "@gitpod/gitpod-protocol"
import { injectable, inject } from 'inversify';
import { log } from '@gitpod/gitpod-protocol/lib/util/logging';
import { GiteaScope } from './scopes';
import { AuthProviderParams } from '../auth/auth-provider';
import { GiteaTokenHelper } from './gitea-token-helper';
import { User } from "@gitpod/gitpod-protocol";
import { injectable, inject } from "inversify";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
import { GiteaScope } from "./scopes";
import { AuthProviderParams } from "../auth/auth-provider";
import { GiteaTokenHelper } from "./gitea-token-helper";

export namespace Gitea {
export class ApiError extends Error {
readonly httpError: { name: string, description: string } | undefined;
readonly httpError: { name: string; description: string } | undefined;
constructor(msg?: string, httpError?: any) {
super(msg);
this.httpError = httpError;
this.name = 'GiteaApiError';
this.name = "GiteaApiError";
}
}
export namespace ApiError {
export function is(something: any): something is ApiError {
return !!something && something.name === 'GiteaApiError';
return !!something && something.name === "GiteaApiError";
}
export function isNotFound(error: ApiError): boolean {
return !!error.httpError?.description.startsWith("404");
@@ -54,12 +65,11 @@ export namespace Gitea {

@injectable()
export class GiteaRestApi {

@inject(AuthProviderParams) readonly config: AuthProviderParams;
@inject(GiteaTokenHelper) protected readonly tokenHelper: GiteaTokenHelper;
protected async create(userOrToken: User | string) {
let oauthToken: string | undefined;
if (typeof userOrToken === 'string') {
if (typeof userOrToken === "string") {
oauthToken = userOrToken;
} else {
const giteaToken = await this.tokenHelper.getTokenWithScopes(userOrToken, GiteaScope.Requirements.DEFAULT);
@@ -69,11 +79,14 @@ export class GiteaRestApi {
return api;
}

public async run<R>(userOrToken: User | string, operation: (g: Api<unknown>) => Promise<any>): Promise<R | Gitea.ApiError> {
public async run<R>(
userOrToken: User | string,
operation: (g: Api<unknown>) => Promise<any>,
): Promise<R | Gitea.ApiError> {
const before = new Date().getTime();
const userApi = await this.create(userOrToken);
try {
const response = (await operation(userApi) as R);
const response = (await operation(userApi)) as R;
return response as R;
} catch (error) {
if (error && typeof error?.response?.status === "number" && error?.response?.status !== 200) {
@@ -95,4 +108,4 @@ export class GiteaRestApi {
log.info(`Gitea request took ${new Date().getTime() - before} ms`);
}
}
}
}
Loading
Oops, something went wrong.