diff --git a/src/civ2-commands.js b/src/civ2-commands.js index 7e41e21..6edef5c 100644 --- a/src/civ2-commands.js +++ b/src/civ2-commands.js @@ -39,6 +39,10 @@ exports.archive = function(repo, branch) { }); }; +exports.deleteBranch = function(repo, branch) { + return ghApi.deleteBranch(repo, branch); +}; + exports.createFeatureCluster = FEATURE => { return buildJob("Chore/feature-clusters/create", undefined, { FEATURE diff --git a/src/civ2.js b/src/civ2.js index 99935a9..bce4348 100644 --- a/src/civ2.js +++ b/src/civ2.js @@ -9,7 +9,7 @@ // deploy to dockercloud - deploys the specified image:tag to docker. (default tag : release-candidate) // deploy to kubernetes - deploys the specified image:tag to kubernetes dev cluster. (default tag : release-candidate) // release stoic - releases in the wild. -// roolback stoic - rollback to stoic >version> +// rollback stoic - rollback to stoic // archive // create feature instance - Creates a cluster on the specified branch // destroy feature instance - Destroys the cluster of the specified branch @@ -138,31 +138,20 @@ module.exports = function(robot) { if (!prMerge) { return res.send("OK"); } - if (prMerge.base !== "master") { - const msg = `The PR ${prMerge.repo}#${ - prMerge.id - } was not forked from master. Won't archive ${prMerge.branch}.`; + + const { repo, branch, id, base } = prMerge; + + if (base !== "master") { + const msg = `The PR ${repo}#${id} was not forked from master. Won't delete ${branch}.`; robot.messageRoom(room, msg); return console.log(msg); } - console.log("It's a PR merge !", prMerge.repo, prMerge.branch); + + console.log("It's a PR merge !", repo, branch); try { - const body = await civ2.archive(prMerge.repo, prMerge.branch); - console.log("Merge OK"); - const bodyContent = JSON.parse(body); - if (bodyContent === "ok") { - const message = ` ${prMerge.branch} of ${ - prMerge.repo - } is now archived as archive/${prMerge.branch}.`; - return robot.messageRoom(room, message); - } - console.log("oops", bodyContent); - const message = `Hum, something unexpected happened. You'd better .`; + await civ2.deleteBranch(repo, branch); + const message = ` of was merged, I deleted it.`; return robot.messageRoom(room, message); } catch (ex) { robot.messageRoom(room, `An error occured (${ex.message}).`); diff --git a/src/github-api.js b/src/github-api.js index 0519898..3ac11f0 100644 --- a/src/github-api.js +++ b/src/github-api.js @@ -14,16 +14,19 @@ const REPOS = [ "pictura" ]; const GITHUB_TOKEN = process.env.GITHUB_TOKEN; +const GITHUB_ORG_NAME = "sutoiku"; + const GitHub = require("github-api"); const gh = new GitHub({ token: GITHUB_TOKEN }); -const helpers = require('./helpers'); +const helpers = require("./helpers"); module.exports = { getAllReposBranchInformation, getPTLink, - createMissingPrs + createMissingPrs, + deleteBranch }; async function mergePrs(branchName) { @@ -59,10 +62,10 @@ async function createMissingPrs(branchName, userName) { ); const user = helpers.getUserFromSlackLogin(userName); - console.log('USER', userName, user); + console.log("USER", userName, user); const assignees = user && [user.githubLogin]; - console.log('ASSIGNEES', assignees); - + console.log("ASSIGNEES", assignees); + const created = {}; for (const repoName of prsToCreate) { const prSpec = { @@ -73,9 +76,7 @@ async function createMissingPrs(branchName, userName) { assignees }; - const { - repo - } = branchInformation[repoName]; + const { repo } = branchInformation[repoName]; created[repoName] = await repo.createPullRequest(prSpec); } return created; @@ -84,25 +85,18 @@ async function createMissingPrs(branchName, userName) { async function getAllReposBranchInformation(branchName) { const allBranches = {}; for (const repoName of REPOS) { - const repo = gh.getRepo("sutoiku", repoName); + const repo = gh.getRepo(GITHUB_ORG_NAME, repoName); const repoData = await repoHasBranch(repo, branchName); - if (repoData !== null) { - allBranches[repoName] = Object.assign({ - repo - }, repoData); - } + if (repoData === null) {continue} + allBranches[repoName] = Object.assign({repo}, repoData); } return allBranches; } async function repoHasBranch(repo, branchName) { try { - const { - data: branch - } = await repo.getBranch(branchName); - const { - data: status - } = await repo.listStatuses(branchName); + const { data: branch } = await repo.getBranch(branchName); + const { data: status } = await repo.listStatuses(branchName); const pr = await getBranchPr(repo, branchName); const reviews = await getReviews(repo, pr); @@ -130,6 +124,12 @@ async function getBranchPr(repo, branchName) { return null; } +async function deleteBranch(repoName, branchName) { + const repo = gh.getRepo(GITHUB_ORG_NAME, repoName); + const pathname = `/repos/${repo.__fullname}/git/refs/heads/${branchName}`; + return requestOnRepo(repo, "DELETE", pathname); +} + // HELPERS function getPrText(branchName, userName, repos) { const ptLink = getPTLink(branchName) || "No PT"; @@ -149,12 +149,20 @@ function getPtIdFromBranchName(branchName) { return match && match[0]; } -async function getReviews(repo, pr){ - if(!pr){return} - return new Promise(function(resolve, reject) { - repo._request('GET', `/repos/${repo.__fullname}/pulls/${pr.number}/reviews`, null, (err, body)=>{ - resolve(body) - }); +async function getReviews(repo, pr) { + if (!pr) { + return; + } + + return requestOnRepo( + repo, + "GET", + `/repos/${repo.__fullname}/pulls/${pr.number}/reviews` + ); +} +function requestOnRepo(repo, method, pathname) { + return new Promise(function(resolve, reject) { + repo._request(method, pathname, null, (err, body) => err ? reject(err) : resolve(body)); }); } diff --git a/test/civ2-commands.test.js b/test/civ2-commands.test.js index 50795fb..523e26e 100644 --- a/test/civ2-commands.test.js +++ b/test/civ2-commands.test.js @@ -143,6 +143,7 @@ describe("civ2", function() { .catch(done); }); }); + describe("Task release", () => { it("calls the right Jenkins job", done => { process.env.HUBOT_JENKINS_AUTH = "bla:toto"; @@ -177,7 +178,6 @@ describe("civ2", function() { }); }); - describe("Task create instance cluster", () => { it("passes the right tag", done => { process.env.HUBOT_JENKINS_AUTH = "bla:toto"; @@ -218,6 +218,23 @@ describe("civ2", function() { }); }); + describe("Github operations", () => { + let civ2; + + beforeEach(() => { + civ2 = require("../src/civ2-commands.js"); + }); + + describe("deleteBranch", () => { + it("should call github API", async () => { + nock("https://api.github.com") + .delete("/repos/sutoiku/pipoRepo/git/refs/heads/branchToDelete") + .reply(204); + + await civ2.deleteBranch("pipoRepo", "branchToDelete"); + }); + }); + }); afterEach(() => { nock.cleanAll();