Skip to content

Commit f444036

Browse files
fix(azure): getRawFile not handling 404 for Azure DevOps (#30066)
1 parent d65effa commit f444036

File tree

3 files changed

+61
-58
lines changed

3 files changed

+61
-58
lines changed

lib/modules/platform/azure/__snapshots__/index.spec.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ exports[`modules/platform/azure/index getJsonFile() supports fetch from another
147147
undefined,
148148
undefined,
149149
undefined,
150+
true,
150151
],
151152
]
152153
`;

lib/modules/platform/azure/index.spec.ts

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,17 +1886,26 @@ describe('modules/platform/azure/index', () => {
18861886
it('returns file content', async () => {
18871887
const data = { foo: 'bar' };
18881888
azureApi.gitApi.mockImplementationOnce(
1889-
() =>
1890-
({
1891-
getItemContent: jest.fn(() =>
1892-
Promise.resolve(Readable.from(JSON.stringify(data))),
1893-
),
1894-
}) as any,
1889+
jest.fn().mockImplementationOnce(() => ({
1890+
getItem: jest.fn(() =>
1891+
Promise.resolve({ content: JSON.stringify(data) }),
1892+
),
1893+
})),
18951894
);
18961895
const res = await azure.getJsonFile('file.json');
18971896
expect(res).toEqual(data);
18981897
});
18991898

1899+
it('returns null when file not found', async () => {
1900+
azureApi.gitApi.mockImplementationOnce(
1901+
jest.fn().mockImplementationOnce(() => ({
1902+
getItem: jest.fn(() => Promise.resolve(null)),
1903+
})),
1904+
);
1905+
const res = await azure.getJsonFile('file.json');
1906+
expect(res).toBeNull();
1907+
});
1908+
19001909
it('returns file content in json5 format', async () => {
19011910
const json5Data = `
19021911
{
@@ -1905,71 +1914,65 @@ describe('modules/platform/azure/index', () => {
19051914
}
19061915
`;
19071916
azureApi.gitApi.mockImplementationOnce(
1908-
() =>
1909-
({
1910-
getItemContent: jest.fn(() =>
1911-
Promise.resolve(Readable.from(json5Data)),
1912-
),
1913-
}) as any,
1917+
jest.fn().mockImplementationOnce(() => ({
1918+
getItem: jest.fn(() => Promise.resolve({ content: json5Data })),
1919+
})),
19141920
);
19151921
const res = await azure.getJsonFile('file.json5');
19161922
expect(res).toEqual({ foo: 'bar' });
19171923
});
19181924

19191925
it('returns file content from branch or tag', async () => {
19201926
const data = { foo: 'bar' };
1921-
azureApi.gitApi.mockImplementationOnce(
1922-
() =>
1923-
({
1924-
getItemContent: jest.fn(() =>
1925-
Promise.resolve(Readable.from(JSON.stringify(data))),
1926-
),
1927-
}) as any,
1927+
azureApi.gitApi.mockResolvedValueOnce(
1928+
partial<IGitApi>({
1929+
getItem: jest.fn(() =>
1930+
Promise.resolve({ content: JSON.stringify(data) }),
1931+
),
1932+
}),
19281933
);
19291934
const res = await azure.getJsonFile('file.json', undefined, 'dev');
19301935
expect(res).toEqual(data);
19311936
});
19321937

19331938
it('throws on malformed JSON', async () => {
1934-
azureApi.gitApi.mockImplementationOnce(
1935-
() =>
1936-
({
1937-
getItemContent: jest.fn(() =>
1938-
Promise.resolve(Readable.from('!@#')),
1939-
),
1940-
}) as any,
1939+
azureApi.gitApi.mockResolvedValueOnce(
1940+
partial<IGitApi>({
1941+
getItemContent: jest.fn(() => Promise.resolve(Readable.from('!@#'))),
1942+
}),
19411943
);
19421944
await expect(azure.getJsonFile('file.json')).rejects.toThrow();
19431945
});
19441946

19451947
it('throws on errors', async () => {
1946-
azureApi.gitApi.mockImplementationOnce(
1947-
() =>
1948-
({
1949-
getItemContent: jest.fn(() => {
1950-
throw new Error('some error');
1951-
}),
1952-
}) as any,
1948+
azureApi.gitApi.mockResolvedValueOnce(
1949+
partial<IGitApi>({
1950+
getItemContent: jest.fn(() => {
1951+
throw new Error('some error');
1952+
}),
1953+
}),
19531954
);
19541955
await expect(azure.getJsonFile('file.json')).rejects.toThrow();
19551956
});
19561957

19571958
it('supports fetch from another repo', async () => {
19581959
const data = { foo: 'bar' };
1959-
const gitApiMock = {
1960-
getItemContent: jest.fn(() =>
1961-
Promise.resolve(Readable.from(JSON.stringify(data))),
1962-
),
1963-
getRepositories: jest.fn(() =>
1964-
Promise.resolve([
1965-
{ id: '123456', name: 'bar', project: { name: 'foo' } },
1966-
]),
1967-
),
1968-
};
1969-
azureApi.gitApi.mockImplementationOnce(() => gitApiMock as any);
1960+
const getItemFn = jest
1961+
.fn()
1962+
.mockResolvedValueOnce({ content: JSON.stringify(data) });
1963+
azureApi.gitApi.mockResolvedValueOnce(
1964+
partial<IGitApi>({
1965+
getItem: getItemFn,
1966+
getRepositories: jest
1967+
.fn()
1968+
.mockResolvedValue([
1969+
{ id: '123456', name: 'bar', project: { name: 'foo' } },
1970+
]),
1971+
}),
1972+
);
19701973
const res = await azure.getJsonFile('file.json', 'foo/bar');
19711974
expect(res).toEqual(data);
1972-
expect(gitApiMock.getItemContent.mock.calls).toMatchSnapshot();
1975+
expect(getItemFn.mock.calls).toMatchSnapshot();
19731976
});
19741977

19751978
it('returns null', async () => {

lib/modules/platform/azure/index.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import * as git from '../../../util/git';
2222
import * as hostRules from '../../../util/host-rules';
2323
import { regEx } from '../../../util/regex';
2424
import { sanitize } from '../../../util/sanitize';
25-
import { streamToString } from '../../../util/streams';
2625
import { ensureTrailingSlash } from '../../../util/url';
2726
import type {
2827
BranchStatusConfig,
@@ -146,20 +145,20 @@ export async function getRawFile(
146145
version: branchOrTag,
147146
} satisfies GitVersionDescriptor;
148147

149-
const buf = await azureApiGit.getItemContent(
150-
repoId,
151-
fileName,
152-
undefined,
153-
undefined,
154-
undefined,
155-
undefined,
156-
undefined,
157-
undefined,
158-
branchOrTag ? versionDescriptor : undefined,
148+
const item = await azureApiGit.getItem(
149+
repoId, // repositoryId
150+
fileName, // path
151+
undefined, // project
152+
undefined, // scopePath
153+
undefined, // recursionLevel
154+
undefined, // includeContentMetadata
155+
undefined, // latestProcessedChange
156+
undefined, // download
157+
branchOrTag ? versionDescriptor : undefined, // versionDescriptor
158+
true, // includeContent
159159
);
160160

161-
const str = await streamToString(buf);
162-
return str;
161+
return item?.content ?? null;
163162
} catch (err) /* istanbul ignore next */ {
164163
if (
165164
err.message?.includes('<title>Azure DevOps Services Unavailable</title>')

0 commit comments

Comments
 (0)