-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
anbraten
wants to merge
23
commits into
gitpod-io:main
Choose a base branch
from
anbraten:gitea
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,027
−5
Draft
Gitea support #8131
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
fb49a9e
first attempt for Gitea
anbraten 1900190
adjust gitea api
anbraten 86ba2fd
adjust to use gitea api
anbraten 9fa680e
update gitea-js
anbraten e505726
replace gitlab names with gitea
anbraten 49ed017
Merge remote-tracking branch 'upstream/main' into gitea
anbraten 7ae3857
further dashboard adjustments
anbraten 5f50678
fix option value
anbraten 3ff0f29
nits
anbraten 1d75b9d
update gitea-js
anbraten bd3bc15
fix login
anbraten 45fec61
Merge remote-tracking branch 'upstream/main' into gitea
anbraten 590ea08
Merge remote-tracking branch 'upstream/main' into gitea
anbraten f1bb3ca
improve context parser
anbraten b093367
Merge remote-tracking branch 'upstream/main' into gitea
anbraten ad2d2b2
format code
ghuntley b5ab0c9
Merge remote-tracking branch 'upstream/main' into gitea
anbraten 944637a
Merge remote-tracking branch 'upstream/main' into gitea
anbraten 9018b84
fix lockfile
anbraten de29566
fix some context parser tests
anbraten dc3fdc8
improve error handling
anbraten 10a6b40
fix issue context
anbraten 27da032
Merge remote-tracking branch 'upstream/main' into gitea
anbraten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,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(); | ||
}); |
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,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); | ||
|
||
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; | ||
} | ||
|
||
} |
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
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
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,105 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* 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 { 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; | ||
constructor(msg?: string, httpError?: any) { | ||
super(msg); | ||
this.httpError = httpError; | ||
this.name = "GiteaApiError"; | ||
} | ||
} | ||
export namespace ApiError { | ||
export function is(something: any): something is ApiError { | ||
return !!something && something.name === "GiteaApiError"; | ||
} | ||
export function isNotFound(error: ApiError): boolean { | ||
return !!error.httpError?.description.startsWith("404"); | ||
} | ||
export function isInternalServerError(error: ApiError): boolean { | ||
return !!error.httpError?.description.startsWith("500"); | ||
} | ||
} | ||
|
||
export function create(host: string, token: string) { | ||
return giteaApi(`https://${host}`, { | ||
customFetch: fetch, | ||
token, | ||
}); | ||
} | ||
|
||
export type Commit = ICommit; | ||
export type Repository = IRepository; | ||
export type ContentsResponse = IContentsResponse; | ||
export type Branch = IBranch; | ||
export type Tag = ITag; | ||
export type PullRequest = IPullRequest; | ||
export type Issue = IIssue; | ||
export type User = IUser; | ||
} | ||
|
||
@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") { | ||
oauthToken = userOrToken; | ||
} else { | ||
const giteaToken = await this.tokenHelper.getTokenWithScopes(userOrToken, GiteaScope.Requirements.DEFAULT); | ||
oauthToken = giteaToken.value; | ||
} | ||
const api = Gitea.create(this.config.host, oauthToken); | ||
return api; | ||
} | ||
|
||
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; | ||
return response as R; | ||
} catch (error) { | ||
if (error && error?.type === "system") { | ||
return new Gitea.ApiError(`Gitea Fetch Error: ${error?.message}`, error); | ||
} | ||
if (error?.error && !error?.data && error?.error?.errors) { | ||
return new Gitea.ApiError(`Gitea Api Error: ${error?.error?.message}`, error?.error); | ||
} | ||
|
||
// log.error(`Gitea request error`, error); | ||
throw error; | ||
} finally { | ||
log.info(`Gitea request took ${new Date().getTime() - before} ms`); | ||
} | ||
} | ||
} |
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,34 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License-AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { Repository } from "@gitpod/gitpod-protocol"; | ||
import { RepoURL } from "../repohost"; | ||
import { Gitea } from "./api"; | ||
|
||
export function convertRepo(repo: Gitea.Repository): Repository | undefined { | ||
if (!repo.clone_url || !repo.name || !repo.owner?.login) { | ||
return undefined; | ||
} | ||
|
||
const host = RepoURL.parseRepoUrl(repo.clone_url)!.host; | ||
|
||
return { | ||
host, | ||
owner: repo.owner.login, | ||
name: repo.name, | ||
cloneUrl: repo.clone_url, | ||
description: repo.description, | ||
avatarUrl: repo.avatar_url, | ||
webUrl: repo.html_url, // TODO: is this correct? | ||
defaultBranch: repo.default_branch, | ||
private: repo.private, | ||
fork: undefined, // TODO: load fork somehow | ||
}; | ||
} | ||
|
||
// export function convertBranch(repo: Gitea.Repository): Branch | undefined { | ||
|
||
// } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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).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.
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. 🙈
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.
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.
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.
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.