Skip to content

Refactors checkStatus function's event data assignment #8

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

WEB_PORT=7777
BAMBOO_URL="<bamboo fqdn, no transport>"
BAMBOO_USERNAME="<bamboo username>"
BAMBOO_PASSWORD="<bamboo password>"
BAMBOO_PROJECT=""
BAMBOO_PLAN=""
GITHUB_USERNAME="<github username>"
GITHUB_PASSWORD="<github password>"
JIRA_URL="<jira cloud url, without the transport and just fqdn>",
JIRA_USERNAME="<jira - atlassian email address / username>",
JIRA_PASSWORD="<jira password>",
JIRA_PROJECT_ID=10501
JIRA_ISSUETYPE_ID=10101
JIRA_LABELS=["pull_request","github"]
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ jspm_packages

# Optional REPL history
.node_repl_history

# Environment Variables
.env
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Purpose
When a user creaes a new Pull request, this application will react to GitHub webhook data and perform tasks while updating Git Hub statuses displayed to end users. During this time, some house keeping is done. The issue is created with all user provided details inside Jira Cloud instance, within a specified project. Once issue is created, the Pull Request details are submitted to Bamboo to begin building a PROJECT-PLAN. Job is started, in Bamboo and application will meader around and waiting for build to complete.
When a user creates a new Pull request, this application will react to GitHub webhook data and perform tasks while updating Git Hub statuses displayed to end users. During this time, some house keeping is done. The issue is created with all user provided details inside Jira Cloud instance, within a specified project. Once issue is created, the Pull Request details are submitted to Bamboo to begin building a PROJECT-PLAN. Job is started, in Bamboo and application will meader around and waiting for build to complete.

Look at [default,json](config/default.json) and review needed settings.
Look at [.env](.env) and review needed settings.

Status results look like screen shot below

Expand Down
133 changes: 67 additions & 66 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,42 @@
var async = require('async');
var config = require('config');
var JiraClient = require('jira-connector');
var http = require('http');
var createHandler = require('github-webhook-handler');
var handler = createHandler({ path: '/webhook', secret: 'myhashsecret' });
var events = require('github-webhook-handler/events');
var GitHub = require('github-api');
var Bamboo = require('bamboo-api');
require('dotenv').config();
const async = require('async');
const JiraClient = require('jira-connector');
const http = require('http');
const createHandler = require('github-webhook-handler');
const handler = createHandler({ path: '/webhook', secret: 'myhashsecret' });
const events = require('github-webhook-handler/events');
const GitHub = require('github-api');
const Bamboo = require('bamboo-api');

// Setup Bamboo connection
const bamboo = new Bamboo(
"https://" + config.bamboo.get('url'),
config.bamboo.get('username'),
config.bamboo.get('password'));
`https://${process.env.BAMBOO_URL}`,
process.env.BAMBOO_USERNAME,
process.env.BAMBOO_PASSWORD);

// Setup GitHub Connection
const github = new GitHub({
username: config.github.get('username'),
password:config.github.get('password')
username: process.env.GITHUB_USERNAME,
password: process.env.GITHUB_PASSWORD
});

// Setup Jira Connection
const jira = new JiraClient({
host: config.jira.get('url'),
host: process.env.JIRA_URL,
basic_auth: {
username: config.jira.get('username'),
password: config.jira.get('password')
username: process.env.JIRA_USERNAME,
password: process.env.JIRA_PASSWORD
}
})

config.bamboo.authorization = Buffer.from(config.bamboo.username + ':' + config.bamboo.password).toString('base64');
const bambooAuth = Buffer.from(`${process.env.BAMBOO_USERNAME}:${process.env.BAMBOO_PASSWORD}`).toString('base64');
// Start listening for webhooks
http.createServer(function (req, res) {
handler(req, res, function (err) {
res.statusCode = 404;
res.end('no such location')
})
}).listen(config.web.get('port'));

function syncItem() {
return {
"issue_number": "",
"jira_key": "",
"issue_url": "",
"smart_url": "",
"contributor": "",
"contributor_url": "",
"title": "",
"pr_branch": "",
"base_branch": "",
"statuses_url": "",
"description": "",
"repo": "",
"sha": ""
}
}
}).listen(process.env.WEB_PORT);

// Report error it is occurs
handler.on('error', function (err) {
Expand All @@ -78,14 +60,14 @@ function jiraIssue(newItem) {
return {
"fields": {
"project": {
"id": config.jira.project.get('id')
"id": process.env.JIRA_PROJECT_ID
},
"summary": newItem.title,
"description": newItem.description,
"issuetype": {
"id": config.jira.issuetype.get('id')
"id": process.env.JIRA_ISSUETYPE_ID
},
"labels": config.jira.get('labels')
"labels": process.env.JIRA_LABELS
},

}
Expand Down Expand Up @@ -116,12 +98,12 @@ function addCommentBambooBuild(newItem,repo,bambooResults,callback){
var http = require("https");
var options = {
"method": "POST",
"hostname": config.bamboo.get('url'),
"hostname": process.env.BAMBOO_URL,
"port": null,
"path": "/rest/api/latest/result/" + bambooResults.buildResultKey + "/comment",
"path": `/rest/api/latest/result/${bambooResults.buildResultKey}/comment`,
"headers": {
"content-type": "application/json",
"authorization": "Basic " + config.bamboo.authorization,
"authorization": `Basic ${bambooAuth}`,
"cache-control": "no-cache",
}
};
Expand All @@ -139,7 +121,7 @@ function addCommentBambooBuild(newItem,repo,bambooResults,callback){
});

req.write(JSON.stringify({ author: 'administrator',
content: newItem.jira_key + ' is linked to this build' }));
content: `${newItem.jira_key} is linked to this build` }));
req.end();
}

