Skip to content
This repository has been archived by the owner on May 25, 2020. It is now read-only.

Commit

Permalink
feat(api): fetch and return last version git head sha
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-dezandee committed Jan 15, 2016
1 parent 9afc4e6 commit a393b0e
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 138 deletions.
40 changes: 40 additions & 0 deletions src/head.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Dependencies
*/

import SemanticReleaseError from '@semantic-release/error';
import request from 'request';
import getRepository from './package.js';

/**
* Privates
*/

const GITHUB_API = 'https://api.github.com';

/**
* Interface
*/

export default function getHead(pack, version, callback) {
const requestSettings = {
url: `${GITHUB_API}/repos/${getRepository(pack)}/tags`,
json: true,
};

request.get(requestSettings, (error, response, tags = []) => {
if (error) return callback(error);

if (response.statusCode === 200) {
tags.forEach(tag => {
if (tag.name === `v${version}`) {
return callback(null, tag.commit.sha);
}
});
}

return callback(new SemanticReleaseError(
`GitHub tag missing on remote: v${version}`
));
});
}
37 changes: 11 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,25 @@
* Dependencies
*/

import SemanticReleaseError from '@semantic-release/error';
import request from 'request';
import atomVersion from './version.js';
import getHead from './head.js';

/**
* Interface
*/

const ATOM_REGISTRY = 'https://atom.io';
export default function lastRelease(pluginConfig, { pkg }, callback) {
atomVersion(pkg, (err1, version = null) => {
if (err1) return callback(err1);
if (!!version) return callback(null, {});

export default function (pluginConfig, { pkg }, callback) {
const requestSettings = {
url: `${ATOM_REGISTRY}/api/packages/${pkg.name}`,
json: true,
};
getHead(pkg, version, (err2, gitHead = null) => {
if (err2) return callback(err2);

request.get(requestSettings, (error, response, body = {}) => {
if (error) {
return callback(error);
}

if (response.statusCode === 404) {
return callback(null, {});
}

if (response.statusCode === 200 && body.releases && body.releases.latest) {
return callback(null, {
version: body.releases.latest,
tag: 'latest',
gitHead,
version,
});
}

const message = body.message || body.error || body;
return callback(new SemanticReleaseError(
`Requesting package failed: ${JSON.stringify(message)}`
));
});
});
}
20 changes: 20 additions & 0 deletions src/package.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Dependencies
*/

import url from 'url';

/**
* Interface
*/

// From https://github.com/atom/apm/blob/master/src%2Fpackages.coffee
export default function getRepository(pack = {}) {
const repository = pack.repository && pack.repository.url || pack.repository;
if (!!repository) {
const repoPath = url.parse(repository.replace(/\.git$/, '')).pathname;
const [name, owner] = repoPath.split('/').slice(-2);
if (name && owner) return `${name}/${owner}`;
}
return null;
}
41 changes: 41 additions & 0 deletions src/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Dependencies
*/

import SemanticReleaseError from '@semantic-release/error';
import request from 'request';

/**
* Privates
*/

const ATOM_REGISTRY = 'https://atom.io';

/**
* Interface
*/

export default function atomVersion(pkg, callback) {
const requestSettings = {
url: `${ATOM_REGISTRY}/api/packages/${pkg.name}`,
json: true,
};

request.get(requestSettings, (error, response, body = {}) => {
if (error) return callback(error);

if (response.statusCode === 404) {
return callback(null, null);
}

const version = body.releases && body.releases.latest;
if (response.statusCode === 200 && !!version) {
return callback(null, version);
}

const message = body.message || body.error || body;
return callback(new SemanticReleaseError(
`Requesting package failed: ${JSON.stringify(message)}`
));
});
}
63 changes: 63 additions & 0 deletions test/head.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Dependencies
*/

import { expect } from 'chai';
import SemanticReleaseError from '@semantic-release/error';
import nock from 'nock';
import getHead from '../src/head.js';

/**
* Tests
*/

const GITHUB_API = 'https://api.github.com';
const github = nock(GITHUB_API);

describe('getHead from github', () => {
it('get sha from version', done => {
github.get('/repos/test/test/tags').reply(200, [{
name: 'v1.1.5',
commit: { sha: 'f9977551c125796199a739f48907eccf71adaca2' },
}]);

getHead({ repository: 'https://github.com/test/test.git' }, '1.1.5', (error, sha) => {
expect(error).to.equal(null);
expect(sha).to.equal('f9977551c125796199a739f48907eccf71adaca2');
done();
});
});

it('missing version', done => {
github.get('/repos/test/test/tags').reply(200, [{
name: 'hey',
commit: { sha: 'f9977551c125796199a739f48907eccf71adaca2' },
}]);

getHead({ repository: 'https://github.com/test/test.git' }, '1.1.5', (error, sha) => {
expect(error).to.exist.and.be.instanceof(SemanticReleaseError);
expect(sha).to.equal(undefined);
done();
});
});

it('missing package', done => {
github.get('/repos/test/missing/tags').reply(404, { message: 'Not Found' });

getHead({ repository: 'https://github.com/test/missing.git' }, '1.0.0', (error, sha) => {
expect(error).to.exist.and.be.instanceof(SemanticReleaseError);
expect(sha).to.equal(undefined);
done();
});
});

it('request error', done => {
github.get('/repos/test/test/tags').replyWithError('Error');

getHead({ repository: 'https://github.com/test/test.git' }, '1.1.5', (error, sha) => {
expect(error).to.exist.and.be.instanceof(Error);
expect(sha).to.equal(undefined);
done();
});
});
});
Empty file added test/index.js
Empty file.
74 changes: 0 additions & 74 deletions test/index.test.js

This file was deleted.

37 changes: 37 additions & 0 deletions test/package.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Dependencies
*/

import { expect } from 'chai';
import getRepository from '../src/package.js';

/**
* Tests
*/

describe('getRepository from package.json', () => {
it('empty', () => {
expect(getRepository()).to.equal(null);
});

it('with object', () => {
expect(getRepository({
repository: {
type: 'git',
url: 'https://github.com/Adezandee/fast-eslint.git',
},
})).to.equal('Adezandee/fast-eslint');
});

it('with string', () => {
expect(getRepository({
repository: 'https://github.com/Adezandee/fast-eslint.git',
})).to.equal('Adezandee/fast-eslint');
});

it('with incomplete', () => {
expect(getRepository({
repository: 'https://github.com/fast-eslint.git',
})).to.equal(null);
});
});
38 changes: 0 additions & 38 deletions test/registry.mock.js

This file was deleted.

0 comments on commit a393b0e

Please sign in to comment.