From e8dbc4070e0062b9ef3ae69f61dc758b3251a342 Mon Sep 17 00:00:00 2001 From: Pedro Pombeiro Date: Mon, 5 Feb 2018 17:12:16 +0100 Subject: [PATCH] Use cleared nomenclature for GH responses --- bot_scripts/assign-approved-pr-to-test.js | 43 +++++++++---------- bot_scripts/assign-new-pr-to-review.js | 12 +++--- .../assign-to-bounty-awaiting-for-approval.js | 20 ++++----- bot_scripts/greet-new-contributor.js | 4 +- 4 files changed, 39 insertions(+), 40 deletions(-) diff --git a/bot_scripts/assign-approved-pr-to-test.js b/bot_scripts/assign-approved-pr-to-test.js index 23e2895..19a1d6d 100644 --- a/bot_scripts/assign-approved-pr-to-test.js +++ b/bot_scripts/assign-approved-pr-to-test.js @@ -43,8 +43,8 @@ async function getReviewApprovalState (github, robot, repo, pullRequest) { const reviewsWithChangesRequested = finalReviews.filter(reviewState => reviewState === 'CHANGES_REQUESTED') if (reviewsWithChangesRequested.length === 0) { // Get detailed pull request - const fullPullRequest = await github.pullRequests.get({owner: repo.owner.login, repo: repo.name, number: pullRequest.number}) - pullRequest = fullPullRequest.data + const fullPullRequestPayload = await github.pullRequests.get({owner: repo.owner.login, repo: repo.name, number: pullRequest.number}) + pullRequest = fullPullRequestPayload.data if (pullRequest.mergeable !== null && pullRequest.mergeable !== undefined && !pullRequest.mergeable) { if (process.env.DRY_RUN || process.env.DRY_RUN_PR_TO_TEST) { robot.log.debug(`pullRequest.mergeable is ${pullRequest.mergeable}, considering as failed`) @@ -91,18 +91,18 @@ async function getPullRequestReviewStates (github, repo, pullRequest) { } async function getProjectFromName (github, ownerName, repoName, projectBoardName) { - const ghprojects = await github.projects.getRepoProjects({ + const ghprojectsPayload = await github.projects.getRepoProjects({ owner: ownerName, repo: repoName, state: 'open' }) - return ghprojects.data.find(p => p.name === projectBoardName) + return ghprojectsPayload.data.find(p => p.name === projectBoardName) } async function getProjectCardForPullRequest (github, columnId, pullRequestUrl) { - const ghcards = await github.projects.getProjectCards({column_id: columnId}) - const ghcard = ghcards.data.find(c => c.content_url === pullRequestUrl) + const ghcardsPayload = await github.projects.getProjectCards({column_id: columnId}) + const ghcard = ghcardsPayload.data.find(c => c.content_url === pullRequestUrl) return ghcard } @@ -146,8 +146,8 @@ async function checkOpenPullRequests (robot, context) { // Fetch column IDs let ghcolumns try { - ghcolumns = await github.projects.getProjectColumns({ project_id: project.id }) - ghcolumns = ghcolumns.data + const ghcolumnsPayload = await github.projects.getProjectColumns({ project_id: project.id }) + ghcolumns = ghcolumnsPayload.data } catch (err) { robot.log.error(`Couldn't fetch the github columns for project: ${err}`, ownerName, repoName, project.id) return @@ -226,12 +226,12 @@ async function assignPullRequestToCorrectColumn (github, robot, repo, pullReques robot.log.debug(`assignPullRequestToTest - Handling Pull Request #${prNumber} on repo ${ownerName}/${repoName}. PR should be in ${dstColumn.name} column`) // Look for PR card in source column(s) - let ghcard = null + let existingGHCard = null let srcColumn = null for (const c of srcColumns) { try { - ghcard = await getProjectCardForPullRequest(github, c.id, pullRequest.issue_url) - if (ghcard) { + existingGHCard = await getProjectCardForPullRequest(github, c.id, pullRequest.issue_url) + if (existingGHCard) { srcColumn = c break } @@ -241,19 +241,19 @@ async function assignPullRequestToCorrectColumn (github, robot, repo, pullReques } } - if (ghcard) { + if (existingGHCard) { // Move PR card to the destination column try { - robot.log.trace(`Found card in source column ${srcColumn.name}`, ghcard.id, srcColumn.id) + robot.log.trace(`Found card in source column ${srcColumn.name}`, existingGHCard.id, srcColumn.id) if (process.env.DRY_RUN || process.env.DRY_RUN_PR_TO_TEST) { - robot.log.info(`Would have moved card ${ghcard.id} to ${dstColumn.name} for PR #${prNumber}`) + robot.log.info(`Would have moved card ${existingGHCard.id} to ${dstColumn.name} for PR #${prNumber}`) } else { // Found in the source column, let's move it to the destination column - await github.projects.moveProjectCard({id: ghcard.id, position: 'bottom', column_id: dstColumn.id}) + await github.projects.moveProjectCard({id: existingGHCard.id, position: 'bottom', column_id: dstColumn.id}) } - robot.log.info(`Moved card ${ghcard.id} to ${dstColumn.name} for PR #${prNumber}`) + robot.log.info(`Moved card ${existingGHCard.id} to ${dstColumn.name} for PR #${prNumber}`) slackHelper.sendMessage(robot, slackClient, room, `Assigned PR to ${dstColumn.name} column\n${pullRequest.html_url}`) } catch (err) { @@ -266,9 +266,9 @@ async function assignPullRequestToCorrectColumn (github, robot, repo, pullReques // Look for PR card in destination column try { - ghcard = await getProjectCardForPullRequest(github, dstColumn.id, pullRequest.issue_url) - if (ghcard) { - robot.log.trace(`Found card in target column, ignoring`, ghcard.id, dstColumn.id) + const existingGHCard = await getProjectCardForPullRequest(github, dstColumn.id, pullRequest.issue_url) + if (existingGHCard) { + robot.log.trace(`Found card in target column, ignoring`, existingGHCard.id, dstColumn.id) return } } catch (err) { @@ -280,14 +280,13 @@ async function assignPullRequestToCorrectColumn (github, robot, repo, pullReques robot.log.info(`Would have created card in ${dstColumn.name} column for PR #${prNumber}`) } else { // It wasn't in either the source nor the destination columns, let's create a new card for it in the destination column - ghcard = await github.projects.createProjectCard({ + const ghcardPayload = await github.projects.createProjectCard({ column_id: dstColumn.id, content_type: 'PullRequest', content_id: pullRequest.id }) - ghcard = ghcard.data - robot.log.info(`Created card ${ghcard.id} in ${dstColumn.name} for PR #${prNumber}`) + robot.log.info(`Created card ${ghcardPayload.data.id} in ${dstColumn.name} for PR #${prNumber}`) } } catch (err) { // We normally arrive here because there is already a card for the PR in another column diff --git a/bot_scripts/assign-new-pr-to-review.js b/bot_scripts/assign-new-pr-to-review.js index c3ce9f0..ce2dcf6 100644 --- a/bot_scripts/assign-new-pr-to-review.js +++ b/bot_scripts/assign-new-pr-to-review.js @@ -55,14 +55,14 @@ async function assignPullRequestToReview (context, robot) { const projectBoardName = projectBoardConfig.name const reviewColumnName = projectBoardConfig['review-column-name'] try { - const ghprojects = await github.projects.getRepoProjects({ + const ghprojectsPayload = await github.projects.getRepoProjects({ owner: ownerName, repo: repoName, state: 'open' }) // Find 'Pipeline for QA' project - const project = ghprojects.data.find(p => p.name === projectBoardName) + const project = ghprojectsPayload.data.find(p => p.name === projectBoardName) if (!project) { robot.log.error(`Couldn't find project ${projectBoardName} in repo ${ownerName}/${repoName}`) return @@ -72,9 +72,9 @@ async function assignPullRequestToReview (context, robot) { // Fetch REVIEW column ID try { - const ghcolumns = await github.projects.getProjectColumns({ project_id: project.id }) + const ghcolumnsPayload = await github.projects.getProjectColumns({ project_id: project.id }) - column = ghcolumns.data.find(c => c.name === reviewColumnName) + column = ghcolumnsPayload.data.find(c => c.name === reviewColumnName) if (!column) { robot.log.error(`Couldn't find ${reviewColumnName} column in project ${project.name}`) return @@ -95,13 +95,13 @@ async function assignPullRequestToReview (context, robot) { if (process.env.DRY_RUN) { robot.log.debug('Would have created card', column.id, payload.pull_request.id) } else { - const ghcard = await github.projects.createProjectCard({ + const ghcardPayload = await github.projects.createProjectCard({ column_id: column.id, content_type: 'PullRequest', content_id: payload.pull_request.id }) - robot.log.debug(`Created card: ${ghcard.data.url}`, ghcard.data.id) + robot.log.debug(`Created card: ${ghcardPayload.data.url}`, ghcardPayload.data.id) } // Send message to Slack diff --git a/bot_scripts/assign-to-bounty-awaiting-for-approval.js b/bot_scripts/assign-to-bounty-awaiting-for-approval.js index 03fc6c3..2c33852 100644 --- a/bot_scripts/assign-to-bounty-awaiting-for-approval.js +++ b/bot_scripts/assign-to-bounty-awaiting-for-approval.js @@ -72,13 +72,13 @@ async function assignIssueToBountyAwaitingForApproval (context, robot, assign) { try { const orgName = ownerName - const ghprojects = await github.projects.getOrgProjects({ + const ghprojectsPayload = await github.projects.getOrgProjects({ org: orgName, state: 'open' }) // Find 'Status SOB Swarm' project - const project = ghprojects.data.find(p => p.name === projectBoardName) + const project = ghprojectsPayload.data.find(p => p.name === projectBoardName) if (!project) { robot.log.error(`Couldn't find project ${projectBoardName} in ${orgName} org`) return @@ -88,9 +88,9 @@ async function assignIssueToBountyAwaitingForApproval (context, robot, assign) { // Fetch bounty-awaiting-approval column ID try { - const ghcolumns = await github.projects.getProjectColumns({ project_id: project.id }) + const ghcolumnsPayload = await github.projects.getProjectColumns({ project_id: project.id }) - column = ghcolumns.data.find(c => c.name === approvalColumnName) + column = ghcolumnsPayload.data.find(c => c.name === approvalColumnName) if (!column) { robot.log.error(`Couldn't find ${approvalColumnName} column in project ${project.name}`) return @@ -106,7 +106,7 @@ async function assignIssueToBountyAwaitingForApproval (context, robot, assign) { return } - let ghcard = null + let ghcardPayload = null if (process.env.DRY_RUN) { if (assign) { robot.log.info(`Would have created card for issue`, column.id, payload.issue.id) @@ -117,12 +117,12 @@ async function assignIssueToBountyAwaitingForApproval (context, robot, assign) { if (assign) { try { // Create project card for the issue in the bounty-awaiting-approval column - ghcard = await github.projects.createProjectCard({ + ghcardPayload = await github.projects.createProjectCard({ column_id: column.id, content_type: 'Issue', content_id: payload.issue.id }) - ghcard = ghcard.data + const ghcard = ghcardPayload.data robot.log(`Created card: ${ghcard.url}`, ghcard.id) } catch (err) { @@ -130,7 +130,7 @@ async function assignIssueToBountyAwaitingForApproval (context, robot, assign) { } } else { try { - ghcard = await getProjectCardForIssue(github, column.id, payload.issue.url) + const ghcard = await getProjectCardForIssue(github, column.id, payload.issue.url) if (ghcard) { await github.projects.deleteProjectCard({id: ghcard.id}) robot.log(`Deleted card: ${ghcard.url}`, ghcard.id) @@ -153,8 +153,8 @@ async function assignIssueToBountyAwaitingForApproval (context, robot, assign) { } async function getProjectCardForIssue (github, columnId, issueUrl) { - const ghcards = await github.projects.getProjectCards({column_id: columnId}) - const ghcard = ghcards.data.find(c => c.content_url === issueUrl) + const ghcardsPayload = await github.projects.getProjectCards({column_id: columnId}) + const ghcard = ghcardsPayload.data.find(c => c.content_url === issueUrl) return ghcard } diff --git a/bot_scripts/greet-new-contributor.js b/bot_scripts/greet-new-contributor.js index 412775f..b64fef9 100644 --- a/bot_scripts/greet-new-contributor.js +++ b/bot_scripts/greet-new-contributor.js @@ -49,14 +49,14 @@ async function greetNewContributor (context, robot) { robot.log(`greetNewContributor - Handling Pull Request #${prNumber} on repo ${ownerName}/${repoName}`) try { - const ghissues = await github.issues.getForRepo({ + const ghissuesPayload = await github.issues.getForRepo({ owner: ownerName, repo: repoName, state: 'all', creator: payload.pull_request.user.login }) - const userPullRequests = ghissues.data.filter(issue => issue.pull_request) + const userPullRequests = ghissuesPayload.data.filter(issue => issue.pull_request) if (userPullRequests.length === 1) { try { const welcomeMessage = welcomeBotConfig.message