Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/delete merged branches 157242544 #1

Merged
merged 3 commits into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/civ2-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 10 additions & 21 deletions src/civ2.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// deploy to dockercloud <optional tag> - deploys the specified image:tag to docker. (default tag : release-candidate)
// deploy to kubernetes <optional tag> - deploys the specified image:tag to kubernetes dev cluster. (default tag : release-candidate)
// release stoic <version> - releases <version> in the wild.
// roolback stoic <version> - rollback to stoic >version>
// rollback stoic <version> - rollback to stoic <version>
// archive <repository> <branch>
// create feature instance <branch> - Creates a cluster on the specified branch
// destroy feature instance <branch> - Destroys the cluster of the specified branch
Expand Down Expand Up @@ -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 = `<https://github.com/sutoiku/${
prMerge.repo
}/branches|Branch> ${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 <https://github.com/sutoiku/${
prMerge.repo
}/branches|check on github>.`;
await civ2.deleteBranch(repo, branch);
const message = `<https://github.com/sutoiku/${repo}/branches|Branch ${branch}> of <https://github.com/sutoiku/${repo}|${repo}> was merged, I deleted it.`;
return robot.messageRoom(room, message);
} catch (ex) {
robot.messageRoom(room, `An error occured (${ex.message}).`);
Expand Down
60 changes: 34 additions & 26 deletions src/github-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = {
Expand All @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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";
Expand All @@ -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));
});
}
19 changes: 18 additions & 1 deletion test/civ2-commands.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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();
Expand Down