Skip to content
Snippets Groups Projects
Commit 7a1d08ea authored by Rhys Arkins's avatar Rhys Arkins Committed by GitHub
Browse files

fix: refactor out remaining do-while loops (#978)

* fix: convert prBody trimming from do-while to recursive

* fix: convert gitlab projects do-while to use pagination
parent 5639f4b6
Branches
No related tags found
No related merge requests found
...@@ -53,20 +53,10 @@ async function getRepos(token, endpoint) { ...@@ -53,20 +53,10 @@ async function getRepos(token, endpoint) {
process.env.GITLAB_ENDPOINT = endpoint; process.env.GITLAB_ENDPOINT = endpoint;
} }
try { try {
let projects = []; const url = `projects?membership=true&per_page=100`;
const perPage = 100; const res = await get(url, { paginate: true });
let i = 1; logger.info(`Discovered ${res.body.length} project(s)`);
let res; return res.body.map(repo => repo.path_with_namespace);
do {
const url = `projects?membership=true&per_page=100&page=${i}`;
res = await get(url);
projects = projects.concat(
res.body.map(repo => repo.path_with_namespace)
);
i += 1;
} while (res.body.length === perPage);
logger.info(`Discovered ${projects.length} project(s)`);
return projects;
} catch (err) { } catch (err) {
logger.error({ err }, `GitLab getRepos error`); logger.error({ err }, `GitLab getRepos error`);
throw err; throw err;
......
...@@ -127,7 +127,7 @@ async function ensurePr(prConfig) { ...@@ -127,7 +127,7 @@ async function ensurePr(prConfig) {
const prTitle = handlebars.compile(config.prTitle)(config); const prTitle = handlebars.compile(config.prTitle)(config);
let prBody; let prBody;
do { async function trimPrBody() {
let prBodyMarkdown = handlebars.compile(config.prBody)(config); let prBodyMarkdown = handlebars.compile(config.prBody)(config);
const atUserRe = /[^`]@([a-z]+\/[a-z]+)/g; const atUserRe = /[^`]@([a-z]+\/[a-z]+)/g;
prBodyMarkdown = prBodyMarkdown.replace(atUserRe, '@​$1'); prBodyMarkdown = prBodyMarkdown.replace(atUserRe, '@​$1');
...@@ -136,8 +136,13 @@ async function ensurePr(prConfig) { ...@@ -136,8 +136,13 @@ async function ensurePr(prConfig) {
prBody = prBody.replace(issueRe, '$1#​$2$3'); prBody = prBody.replace(issueRe, '$1#​$2$3');
const backTickRe = /`([^/]*?)`/g; const backTickRe = /`([^/]*?)`/g;
prBody = prBody.replace(backTickRe, '<code>$1</code>'); prBody = prBody.replace(backTickRe, '<code>$1</code>');
// istanbul ignore if
if (prBody.length > 250000) {
config.upgrades.pop(); config.upgrades.pop();
} while (prBody.length > 250000); trimPrBody();
}
}
trimPrBody();
try { try {
// Check if existing PR exists // Check if existing PR exists
......
...@@ -299,24 +299,13 @@ Object { ...@@ -299,24 +299,13 @@ Object {
} }
`; `;
exports[`api/gitlab getRepos should fetch multiple pages 1`] = `
Array [
Array [
"projects?membership=true&per_page=100&page=1",
],
Array [
"projects?membership=true&per_page=100&page=2",
],
Array [
"projects?membership=true&per_page=100&page=3",
],
]
`;
exports[`api/gitlab getRepos should return an array of repos 1`] = ` exports[`api/gitlab getRepos should return an array of repos 1`] = `
Array [ Array [
Array [ Array [
"projects?membership=true&per_page=100&page=1", "projects?membership=true&per_page=100",
Object {
"paginate": true,
},
], ],
] ]
`; `;
...@@ -331,7 +320,10 @@ Array [ ...@@ -331,7 +320,10 @@ Array [
exports[`api/gitlab getRepos should support a custom endpoint 1`] = ` exports[`api/gitlab getRepos should support a custom endpoint 1`] = `
Array [ Array [
Array [ Array [
"projects?membership=true&per_page=100&page=1", "projects?membership=true&per_page=100",
Object {
"paginate": true,
},
], ],
] ]
`; `;
......
...@@ -61,25 +61,6 @@ describe('api/gitlab', () => { ...@@ -61,25 +61,6 @@ describe('api/gitlab', () => {
expect(get.mock.calls).toMatchSnapshot(); expect(get.mock.calls).toMatchSnapshot();
expect(repos).toMatchSnapshot(); expect(repos).toMatchSnapshot();
}); });
it('should fetch multiple pages', async () => {
const repoCount = 250;
const projects = [];
for (let i = 0; i < repoCount; i += 1) {
projects.push({ path_with_namespace: `project${i}` });
}
get.mockImplementationOnce(() => ({
body: projects.slice(0, 100),
}));
get.mockImplementationOnce(() => ({
body: projects.slice(100, 200),
}));
get.mockImplementationOnce(() => ({
body: projects.slice(200),
}));
const repos = await gitlab.getRepos('sometoken');
expect(get.mock.calls).toMatchSnapshot();
expect(repos.length).toBe(repoCount);
});
}); });
async function initRepo(...args) { async function initRepo(...args) {
......
...@@ -114,6 +114,7 @@ describe('config/index', () => { ...@@ -114,6 +114,7 @@ describe('config/index', () => {
'--token=abc', '--token=abc',
]); ]);
get.mockImplementationOnce(() => ({ get.mockImplementationOnce(() => ({
headers: {},
body: [ body: [
{ {
path_with_namespace: 'a/b', path_with_namespace: 'a/b',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment