Skip to content

Improve handling of missing files for BB and BBS #20267

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

Merged
merged 6 commits into from
Oct 7, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TestBitbucketServerFileProvider {
verified: true,
description: "",
icon: "",
host: "bitbucket.gitpod-self-hosted.com",
host: "bitbucket.gitpod-dev.com",
oauth: {
callBackUrl: "",
clientId: "not-used",
Expand All @@ -45,7 +45,7 @@ class TestBitbucketServerFileProvider {
public before() {
const container = new Container();
container.load(
new ContainerModule((bind, unbind, isBound, rebind) => {
new ContainerModule((bind) => {
bind(BitbucketServerFileProvider).toSelf().inSingletonScope();
bind(BitbucketServerContextParser).toSelf().inSingletonScope();
bind(AuthProviderParams).toConstantValue(TestBitbucketServerFileProvider.AUTH_HOST_CONFIG);
Expand Down Expand Up @@ -93,13 +93,13 @@ class TestBitbucketServerFileProvider {
const result = await this.service.getGitpodFileContent(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
{
revision: "master",
revision: "main",
repository: <Repository>{
cloneUrl: "https://bitbucket.gitpod-self-hosted.com/projects/FOO/repos/repo123",
webUrl: "https://bitbucket.gitpod-self-hosted.com/projects/FOO/repos/repo123",
name: "repo123",
repoKind: "projects",
owner: "FOO",
cloneUrl: "https://bitbucket.gitpod-dev.com/users/filip/repos/spring-petclinic",
webUrl: "https://bitbucket.gitpod-dev.com/users/filip/repos/spring-petclinic",
name: "spring-petclinic",
repoKind: "users",
owner: "filip",
},
} as any,
this.user,
Expand All @@ -111,19 +111,43 @@ class TestBitbucketServerFileProvider {
@test async test_getLastChangeRevision_ok() {
const result = await this.service.getLastChangeRevision(
{
owner: "FOO",
name: "repo123",
repoKind: "projects",
revision: "foo",
host: "bitbucket.gitpod-self-hosted.com",
cloneUrl: "https://bitbucket.gitpod-self-hosted.com/projects/FOO/repos/repo123",
webUrl: "https://bitbucket.gitpod-self-hosted.com/projects/FOO/repos/repo123",
owner: "filip",
name: "spring-petclinic",
repoKind: "users",
revision: "ft/invalid-docker",
host: "bitbucket.gitpod-dev.com",
cloneUrl: "https://bitbucket.gitpod-dev.com/users/filip/repos/spring-petclinic",
webUrl: "https://bitbucket.gitpod-dev.com/users/filip/repos/spring-petclinic",
} as Repository,
"foo",
"ft/invalid-docker",
this.user,
"folder/sub/test.txt",
".gitpod.yml",
);
expect(result).to.equal("1384b6842d73b8705feaf45f3e8aa41f00529042");
expect(result).to.equal("7e38d77cc599682f543f71da36328307e35caa94");
}

@test async test_getLastChangeRevision_not_found() {
// it looks like expecting a promise to throw doesn't work, so we hack it with a try-catch
let didThrow = false;
try {
await this.service.getLastChangeRevision(
{
owner: "filip",
name: "spring-petclinic",
repoKind: "users",
revision: "ft/invalid-docker",
host: "bitbucket.gitpod-dev.com",
cloneUrl: "https://bitbucket.gitpod-dev.com/users/filip/repos/spring-petclinic",
webUrl: "https://bitbucket.gitpod-dev.com/users/filip/repos/spring-petclinic",
} as Repository,
"ft/invalid-docker",
this.user,
"gitpod.Dockerfile",
);
} catch (err) {
didThrow = true;
}
expect(didThrow).to.be.true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ export class BitbucketServerFileProvider implements FileProvider {
repositorySlug: name,
query: { limit: 1, path, shaOrRevision: revisionOrBranch },
});
return result.values![0].id;
const lastCommit = result.values?.[0]?.id;
if (!lastCommit) {
throw new Error(`File ${path} does not exist in repository ${repository.owner}/${repository.name}`);
}

return lastCommit;
}

public async getFileContent(commit: Commit, user: User, path: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TestBitbucketFileProvider {
This is the readme of the second branch.`);
}

@test public async testGetLastChangeRevision() {
@test public async testGetLastChangeRevision_ok() {
const result = await this.fileProvider.getLastChangeRevision(
{ owner: "gitpod", name: "integration-tests" } as Repository,
"second-branch",
Expand All @@ -84,6 +84,22 @@ This is the readme of the second branch.`);
);
expect(result).to.equal("5a24a0c8a7b42c2e6418593d788e17cb987bda25");
}

@test public async testGetLastChangeRevision_not_found() {
// it looks like expecting a promise to throw doesn't work, so we hack it with a try-catch
let didThrow = false;
try {
await this.fileProvider.getLastChangeRevision(
{ owner: "gitpod", name: "integration-tests" } as Repository,
"da2119f51b0e744cb6b36399f8433b477a4174ef", // a pinned commit on master
this.user,
"gitpod.Dockerfile",
);
} catch (err) {
didThrow = true;
}
expect(didThrow).to.be.true;
}
}

module.exports = new TestBitbucketFileProvider();
23 changes: 17 additions & 6 deletions components/server/src/bitbucket/bitbucket-file-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,26 @@ export class BitbucketFileProvider implements FileProvider {
try {
const api = await this.apiFactory.create(user);
const fileMetaData = (
await api.repositories.readSrc({
await api.repositories.listFileHistory({
workspace: repository.owner,
repo_slug: repository.name,
commit: revisionOrBranch,
pagelen: 1,
renames: "false",
path,
format: "meta",
})
).data;
return (fileMetaData as any).commit.hash;
const lastCommit = fileMetaData.values?.[0].commit?.hash;
if (!lastCommit) {
throw new Error(`No commits found for ${path} in repository ${repository.owner}/${repository.name}`);
}

return lastCommit;
} catch (err) {
if (err.status && err.status === 404) {
throw new Error(`File ${path} does not exist in repository ${repository.owner}/${repository.name}`);
}

log.error({ userId: user.id }, err);
throw new Error(`Could not fetch ${path} of repository ${repository.owner}/${repository.name}: ${err}`);
}
Expand All @@ -51,13 +61,14 @@ export class BitbucketFileProvider implements FileProvider {
return undefined;
}

const { repository, revision } = commit;
try {
const api = await this.apiFactory.create(user);
const contents = (
await api.repositories.readSrc({
workspace: commit.repository.owner,
repo_slug: commit.repository.name,
commit: commit.revision,
workspace: repository.owner,
repo_slug: repository.name,
commit: revision,
path,
})
).data;
Expand Down
Loading