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
Show file tree
Hide file tree
Changes from 6 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
3 changes: 3 additions & 0 deletions components/dashboard/src/images/gitea.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions components/dashboard/src/provider-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import bitbucket from './images/bitbucket.svg';
import github from './images/github.svg';
import gitlab from './images/gitlab.svg';
import gitea from './images/gitea.svg';
import { gitpodHostUrl } from "./service/service";

function iconForAuthProvider(type: string) {
Expand All @@ -17,6 +18,8 @@ function iconForAuthProvider(type: string) {
return <img className="fill-current filter-grayscale w-5 h-5 ml-3 mr-3 my-auto" src={gitlab} />;
case "Bitbucket":
return <img className="fill-current filter-grayscale w-5 h-5 ml-3 mr-3 my-auto" src={bitbucket} />;
case "Gitea":
return <img className="fill-current filter-grayscale w-5 h-5 ml-3 mr-3 my-auto" src={gitea} />;
default:
return <></>;
}
Expand Down
33 changes: 17 additions & 16 deletions components/server/ee/src/auth/host-container-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@ import { injectable, interfaces } from "inversify";
import { HostContainerMapping } from "../../../src/auth/host-container-mapping";
import { gitlabContainerModuleEE } from "../gitlab/container-module";
import { bitbucketContainerModuleEE } from "../bitbucket/container-module";
import { giteaContainerModuleEE } from "../gitea/container-module";

@injectable()
export class HostContainerMappingEE extends HostContainerMapping {
public get(type: string): interfaces.ContainerModule[] | undefined {
let modules = super.get(type) || [];

public get(type: string): interfaces.ContainerModule[] | undefined {
let modules = super.get(type) || [];

switch (type) {
case "GitLab":
return (modules || []).concat([gitlabContainerModuleEE]);
case "Bitbucket":
return (modules || []).concat([bitbucketContainerModuleEE]);
// case "BitbucketServer":
// FIXME
// return (modules || []).concat([bitbucketContainerModuleEE]);
default:
return modules;
}
switch (type) {
case "GitLab":
return (modules || []).concat([gitlabContainerModuleEE]);
case "Bitbucket":
return (modules || []).concat([bitbucketContainerModuleEE]);
case "Gitea":
return (modules || []).concat([giteaContainerModuleEE]);
// case "BitbucketServer":
// FIXME
// return (modules || []).concat([bitbucketContainerModuleEE]);
default:
return modules;
}

}
}
}
4 changes: 4 additions & 0 deletions components/server/ee/src/container-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import { GitLabAppSupport } from "./gitlab/gitlab-app-support";
import { Config } from "../../src/config";
import { SnapshotService } from "./workspace/snapshot-service";
import { BitbucketAppSupport } from "./bitbucket/bitbucket-app-support";
import { GiteaAppSupport } from "./gitea/gitea-app-support";
import { GiteaApp } from "./prebuilds/gitea-app";

export const productionEEContainerModule = new ContainerModule((bind, unbind, isBound, rebind) => {
rebind(Server).to(ServerEE).inSingletonScope();
Expand All @@ -68,6 +70,8 @@ export const productionEEContainerModule = new ContainerModule((bind, unbind, is
bind(GitLabAppSupport).toSelf().inSingletonScope();
bind(BitbucketApp).toSelf().inSingletonScope();
bind(BitbucketAppSupport).toSelf().inSingletonScope();
bind(GiteaApp).toSelf().inSingletonScope();
bind(GiteaAppSupport).toSelf().inSingletonScope();

bind(LicenseEvaluator).toSelf().inSingletonScope();
bind(LicenseKeySource).to(DBLicenseKeySource).inSingletonScope();
Expand Down
13 changes: 13 additions & 0 deletions components/server/ee/src/gitea/container-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
* Licensed under the Gitpod Enterprise Source Code License,
* See License.enterprise.txt in the project root folder.
*/

import { ContainerModule } from "inversify";
import { GiteaService } from "../prebuilds/gitea-service";
import { RepositoryService } from "../../../src/repohost/repo-service";

export const giteaContainerModuleEE = new ContainerModule((_bind, _unbind, _isBound, rebind) => {
rebind(RepositoryService).to(GiteaService).inSingletonScope();
});
58 changes: 58 additions & 0 deletions components/server/ee/src/gitea/gitea-app-support.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
* Licensed under the Gitpod Enterprise Source Code License,
* See License.enterprise.txt in the project root folder.
*/

import { AuthProviderInfo, ProviderRepository, User } from "@gitpod/gitpod-protocol";
import { inject, injectable } from "inversify";
import { TokenProvider } from "../../../src/user/token-provider";
import { UserDB } from "@gitpod/gitpod-db/lib";
import { Gitea } from "../../../src/gitea/api";

@injectable()
export class GiteaAppSupport {

@inject(UserDB) protected readonly userDB: UserDB;
@inject(TokenProvider) protected readonly tokenProvider: TokenProvider;

async getProviderRepositoriesForUser(params: { user: User, provider: AuthProviderInfo }): Promise<ProviderRepository[]> {
const token = await this.tokenProvider.getTokenForHost(params.user, params.provider.host);
const oauthToken = token.value;
const api = Gitea.create(`https://${params.provider.host}`, oauthToken);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two tiny nitpicks, 1. It is possible for gitea to exist on a subpath (eg. https://example.com/gitea/ etc..), and 2. some users may wish not to use https (not a great idea, but one that people make for a variety of reasons).

You may wish instead of host, to use something like baseURL where users can fill the url scheme and full url (incl. a sub-path if they so require).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good points. I will have a look and do some tests later. Currently my main problem is still getting a Gitpod instance with those changes up and running. 🙈

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the issue a matter of compute resources available to you, or being able to apply your changes to a running deploy? If the former, the Gitea project likely can assist in providing compute resources, if the later I'm sure if you post the issue you are running into then someone can help.

Copy link
Author

@anbraten anbraten Mar 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I currently using a cluster on vultr with with some credit I had lying around, so should be fine for the beginning, but I will come back to your offer if needed. Main problem is building images of the needed components and deploying those atm. It requires some manual intervention which loads to a pretty slow dev cycle as I have to rebuild the server, deploy it and look for changes ... The gitpod team seems to use telepresence for that, but I am not quite sure how to integrate that with my own cluster.


const result: ProviderRepository[] = [];
const ownersRepos: ProviderRepository[] = [];

const identity = params.user.identities.find(i => i.authProviderId === params.provider.authProviderId);
if (!identity) {
return result;
}
const usersAccount = identity.authName;

// TODO: check if valid
const projectsWithAccess = await api.user.userCurrentListRepos({ limit: 100 });
for (const project of projectsWithAccess.data) {
const path = project.full_name as string;
const cloneUrl = project.clone_url as string;
const updatedAt = project.updated_at as string;
const accountAvatarUrl = project.owner?.avatar_url as string;
const account = project.owner?.login as string;

(account === usersAccount ? ownersRepos : result).push({
name: project.name as string,
path,
account,
cloneUrl,
updatedAt,
accountAvatarUrl,
// inUse: // todo(at) compute usage via ProjectHooks API
})
}

// put owner's repos first. the frontend will pick first account to continue with
result.unshift(...ownersRepos);
return result;
}

}
86 changes: 86 additions & 0 deletions components/server/ee/src/prebuilds/gitea-app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
* Licensed under the Gitpod Enterprise Source Code License,
* See License.enterprise.txt in the project root folder.
*/

import * as express from 'express';
import { postConstruct, injectable, inject } from 'inversify';
import { ProjectDB, TeamDB, UserDB } from '@gitpod/gitpod-db/lib';
import { Project, User, StartPrebuildResult } from '@gitpod/gitpod-protocol';
import { PrebuildManager } from '../prebuilds/prebuild-manager';
import { TraceContext } from '@gitpod/gitpod-protocol/lib/util/tracing';
import { TokenService } from '../../../src/user/token-service';
import { HostContextProvider } from '../../../src/auth/host-context-provider';
// import { GiteaService } from './gitea-service';

@injectable()
export class GiteaApp {

@inject(UserDB) protected readonly userDB: UserDB;
@inject(PrebuildManager) protected readonly prebuildManager: PrebuildManager;
@inject(TokenService) protected readonly tokenService: TokenService;
@inject(HostContextProvider) protected readonly hostCtxProvider: HostContextProvider;
@inject(ProjectDB) protected readonly projectDB: ProjectDB;
@inject(TeamDB) protected readonly teamDB: TeamDB;

protected _router = express.Router();
public static path = '/apps/gitea/';

@postConstruct()
protected init() {
// TODO
}

protected async findUser(ctx: TraceContext, context: GiteaPushHook, req: express.Request): Promise<User> {
// TODO
return {} as User;
}

protected async handlePushHook(ctx: TraceContext, body: GiteaPushHook, user: User): Promise<StartPrebuildResult | undefined> {
// TODO
return undefined;
}

/**
* Finds the relevant user account and project to the provided webhook event information.
*
* First of all it tries to find the project for the given `cloneURL`, then it tries to
* find the installer, which is also supposed to be a team member. As a fallback, it
* looks for a team member which also has a gitlab.com connection.
*
* @param cloneURL of the webhook event
* @param webhookInstaller the user account known from the webhook installation
* @returns a promise which resolves to a user account and an optional project.
*/
protected async findProjectAndOwner(cloneURL: string, webhookInstaller: User): Promise<{ user: User, project?: Project }> {
// TODO
return {} as { user: User, project?: Project };
}

protected createContextUrl(body: GiteaPushHook) {
// TODO
return {};
}

get router(): express.Router {
return this._router;
}

protected getBranchFromRef(ref: string): string | undefined {
const headsPrefix = "refs/heads/";
if (ref.startsWith(headsPrefix)) {
return ref.substring(headsPrefix.length);
}

return undefined;
}
}

interface GiteaPushHook {
}

interface GiteaRepository {
}

interface GiteaProject {}
15 changes: 15 additions & 0 deletions components/server/ee/src/prebuilds/gitea-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
* Licensed under the Gitpod Enterprise Source Code License,
* See License.enterprise.txt in the project root folder.
*/

import { RepositoryService } from "../../../src/repohost/repo-service";
import { inject, injectable } from "inversify";
import { GiteaRestApi } from "../../../src/gitea/api";

@injectable()
export class GiteaService extends RepositoryService {
@inject(GiteaRestApi) protected readonly giteaApi: GiteaRestApi;
// TODO: complete?
}
2 changes: 2 additions & 0 deletions components/server/ee/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import { log } from '@gitpod/gitpod-protocol/lib/util/logging';
import { GitLabApp } from './prebuilds/gitlab-app';
import { BitbucketApp } from './prebuilds/bitbucket-app';
import { GithubApp } from './prebuilds/github-app';
import { GiteaApp } from './prebuilds/gitea-app';
import { SnapshotService } from './workspace/snapshot-service';

export class ServerEE<C extends GitpodClient, S extends GitpodServer> extends Server<C, S> {
@inject(GithubApp) protected readonly githubApp: GithubApp;
@inject(GitLabApp) protected readonly gitLabApp: GitLabApp;
@inject(BitbucketApp) protected readonly bitbucketApp: BitbucketApp;
@inject(GiteaApp) protected readonly giteaApp: GiteaApp;
@inject(SnapshotService) protected readonly snapshotService: SnapshotService;

public async init(app: express.Application) {
Expand Down
1 change: 1 addition & 0 deletions components/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"express-mysql-session": "^2.1.0",
"express-session": "^1.15.6",
"fs-extra": "^10.0.0",
"gitea-js": "^1.1.0",
"google-protobuf": "^3.18.0-rc.2",
"heapdump": "^0.3.15",
"inversify": "^5.0.1",
Expand Down
3 changes: 2 additions & 1 deletion components/server/src/auth/auth-provider-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Config } from "../config";
import { v4 as uuidv4 } from 'uuid';
import { oauthUrls as githubUrls } from "../github/github-urls";
import { oauthUrls as gitlabUrls } from "../gitlab/gitlab-urls";
import { oauthUrls as giteaUrls } from "../gitea/gitea-urls";
import { log } from '@gitpod/gitpod-protocol/lib/util/logging';

@injectable()
Expand Down Expand Up @@ -107,7 +108,7 @@ export class AuthProviderService {
}
protected initializeNewProvider(newEntry: AuthProviderEntry.NewEntry): AuthProviderEntry {
const { host, type, clientId, clientSecret } = newEntry;
const urls = type === "GitHub" ? githubUrls(host) : (type === "GitLab" ? gitlabUrls(host) : undefined);
const urls = type === "GitHub" ? githubUrls(host) : (type === "GitLab" ? gitlabUrls(host) : (type === "Gitea" ? giteaUrls(host) : undefined));
if (!urls) {
throw new Error("Unexpected service type.");
}
Expand Down
37 changes: 19 additions & 18 deletions components/server/src/auth/host-container-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,27 @@ import { githubContainerModule } from "../github/github-container-module";
import { gitlabContainerModule } from "../gitlab/gitlab-container-module";
import { genericAuthContainerModule } from "./oauth-container-module";
import { bitbucketContainerModule } from "../bitbucket/bitbucket-container-module";
import { giteaContainerModule } from "../gitea/gitea-container-module";
import { bitbucketServerContainerModule } from "../bitbucket-server/bitbucket-server-container-module";

@injectable()
export class HostContainerMapping {

public get(type: string): interfaces.ContainerModule[] | undefined {
switch (type) {
case "GitHub":
return [githubContainerModule];
case "GitLab":
return [gitlabContainerModule];
case "OAuth":
return [genericAuthContainerModule];
case "Bitbucket":
return [bitbucketContainerModule];
case "BitbucketServer":
return [bitbucketServerContainerModule];
default:
return undefined;
}
public get(type: string): interfaces.ContainerModule[] | undefined {
switch (type) {
case "GitHub":
return [githubContainerModule];
case "GitLab":
return [gitlabContainerModule];
case "OAuth":
return [genericAuthContainerModule];
case "Bitbucket":
return [bitbucketContainerModule];
case "Gitea":
return [giteaContainerModule];
case "BitbucketServer":
return [bitbucketServerContainerModule];
default:
return undefined;
}

}
}
}
Loading