Skip to content

Commit

Permalink
Merge branch 'develop' minor
Browse files Browse the repository at this point in the history
First release
  • Loading branch information
SimonDevelop committed Jan 16, 2019
2 parents a22a8c5 + ab27e18 commit 20fd8a6
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 6 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
[![version](https://img.shields.io/badge/Version-0.0.0-brightgreen.svg)](https://github.com/SimonDevelop/releaser-bot/releases/tag/0.0.0)
[![version](https://img.shields.io/badge/Version-0.1.0-brightgreen.svg)](https://github.com/SimonDevelop/releaser-bot/releases/tag/0.1.0)
[![Minimum Node Version](https://img.shields.io/badge/node-%3E%3D%206-brightgreen.svg)](https://nodejs.org/en/)
[![GitHub license](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/SimonDevelop/releaser-bot/blob/master/LICENSE)
# releaser-bot
Simple bot that handles release generation on github projects.

## How it works
The bot will just listen to the webhooks of your projects github, is a push is detect and the message of the commit corresponds to the one configured in your config.json, it will create a new release on this commit.

With your type message commit in config.json, use `patch`, `minor` and `major` to specify the release.

## Install
```bash
$ npm install
$ cp config.example.json config.json
```
Edit `config.json` for the github API key of bot account and your github repositories.
Edit `config.json` for your personal token github of bot account, trigger commit message and your github repositories.

## Run
```bash
$ npm start
```

### Work in progress...
## Webhooks
Add the address of your nodejs server to the webhooks of your github projects classified in your config.json file.
Specify the content type on application/json and check `Just the push event.`.

## TODO
- [x] Simple release creation
- [ ] Add description release
7 changes: 4 additions & 3 deletions config.example.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"key": "<YOUR_GITHUB_API_KEY>",
"repositories": {
"token": "<GITHUB_ACCOUNT_TOKEN>",
"commitMessage": "<COMMIT_MESSAGE>",
"repositories": [
"<YOUR_GITHUB>/<REPOSITORY>"
}
]
}
135 changes: 135 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,137 @@
const config = require("./config.json");
const http = require('http');
const request = require('request');
var EventEmitter = require("events").EventEmitter;
var data = new EventEmitter();
var apiUrl = "https://api.github.com";

// Default request options
var options = {
method: "GET",
headers: {
'Content-type': 'application/json',
'User-Agent': 'releaser-bot',
'Authorization': 'token '+config.token
}, json: true
};

// Server for webhooks
http.createServer(function (req, res) {
if (req.method == 'POST') {
var b = '';
req.on('data', function (d) {
b += d;
});
req.on('end', function () {
try {
var post = JSON.parse(b);
console.log("webhook detected for "+post.repository.full_name);
checkRepos(post.repository.full_name);
res.writeHead(200, {"Content-Type": "application/json"});
res.write('{"message": "Webhook received!"}');
res.end();
} catch (err) {
console.log(err);
res.writeHead(500, {"Content-Type": "application/json"});
res.write('{"message": "Bad Post Data."}');
res.end();
}
});
} else {
res.end('not found');
}
}).listen(9898, "127.0.0.1");

// Check repos
function checkRepos(name) {
for (var i=0; i < config.repositories.length; i++) {
if (name == config.repositories[i]) {
options.url = apiUrl+"/repos/"+config.repositories[i];
// Charge repos
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
data.json = body;
data.emit('commit');
}
});
}
}
}

// Check last commit
data.on('commit', function () {
options.url = apiUrl+"/repos/"+data.json.full_name+"/commits";
// Charge last commit
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
var regMessage = new RegExp(config.commitMessage, "gi");
if (body[0].commit.message.match(regMessage) != null
&& (typeof data.commit == "undefined" || data.commit != body[0].sha.substr(0, 6))) {
data.commit = body[0].sha.substr(0, 6);
data.emit('release');
} else {
console.log("last commit is not new release");
}
}
});
});

// Check last release
data.on('release', function () {
options.url = apiUrl+"/repos/"+data.json.full_name+"/releases";
// Charge last release
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
data.releases = body;
data.emit('create');
}
});
});

// Create new release
data.on('create', function () {
var newRelease = {};
var lastRelease = '';
if (data.releases.length > 0) {
lastRelease = data.releases[0].tag_name;
var splited = lastRelease.split(".");
var major = parseInt(splited[0]);
var minor = parseInt(splited[1]);
var patch = parseInt(splited[2]);

var regMessageMajor = new RegExp("major", "gi");
var regMessageMinor = new RegExp("minor", "gi");
if (data.releases[0].body.match(regMessageMajor) != null) {
major++;
} else if (data.releases[0].body.match(regMessageMinor) != null) {
minor++;
} else {
patch++;
}
newRelease.tag = major+"."+minor+"."+patch;
newRelease.name = "Release "+major+"."+minor+"."+patch;
} else {
newRelease.tag = "0.0.1";
newRelease.name = "Release 0.0.1";
}

// Request for create new release
options.method = "POST";
options.url = apiUrl+"/repos/"+data.json.full_name+"/releases";
options.json = {
"tag_name": newRelease.tag,
"target_commitish": "master",
"name": newRelease.name,
"body": "",
"draft": false,
"prerelease": false
};
request(options, function (error, response, body) {
if (!error && response.statusCode == 201) {
console.log(newRelease.name+" for "+data.json.full_name+" is created!");
} else {
console.log("Creating "+newRelease.name+" for "+data.json.full_name+" is failed!");
}
});
options.method = "GET";
});

0 comments on commit 20fd8a6

Please sign in to comment.