Expand Down Expand Up @@ -167,22 +149,9 @@ function getBuildStatus(newItem,repo,bambooResults,callback) {

function checkStatus(event,callback) {
if (isNewPr(event) == true) {
var newItem = new syncItem();
newItem.issue_number = event.payload.number;
newItem.contributor = event.payload.sender.login;
newItem.repo = event.payload.repository.full_name;
newItem.contributor_url = event.payload.sender.html_url;
newItem.smart_url = event.payload.repository.full_name + "#" + event.payload.number;
newItem.title = event.payload.pull_request.title;
newItem.description = event.payload.pull_request.body + " \r\n\r\nReferences: " + event.payload.repository.full_name + "#" + event.payload.number;
newItem.pr_branch = event.payload.pull_request.head.ref;
newItem.base_branch = event.payload.pull_request.base.ref;
newItem.issue_url = event.payload.pull_request.issue_url;
newItem.jiraIssue_url = "";
newItem.jira_key = "";
newItem.sha = event.payload.pull_request.head.sha;
newItem.statuses_url = event.payload.pull_request.statuses_url;
var repo = github.getRepo(newItem.repo);
const newItem = buildSyncItem(event);
const repo = github.getRepo(newItem.repo);

repo.updateStatus(newItem.sha, {
state: 'pending', //The state of the status. Can be one of: pending, success, error, or failure.
description: 'Checking your work...',
Expand Down Expand Up @@ -307,7 +276,7 @@ function checkStatus(event,callback) {
function(newItem,repo,callback){
var urlParams = {"os_authType": "basic","bamboo.variable.GITHUB_ISSUE_ID": newItem.issue_number, "bamboo.variable.JIRAISSUE_KEY": newItem.jira_key,"bamboo.variable.GITHUB_SHA": newItem.sha,"bamboo.variable.GITHUBISSUE_KEY": newItem.issue_number,"bamboo.variable.ISSUE_ID": newItem.issue_number,"bamboo.variable.JIRAISSUE_URL": newItem.jiraIssue_url,"bamboo.variable.GITHUB_ISSUE_URL": newItem.issue_url,"bamboo.variable.GITHUB_REPO": newItem.repo,"bamboo.variable.GITHUBISSUE_USER": "openanthem","bamboo.variable.JIRAISSUE_ID": newItem.jira_key};
var buildParams = {"executeAllStages": true};
bamboo.buildPlan(config.bamboo.get('project') + "-" + config.bamboo.get('plan'), function(error, result) {
bamboo.buildPlan(`${process.env.BAMBOO_PROJECT}-${process.env.BAMBOO_PLAN}`, function(error, result) {
if (error) {
repo.updateStatus(newItem.sha, {
state: 'failure', //The state of the status. Can be one of: pending, success, error, or failure.
Expand All @@ -331,9 +300,9 @@ function checkStatus(event,callback) {
function(newItem,repo,bambooResults,callback) {
repo.updateStatus(newItem.sha, {
state: 'pending', //The state of the status. Can be one of: pending, success, error, or failure.
description: 'Waiting on build: ' + bambooResults.buildNumber,
description: `Waiting on build: ${bambooResults.buildNumber}`,
context: 'Verifying build',
target_url: "https://bamboo.previewmy.net/browse/" + bambooResults.buildResultKey
target_url: `https://bamboo.previewmy.net/browse/${bambooResults.buildResultKey}`
});
callback(null,newItem,repo,bambooResults);
},
Expand All @@ -350,9 +319,9 @@ function checkStatus(event,callback) {
} else {
repo.updateStatus(newItem.sha, {
state: 'success', //The state of the status. Can be one of: pending, success, error, or failure.
description: 'Build finished...' + bambooResults.buildNumber,
description: `Build finished... ${bambooResults.buildNumber}`,
context: 'Verifying build',
target_url: "https://bamboo.previewmy.net/browse/" + bambooResults.buildResultKey
target_url: `https://bamboo.previewmy.net/browse/${bambooResults.buildResultKey}`
});
callback(null,newItem,repo,bambooResults);
}
Expand All @@ -377,4 +346,36 @@ handler.on('pull_request', function(event){
checkStatus(event,function(){
console.log('Finished processing');
})
});
});

function buildSyncItem(event) {
const { number: issue_number, pull_request, repository, sender} = event.payload;
const { full_name: repoName } = repository;
const { login: contributor, html_url: contributor_url} = sender;
const { body, head, issue_url, statuses_url, title } = pull_request;
const smart_url = `${repoName}#${issue_number}`;
const description = `
${body}

References: ${repoName}#${issue_number}
`;

const newItem = {
contributor,
contributor_url,
description,
base_branch: base.ref,
jira_key: '',
jiraIssue_url: '',
issue_number,
issue_url,
pr_branch: head.ref,
repo: repoName,
sha: head.sha,
smart_url,
statuses_url,
title
};

return newItem;
}
31 changes: 0 additions & 31 deletions config/default.json

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"async": "^2.1.4",
"bamboo-api": "0.0.9",
"config": "^1.24.0",
"dotenv": "^4.0.0",
"github-api": "^3.0.0",
"github-webhook-handler": "^0.6.0",
"jira-connector": "^2.5.0"
Expand Down