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

[server][github] fix file provider for self-managed GHE #13108

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion components/server/src/github/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export class GitHubRestApi {
}

protected get userAgent() {
return new URL(this.config.oauth!.callBackUrl).hostname;
return (this.config.oauth && new URL(this.config.oauth?.callBackUrl)?.hostname) || "GitPod unknown";
}

/**
Expand Down
30 changes: 21 additions & 9 deletions components/server/src/github/file-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import { injectable, inject } from "inversify";

import { FileProvider, MaybeContent } from "../repohost/file-provider";
import { Commit, User, Repository } from "@gitpod/gitpod-protocol";
import { GitHubGraphQlEndpoint, GitHubRestApi } from "./api";
import { GitHubRestApi } from "./api";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";

@injectable()
export class GithubFileProvider implements FileProvider {
@inject(GitHubGraphQlEndpoint) protected readonly githubGraphQlApi: GitHubGraphQlEndpoint;
@inject(GitHubRestApi) protected readonly githubApi: GitHubRestApi;

public async getGitpodFileContent(commit: Commit, user: User): Promise<MaybeContent> {
Expand Down Expand Up @@ -56,14 +55,27 @@ export class GithubFileProvider implements FileProvider {
}

try {
const contents = await this.githubGraphQlApi.getFileContents(
user,
commit.repository.owner,
commit.repository.name,
commit.revision,
path,
const response = await this.githubApi.run(user, (api) =>
api.repos.getContent({
owner: commit.repository.owner,
repo: commit.repository.name,
path,
ref: commit.revision,
headers: {
accept: "application/vnd.github.VERSION.raw",
},
}),
);
return contents;
if (response.status === 200) {
if (typeof response.data === "string") {
return response.data;
}
console.warn("GithubFileProvider.getFileContent – unexpected response type.", {
headers: response.headers,
type: typeof response.data,
});
}
return undefined;
} catch (err) {
log.debug(err);
}
Expand Down
74 changes: 74 additions & 0 deletions components/server/src/github/github-file-provider.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* 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 { User } from "@gitpod/gitpod-protocol";
import { skipIfEnvVarNotSet } from "@gitpod/gitpod-protocol/lib/util/skip-if";
import { expect } from "chai";
import { Container, ContainerModule } from "inversify";
import { suite, retries, test, timeout } from "mocha-typescript";
import { AuthProviderParams } from "../auth/auth-provider";
import { DevData } from "../dev/dev-data";
import { TokenProvider } from "../user/token-provider";
import { GitHubRestApi } from "./api";

import { GithubFileProvider } from "./file-provider";
import { GitHubTokenHelper } from "./github-token-helper";

@suite(timeout(10000), retries(2), skipIfEnvVarNotSet("GITPOD_TEST_TOKEN_GITHUB"))
class TestFileProvider {
static readonly AUTH_HOST_CONFIG: Partial<AuthProviderParams> = {
id: "Public-GitHub",
type: "GitHub",
verified: true,
description: "",
icon: "",
host: "github.com",
};

protected fileProvider: GithubFileProvider;
protected user: User;
protected container: Container;

public before() {
this.container = new Container();
this.container.load(
new ContainerModule((bind, unbind, isBound, rebind) => {
bind(GitHubRestApi).toSelf().inSingletonScope();
bind(AuthProviderParams).toConstantValue(TestFileProvider.AUTH_HOST_CONFIG);
bind(GitHubTokenHelper).toSelf().inSingletonScope();
bind(TokenProvider).toConstantValue(<TokenProvider>{
getTokenForHost: async () => DevData.createGitHubTestToken(),
getFreshPortAuthenticationToken: async (user: User, workspaceId: string) =>
DevData.createPortAuthTestToken(workspaceId),
});
bind(GithubFileProvider).toSelf().inSingletonScope();
}),
);
this.fileProvider = this.container.get(GithubFileProvider);
this.user = DevData.createTestUser();
}

@test public async testFileContent() {
const result = await this.fileProvider.getFileContent(
{
repository: {
owner: "gitpod-io",
name: "gitpod",
host: "github.com",
cloneUrl: "unused in test",
},
revision: "af51739d341bb2245598e275336ae9f730e3b41a",
},
this.user,
"License.txt",
);
expect(result).to.not.be.undefined;
expect(result).to.contain(`To determine under which license you may use a file from the Gitpod source code,
please resort to the header of that file.`);
}
}

module.exports = new TestFileProvider();