Skip to content

Commit bee3be2

Browse files
committed
[context parser] Check current ref for Docker image existence
1 parent 18b92d8 commit bee3be2

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

components/server/src/azure-devops/azure-file-provider.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ export class AzureDevOpsFileProvider implements FileProvider {
3434
): Promise<string> {
3535
const [azOrgId, azProject] = getOrgAndProject(repository.owner);
3636
const repoName = repository.name;
37+
const notFoundError = new Error(
38+
`File ${path} does not exist in repository ${repository.owner}/${repository.name}`,
39+
);
40+
const fileExists =
41+
(await this.getFileContent({ repository, revision: revisionOrBranch }, user, path)) !== undefined;
42+
if (!fileExists) {
43+
throw notFoundError;
44+
}
45+
3746
const results = await Promise.allSettled([
3847
this.azureDevOpsApi.getCommits(user, azOrgId, azProject, repoName, {
3948
filterCommit: {
@@ -71,7 +80,7 @@ export class AzureDevOpsFileProvider implements FileProvider {
7180
}
7281
}
7382
// TODO(hw): [AZ] proper handle error
74-
throw new Error(`File ${path} does not exist in repository ${repository.owner}/${repository.name}`);
83+
throw notFoundError;
7584
}
7685

7786
public async getFileContent(commit: Commit, user: User, path: string): Promise<MaybeContent> {

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,28 @@ export class BitbucketServerFileProvider implements FileProvider {
2424
path: string,
2525
): Promise<string> {
2626
const { owner, name, repoKind } = repository;
27-
2827
if (!repoKind) {
2928
throw new Error("Repo kind is missing.");
3029
}
30+
const notFoundError = new Error(
31+
`File ${path} does not exist in repository ${repository.owner}/${repository.name}`,
32+
);
33+
34+
const fileExists =
35+
(await this.getFileContent({ repository, revision: revisionOrBranch }, user, path)) !== undefined;
36+
if (!fileExists) {
37+
throw notFoundError;
38+
}
3139

32-
const result = await this.api.getCommits(user, {
40+
const commits = await this.api.getCommits(user, {
3341
owner,
3442
repoKind,
3543
repositorySlug: name,
3644
query: { limit: 1, path, shaOrRevision: revisionOrBranch },
3745
});
38-
const lastCommit = result.values?.[0]?.id;
46+
const lastCommit = commits.values?.[0]?.id;
3947
if (!lastCommit) {
40-
throw new Error(`File ${path} does not exist in repository ${repository.owner}/${repository.name}`);
48+
throw notFoundError;
4149
}
4250

4351
return lastCommit;

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,29 @@ export class GithubFileProvider implements FileProvider {
2929
user: User,
3030
path: string,
3131
): Promise<string> {
32+
const notFoundError = new Error(
33+
`File ${path} does not exist in repository ${repository.owner}/${repository.name}`,
34+
);
35+
const fileExists =
36+
(await this.getFileContent({ repository, revision: revisionOrBranch }, user, path)) !== undefined;
37+
if (!fileExists) {
38+
throw notFoundError;
39+
}
40+
3241
const commits = (
3342
await this.githubApi.run(user, (gh) =>
3443
gh.repos.listCommits({
3544
owner: repository.owner,
3645
repo: repository.name,
3746
sha: revisionOrBranch,
38-
// per_page: 1, // we need just the last one right?
47+
per_page: 1, // we just need the last one
3948
path,
4049
}),
4150
)
4251
).data;
43-
4452
const lastCommit = commits && commits[0];
4553
if (!lastCommit) {
46-
throw new Error(`File ${path} does not exist in repository ${repository.owner}/${repository.name}`);
54+
throw notFoundError;
4755
}
4856

4957
return lastCommit.sha;
@@ -83,7 +91,7 @@ export class GithubFileProvider implements FileProvider {
8391
}
8492
return undefined;
8593
} catch (err) {
86-
log.debug("Failed to get Github file content", err, {
94+
log.debug("Failed to get GitHub file content", err, {
8795
request: params,
8896
});
8997
}

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,28 @@ export class GitlabFileProvider implements FileProvider {
2929
user: User,
3030
path: string,
3131
): Promise<string> {
32-
const result = await this.gitlabApi.run<GitLab.Commit[]>(user, async (g) => {
33-
return g.Commits.all(`${repository.owner}/${repository.name}`, { path, refName: revisionOrBranch });
34-
});
32+
const notFoundError = new Error(
33+
`File ${path} does not exist in repository ${repository.owner}/${repository.name}`,
34+
);
3535

36-
if (GitLab.ApiError.is(result)) {
37-
throw result;
36+
const fileExists =
37+
(await this.getFileContent({ repository, revision: revisionOrBranch }, user, path)) !== undefined;
38+
if (!fileExists) {
39+
throw notFoundError;
3840
}
3941

40-
const lastCommit = result[0];
42+
const commitsResult = await this.gitlabApi.run<GitLab.Commit[]>(user, async (g) => {
43+
return g.Commits.all(`${repository.owner}/${repository.name}`, { path, refName: revisionOrBranch });
44+
});
45+
if (GitLab.ApiError.is(commitsResult)) {
46+
throw commitsResult;
47+
}
4148

49+
const lastCommit = commitsResult[0];
4250
if (!lastCommit) {
43-
throw new Error(`File ${path} does not exist in repository ${repository.owner}/${repository.name}`);
51+
throw notFoundError;
4452
}
53+
4554
return lastCommit.id;
4655
}
4756

0 commit comments

Comments
 (0)