Skip to content

Commit f28e996

Browse files
AlexTugarevroboquat
authored andcommitted
[server][github] fix file provider for self-managed GHE
1 parent fb4d2e7 commit f28e996

File tree

3 files changed

+96
-10
lines changed

3 files changed

+96
-10
lines changed

components/server/src/github/api.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export class GitHubRestApi {
156156
}
157157

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

162162
/**

components/server/src/github/file-provider.ts

+21-9
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import { injectable, inject } from "inversify";
88

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

1414
@injectable()
1515
export class GithubFileProvider implements FileProvider {
16-
@inject(GitHubGraphQlEndpoint) protected readonly githubGraphQlApi: GitHubGraphQlEndpoint;
1716
@inject(GitHubRestApi) protected readonly githubApi: GitHubRestApi;
1817

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

5857
try {
59-
const contents = await this.githubGraphQlApi.getFileContents(
60-
user,
61-
commit.repository.owner,
62-
commit.repository.name,
63-
commit.revision,
64-
path,
58+
const response = await this.githubApi.run(user, (api) =>
59+
api.repos.getContent({
60+
owner: commit.repository.owner,
61+
repo: commit.repository.name,
62+
path,
63+
ref: commit.revision,
64+
headers: {
65+
accept: "application/vnd.github.VERSION.raw",
66+
},
67+
}),
6568
);
66-
return contents;
69+
if (response.status === 200) {
70+
if (typeof response.data === "string") {
71+
return response.data;
72+
}
73+
console.warn("GithubFileProvider.getFileContent – unexpected response type.", {
74+
headers: response.headers,
75+
type: typeof response.data,
76+
});
77+
}
78+
return undefined;
6779
} catch (err) {
6880
log.debug(err);
6981
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License-AGPL.txt in the project root for license information.
5+
*/
6+
7+
import { User } from "@gitpod/gitpod-protocol";
8+
import { skipIfEnvVarNotSet } from "@gitpod/gitpod-protocol/lib/util/skip-if";
9+
import { expect } from "chai";
10+
import { Container, ContainerModule } from "inversify";
11+
import { suite, retries, test, timeout } from "mocha-typescript";
12+
import { AuthProviderParams } from "../auth/auth-provider";
13+
import { DevData } from "../dev/dev-data";
14+
import { TokenProvider } from "../user/token-provider";
15+
import { GitHubRestApi } from "./api";
16+
17+
import { GithubFileProvider } from "./file-provider";
18+
import { GitHubTokenHelper } from "./github-token-helper";
19+
20+
@suite(timeout(10000), retries(2), skipIfEnvVarNotSet("GITPOD_TEST_TOKEN_GITHUB"))
21+
class TestFileProvider {
22+
static readonly AUTH_HOST_CONFIG: Partial<AuthProviderParams> = {
23+
id: "Public-GitHub",
24+
type: "GitHub",
25+
verified: true,
26+
description: "",
27+
icon: "",
28+
host: "github.com",
29+
};
30+
31+
protected fileProvider: GithubFileProvider;
32+
protected user: User;
33+
protected container: Container;
34+
35+
public before() {
36+
this.container = new Container();
37+
this.container.load(
38+
new ContainerModule((bind, unbind, isBound, rebind) => {
39+
bind(GitHubRestApi).toSelf().inSingletonScope();
40+
bind(AuthProviderParams).toConstantValue(TestFileProvider.AUTH_HOST_CONFIG);
41+
bind(GitHubTokenHelper).toSelf().inSingletonScope();
42+
bind(TokenProvider).toConstantValue(<TokenProvider>{
43+
getTokenForHost: async () => DevData.createGitHubTestToken(),
44+
getFreshPortAuthenticationToken: async (user: User, workspaceId: string) =>
45+
DevData.createPortAuthTestToken(workspaceId),
46+
});
47+
bind(GithubFileProvider).toSelf().inSingletonScope();
48+
}),
49+
);
50+
this.fileProvider = this.container.get(GithubFileProvider);
51+
this.user = DevData.createTestUser();
52+
}
53+
54+
@test public async testFileContent() {
55+
const result = await this.fileProvider.getFileContent(
56+
{
57+
repository: {
58+
owner: "gitpod-io",
59+
name: "gitpod",
60+
host: "github.com",
61+
cloneUrl: "unused in test",
62+
},
63+
revision: "af51739d341bb2245598e275336ae9f730e3b41a",
64+
},
65+
this.user,
66+
"License.txt",
67+
);
68+
expect(result).to.not.be.undefined;
69+
expect(result).to.contain(`To determine under which license you may use a file from the Gitpod source code,
70+
please resort to the header of that file.`);
71+
}
72+
}
73+
74+
module.exports = new TestFileProvider();

0 commit comments

Comments
 (0)