Skip to content

Commit 951d0d2

Browse files
committed
feat(parser): Add GitLab parser
1 parent af61b1b commit 951d0d2

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

build.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ var Build = function (req, res, payload, build_manager, id) {
2626
};
2727

2828
// Load and check payload
29-
self.parser = req.headers['travis-repo-slug'] ?
30-
new parser.Travis(payload, req.headers, self.build_manager.config) :
31-
new parser.GitHub(payload, req.headers, self.build_manager.config);
29+
if (req.headers['travis-repo-slug']) {
30+
self.parser = new parser.Travis(payload, req.headers, self.build_manager.config);
31+
} else if (req.headers['X-GitHub-Delivery']) {
32+
self.parser = new parser.GitHub(payload, req.headers, self.build_manager.config);
33+
} else {
34+
self.parser = new parser.GitLab(payload, req.headers, self.build_manager.config);
35+
}
3236

3337
self.ui.payload = self.parser.parse_body();
3438
};

parser.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,45 @@ var GitHub = (function () {
4747
return gh_parser;
4848
})();
4949

50+
var GitLab = (function () {
51+
var gl_parser = function () { Parser.apply(this, arguments); };
52+
util.inherits(gl_parser, Parser);
53+
54+
gl_parser.prototype.parse_body = function () {
55+
try { return (this.payload = JSON.parse(this.body)); }
56+
catch (e) { return undefined; }
57+
};
58+
59+
gl_parser.prototype.verify_signature = function () {
60+
return true;
61+
};
62+
63+
gl_parser.prototype.extract = function () {
64+
if (!(this.payload.repository &&
65+
this.payload.repository.git_ssh_url &&
66+
this.payload.repository.homepage &&
67+
this.payload.ref &&
68+
this.payload.total_commits_count &&
69+
this.payload.commits)) {
70+
return undefined;
71+
}
72+
73+
var slug = this.payload.repository.git_ssh_url.split(':')[1];
74+
if (slug.endsWith('.git')) {
75+
slug = slug.slice(0,-4);
76+
}
77+
78+
return (this.data = {
79+
slug: slug,
80+
branch: this.payload.ref.replace(/^refs\/heads\//, ''),
81+
url: this.payload.repository.homepage,
82+
commit: this.payload.total_commits_count > 0 ? this.payload.commits.slice(-1) : undefined
83+
});
84+
};
85+
86+
return gl_parser;
87+
})();
88+
5089
var Travis = (function () {
5190
var travis_parser = function () { Parser.apply(this, arguments); };
5291
util.inherits(travis_parser, Parser);
@@ -82,5 +121,6 @@ var Travis = (function () {
82121

83122
module.exports = {
84123
GitHub: GitHub,
124+
GitLab: GitLab,
85125
Travis: Travis
86126
};

0 commit comments

Comments
 (0)