Skip to content

Commit

Permalink
BXMSPROD-942 git.getForkedProject multipage (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ginxo authored Aug 25, 2020
1 parent c7c617a commit d456e63
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Just defining the **build chain** flow in every project from the chain, the tool
- It will try to checkout SOURCE_GROUP:PROJECT:SOURCE_BRANCH. In case the it exists and it has a pull request over the TARGET_GROUP:PROJECT:TARGET_BRANCH it will check it out and will merge it with target branch.
- If previous checkout fails, it will try the same with TARGET_GROUP:PROJECT:SOURCE_BRANCH this time.
- If previous checkout fails, it will checkout TARGET_GROUP:PROJECT:TARGET_BRANCH.
> **_Warning:_** It will fail in case it can't be done automatically, properly informing to please resolve conflicts.
> **_Warning:_** It will fail in case it can't be done automatically, properly informing to please resolve conflicts.
- Once all the projects are checked out, it will run as many commands are defined in `build-command-upstream` input (it will take `build-command` input in case there's no `build-command-upstream` defined) for every parent dependency starting from the highest level of the hierarchy to the lowest one.

Expand Down
37 changes: 31 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17257,21 +17257,46 @@ async function hasOriginPullRequest(octokit, owner, repo, branch) {
}
}

async function getForkedProject(octokit, owner, repo, wantedOwner) {
async function getForkedProject(
octokit,
owner,
repo,
wantedOwner,
page = 1,
per_page = 100
) {
assert(owner, "owner is not defined");
assert(repo, "repo is not defined");
assert(wantedOwner, "wantedOwner is not defined");
assert(page, "page is not defined");
try {
const { status, data } = await octokit.repos.listForks({
owner,
repo
repo,
page
});
return status == 200
? data.find(forkedProject => forkedProject.owner.login === wantedOwner)
: undefined;
if (status == 200) {
if (data && data.length > 0) {
const forkedProject = data.find(
forkedProject => forkedProject.owner.login === wantedOwner
);
return forkedProject
? forkedProject
: await getForkedProject(
octokit,
owner,
repo,
wantedOwner,
++page,
per_page
);
} else {
return undefined;
}
}
} catch (e) {
logger.error(
`Error getting forked project list from https://api.github.com/repos/${owner}/${repo}/forks'".`
`Error getting forked project list from https://api.github.com/repos/${owner}/${repo}/forks?per_page=${per_page}&page=${page}'".`
);
throw e;
}
Expand Down
37 changes: 31 additions & 6 deletions src/lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,21 +276,46 @@ async function hasOriginPullRequest(octokit, owner, repo, branch) {
}
}

async function getForkedProject(octokit, owner, repo, wantedOwner) {
async function getForkedProject(
octokit,
owner,
repo,
wantedOwner,
page = 1,
per_page = 100
) {
assert(owner, "owner is not defined");
assert(repo, "repo is not defined");
assert(wantedOwner, "wantedOwner is not defined");
assert(page, "page is not defined");
try {
const { status, data } = await octokit.repos.listForks({
owner,
repo
repo,
page
});
return status == 200
? data.find(forkedProject => forkedProject.owner.login === wantedOwner)
: undefined;
if (status == 200) {
if (data && data.length > 0) {
const forkedProject = data.find(
forkedProject => forkedProject.owner.login === wantedOwner
);
return forkedProject
? forkedProject
: await getForkedProject(
octokit,
owner,
repo,
wantedOwner,
++page,
per_page
);
} else {
return undefined;
}
}
} catch (e) {
logger.error(
`Error getting forked project list from https://api.github.com/repos/${owner}/${repo}/forks'".`
`Error getting forked project list from https://api.github.com/repos/${owner}/${repo}/forks?per_page=${per_page}&page=${page}'".`
);
throw e;
}
Expand Down
36 changes: 31 additions & 5 deletions test/git.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,11 @@ test("getForkedProject existing", async () => {
test("getForkedProject not existing", async () => {
const octokit = {
repos: {
listForks: jest.fn(({ owner, repo }) => {
return owner === "ownerx" && repo === "repox"
listForks: jest.fn(({ owner, repo, page }) => {
return owner === "ownerx" && repo === "repox" && page == 1
? { status: 200, data: forkedProjectListInfo }
: owner === "ownerx" && repo === "repox" && page == 2
? { status: 304, data: forkedProjectListInfoEmpty }
: undefined;
})
}
Expand All @@ -260,15 +262,15 @@ test("getForkedProject not existing", async () => {
"weirdowner"
);

expect(octokit.repos.listForks).toHaveBeenCalledTimes(1);
expect(octokit.repos.listForks).toHaveBeenCalledTimes(2);
expect(result).toBeUndefined();
});

test("getForkedProject empty", async () => {
const octokit = {
repos: {
listForks: jest.fn(({ owner, repo }) => {
return owner === "ownerx" && repo === "repox"
listForks: jest.fn(({ owner, repo, page }) => {
return owner === "ownerx" && repo === "repox" && page == 1
? { status: 200, data: forkedProjectListInfoEmpty }
: undefined;
})
Expand All @@ -285,3 +287,27 @@ test("getForkedProject empty", async () => {
expect(octokit.repos.listForks).toHaveBeenCalledTimes(1);
expect(result).toBeUndefined();
});

test("getForkedProject second page empty", async () => {
const octokit = {
repos: {
listForks: jest.fn(({ owner, repo, page }) => {
return owner === "ownerx" && repo === "repox" && page == 1
? { status: 200, data: forkedProjectListInfo }
: owner === "ownerx" && repo === "repox" && page == 2
? { status: 200, data: forkedProjectListInfoEmpty }
: undefined;
})
}
};

const result = await git.getForkedProject(
octokit,
"ownerx",
"repox",
"weirdowner"
);

expect(octokit.repos.listForks).toHaveBeenCalledTimes(2);
expect(result).toBeUndefined();
});

0 comments on commit d456e63

Please sign in to comment.