Skip to content
This repository has been archived by the owner on Aug 23, 2024. It is now read-only.

Commit

Permalink
Add release tests, update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Ascari committed Oct 13, 2015
1 parent 272d956 commit 4b87acd
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 48 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ npm-debug.log
*.swp
docs
.DS_Store

test/config.json
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -996,13 +996,37 @@ ghpr.files(callback); //array of files
```
## Github releases api

### Create release (POST /repos/pksunkara/releases)
```js
ghrepo.release({
tag_name: 'v1.0.0',
draft: true
}, callback);
```

#### Upload assets in a release (POST /uploads.github.com/repos/pksunkara/hub/releases/37/assets?name=archve-v0.0.1.zip)

```js
const file = fs.readFileSync(__dirname, 'archive-v0.0.1.zip');
ghrelease.assets('uploads.github.com', 'archive-v0.0.1.zip', file, 'application/zip', callback);
var archive = fs.readFileSync(__dirname, 'archive-v0.0.1.zip');
// Will upload a file with the default options
/*
{
name: 'archive.zip',
contentType: 'application/zip',
uploadHost: 'uploads.github.com'
}
*/
ghrelease.uploadAssets(archive, callback);

// Will upload a file with your custom options
var image = fs.readFileSync(__dirname, 'octonode.png');
var options = {
name: 'octonode.png',
contentType: 'image/png',
uplaodHost: 'uploads.github.com'
};
ghrelease.uplaodAssets(image, options, callback)
```

## Github gists api

#### List authenticated user's gists (GET /gists)
Expand Down Expand Up @@ -1235,6 +1259,9 @@ ghsearch.code({
```

## Testing
Before run test copy the `config.example.json` file in `test` folder to `config.json` and populate it with your github information.

Is suggested to fork `https://github.com/octocat/Spoon-Knife` repo and run test on your copy.
```
npm test
```
Expand Down
4 changes: 2 additions & 2 deletions lib/octonode/client.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions lib/octonode/release.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 6 additions & 17 deletions lib/octonode/repo.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/octonode/client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ class Client

# Github api POST request
post: (path, content, options, callback) ->
if !callback?
if !callback? and typeof options is 'function'
callback = options
options = {}

reqDefaultOption =
uri: @buildUrl path, options.params
uri: @buildUrl path, options.query
method: 'POST'
headers:
'Content-Type': 'application/json'
Expand Down
15 changes: 11 additions & 4 deletions src/octonode/release.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@ class Release

# Attach a file to a release
# '/repos/pksunkara/hub/releases/37/assets?name=archive.zip' POST
assets: (uploadHost, name, file, contentType, cb) ->
uploadAssets: (file, optionsOrCb, cb) ->
if !cb? and typeof optionsOrCb is 'function'
cb = optionsOrCb
optionsOrCb = {}

options =
params:
name: name
query:
name: optionsOrCb.name || 'archive.zip'
body: file
headers:
'Content-Type': contentType
'Content-Type': optionsOrCb.contentType || 'application/zip'

uploadHost = optionsOrCb.uploadHost || 'uploads.github.com'

@client.post "https://#{uploadHost}/repos/#{@repo}/releases/#{@number}/assets", null, options, (err, s, b, h) ->
return cb(err) if err
if s isnt 201 then cb(new Error("Release assets error")) else cb null, b, h
Expand Down
24 changes: 9 additions & 15 deletions src/octonode/repo.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,17 @@ class Repo
return cb(err) if err
if s isnt 200 then cb(new Error("Repo releases error")) else cb null, b, h

release: (numberOrCb, cb) ->
if typeof cb is 'function' and typeof numberOrCb is 'object'
@createRelease numberOrCb, cb
# Get release instance for a repo
release: (numberOrRelease, cb) ->
if typeof cb is 'function' and typeof numberOrRelease is 'object'
@createRelease numberOrRelease, cb
else
@client.release @name, numberOrCb
@client.release @name, numberOrRelease

# Create a relase for a repository
# '/repos/pksunkara/hub/releases' POST
createRelease: (tag, cbOrOptions, cb) ->
if !cb? and cbOrOptions
cb = cbOrOptions
cbOrOptions = null
param = {tag_name: "#{tag}"}
else if typeof cbOrOptions is 'object'
param = cbOrOptions
param.tag_name = "#{tag}"
@client.post "/repos/#{@name}/releases", param, (err, s, b, h) ->
# Create a relase for a repo
# '/repo/pksunkara/releases' POST
createRelease: (release, cb) ->
@client.post "/repos/#{@name}/releases", release, (err, s, b, h) ->
return cb(err) if err
if s isnt 201 then cb(new Error("Repo createRelease error")) else cb null, b, h

Expand Down
7 changes: 7 additions & 0 deletions test/config.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"repo": "username/Spoon-Knife",
"auth": {
"username": "username",
"password": "******"
}
}
Binary file added test/file.zip
Binary file not shown.
Binary file added test/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions test/release/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
var asrt = require('assert')
, fs = require('fs')
, path = require('path')
, vows = require('vows')
, sprt = require('../support')
, ghub = require('../../lib/octonode')
, config
, releaseNumber
, client;
config = JSON.parse(fs.readFileSync(path.join(__dirname, '../config.json')));

client = ghub.client(config.auth);

vows.describe('Release').addBatch({
'get release': {
topic: function() {
var release = client.release(config.repo, 1);
release.info(this.callback);
},
'shouldn\'t find the related release': function(err, result) {
asrt.equal(err.statusCode, 404);
asrt.equal(err.body.message, 'Not Found');
}
}
}).addBatch({
'create release': {
topic: function() {
var repo = client.repo(config.repo);
repo.release({ tag_name: 'v1.0.0', draft: true }, this.callback);
},
'should create a release with tag v1.0.0': function(err, result) {
var tag = result.tag_name;
var draft = result.draft;
releaseNumber = result.id;
asrt.equal(result.tag_name, 'v1.0.0');
asrt.equal(result.draft, true);
}
}
}).addBatch({
'upload assets': {
topic: function() {
return client.release(config.repo, releaseNumber);
},
'with no options': {
topic: function(release) {
var file = fs.readFileSync(path.join(__dirname, '../file.zip'));
release.uploadAssets(file, this.callback);

},
'should upload an asset without passing custom options': function(err, result) {
asrt.equal(result.name, 'archive.zip');
asrt.equal(result.content_type, 'application/zip');
}
},
'with custom options': {
topic: function(release) {
var file = fs.readFileSync(path.join(__dirname, '../image.png'));
var options = {
name: 'octonode.png',
contentType: 'image/png',
uploadHost: 'uploads.github.com'
};
release.uploadAssets(file, options, this.callback);

},
'should upload an asset passing custom options': function(err, result) {
asrt.equal(result.name, 'octonode.png');
asrt.equal(result.content_type, 'image/png');
}
}
}
})
.export(module);

0 comments on commit 4b87acd

Please sign in to comment.