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

Commit

Permalink
Merge pull request #203 from ascariandrea/create-release
Browse files Browse the repository at this point in the history
Release API.
  • Loading branch information
pksunkara committed Oct 13, 2015
2 parents f6873f2 + 4b87acd commit cbbfee1
Show file tree
Hide file tree
Showing 14 changed files with 308 additions and 22 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
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var ghissue = client.issue('pksunkara/hub', 37);
var ghmilestone = client.milestone('pksunkara/hub', 37);
var ghlabel = client.label('pksunkara/hub', 'todo');
var ghpr = client.pr('pksunkara/hub', 37);
var ghrelease = client.release('pksunkara/hub', 37);
var ghgist = client.gist();
var ghteam = client.team(37);
var ghnotification = client.notification(37);
Expand Down Expand Up @@ -993,7 +994,39 @@ ghpr.comments(callback); //array of comments
```js
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
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 @@ -1226,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
1 change: 1 addition & 0 deletions lib/octonode.js

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

40 changes: 28 additions & 12 deletions lib/octonode/client.js

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

59 changes: 59 additions & 0 deletions lib/octonode/release.js

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

21 changes: 21 additions & 0 deletions lib/octonode/repo.js

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

3 changes: 3 additions & 0 deletions src/octonode.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ octonode = module.exports =
# [Team](octonode/team.html) class for github
team: require './octonode/team'

# [Release](octonode/release.html) class for github
release: require './octonode/release'

# [Gist](octonode/gist.html) class for github
gist: require './octonode/gist'

Expand Down
34 changes: 24 additions & 10 deletions src/octonode/client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Org = require './org'
Gist = require './gist'
Team = require './team'
Pr = require './pr'
Release = require './release'
Issue = require './issue'
Milestone = require './milestone'
Label = require './label'
Expand Down Expand Up @@ -64,6 +65,9 @@ class Client
pr: (repo, number) ->
new Pr repo, number, @

release: (repo, number) ->
new Release repo, number, @

# Get search instance for client
search: ->
new Search @
Expand Down Expand Up @@ -107,12 +111,14 @@ class Client
else
separator = '?'

urlFromPath = url.parse path

_url = url.format
protocol: @options and @options.protocol or "https:"
auth: if @token and @token.username and @token.password then "#{@token.username}:#{@token.password}" else ''
hostname: @options and @options.hostname or "api.github.com"
port: @options and @options.port
pathname: path
protocol: urlFromPath.protocol or @options and @options.protocol or "https:"
auth: urlFromPath.auth or if @token and @token.username and @token.password then "#{@token.username}:#{@token.password}" else ''
hostname: urlFromPath.hostname or @options and @options.hostname or "api.github.com"
port: urlFromPath.port or @options and @options.port
pathname: urlFromPath.pathname
query: query

if q
Expand Down Expand Up @@ -162,14 +168,22 @@ class Client
@errorHandle res, body, callback

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

reqDefaultOption =
uri: @buildUrl path, options.query
method: 'POST'
body: JSON.stringify content
headers:
'Content-Type': 'application/json'
), (err, res, body) =>

if content
reqDefaultOption.body = JSON.stringify content

reqOpt = @requestOptions extend reqDefaultOption, options
@request reqOpt, (err, res, body) =>
return callback(err) if err
@errorHandle res, body, callback

Expand Down
40 changes: 40 additions & 0 deletions src/octonode/release.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# release.coffee: Github release class
#
# Copyright © 2013 Josh Priestley. All rights reserved
#

# Initiate class
class Release

constructor: (@repo, @number, @client) ->

# Get a single release
# '/repos/pksunkara/hub/releases/37' GET
info: (cb) ->
@client.get "/repos/#{@repo}/releases/#{@number}", (err, s, b, h) ->
return cb(err) if err
if s isnt 200 then cb(new Error("Release info error")) else cb null, b, h

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

options =
query:
name: optionsOrCb.name || 'archive.zip'
body: file
headers:
'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

# Export module
module.exports = Release
14 changes: 14 additions & 0 deletions src/octonode/repo.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ class Repo
return cb(err) if err
if s isnt 200 then cb(new Error("Repo releases error")) else cb null, b, h

# 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, numberOrRelease

# 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

# Get the languages for a repository
# '/repos/pksunkara/hub/languages' GET
languages: (cb) ->
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.
Loading

0 comments on commit cbbfee1

Please sign in to comment.