Skip to content

Commit

Permalink
feat(gitlab): fetching release notes from GitLab, closes #21
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Aug 17, 2016
1 parent b7cf41f commit d938143
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 18 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ You can also pass second argument to keep version cleanup error messages quiet
available(query, true) ...
## Supported servers
Public and private NPM registries, GitHub (public) and GitLab
(public and private) servers. For private GitLab server, you should have
environment variable `GITLAB_AUTH_TOKEN` set with your
[personal access token](https://gitlab.com/profile/account).
```sh
GITLAB_AUTH_TOKEN=xxxyyyy vers @org/my-module
```
## Debug
To debug this program, run it with `DEBUG=vers` variable
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"check-more-types": "2.22.0",
"console.table": "0.7.0",
"debug": "2.2.0",
"gitlab": "1.7.1",
"lazy-ass": "1.5.0",
"lodash": "4.14.2",
"moment": "2.14.1",
Expand Down
59 changes: 45 additions & 14 deletions src/fetch-release-notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ var request = require('axios');
var la = require('lazy-ass');
var check = require('check-more-types');
var debug = require('debug')('vers');
// var gitlab = require('gitlab');
var parseGitServerUrl = require('./git-server-url');

function urlFromGitHub(user, repo) {
const githubUrl = 'https://api.github.com/repos/';
const url = githubUrl + user + '/' + repo + '/releases';
return url;
function githubRemoteApi(url, user, repo) {
const server = parseGitServerUrl(url);
la(check.unemptyString(server), 'missing remote server url from', url);
const apiUrl = server + '/repos/' + user + '/' + repo + '/releases';
return apiUrl;
}

function gitlabRemoteApi(url, user, repo) {
const server = parseGitServerUrl(url);
la(check.unemptyString(server), 'missing remote server url from', url);
const apiUrl = server + '/api/v3/projects/' + user + '%2F' + repo + '/repository/tags';
return apiUrl;
}

function isGitHub(type) {
Expand All @@ -30,31 +38,54 @@ function supportedServer(type) {
);
}

function fetchFromGitlab(repo) {
la(repo.server === 'gitlab', 'invalid gitlab server', repo);
function is200(r) {
la(r.status === 200, 'received error status', r.status);
return r.data;
}

function fetchFromGitlab(available, repo) {
la(isGitlab(repo.server), 'invalid gitlab server', repo);

debug('fetching release notes from gitlab server');
debug(repo);

const url = gitlabRemoteApi(repo.url, repo.user, repo.repo);
debug('fetching releases from gitlab %s', url);

if (!hasGitLabToken()) {
console.error('Missing gitlab access token');
return available;
}
return available;

var options = {
method: 'GET',
url: url,
headers: {
'PRIVATE-TOKEN': process.env.GITLAB_AUTH_TOKEN
}
};
return request(options)
.then(is200)
.then(function (list) {
la(check.array(list), 'expected list of tags', list);
debug('fetched %d tags from gitlab', list.length);
available.releases = list;
return available;
})
.catch(function (err) {
debug('error fetching release notes from', url);
debug(err);
return available;
});
}

function fetchFromGitHub(available, repo) {
la(isGitHub(repo.server), 'invalid github server', repo);
const url = urlFromGitHub(repo.user, repo.repo);
const url = githubRemoteApi(repo.url, repo.user, repo.repo);
debug('fetching releases from github %s', url);

return request.get(url)
.then(function (r) {
la(r.status === 200, 'received error status', r.status);
return r.data;
})
.then(is200)
.then(function (list) {
debug('got %d releases', list.length);
la(check.array(list), 'expected list of releases', list);
Expand All @@ -63,7 +94,7 @@ function fetchFromGitHub(available, repo) {
return available;
})
.catch(function (err) {
debug('error fetching release notes');
debug('error fetching release notes from', url);
debug(err);
return available;
});
Expand Down
2 changes: 1 addition & 1 deletion src/git-server-url-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ describe('git server url', function () {
it('returns github url', function () {
var git = 'git+https://github.com/bahmutov/manpm.git';
var url = server(git);
la(url === 'https://github.com', url);
la(url === 'https://api.github.com', url);
});
});
7 changes: 7 additions & 0 deletions src/git-server-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ const parseUrl = require('url').parse;
const gitHttps = /^git\+https:\/\//;
const gitAt = /^git@/;

function isGitHub(hostname) {
return hostname.indexOf('github') !== -1;
}

function parseGitHttps(url) {
la(gitHttps.test(url), 'invalid', url);
const removedGit = url.replace(/^git\+/, '');
const parsed = parseUrl(removedGit);
if (isGitHub(parsed.hostname)) {
return parsed.protocol + '//api.' + parsed.hostname;
}
return parsed.protocol + '//' + parsed.hostname;
}

Expand Down
14 changes: 12 additions & 2 deletions src/human-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function removeBoldAroundScope(s) {
return s.replace(match[0], match[1] + ' ');
}

function cleanReleaseNotes(notes) {
function cleanReleaseNotesGitHub(notes) {
la(is.unemptyString(notes), 'expected notes string', notes);
const lines = notes.split('\n')
.map((s) => s.trim())
Expand All @@ -119,6 +119,12 @@ function cleanReleaseNotes(notes) {
return lines[0];
}

function cleanReleaseNotesGitLab(commit) {
la(is.object(commit), 'missing commit', commit);
la(is.string(commit.message), 'missing commit message in', commit);
return commit.message.split('\n')[0].trim();
}

/*
returns list of releases as objects, each one being
{
Expand Down Expand Up @@ -149,8 +155,12 @@ function toHumanFormat(releases) {
o.name === 'v' + r.version;
}
const release = _.find(releases.releases, versionMatch);
// debug('for release %s found notes', r.version);
// debug(release);

if (release) {
r.release = cleanReleaseNotes(release.body);
r.release = release.body ? cleanReleaseNotesGitHub(release.body)
: cleanReleaseNotesGitLab(release.commit);
}
});
}
Expand Down

0 comments on commit d938143

Please sign in to comment.