├── .gitattributes ├── .jshintignore ├── test ├── mocha.opts ├── Dockerfile ├── test.js └── authenticated-tests.js ├── .github └── FUNDING.yml ├── .npmignore ├── .travis.yml ├── .gitignore ├── package.json ├── .jshintrc ├── .jscsrc ├── CHANGES.md ├── gulpfile.js ├── STYLE.md ├── README.md ├── LICENSE └── src └── api.js /.gitattributes: -------------------------------------------------------------------------------- 1 | * text eol=lf -------------------------------------------------------------------------------- /.jshintignore: -------------------------------------------------------------------------------- 1 | node_modules/** -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --recursive -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [RyanTheAllmighty] 2 | -------------------------------------------------------------------------------- /test/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:16.04 2 | 3 | RUN echo Hello 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | test/ 2 | 3 | .gitattributes 4 | .gitignore 5 | .jscsrc 6 | .jshintrc 7 | gulpfile.js 8 | STYLE.md -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '4.2' 4 | - '5' 5 | before_script: 6 | - npm install -g gulp 7 | cache: 8 | directories: 9 | - $HOME/.npm -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | 29 | # WebStorm files 30 | .idea 31 | 32 | # My play file 33 | play.js -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "docker-hub-api", 3 | "version": "0.8.0", 4 | "description": "Docker Hub API is an API library written for NodeJS to access the official Docker Hub/Registry", 5 | "main": "src/api.js", 6 | "engines": { 7 | "node": ">=4.2.0" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/RyanTheAllmighty/Docker-Hub-API.git" 12 | }, 13 | "author": "Ryan Dowling ", 14 | "license": "GPL-3.0", 15 | "bugs": { 16 | "url": "https://github.com/RyanTheAllmighty/Docker-Hub-API/issues" 17 | }, 18 | "homepage": "https://github.com/RyanTheAllmighty/Docker-Hub-API", 19 | "scripts": { 20 | "test": "gulp" 21 | }, 22 | "dependencies": { 23 | "lodash": "^3.10.1", 24 | "request": "^2.67.0" 25 | }, 26 | "devDependencies": { 27 | "chai": "^3.3.0", 28 | "gulp": "^3.9.0", 29 | "gulp-jscs": "^3.0.2", 30 | "gulp-jshint": "^1.12.0", 31 | "gulp-mocha": "^2.2.0", 32 | "mocha": "^2.3.3", 33 | "sinon": "^1.17.1" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "maxerr": 50, 3 | "bitwise": true, 4 | "camelcase": false, 5 | "curly": true, 6 | "eqeqeq": true, 7 | "forin": true, 8 | "freeze": true, 9 | "immed": false, 10 | "indent": 4, 11 | "latedef": false, 12 | "newcap": true, 13 | "noarg": true, 14 | "noempty": true, 15 | "nonbsp": true, 16 | "nonew": true, 17 | "plusplus": false, 18 | "quotmark": "single", 19 | "undef": true, 20 | "unused": true, 21 | "strict": true, 22 | "maxparams": false, 23 | "maxdepth": false, 24 | "maxstatements": false, 25 | "maxcomplexity": false, 26 | "maxlen": 200, 27 | "asi": false, 28 | "boss": false, 29 | "debug": false, 30 | "eqnull": false, 31 | "esnext": true, 32 | "moz": false, 33 | "evil": false, 34 | "expr": false, 35 | "funcscope": false, 36 | "globalstrict": true, 37 | "iterator": false, 38 | "lastsemic": false, 39 | "laxbreak": false, 40 | "laxcomma": false, 41 | "loopfunc": false, 42 | "multistr": false, 43 | "noyield": false, 44 | "notypeof": false, 45 | "proto": false, 46 | "scripturl": false, 47 | "shadow": false, 48 | "sub": false, 49 | "supernew": false, 50 | "validthis": false, 51 | "browser": true, 52 | "browserify": false, 53 | "couch": false, 54 | "devel": true, 55 | "dojo": false, 56 | "jasmine": false, 57 | "jquery": false, 58 | "mocha": true, 59 | "mootools": false, 60 | "node": true, 61 | "nonstandard": false, 62 | "phantom": false, 63 | "prototypejs": false, 64 | "qunit": false, 65 | "rhino": false, 66 | "shelljs": false, 67 | "typed": false, 68 | "worker": false, 69 | "wsh": false, 70 | "yui": false 71 | } -------------------------------------------------------------------------------- /.jscsrc: -------------------------------------------------------------------------------- 1 | { 2 | "requireCurlyBraces": [ 3 | "if", 4 | "else", 5 | "for", 6 | "while", 7 | "do", 8 | "try", 9 | "catch" 10 | ], 11 | "requireOperatorBeforeLineBreak": true, 12 | "maximumLineLength": { 13 | "value": 200, 14 | "allowComments": true, 15 | "allowRegex": true 16 | }, 17 | "validateIndentation": 4, 18 | "validateQuoteMarks": "'", 19 | 20 | "disallowMultipleLineStrings": true, 21 | "disallowMixedSpacesAndTabs": true, 22 | "disallowSpaceAfterPrefixUnaryOperators": true, 23 | "disallowMultipleVarDecl": null, 24 | 25 | "requireSpaceAfterKeywords": [ 26 | "if", 27 | "else", 28 | "for", 29 | "while", 30 | "do", 31 | "switch", 32 | "return", 33 | "try", 34 | "catch" 35 | ], 36 | "requireSpaceBeforeBinaryOperators": [ 37 | "=", "+=", "-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=", 38 | "&=", "|=", "^=", "+=", 39 | 40 | "+", "-", "*", "/", "%", "<<", ">>", ">>>", "&", 41 | "|", "^", "&&", "||", "===", "==", ">=", 42 | "<=", "<", ">", "!=", "!==" 43 | ], 44 | "requireSpaceAfterBinaryOperators": true, 45 | "requireSpacesInConditionalExpression": true, 46 | "requireSpaceBeforeBlockStatements": true, 47 | "disallowSpacesInsideObjectBrackets": "all", 48 | "disallowSpacesInsideArrayBrackets": "all", 49 | "disallowSpacesInsideParentheses": true, 50 | 51 | "jsDoc": { 52 | "checkAnnotations": true, 53 | "checkParamNames": true, 54 | "requireParamTypes": true, 55 | "checkReturnTypes": true, 56 | "checkTypes": true 57 | }, 58 | 59 | "disallowMultipleLineBreaks": true, 60 | 61 | "disallowCommaBeforeLineBreak": null, 62 | "disallowDanglingUnderscores": null, 63 | "disallowEmptyBlocks": null, 64 | "disallowTrailingComma": null, 65 | "disallowTrailingWhitespace": null, 66 | "requireCommaBeforeLineBreak": null, 67 | "requireDotNotation": null, 68 | "requireMultipleVarDecl": null, 69 | "requireParenthesesAroundIIFE": true, 70 | "requireCamelCaseOrUpperCaseIdentifiers": null 71 | } -------------------------------------------------------------------------------- /CHANGES.md: -------------------------------------------------------------------------------- 1 | # Changes 2 | This file marks the changes in each version. 3 | 4 | ## 0.8 5 | ### 0.8.0 6 | #### Additions 7 | - Add `logout()` method 8 | 9 | #### Fixes 10 | - Fix the logic around error throwing in requests with `error: false` in the response 11 | 12 | ## 0.7 13 | ### 0.7.0 14 | #### Additions 15 | - Add in setRepositoryPrivacy option. Closes #20 16 | 17 | ## 0.6 18 | ### 0.6.0 19 | #### Additions 20 | - Add package-lock.json 21 | 22 | #### Fixes 23 | - Fix issues with createAutomatedBuild not working with users passed in parameters. Fixes #14 24 | - Fix saveBuildTag not being defined as in the README. Fixes #12 25 | 26 | ## 0.5 27 | ### 0.5.1 28 | #### Fixes 29 | - Fix entrypoint in package.json. 30 | 31 | ### 0.5.0 32 | #### Additions 33 | - Add in methods for querying and interacting with automated builds. 34 | - Add in methods for querying and interacting with collaborators for repositories. 35 | - Add in `registrySettings` method to get registry settings (number of private repositories available/used) for the logged in user. 36 | 37 | ## 0.4 38 | ### 0.4.0 39 | #### Additions 40 | - Add in `deleteTag` thanks to [Borales](https://github.com/Borales). 41 | 42 | ## 0.3 43 | ### 0.3.1 44 | #### Fixes 45 | - Fix issue with creating non private repositories. 46 | 47 | ### 0.3.0 48 | #### Additions 49 | - Add in `starRepository` and `unstarRepository` methods to star/unstar a given repository. 50 | - Add in `createRepository` and `deleteRepository` methods to create/delete a repository. 51 | 52 | ## 0.2 53 | ### 0.2.0 54 | #### Additions 55 | - Add in `comments` method to get the comments for a repository. 56 | - Add in `setRepositoryDescription` method to set the short/full description for your own repositories. 57 | - Add in `webhooks` method to get the webhooks for a repository you own. 58 | - Add in `createWebhook` method to create a webhook for a repository you own. 59 | - Add in `createWebhookHook` method to create a webhook hook for an existing webhook. 60 | - Add in `deleteWebhook` method to delete a webhook for a repository you own. 61 | 62 | ## 0.1 63 | ### 0.1.0 64 | #### Additions 65 | - Add in logging in methods to access authenticated routes. 66 | 67 | ## 0.0 68 | ### 0.0.2 69 | #### Additions 70 | - Addition of a bunch of stuff (technical I know). 71 | 72 | ### 0.0.1 73 | #### Additions 74 | - Initial release 75 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Docker Hub API - https://github.com/RyanTheAllmighty/Docker-Hub-API 3 | * Copyright (C) 2015 RyanTheAllmighty 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | (function () { 20 | 'use strict'; 21 | 22 | const gulp = require('gulp'); 23 | const jscs = require('gulp-jscs'); 24 | const mocha = require('gulp-mocha'); 25 | const jshint = require('gulp-jshint'); 26 | 27 | const options = { 28 | files: { 29 | js: ['lib/**/*.js', 'test/**/*.js'], 30 | tests: ['test/**/*.js'] 31 | } 32 | }; 33 | 34 | gulp.task('jshint', function () { 35 | return gulp.src(options.files.js) 36 | .pipe(jshint()) 37 | .pipe(jshint.reporter()) 38 | .pipe(jshint.reporter('fail')); 39 | }); 40 | 41 | gulp.task('jscs', function () { 42 | return gulp.src(options.files.js) 43 | .pipe(jscs()) 44 | .pipe(jscs.reporter()) 45 | .pipe(jscs.reporter('fail')); 46 | }); 47 | 48 | gulp.task('test', function () { 49 | return gulp.src(options.files.tests) 50 | .pipe(mocha({ 51 | reporter: 'min', 52 | clearRequireCache: true, 53 | ignoreLeaks: true 54 | })); 55 | }); 56 | 57 | gulp.task('watch', function () { 58 | gulp.watch(options.files.js, ['jshint', 'jscs', 'test']); 59 | gulp.start('default'); 60 | }); 61 | 62 | // The style task which checks styling standards 63 | gulp.task('style', ['jshint', 'jscs']); 64 | 65 | // The default task (called when you run `gulp` from cli) 66 | gulp.task('default', ['jshint', 'jscs', 'test']); 67 | })(); -------------------------------------------------------------------------------- /STYLE.md: -------------------------------------------------------------------------------- 1 | # Style Guide 2 | This is a simple style guide of how I style and present my code (at least try to). 3 | 4 | # Coding Standards 5 | + All line lengths must be kept less than 200 characters and use 4 spaces rather than tab characters. 6 | + All JSON documents should use 4 space indentation. 7 | + Don't do large code commits. My preference is a single commit for a single fix/addition rather than bundled up commits. 8 | + Document appropriately. While there is no need to put single line comments in for everything, having doc blocks and comments where necessary helps others see what the code does. 9 | + Make sure all code adheres to the provided JSHint and JSCS standards. Running 'gulp style' will run both checkers and check for any issues 10 | + Increment the version in the package.json before the last commit before release. Nothing worse than having to question if it's already been incremented or not 11 | 12 | # Classes 13 | Since this application is using ECMAScript 6, we have access to classes. 14 | 15 | Classes should always use Symbols to hide the original data so it cannot be accessed with appropriate getters and setters. 16 | 17 | Immediately in the body of the class should be the constructor followed by any getter/setter methods (using the get/set keyword) and then followed by any other methods of the class. 18 | 19 | Lastly any callbacks used internally by the class should be referenced at the bottom of the file (outside of the class) for JSDoc purposes. 20 | 21 | # Requires 22 | When using the require syntax in js files, all require statements should be at the top of the file, under the license declaration and strict statement. 23 | 24 | When using modules from different methods, they should be separated out into groups, with the applications internal modules first, then any application internal classes and then finally any external modules. 25 | 26 | All require groups should be sorted by length with the shortest statement at the top and the longest at the bottom. In the case of a tie, it should be in alphabetical order by the variables name. 27 | 28 | # Styling Guidelines 29 | For details on JSDoc used for all JavaScript files, see [this website](http://usejsdoc.org/). 30 | 31 | + Make sure all doc block information has a period at the end. 32 | + Make sure all doc block @ elements don't have a period at the end. 33 | + Make sure all type declarations use the Type definitions. For instance {String} instead of {string}. 34 | + Make sure all comments after the - in @ doc block elements start with a lowercase 35 | + Make sure all comments not in doc blocks don't end in a period. 36 | + Make sure there is a blank line between any main doc block information and any @elements. 37 | + Make sure all callbacks are documented at the very bottom of the file. 38 | + Make sure all JS files included with script tags are wrapped in IIFE's. 39 | + Make sure there are no multi line variables. All variables should be declared one per line with no multi line declarations. 40 | + Make sure to use let instead of var wherever possible. 41 | + When needing to access this in a callback of a method, the variable to store this should be called self. For example (let self = this;). 42 | + All files should be saved with lowercamelCase names. 43 | + All classes should be UpperCamelCase with no spaces or other non alphanumeric characters. 44 | 45 | ## Example 46 | // Some comment. Which doesn't end in a period 47 | 48 | /** 49 | * Where the magic happens. Notice I end in a period. 50 | * 51 | * @param {String} arguments - lower case start. All the arguments passed in from the command line, notice I don't end in a period 52 | */ -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Docker Hub API - https://github.com/RyanTheAllmighty/Docker-Hub-API 3 | * Copyright (C) 2015 RyanTheAllmighty 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | (function () { 20 | 'use strict'; 21 | 22 | const expect = require('chai').expect; 23 | 24 | const dhAPI = require('../src/api'); 25 | 26 | describe('Docker Hub API', function () { 27 | this.timeout(10000); 28 | 29 | describe('#comments', function () { 30 | it('should fetch all the repositories for a user', function () { 31 | return dhAPI.comments('nginx').then(function (info) { 32 | expect(info).to.be.an('array'); 33 | expect(info[0]).to.have.property('id'); 34 | expect(info[0]).to.have.property('comment'); 35 | }); 36 | }); 37 | }); 38 | 39 | describe('#repository', function () { 40 | it('should fetch details about an official nginx image', function () { 41 | return dhAPI.repository('nginx').then(function (info) { 42 | expect(info.user).to.equal('library'); 43 | expect(info.name).to.equal('nginx'); 44 | }); 45 | }); 46 | 47 | it('should fetch details about an official nginx image when _ is passed in as the username', function () { 48 | return dhAPI.repository('_', 'nginx').then(function (info) { 49 | expect(info.user).to.equal('library'); 50 | expect(info.name).to.equal('nginx'); 51 | }); 52 | }); 53 | 54 | it('should fetch details about a given users nginx image', function () { 55 | return dhAPI.repository('ryantheallmighty', 'nginx').then(function (info) { 56 | expect(info.user).to.equal('ryantheallmighty'); 57 | expect(info.name).to.equal('nginx'); 58 | }); 59 | }); 60 | 61 | it('should fetch details about a given users nginx image when the username is not all lowercase', function () { 62 | return dhAPI.repository('RyanTheAllmighty', 'nginx').then(function (info) { 63 | expect(info.user).to.equal('ryantheallmighty'); 64 | expect(info.name).to.equal('nginx'); 65 | }); 66 | }); 67 | }); 68 | 69 | describe('#repositories', function () { 70 | it('should fetch all the repositories for a user', function () { 71 | return dhAPI.repositories('ryantheallmighty').then(function (info) { 72 | expect(info).to.be.an('array'); 73 | expect(info[0]).to.have.property('namespace').and.equal('ryantheallmighty'); 74 | }); 75 | }); 76 | }); 77 | 78 | describe('#repositoriesStarred', function () { 79 | it('should fetch all the stars for a given user', function () { 80 | return dhAPI.repositoriesStarred('ryantheallmighty').then(function (info) { 81 | expect(info).to.be.an('array'); 82 | expect(info[0]).to.have.property('user').and.equal('ryantheallmighty'); 83 | }); 84 | }); 85 | }); 86 | 87 | describe('#tags', function () { 88 | it('should fetch all the tags for an official repository', function () { 89 | return dhAPI.tags('nginx').then(function (info) { 90 | expect(info).to.be.an('array'); 91 | expect(info.length).to.not.equal(1); 92 | expect(info[0]).to.have.property('repository').and.equal(21171); 93 | }); 94 | }); 95 | 96 | it('should fetch all the tags for a given users repository', function () { 97 | return dhAPI.tags('ryantheallmighty', 'nginx').then(function (info) { 98 | expect(info).to.be.an('array'); 99 | expect(info.length).to.not.equal(1); 100 | expect(info[0]).to.have.property('creator').and.equal(534804); 101 | }); 102 | }); 103 | 104 | it('should fetch a single result per page for an official repository', function () { 105 | return dhAPI.tags('nginx', {perPage: 1}).then(function (info) { 106 | expect(info).to.be.an('array'); 107 | expect(info.length).to.equal(1); 108 | expect(info[0]).to.have.property('repository').and.equal(21171); 109 | }); 110 | }); 111 | 112 | it('should fetch a single result per page for a given users repository', function () { 113 | return dhAPI.tags('ryantheallmighty', 'nginx', {perPage: 1}).then(function (info) { 114 | expect(info).to.be.an('array'); 115 | expect(info.length).to.equal(1); 116 | expect(info[0]).to.have.property('creator').and.equal(534804); 117 | }); 118 | }); 119 | }); 120 | 121 | describe('#user', function () { 122 | it('should fetch details about a user', function () { 123 | return dhAPI.user('ryantheallmighty').then(function (info) { 124 | expect(info.id).to.equal('73cdba6ec4154672a2ef01c292f38567'); 125 | expect(info.username).to.equal('ryantheallmighty'); 126 | }); 127 | }); 128 | }); 129 | }); 130 | })(); -------------------------------------------------------------------------------- /test/authenticated-tests.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Docker Hub API - https://github.com/RyanTheAllmighty/Docker-Hub-API 3 | * Copyright (C) 2015 RyanTheAllmighty 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | (function () { 20 | 'use strict'; 21 | 22 | const expect = require('chai').expect; 23 | 24 | const dhAPI = require('../src/api'); 25 | 26 | let loginToken = process.env.DOCKER_HUB_LOGIN_TOKEN; 27 | 28 | describe('Docker Hub API - Logging In', function () { 29 | this.timeout(10000); 30 | 31 | before(function () { 32 | dhAPI.setCacheOptions({enabled: false}); 33 | }); 34 | 35 | describe('#login', function () { 36 | it('should login to a Docker Hub account', function () { 37 | return dhAPI.login(process.env.DOCKER_HUB_USERNAME, process.env.DOCKER_HUB_PASSWORD).then(function (info) { 38 | expect(info).to.not.be.an('undefined'); 39 | 40 | loginToken = info.token; 41 | 42 | return dhAPI.loggedInUser(); 43 | }).then(function (info) { 44 | expect(info).to.have.property('id'); 45 | expect(info).to.have.property('username'); 46 | expect(info).to.have.property('is_admin'); 47 | }); 48 | }); 49 | }); 50 | 51 | describe('#setLoginToken', function () { 52 | it('should allow authenticated requests after setting the login token', function () { 53 | dhAPI.setLoginToken(loginToken); 54 | 55 | return dhAPI.loggedInUser().then(function (info) { 56 | expect(info).to.have.property('id'); 57 | expect(info).to.have.property('username'); 58 | expect(info).to.have.property('is_admin'); 59 | }); 60 | }); 61 | }); 62 | }); 63 | 64 | describe('Docker Hub API - Authenticated Routes', function () { 65 | this.timeout(10000); 66 | 67 | before(function () { 68 | dhAPI.setCacheOptions({enabled: true}); 69 | dhAPI.setLoginToken(loginToken); 70 | }); 71 | 72 | describe('#loggedInUser', function () { 73 | it('should get information about the logged in user', function () { 74 | return dhAPI.loggedInUser().then(function (info) { 75 | expect(info).to.have.property('id'); 76 | expect(info).to.have.property('username'); 77 | expect(info).to.have.property('is_admin'); 78 | }); 79 | }); 80 | }); 81 | 82 | describe('#setRepositoryDescription', function () { 83 | it('should set the descriptions for a repository', function () { 84 | return dhAPI.loggedInUser().then(function (user) { 85 | return dhAPI.repositories(user.username).then(function (repos) { 86 | if (repos.length === 0) { 87 | expect([]).to.be.an('undefined'); // Fail this test since we cannot progress 88 | } 89 | 90 | return dhAPI.repository(user.username, repos[0].name).then(function (repo) { 91 | return dhAPI.setRepositoryDescription(user.username, repos[0].name, {short: repo.description, full: repo.full_description}).then(function (info) { 92 | expect(info).to.have.property('user'); 93 | expect(info).to.have.property('name'); 94 | }); 95 | }); 96 | }); 97 | }); 98 | }); 99 | }); 100 | 101 | describe('Starring Repository Methods', function () { 102 | describe('#starRepository', function () { 103 | it('should star a given repository', function () { 104 | return dhAPI.loggedInUser().then(function (user) { 105 | return dhAPI.repositories(user.username).then(function (repos) { 106 | if (repos.length === 0) { 107 | expect([]).to.be.an('undefined'); // Fail this test since we cannot progress 108 | } 109 | 110 | return dhAPI.starRepository(user.username, repos[0].name); 111 | }); 112 | }); 113 | }); 114 | }); 115 | 116 | describe('#unstarRepository', function () { 117 | it('should star a given repository', function () { 118 | return dhAPI.loggedInUser().then(function (user) { 119 | return dhAPI.repositories(user.username).then(function (repos) { 120 | if (repos.length === 0) { 121 | expect([]).to.be.an('undefined'); // Fail this test since we cannot progress 122 | } 123 | 124 | return dhAPI.unstarRepository(user.username, repos[0].name); 125 | }); 126 | }); 127 | }); 128 | }); 129 | }); 130 | 131 | describe('Creating Repository Methods', function () { 132 | let repositoryName = `test-${Date.now() / 1000}`; 133 | 134 | describe('#createRepository', function () { 135 | it('should create a repository', function () { 136 | return dhAPI.loggedInUser().then(function (user) { 137 | return dhAPI.createRepository(user.username, repositoryName, {is_private: false, description: 'Test', full_description: 'Test'}).then(function (info) { 138 | expect(info).to.be.an('object'); 139 | expect(info).to.have.property('user'); 140 | expect(info).to.have.property('name'); 141 | expect(info).to.have.property('is_private'); 142 | expect(info.user).to.equal(user.username); 143 | expect(info.name).to.equal(repositoryName); 144 | expect(info.is_private).to.equal(false); 145 | }); 146 | }); 147 | }); 148 | }); 149 | 150 | describe('#deleteRepository', function () { 151 | it('should delete a repository', function () { 152 | return dhAPI.loggedInUser().then(function (user) { 153 | return dhAPI.deleteRepository(user.username, repositoryName); 154 | }); 155 | }); 156 | }); 157 | }); 158 | 159 | describe('#webhooks', function () { 160 | it('should get the webhooks for a repository', function () { 161 | return dhAPI.loggedInUser().then(function (user) { 162 | return dhAPI.repositories(user.username).then(function (repos) { 163 | if (repos.length === 0) { 164 | expect([]).to.be.an('undefined'); // Fail this test since we cannot progress 165 | } 166 | 167 | return dhAPI.webhooks(user.username, repos[0].name).then(function (info) { 168 | expect(info).to.be.an('array'); 169 | }); 170 | }); 171 | }); 172 | }); 173 | }); 174 | 175 | describe('Webhook Modifying Methods', function () { 176 | let webhookName = `Test-${Date.now() / 1000}`; 177 | let webhookID; 178 | 179 | it('should create a webhook for a repository', function () { 180 | return dhAPI.loggedInUser().then(function (user) { 181 | return dhAPI.repositories(user.username).then(function (repos) { 182 | if (repos.length === 0) { 183 | expect([]).to.be.an('undefined'); // Fail this test since we cannot progress 184 | } 185 | 186 | return dhAPI.createWebhook(user.username, repos[0].name, webhookName).then(function (info) { 187 | expect(info).to.be.an('object'); 188 | expect(info).to.have.property('id'); 189 | expect(info).to.have.property('name'); 190 | expect(info.name).to.equal(webhookName); 191 | 192 | webhookID = info.id; 193 | }); 194 | }); 195 | }); 196 | }); 197 | 198 | it('should create a webhook hook for an existing webhook', function () { 199 | return dhAPI.loggedInUser().then(function (user) { 200 | return dhAPI.repositories(user.username).then(function (repos) { 201 | if (repos.length === 0) { 202 | expect([]).to.be.an('undefined'); // Fail this test since we cannot progress 203 | } 204 | 205 | return dhAPI.createWebhookHook(user.username, repos[0].name, webhookID, 'http://www.example.com').then(function (info) { 206 | expect(info).to.be.an('object'); 207 | expect(info).to.have.property('id'); 208 | expect(info).to.have.property('hook_url'); 209 | expect(info.hook_url).to.equal('http://www.example.com'); 210 | }); 211 | }); 212 | }); 213 | }); 214 | 215 | it('should delete a webhook for a repository', function () { 216 | return dhAPI.loggedInUser().then(function (user) { 217 | return dhAPI.repositories(user.username).then(function (repos) { 218 | if (repos.length === 0) { 219 | expect([]).to.be.an('undefined'); // Fail this test since we cannot progress 220 | } 221 | 222 | return dhAPI.deleteWebhook(user.username, repos[0].name, webhookID).then(function () { 223 | expect([]).to.be.an('array'); // If were here we're all good 224 | }); 225 | }); 226 | }); 227 | }); 228 | }); 229 | }); 230 | })(); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [Looking for a maintainer](https://github.com/RyanTheAllmighty/Docker-Hub-API/issues/33) 2 | 3 | # Docker-Hub-API 4 | [![Build Status](https://img.shields.io/travis/RyanTheAllmighty/Docker-Hub-API.svg?style=flat-square)](https://travis-ci.org/RyanTheAllmighty/Docker-Hub-API) 5 | [![NPM Downloads](https://img.shields.io/npm/dt/docker-hub-api.svg?style=flat-square)](https://www.npmjs.com/package/docker-hub-api) 6 | [![NPM Version](https://img.shields.io/npm/v/docker-hub-api.svg?style=flat-square)](https://www.npmjs.com/package/docker-hub-api) 7 | [![Issues](https://img.shields.io/github/issues/RyanTheAllmighty/Docker-Hub-API.svg?style=flat-square)](https://github.com/RyanTheAllmighty/Docker-Hub-API/issues) 8 | [![License](https://img.shields.io/badge/license-GPLv3-blue.svg?style=flat-square)](https://raw.githubusercontent.com/RyanTheAllmighty/Docker-Hub-API/master/LICENSE) 9 | 10 | Docker Hub API is an API library written for NodeJS to access the official Docker Hub/Registry. 11 | 12 | ## Install 13 | To install this package into your project simply run the following: 14 | 15 | ```sh 16 | npm install --save docker-hub-api 17 | ``` 18 | 19 | Once installed you can start to use this package by requiring the module: 20 | 21 | ```js 22 | let dockerHubAPI = require('docker-hub-api'); 23 | ``` 24 | 25 | ## Caching 26 | This package will be default cache all calls to the same resource for 5 minutes so that calls to the same resource returning the same data will not re query the Docker Hub API. 27 | 28 | This can be turned off or the expire time adjusted by adding the following code: 29 | 30 | ```js 31 | let dockerHubAPI = require('docker-hub-api'); 32 | dockerHubAPI.setCacheOptions({enabled: true, time: 60}); // This will enable the cache and cache things for 60 seconds 33 | ``` 34 | 35 | ## Logging In 36 | In order to access authenticated routes of the API you must login. 37 | 38 | There are 2 methods available to do that. 39 | 40 | ### login(username, password) 41 | This method logs in via the API and gets a login token for your account. 42 | 43 | For security purposes you should make sure to never commit any code with login details. Using environment variables is one way to get around that: 44 | 45 | ```js 46 | let dockerHubAPI = require('docker-hub-api'); 47 | dockerHubAPI.login(process.env.DOCKER_HUB_USERNAME, process.env.DOCKER_HUB_PASSWORD); 48 | ``` 49 | 50 | The login method also returns the response from Docker Hub which includes the login token if you wish to get that token: 51 | 52 | ```js 53 | let dockerHubAPI = require('docker-hub-api'); 54 | dockerHubAPI.login(process.env.DOCKER_HUB_USERNAME, process.env.DOCKER_HUB_PASSWORD).then(function(info) { 55 | console.log(`My Docker Hub login token is '${info.token}'!`); 56 | }); 57 | ``` 58 | 59 | ### logout() 60 | This logs you out of Docker Hub. 61 | 62 | No response is sent back, but any issues will throw. 63 | 64 | ### setLoginToken(token) 65 | This works similar to above, but uses an existing login token so that you don't need to make a login request. 66 | 67 | Again like above, you should move this off to an environment variable and never expose it: 68 | 69 | ```js 70 | let dockerHubAPI = require('docker-hub-api'); 71 | dockerHubAPI.setLoginToken(process.env.DOCKER_HUB_LOGIN_TOKEN); 72 | ``` 73 | 74 | ## Usage 75 | This is a complete list of methods available from this package. All methods return ES6 promises. 76 | 77 | There are 2 types of requests. Authenticated requests and non authenticated requests. As the names suggest, authenticated requests require you to have authenticated/logged in with Docker Hub. 78 | 79 | Since all the methods return Promises, you can use async/await. 80 | 81 | ### Non Authenticated Requests 82 | These requests require no authentication and can be made right away with no issues. 83 | 84 | #### comments(username, repository, options) 85 | This gets the comments for a given repository/user combination. As per the [repository](#repositoryusername-repository) method above, if the username is left out, it will query the official repository. 86 | 87 | You can also pass in options to limit the number of results per page and the page to go to like so: 88 | 89 | ```js 90 | { 91 | perPage: 10, 92 | page: 4 93 | } 94 | ``` 95 | 96 | Below is an example of what's returned: 97 | 98 | ```json 99 | [ 100 | { 101 | "id": 1035, 102 | "user": "kyma", 103 | "comment": "What OS is this built on?", 104 | "created_on": "2014-06-09T16:27:15Z", 105 | "updated_on": "2014-06-09T16:27:16Z" 106 | }, 107 | { 108 | "id": 1042, 109 | "user": "hacfi", 110 | "comment": "@kyma debian:jessie ... see\r\nhttps://github.com/docker-library/nginx/blob/docker-v1.7.1/Dockerfile#L1", 111 | "created_on": "2014-06-09T20:27:55Z", 112 | "updated_on": "2014-06-09T20:27:56Z" 113 | } 114 | ] 115 | ``` 116 | 117 | #### repository(username, name) 118 | This gets information about a repository with a given name. If the username is left out or '_' is provided, then it will get the base library repositories (official repositories). 119 | 120 | Below is a sample of what's returned: 121 | 122 | ```json 123 | { 124 | "user": "ryantheallmighty", 125 | "name": "nginx", 126 | "namespace": "ryantheallmighty", 127 | "status": 1, 128 | "description": "A short description", 129 | "is_private": false, 130 | "is_automated": false, 131 | "can_edit": false, 132 | "star_count": 0, 133 | "pull_count": 55, 134 | "last_updated": "2015-12-10T08:48:49.665081Z", 135 | "has_starred": false, 136 | "full_description": "A full description" 137 | } 138 | ``` 139 | 140 | #### repositories(username) 141 | This gets information about a user's repositories. 142 | 143 | Below is an example of what's returned: 144 | 145 | ```json 146 | [ 147 | { 148 | "namespace": "ryantheallmighty", 149 | "name": "composer" 150 | }, 151 | { 152 | "namespace": "ryantheallmighty", 153 | "name": "hhvm" 154 | } 155 | ] 156 | ``` 157 | 158 | #### repositoriesStarred(username, options) 159 | This gets information about a user's starred repositories. 160 | 161 | You can also pass in options to limit the number of results per page and the page to go to like so: 162 | 163 | ```js 164 | { 165 | perPage: 10, 166 | page: 4 167 | } 168 | ``` 169 | 170 | Below is an example of what's returned: 171 | 172 | ```json 173 | [ 174 | { 175 | "user": "ryantheallmighty", 176 | "name": "nginx", 177 | "namespace": "ryantheallmighty", 178 | "status": 1, 179 | "description": "Short description", 180 | "is_private": false, 181 | "is_automated": false, 182 | "can_edit": false, 183 | "star_count": 1, 184 | "pull_count": 57, 185 | "last_updated": "2015-12-10T08:48:49.665081Z" 186 | } 187 | ] 188 | ``` 189 | 190 | #### tags(username, repository, options) 191 | This gets the tags for a given repository/user combination. As per the [repository](#repositoryusername-repository) method above, if the username is left out, it will query the official repository. 192 | 193 | You can also pass in options to limit the number of results per page and the page to go to like so: 194 | 195 | ```js 196 | { 197 | perPage: 10, 198 | page: 4 199 | } 200 | ``` 201 | 202 | Below is an example of what's returned: 203 | 204 | ```json 205 | [ 206 | { 207 | "name": "latest", 208 | "full_size": 61215330, 209 | "id": 1493440, 210 | "repository": 433668, 211 | "creator": 534804, 212 | "last_updater": 534804, 213 | "last_updated": "2015-12-10T08:48:48.697697Z", 214 | "image_id": null, 215 | "v2": true 216 | } 217 | ] 218 | ``` 219 | 220 | #### user(username) 221 | This gets information about a user with the given username. 222 | 223 | Below is an example of what's returned: 224 | 225 | ```json 226 | { 227 | "id": "73cdba6ec4154672a2ef01c292f38567", 228 | "username": "ryantheallmighty", 229 | "full_name": "Ryan Dowling", 230 | "location": "Victoria, Australia", 231 | "company": "ATLauncher", 232 | "profile_url": "", 233 | "date_joined": "2015-12-01T10:42:00.663328Z", 234 | "gravatar_url": "https://secure.gravatar.com/avatar/af74a121defc2d50f39c7ee3641131cc.jpg?s=80&r=g&d=mm" 235 | } 236 | ``` 237 | 238 | ### Authenticated Requests 239 | These requests require you to [login](#logging-in) to Docker Hub to access. 240 | 241 | #### loggedInUser() 242 | This returns information about the current logged in user. It's good to make sure that the login is working. 243 | 244 | Below is an example of what's returned: 245 | 246 | ```json 247 | { 248 | "id": "73cdba6ec4154672a2ef01c292f38567", 249 | "username": "ryantheallmighty", 250 | "full_name": "Ryan Dowling", 251 | "location": "Victoria, Australia", 252 | "company": "ATLauncher", 253 | "gravatar_email": "", 254 | "is_staff": false, 255 | "is_admin": false, 256 | "profile_url": "", 257 | "date_joined": "2015-12-01T10:42:00.663328Z", 258 | "gravatar_url": "https://secure.gravatar.com/avatar/af74a121defc2d50f39c7ee3641131cc.jpg?s=80&r=g&d=mm" 259 | } 260 | ``` 261 | 262 | #### addCollaborator(username, repository, collaborator) 263 | This adds the given collaborator to a given repository/user combination. 264 | 265 | Below is an example of what's returned: 266 | 267 | ```json 268 | { 269 | "user": "username" 270 | } 271 | ``` 272 | 273 | #### buildDetails(username, repository, code) 274 | This gets the details for a given build code for a given repository/user combination. 275 | 276 | Below is an example of what's returned: 277 | 278 | ```json 279 | { 280 | "id": 3333355, 281 | "status": -1, 282 | "created_date": "2016-01-28T09:39:11.261907Z", 283 | "last_updated": "2016-01-28T09:43:27.832295Z", 284 | "build_code": "bxfqrgppbdi3dhiumvp7k3", 285 | "dockertag_name": "latest", 286 | "cause": null, 287 | "build_results": { 288 | "build_code": "bxfqrgppbdi3dhiumvp7k3", 289 | "build_path": "/", 290 | "buildmetrics": { 291 | "uploaded": null, 292 | "built": null, 293 | "created": "2016-01-28T09:38:01.272772Z", 294 | "started": "2016-01-28T09:41:14.249883Z", 295 | "cloned": "2016-01-28T09:42:15.427018Z", 296 | "readme": "2016-01-28T09:41:16.314344Z", 297 | "finished": null, 298 | "error": "2016-01-28T09:41:18.388163Z", 299 | "claimed": "2016-01-28T09:37:02.230838Z", 300 | "bundled": null, 301 | "dockerfile": "2016-01-28T09:41:17.398032Z", 302 | "failure": null 303 | }, 304 | "created_at": "2016-01-28T09:39:11.261Z", 305 | "docker_repo": "ryantheallmighty/maven-custom", 306 | "docker_tag": "latest", 307 | "docker_user": "ryantheallmighty", 308 | "dockerfile_contents": "FROM 3-jdk-8\n\nRUN apt-get update && apt-get install -y sudo && echo \"ALL ALL=(ALL) NOPASSWD: ALL\" >> /etc/sudoers && rm -rf /var/lib/apt/lists/*\n", 309 | "error": "Build process failed: Error: image library/3-jdk-8:latest not found", 310 | "failure": "Build failed: Error: image library/3-jdk-8:latest not found", 311 | "last_updated": "2016-01-28T09:43:27.832Z", 312 | "logs": "Cloning into 'bxfqrgppbdi3dhiumvp7k3'...\nKernelVersion: 3.13.0-40-generic\nOs: linux\nBuildTime: Mon Oct 12 05:37:18 UTC 2015\nApiVersion: 1.20\nVersion: 1.8.3\nGitCommit: f4bf5c7\nArch: amd64\nGoVersion: go1.4.2\nStep 0 : FROM 3-jdk-8\nError: image library/3-jdk-8:latest not found", 313 | "readme_contents": "# docker-maven-custom\nThis is a custom Docker image with Maven with sudo installed to allow non root users to run commands\n", 314 | "source_branch": "master", 315 | "source_type": "git", 316 | "source_url": "https://github.com/RyanTheAllmighty/docker-maven-custom.git" 317 | } 318 | } 319 | ``` 320 | 321 | #### buildHistory(username, repository, options) 322 | This gets the build history for a given repository/user combination. 323 | 324 | You can also pass in options to limit the number of results per page and the page to go to like so: 325 | 326 | ```js 327 | { 328 | perPage: 10, 329 | page: 4 330 | } 331 | ``` 332 | 333 | Below is an example of what's returned: 334 | 335 | ```json 336 | [ 337 | { 338 | "id": 3333420, 339 | "status": 10, 340 | "created_date": "2016-01-28T09:44:47.667135Z", 341 | "last_updated": "2016-01-28T09:55:56.381992Z", 342 | "build_code": "bhgpwqvc69xxhqnqnxukurq", 343 | "dockertag_name": "latest", 344 | "cause": null 345 | }, 346 | { 347 | "id": 3333355, 348 | "status": -1, 349 | "created_date": "2016-01-28T09:39:11.261907Z", 350 | "last_updated": "2016-01-28T09:43:27.832295Z", 351 | "build_code": "bxfqrgppbdi3dhiumvp7k3", 352 | "dockertag_name": "latest", 353 | "cause": null 354 | } 355 | ] 356 | ``` 357 | 358 | The status refers to the outcome of the build where: 359 | 360 | - -1 means it failed 361 | - 10 means it succeeded 362 | 363 | There may be other status codes but those are the ones I'm aware of. 364 | 365 | I'm unaware of any cases where cause doesn't equal null. 366 | 367 | #### buildLinks(username, repository) 368 | This gets the build links for a given repository/user combination. 369 | 370 | Below is an example of what's returned: 371 | 372 | ```json 373 | [ 374 | { 375 | "id": 12959, 376 | "from_repo": "ryantheallmighty/maven-custom", 377 | "to_repo": "maven" 378 | } 379 | ] 380 | ``` 381 | 382 | #### buildSettings(username, repository) 383 | This gets the build settings for a given repository/user combination. This doesn't include links or build triggers. 384 | 385 | Below is an example of what's returned: 386 | 387 | ```json 388 | { 389 | "repository": 501609, 390 | "build_name": "RyanTheAllmighty/docker-maven-custom", 391 | "provider": "github", 392 | "source_url": "https://github.com/RyanTheAllmighty/docker-maven-custom.git", 393 | "docker_url": "ryantheallmighty/maven-custom", 394 | "repo_web_url": "https://github.com/RyanTheAllmighty/docker-maven-custom", 395 | "repo_type": "git", 396 | "active": true, 397 | "repo_id": "50572157", 398 | "build_tags": [ 399 | { 400 | "id": 213846, 401 | "name": "{sourceref}", 402 | "dockerfile_location": "/", 403 | "source_name": "/^([^m]|.[^a]|..[^s]|...[^t]|....[^e]|.....[^r]|.{0,5}$|.{7,})/", 404 | "source_type": "Branch" 405 | }, 406 | { 407 | "id": 213845, 408 | "name": "latest", 409 | "dockerfile_location": "/", 410 | "source_name": "master", 411 | "source_type": "Branch" 412 | } 413 | ], 414 | "deploykey": null, 415 | "hook_id": null, 416 | "webhook_id": "7097505" 417 | } 418 | ``` 419 | 420 | #### buildTrigger(username, repository) 421 | This gets the build trigger for a given repository/user combination. 422 | 423 | Below is an example of what's returned: 424 | 425 | ```json 426 | { 427 | "token": "UUID-HERE", 428 | "active": true, 429 | "trigger_url": "https://registry.hub.docker.com/u/ryantheallmighty/maven-custom/trigger/UUID-HERE/" 430 | } 431 | ``` 432 | 433 | #### buildTriggerHistory(username, repository) 434 | This gets the build triggers for a given repository/user combination. 435 | 436 | Below is an example of what's returned: 437 | 438 | ```json 439 | [ 440 | { 441 | "ip_address": "203.219.62.185 ", 442 | "result": "triggered", 443 | "result_desc": "Build Triggered", 444 | "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36", 445 | "request_body": null, 446 | "created": "2016-02-17T01:49:36.614030Z", 447 | "build_code": "bqxx7gwbqgbn9pvs72vthnz" 448 | } 449 | ] 450 | ``` 451 | 452 | In my testing the ip_address filed contained a large amount of whitespace after the actual IP address. I've left this in as Docker Hub returns it, so you may wish to do your own trimming. 453 | 454 | #### collaborators(username, repository) 455 | This gets the collaborators for a given repository/user combination. 456 | 457 | Below is an example of what's returned: 458 | 459 | ```json 460 | [ 461 | { 462 | "user": "ryantheallmighty" 463 | } 464 | ] 465 | ``` 466 | 467 | #### createBuildLink(username, repository, to_repo) 468 | This creates a build link to a given repository to the given to_repo which should be in format 'username/repository' or just 'repository' for official repositories. 469 | 470 | Below is an example of what's returned: 471 | 472 | ```json 473 | { 474 | "id": 13924, 475 | "from_repo": "ryantheallmighty/maven-custom", 476 | "to_repo": "debian" 477 | } 478 | ``` 479 | 480 | #### createBuildTag(username, repository, details) 481 | This creates a build tag to a given repository. 482 | 483 | Passing in an object with the details: 484 | 485 | ```js 486 | { 487 | dockerfile_location: '/' 488 | name: 'tag-name' 489 | source_name: 'master' 490 | source_type: 'Branch' 491 | } 492 | ``` 493 | 494 | You may leave out the dockerfile_location and it will default to '/' and the same for name which will default to 'latest'. 495 | 496 | source_type is either Branch or Tag and source_name should be the name of the tag or branch to build from. source_type will default to 'Branch' if left out. 497 | 498 | Below is an example of what's returned: 499 | 500 | ```json 501 | { 502 | "id": 228073, 503 | "name": "tag-name", 504 | "dockerfile_location": "/", 505 | "source_name": "master", 506 | "source_type": "Branch" 507 | } 508 | ``` 509 | 510 | #### createRepository(username, name, details) 511 | This creates a new repository under the username and name provided with the details provided. 512 | 513 | Passing in an object with the details: 514 | 515 | ```js 516 | { 517 | description: 'Test', 518 | full_description: 'Test', 519 | is_private: false 520 | } 521 | ``` 522 | 523 | Below is an example of what's returned: 524 | 525 | ```json 526 | { 527 | "user": "ryantheallmighty", 528 | "name": "test", 529 | "namespace": "ryantheallmighty", 530 | "status": 0, 531 | "description": "Test", 532 | "is_private": false, 533 | "is_automated": false, 534 | "can_edit": true, 535 | "star_count": 0, 536 | "pull_count": 0, 537 | "last_updated": null, 538 | "full_description": "Test" 539 | } 540 | ``` 541 | 542 | #### createAutomatedBuild(username, name, details) 543 | This creates a new automated build under the username and name provided with the details provided. 544 | 545 | An example of what the Docker Hub web frontend does: 546 | 547 | ```json 548 | { 549 | "name": "dotfiles", 550 | "namespace": "ryantheallmighty", 551 | "description": "asdasdasd", 552 | "vcs_repo_name": "ryantheallmighty/dotfiles", 553 | "provider": "github", 554 | "dockerhub_repo_name": "ryantheallmighty/dotfiles", 555 | "is_private":false, 556 | "active":true, 557 | "build_tags":[ 558 | { 559 | "name": "latest", 560 | "source_type": "Branch", 561 | "source_name": "master", 562 | "dockerfile_location": "/" 563 | }, 564 | { 565 | "name": "{sourceref}", 566 | "source_type": "Branch", 567 | "source_name": "/^([^m]|.[^a]|..[^s]|...[^t]|....[^e]|.....[^r]|.{0,5}$|.{7,})/", 568 | "dockerfile_location": "/" 569 | } 570 | ] 571 | } 572 | ``` 573 | 574 | If no build_tags are provided then it will create the default build tag of tag name 'latest' on branch 'master' with a dockerfile location of '/'. 575 | 576 | Below is an example of what's returned: 577 | 578 | ```json 579 | { 580 | "repository": 532085, 581 | "build_name": "RyanTheAllmighty/AllmightyBot-Node-Server", 582 | "provider": "github", 583 | "source_url": "https://github.com/RyanTheAllmighty/AllmightyBot-Node-Server.git", 584 | "docker_url": "ryantheallmighty/allmightybot-node-server", 585 | "repo_web_url": "https://github.com/RyanTheAllmighty/AllmightyBot-Node-Server", 586 | "repo_type": "git", 587 | "active": true, 588 | "repo_id": 30229068, 589 | "build_tags": [ 590 | { 591 | "id": 228108, 592 | "name": "test", 593 | "dockerfile_location": "/", 594 | "source_name": "test", 595 | "source_type": "Branch" 596 | }, 597 | { 598 | "id": 228107, 599 | "name": "latest", 600 | "dockerfile_location": "/", 601 | "source_name": "master", 602 | "source_type": "Branch" 603 | } 604 | ], 605 | "deploykey": null, 606 | "hook_id": null, 607 | "webhook_id": 7336911 608 | } 609 | ``` 610 | 611 | Please note that the only 2 providers at the time of writing this are 'github' and 'bitbucket' and other source source url's and providers will most likely not work. 612 | 613 | #### createWebhook(username, name, webhookName) 614 | This creates a new webhook for a repository you own. 615 | 616 | Below is an example of what's returned: 617 | 618 | ```json 619 | { 620 | "id": 8551, 621 | "name": "Test", 622 | "active": true, 623 | "expect_final_callback": true, 624 | "creator": "ryantheallmighty", 625 | "last_updated": "2015-12-12T11:25:48.808294Z", 626 | "last_updater": "ryantheallmighty", 627 | "hooks": [] 628 | } 629 | ``` 630 | 631 | Once a webhook has been created you can then add hook urls to it with [createWebhookHook](#createWebhookHook) below. 632 | 633 | #### createWebhookHook(username, name, webhookID, url) 634 | This creates a new webhook hook for an existing webhook. 635 | 636 | Below is an example of what's returned: 637 | 638 | ```json 639 | { 640 | "id": 10020, 641 | "creator": "ryantheallmighty", 642 | "last_updater": "ryantheallmighty", 643 | "hook_url": "https://www.example.com", 644 | "date_added": "2015-12-12T11:53:24.743298Z", 645 | "last_updated": "2015-12-12T11:53:24.746349Z", 646 | "active": true 647 | } 648 | ``` 649 | 650 | #### deleteBuildLink(username, name, id) 651 | This deletes a build link given by the id for the given repository. 652 | 653 | This method returns nothing on success, but an error in the .catch() block of the promise indicates an error there. 654 | 655 | #### deleteBuildTag(username, name, id) 656 | This deletes a build tag given by the id for the given repository. 657 | 658 | This method returns nothing on success, but an error in the .catch() block of the promise indicates an error there. 659 | 660 | #### deleteCollaborator(username, name, collaborator) 661 | This deletes a collaborator given by their username for the given repository. 662 | 663 | This method returns nothing on success, but an error in the .catch() block of the promise indicates an error there. 664 | 665 | #### deleteRepository(username, name) 666 | This deletes a repository you own. 667 | 668 | **WARNING**: There is no going back once this method is called! So be absolutely sure this is what you want. 669 | 670 | This method returns nothing on success, but an error in the .catch() block of the promise indicates an error there. 671 | 672 | #### deleteWebhook(username, name, webhookID) 673 | This deletes a webhook for a repository you own. 674 | 675 | This method returns nothing on success, but an error in the .catch() block of the promise indicates an error there. 676 | 677 | #### registrySettings() 678 | This gets the registry settings for the current logged in user containing information about the number of private repositories used/available. 679 | 680 | Below is an example of what's returned: 681 | 682 | ```json 683 | { 684 | "private_repo_used": 0, 685 | "num_free_private_repos": 0, 686 | "private_repo_limit": 1, 687 | "private_repo_available": 1, 688 | "private_repo_percent_used": 0.0, 689 | "default_repo_visibility": "public" 690 | } 691 | ``` 692 | 693 | #### saveBuildTag(username, repository, id, details) 694 | This saves the details of a given build tag id in the given repository. 695 | 696 | Passing in an object with the details: 697 | 698 | ```js 699 | { 700 | name: 'tag-name' 701 | dockerfile_location: '/' 702 | source_name: 'master' 703 | source_type: 'Branch' 704 | } 705 | ``` 706 | 707 | You may leave out the dockerfile_location and it will default to '/'. 708 | 709 | source_type is either Branch or Tag and source_name should be the name of the tag or branch to build from. source_name will default to 'master' and source_type will default to 'Branch' if left out. 710 | 711 | Below is an example of what's returned: 712 | 713 | ```json 714 | { 715 | "id": 213845, 716 | "name": "latest", 717 | "dockerfile_location": "/", 718 | "source_name": "master", 719 | "source_type": "Branch" 720 | } 721 | ``` 722 | 723 | Please note that trying to trigger a build with details which don't match any of the repositories build tags will have no effect and will return an empty array. 724 | 725 | #### setRepositoryDescription(username, name, descriptions) 726 | This sets one or both of the descriptions for a repository you own. 727 | 728 | Passing in an object with the short, full, or both descriptions: 729 | 730 | ```js 731 | { 732 | short: "A short description", 733 | full: "A full description" 734 | } 735 | ``` 736 | 737 | This returns the same information as [repository(username, name)](#repositoryusername-name). 738 | 739 | #### setRepositoryPrivacy(username, name, private) 740 | This sets the privacy (public or private) for a given repository. 741 | 742 | Pass in `true` for the `privacy` field to make the repository private, and `false` for public. 743 | 744 | This method returns nothing on success, but an error in the .catch() block of the promise indicates an error there. 745 | 746 | #### starRepository(username, name) 747 | This stars a given repository. 748 | 749 | This method returns nothing on success, but an error in the .catch() block of the promise indicates an error there. 750 | 751 | #### triggerBuild(username, repository, details) 752 | This triggers a build of the repository. 753 | 754 | Passing in an object with the details: 755 | 756 | ```js 757 | { 758 | dockerfile_location: '/' 759 | source_name: 'master' 760 | source_type: 'Branch' 761 | } 762 | ``` 763 | 764 | You may leave out the dockerfile_location and it will default to '/'. 765 | 766 | source_type is either Branch or Tag and source_name should be the name of the tag or branch to build from. source_name will default to 'master' and source_type will default to 'Branch' if left out. 767 | 768 | Below is an example of what's returned: 769 | 770 | ```json 771 | [ 772 | { 773 | "source_type": "Branch", 774 | "source_name": "test", 775 | "evaluated_tag_name": "latest", 776 | "build_code": "bpzaqzu4nxpypzjren5vapt", 777 | "msg": "Build submitted", 778 | "queued_for_retry": false 779 | } 780 | ] 781 | ``` 782 | 783 | Please note that trying to trigger a build with details which don't match any of the repositories build tags will have no effect and will return an empty array. 784 | 785 | #### unstarRepository(username, name) 786 | This unstars a given repository. 787 | 788 | This method returns nothing on success, but an error in the .catch() block of the promise indicates an error there. 789 | 790 | #### webhooks(username, name, options) 791 | This gets the webhooks for a repository you own. 792 | 793 | You can also pass in options to limit the number of results per page and the page to go to like so: 794 | 795 | ```js 796 | { 797 | perPage: 10, 798 | page: 4 799 | } 800 | ``` 801 | 802 | Below is an example of what's returned: 803 | 804 | ```json 805 | [ 806 | { 807 | "id": 8550, 808 | "name": "Test", 809 | "active": true, 810 | "expect_final_callback": true, 811 | "creator": "ryantheallmighty", 812 | "last_updated": "2015-12-12T11:19:08.163295Z", 813 | "last_updater": "ryantheallmighty", 814 | "hooks": [ 815 | { 816 | "id": 10014, 817 | "creator": "ryantheallmighty", 818 | "last_updater": "ryantheallmighty", 819 | "hook_url": "https://www.example.com", 820 | "date_added": "2015-12-12T11:19:08.474922Z", 821 | "last_updated": "2015-12-12T11:19:08.477809Z", 822 | "active": true 823 | } 824 | ] 825 | } 826 | ] 827 | ``` 828 | 829 | ## Support 830 | If you're having issues please feel free to [open an issue](https://github.com/RyanTheAllmighty/Docker-Hub-API/issues/new). 831 | 832 | ## Testing & Linting 833 | To run this applications tests and linter, simply install Gulp globally with the below command: 834 | 835 | ``` 836 | npm install -g gulp 837 | ``` 838 | 839 | Then run the following command in the directory this repository was cloned into: 840 | 841 | ``` 842 | gulp 843 | ``` 844 | 845 | The gulpfile gives access to a few methods shown below: 846 | 847 | - jscs: Runs the JSCS tool to check JS code. 848 | - jshint: Runs the JSHint tool to check JS code. 849 | - test: Runs the mocha tests. 850 | - style: Runs the jscs and jshint tasks to check JS code. 851 | - watch: Runs all 3 main tasks and then watches for file changes to rerun those tasks constantly as files are changed. 852 | 853 | By default Gulp is set to run the jscs, jshint and test tasks when no arguments are provided to it. 854 | 855 | ### Note about authentication 856 | The tests will test authenticated routes which require access to three environment variables to work correctly: 857 | 858 | - DOCKER_HUB_USERNAME 859 | - DOCKER_HUB_PASSWORD 860 | - DOCKER_HUB_LOGIN_TOKEN 861 | 862 | The values for these should be pretty straight forward besides the DOCKER_HUB_LOGIN_TOKEN which is a JWT token sent by Docker HUB after a login. 863 | 864 | The best way to retrieve it is to check your browsers cookies for 'https://hub.docker.com' and copy the contents of the cookie named 'jwt'. 865 | 866 | ## Coding standards & styling guidelines 867 | Please see the [STYLE.md](https://github.com/RyanTheAllmighty/Docker-Hub-API/blob/master/STYLE.md) file for coding standards and style guidelines. 868 | 869 | ## License 870 | This work is licensed under the GNU General Public License v3.0. To view a copy of this license, visit http://www.gnu.org/licenses/gpl-3.0.txt. 871 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | 676 | -------------------------------------------------------------------------------- /src/api.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Docker Hub API - https://github.com/RyanTheAllmighty/Docker-Hub-API 3 | * Copyright (C) 2015 RyanTheAllmighty 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | (function() { 20 | 'use strict'; 21 | 22 | //let _ = require('lodash'); 23 | let request = require('request'); 24 | 25 | let apiVersion = 2; 26 | 27 | let cache = {}; 28 | let cacheEnabled = true; 29 | let cacheTimeSeconds = 300; 30 | 31 | let loggedInToken = null; 32 | 33 | module.exports = { 34 | /** 35 | * This logs into Docker Hub with the given username and password. 36 | * 37 | * You may choose to bypass this by providing a login token directly via the setLoginToken(token) method. 38 | * 39 | * @param {String} username - the username of your Docker Hub account 40 | * @param {String} password - the password for that Docker Hub account 41 | * @returns {Promise} 42 | */ 43 | login: function(username, password) { 44 | return new Promise( 45 | function(resolve, reject) { 46 | if (!username || !password) { 47 | return reject( 48 | new Error( 49 | 'Both username and password must be passed to this function!', 50 | ), 51 | ); 52 | } 53 | 54 | this.makePostRequest('users/login/', { username, password }) 55 | .then(function(info) { 56 | if (!info.token) { 57 | return reject( 58 | new Error( 59 | 'Error logging into Docker Hub! No login token sent back!', 60 | ), 61 | ); 62 | } 63 | 64 | loggedInToken = info.token; 65 | 66 | return resolve(info); 67 | }) 68 | .catch(reject); 69 | }.bind(this), 70 | ); 71 | }, 72 | /** 73 | * This logs you out of Docker Hub. 74 | * 75 | * @returns {Promise} 76 | */ 77 | logout: function() { 78 | return new Promise( 79 | function(resolve, reject) { 80 | if (!loggedInToken) { 81 | return reject( 82 | new Error( 83 | 'No login token found! Please login() or setLoginToken() to continue!', 84 | ), 85 | ); 86 | } 87 | 88 | this.makePostRequest('logout/') 89 | .then(function() { 90 | return resolve(); 91 | }) 92 | .catch(reject); 93 | }.bind(this), 94 | ); 95 | }, 96 | /** 97 | * This gets information about the current logged in user. 98 | * 99 | * @returns {Promise} 100 | */ 101 | loggedInUser: function() { 102 | return new Promise( 103 | function(resolve, reject) { 104 | if (!loggedInToken) { 105 | return reject( 106 | new Error( 107 | 'No login token found! Please login() or setLoginToken() to continue!', 108 | ), 109 | ); 110 | } 111 | 112 | this.makeGetRequest('user/') 113 | .then(resolve) 114 | .catch(reject); 115 | }.bind(this), 116 | ); 117 | }, 118 | /** 119 | * This will set the caching options. 120 | * 121 | * @param {{enabled: Boolean, time: Number}} options - the options to set for the caching options 122 | */ 123 | setCacheOptions: function(options) { 124 | if (typeof options.enabled !== 'undefined') { 125 | cacheEnabled = options.enabled; 126 | } 127 | 128 | if (options.time) { 129 | cacheTimeSeconds = options.time; 130 | } 131 | 132 | // Clear the cache 133 | cache = {}; 134 | }, 135 | /** 136 | * This sets the login token for authenticated Docker Hub requests. 137 | * 138 | * @param {String} token - the login token for Docker Hub 139 | */ 140 | setLoginToken: function(token) { 141 | loggedInToken = token; 142 | }, 143 | /** 144 | * Adds a collaborator to the given repository. 145 | * 146 | * @param {String} username - the username to add a collaborator to 147 | * @param {String} name - the name of the repository to add a collaborator to 148 | * @param {String} collaborator - the username of the collaborator to add 149 | * @returns {Promise} 150 | */ 151 | addCollaborator: function(username, name, collaborator) { 152 | return new Promise( 153 | function(resolve, reject) { 154 | if (!username || typeof username !== 'string') { 155 | return reject(new Error('Username must be provided!')); 156 | } 157 | 158 | if (!name || typeof name !== 'string') { 159 | return reject(new Error('Repository name must be provided!')); 160 | } 161 | 162 | if (!collaborator || typeof collaborator !== 'string') { 163 | return reject(new Error('Collaborator username must be provided!')); 164 | } 165 | 166 | // Make sure the username/collaborator is all lowercase as per Docker Hub requirements 167 | username = username.toLowerCase(); 168 | collaborator = collaborator.toLowerCase(); 169 | 170 | this.makePostRequest(`repositories/${username}/${name}/collaborators/`, { 171 | user: collaborator, 172 | }) 173 | .then(resolve) 174 | .catch(reject); 175 | }.bind(this), 176 | ); 177 | }, 178 | /** 179 | * Gets the details for a given build of a repository. 180 | * 181 | * @param {String} username - the username to get the comments for 182 | * @param {String} name - the name of the repository to get the comments for 183 | * @param {String} code - the code of the build to get the details for 184 | * @returns {Promise} 185 | */ 186 | buildDetails: function(username, name, code) { 187 | return new Promise( 188 | function(resolve, reject) { 189 | if (!username || typeof username !== 'string') { 190 | return reject(new Error('Username must be provided!')); 191 | } 192 | 193 | if (!name || typeof name !== 'string') { 194 | return reject(new Error('Repository name must be provided!')); 195 | } 196 | 197 | if (!code || typeof code !== 'string') { 198 | return reject(new Error('Repository code must be provided!')); 199 | } 200 | 201 | // Make sure the username is all lowercase as per Docker Hub requirements 202 | username = username.toLowerCase(); 203 | 204 | this.makeGetRequest(`repositories/${username}/${name}/buildhistory/${code}`) 205 | .then(resolve) 206 | .catch(reject); 207 | }.bind(this), 208 | ); 209 | }, 210 | /** 211 | * Get the history of all the builds done for a given repository. 212 | * 213 | * @param {String} username - the username to get the build history for 214 | * @param {String} name - the name of the repository to get the build history for 215 | * @param {{page: Number, perPage: Number}} [options] - the options for this call 216 | * @returns {Promise} 217 | */ 218 | buildHistory: function(username, name, options) { 219 | return new Promise( 220 | function(resolve, reject) { 221 | if (!username || typeof username !== 'string') { 222 | return reject(new Error('Username must be provided!')); 223 | } 224 | 225 | if (!name || typeof name !== 'string') { 226 | return reject(new Error('Repository name must be provided!')); 227 | } 228 | 229 | if (!options) { 230 | options = { page: 1, perPage: 100 }; 231 | } 232 | 233 | // Make sure the username is all lowercase as per Docker Hub requirements 234 | username = username.toLowerCase(); 235 | 236 | this.makeGetRequest( 237 | `repositories/${username}/${name}/buildhistory?page_size=${options.perPage || 238 | 100}&page=${options.page || 1}`, 239 | 'results', 240 | ) 241 | .then(resolve) 242 | .catch(reject); 243 | }.bind(this), 244 | ); 245 | }, 246 | /** 247 | * Gets the build links for a given repository. 248 | * 249 | * @param {String} username - the username to get the comments for 250 | * @param {String} name - the name of the repository to get the comments for 251 | * @returns {Promise} 252 | */ 253 | buildLinks: function(username, name) { 254 | return new Promise( 255 | function(resolve, reject) { 256 | if (!username || typeof username !== 'string') { 257 | return reject(new Error('Username must be provided!')); 258 | } 259 | 260 | if (!name || typeof name !== 'string') { 261 | return reject(new Error('Repository name must be provided!')); 262 | } 263 | 264 | // Make sure the username is all lowercase as per Docker Hub requirements 265 | username = username.toLowerCase(); 266 | 267 | this.makeGetRequest(`repositories/${username}/${name}/links`, 'results') 268 | .then(resolve) 269 | .catch(reject); 270 | }.bind(this), 271 | ); 272 | }, 273 | /** 274 | * Gets the build settings for a given repository. 275 | * 276 | * @param {String} username - the username to get the comments for 277 | * @param {String} name - the name of the repository to get the comments for 278 | * @returns {Promise} 279 | */ 280 | buildSettings: function(username, name) { 281 | return new Promise( 282 | function(resolve, reject) { 283 | if (!username || typeof username !== 'string') { 284 | return reject(new Error('Username must be provided!')); 285 | } 286 | 287 | if (!name || typeof name !== 'string') { 288 | return reject(new Error('Repository name must be provided!')); 289 | } 290 | 291 | // Make sure the username is all lowercase as per Docker Hub requirements 292 | username = username.toLowerCase(); 293 | 294 | this.makeGetRequest(`repositories/${username}/${name}/autobuild`) 295 | .then(resolve) 296 | .catch(reject); 297 | }.bind(this), 298 | ); 299 | }, 300 | /** 301 | * Gets the build trigger for a given repository. 302 | * 303 | * @param {String} username - the username to get the comments for 304 | * @param {String} name - the name of the repository to get the comments for 305 | * @returns {Promise} 306 | */ 307 | buildTrigger: function(username, name) { 308 | return new Promise( 309 | function(resolve, reject) { 310 | if (!username || typeof username !== 'string') { 311 | return reject(new Error('Username must be provided!')); 312 | } 313 | 314 | if (!name || typeof name !== 'string') { 315 | return reject(new Error('Repository name must be provided!')); 316 | } 317 | 318 | // Make sure the username is all lowercase as per Docker Hub requirements 319 | username = username.toLowerCase(); 320 | 321 | this.makeGetRequest(`repositories/${username}/${name}/buildtrigger`) 322 | .then(resolve) 323 | .catch(reject); 324 | }.bind(this), 325 | ); 326 | }, 327 | /** 328 | * Gets the build trigger history for a given repository. 329 | * 330 | * @param {String} username - the username to get the comments for 331 | * @param {String} name - the name of the repository to get the comments for 332 | * @returns {Promise} 333 | */ 334 | buildTriggerHistory: function(username, name) { 335 | return new Promise( 336 | function(resolve, reject) { 337 | if (!username || typeof username !== 'string') { 338 | return reject(new Error('Username must be provided!')); 339 | } 340 | 341 | if (!name || typeof name !== 'string') { 342 | return reject(new Error('Repository name must be provided!')); 343 | } 344 | 345 | // Make sure the username is all lowercase as per Docker Hub requirements 346 | username = username.toLowerCase(); 347 | 348 | this.makeGetRequest( 349 | `repositories/${username}/${name}/buildtrigger/history`, 350 | 'results', 351 | ) 352 | .then(resolve) 353 | .catch(reject); 354 | }.bind(this), 355 | ); 356 | }, 357 | /** 358 | * Gets the collaborators for a repository. 359 | * 360 | * @param {String} username - the username to get the collaborators for 361 | * @param {String} name - the name of the repository to get the collaborators for 362 | * @returns {Promise} 363 | */ 364 | collaborators: function(username, name) { 365 | return new Promise( 366 | function(resolve, reject) { 367 | if (!username || typeof username !== 'string') { 368 | return reject(new Error('Username must be provided!')); 369 | } 370 | 371 | if (!name || typeof name !== 'string') { 372 | return reject(new Error('Repository name must be provided!')); 373 | } 374 | 375 | // Make sure the username is all lowercase as per Docker Hub requirements 376 | username = username.toLowerCase(); 377 | 378 | this.makeGetRequest(`repositories/${username}/${name}/collaborators`, 'results') 379 | .then(resolve) 380 | .catch(reject); 381 | }.bind(this), 382 | ); 383 | }, 384 | /** 385 | * Gets the comments for a repository. 386 | * 387 | * @param {String} username - the username to get the comments for 388 | * @param {String} name - the name of the repository to get the comments for 389 | * @param {{page: Number, perPage: Number}} [options] - the options for this call 390 | * @returns {Promise} 391 | */ 392 | comments: function(username, name, options) { 393 | return new Promise( 394 | function(resolve, reject) { 395 | // If no name is passed in, then the user wants an official repository 396 | if (username && !name && !options) { 397 | name = username; 398 | username = 'library'; 399 | options = { page: 1, perPage: 100 }; 400 | } else if (username && name && !options) { 401 | if (name instanceof Object) { 402 | options = name; 403 | name = username; 404 | username = 'library'; 405 | } else { 406 | options = { page: 1, perPage: 100 }; 407 | } 408 | } 409 | 410 | // If username is '_' then we're trying to get an official repository 411 | if (username === '_') { 412 | username = 'library'; 413 | } 414 | 415 | if (!options) { 416 | options = { page: 1, perPage: 100 }; 417 | } 418 | 419 | // Make sure the username is all lowercase as per Docker Hub requirements 420 | username = username.toLowerCase(); 421 | 422 | this.makeGetRequest( 423 | `repositories/${username}/${name}/comments?page_size=${options.perPage || 424 | 100}&page=${options.page || 1}`, 425 | 'results', 426 | ) 427 | .then(resolve) 428 | .catch(reject); 429 | }.bind(this), 430 | ); 431 | }, 432 | /** 433 | * Creates a build link for a given repository to the given repository. 434 | * 435 | * @param {String} username - the username to create a build link for 436 | * @param {String} name - the name of the repository to create a build link for 437 | * @param {String} to_repo - the repo to link this automated build to 438 | * @returns {Promise} 439 | */ 440 | createBuildLink: function(username, name, to_repo) { 441 | return new Promise( 442 | function(resolve, reject) { 443 | if (!username || typeof username !== 'string') { 444 | return reject(new Error('Username must be provided!')); 445 | } 446 | 447 | if (!name || typeof name !== 'string') { 448 | return reject(new Error('Repository name must be provided!')); 449 | } 450 | 451 | if (!to_repo || typeof to_repo !== 'string') { 452 | return reject(new Error('Repo to link to must be provided!')); 453 | } 454 | 455 | // Check to see if the user provided a username for the to_repo 456 | if (to_repo.indexOf('/') === -1) { 457 | to_repo = 'library/' + to_repo; 458 | } 459 | 460 | // If to-repo has '_/' then we're trying to get an official repository 461 | if (to_repo.substr(0, 2) === '_/') { 462 | to_repo = 'library/' + to_repo.substr(2); 463 | } 464 | 465 | // Make sure the username is all lowercase as per Docker Hub requirements 466 | username = username.toLowerCase(); 467 | 468 | this.makePostRequest(`repositories/${username}/${name}/links`, { to_repo }) 469 | .then(resolve) 470 | .catch(reject); 471 | }.bind(this), 472 | ); 473 | }, 474 | /** 475 | * Creates a build tag for a given repository. 476 | * 477 | * @param {String} username - the username to create a build tag for 478 | * @param {String} name - the name of the repository to create a build tag for 479 | * @param {Object} details - the details of the build tag 480 | * @returns {Promise} 481 | */ 482 | createBuildTag: function(username, name, details) { 483 | return new Promise( 484 | function(resolve, reject) { 485 | if (!username || typeof username !== 'string') { 486 | return reject(new Error('Username must be provided!')); 487 | } 488 | 489 | if (!name || typeof name !== 'string') { 490 | return reject(new Error('Repository name must be provided!')); 491 | } 492 | 493 | if (!details || typeof details !== 'object') { 494 | return reject(new Error('Tag details must be provided!')); 495 | } 496 | 497 | // Make sure the username is all lowercase as per Docker Hub requirements 498 | username = username.toLowerCase(); 499 | 500 | // Build our object to post 501 | let obj = { 502 | isNew: true, 503 | namespace: username, 504 | repoName: name, 505 | name: details.name || 'latest', 506 | dockerfile_location: details.dockerfile_location || '/', 507 | source_type: details.source_type || 'Branch', 508 | source_name: details.source_name || 'master', 509 | }; 510 | 511 | this.makePostRequest(`repositories/${username}/${name}/autobuild/tags`, obj) 512 | .then(resolve) 513 | .catch(reject); 514 | }.bind(this), 515 | ); 516 | }, 517 | /** 518 | * Creates an automated build. 519 | * 520 | * @param {String} username - the username of the automated build to create 521 | * @param {String} name - the name of the automated build to create 522 | * @param {Object} details - the details of the new automated build 523 | * @returns {Promise} 524 | */ 525 | createAutomatedBuild: function(username, name, details) { 526 | return new Promise( 527 | function(resolve, reject) { 528 | if (!username || typeof username !== 'string') { 529 | return reject(new Error('Username must be provided!')); 530 | } 531 | 532 | if (!name || typeof name !== 'string') { 533 | return reject(new Error('Repository name must be provided!')); 534 | } 535 | 536 | if (!details || typeof details !== 'object') { 537 | return reject(new Error('Details must be provided!')); 538 | } 539 | 540 | // Make sure the username is all lowercase as per Docker Hub requirements 541 | username = username.toLowerCase(); 542 | 543 | let obj = { 544 | name, 545 | namespace: username, 546 | active: true, 547 | dockerhub_repo_name: `${username}/${name}`, 548 | is_private: false, 549 | ...details, 550 | }; 551 | 552 | return this.makePostRequest(`repositories/${username}/${name}/autobuild/`, obj) 553 | .then(resolve) 554 | .catch(reject); 555 | }.bind(this), 556 | ); 557 | }, 558 | /** 559 | * Creates a repository. 560 | * 561 | * @param {String} username - the username of the repository to create 562 | * @param {String} name - the name of the repository to create 563 | * @param {Object} details - the details of the new repository 564 | * @returns {Promise} 565 | */ 566 | createRepository: function(username, name, details) { 567 | return new Promise( 568 | function(resolve, reject) { 569 | if (!username || typeof username !== 'string') { 570 | return reject(new Error('Username must be provided!')); 571 | } 572 | 573 | if (!name || typeof name !== 'string') { 574 | return reject(new Error('Repository name must be provided!')); 575 | } 576 | 577 | if (!details || typeof details !== 'object') { 578 | return reject(new Error('Details must be provided!')); 579 | } 580 | 581 | // Make sure the username is all lowercase as per Docker Hub requirements 582 | username = username.toLowerCase(); 583 | 584 | let obj = { 585 | name, 586 | namespace: username, 587 | }; 588 | 589 | if (typeof details.is_private === 'boolean') { 590 | obj.is_private = details.is_private; 591 | } 592 | 593 | if (details.description) { 594 | obj.description = details.description; 595 | } 596 | 597 | if (details.full_description) { 598 | obj.full_description = details.full_description; 599 | } 600 | 601 | return this.makePostRequest(`repositories/`, obj) 602 | .then(resolve) 603 | .catch(reject); 604 | }.bind(this), 605 | ); 606 | }, 607 | /** 608 | * Creates a webhook for the given username and repository. 609 | * 610 | * @param {String} username - the username of the repository to create a webhook for 611 | * @param {String} name - the name of the repository to create a webhook for 612 | * @param {String} webhookName - the name of webhook to create 613 | * @returns {Promise} 614 | */ 615 | createWebhook: function(username, name, webhookName) { 616 | return new Promise( 617 | function(resolve, reject) { 618 | if (!username || typeof username !== 'string') { 619 | return reject(new Error('Username must be provided!')); 620 | } 621 | 622 | if (!name || typeof name !== 'string') { 623 | return reject(new Error('Repository name must be provided!')); 624 | } 625 | 626 | if (!webhookName || typeof webhookName !== 'string') { 627 | return reject(new Error('Webhook name must be provided!')); 628 | } 629 | 630 | // Make sure the username is all lowercase as per Docker Hub requirements 631 | username = username.toLowerCase(); 632 | 633 | return this.makePostRequest(`repositories/${username}/${name}/webhooks/`, { 634 | name: webhookName, 635 | }) 636 | .then(resolve) 637 | .catch(reject); 638 | }.bind(this), 639 | ); 640 | }, 641 | /** 642 | * Creates a hook for an existing webhook. 643 | * 644 | * @param {String} username - the username of the repository to create a hook for 645 | * @param {String} name - the name of the repository to create a hook for 646 | * @param {String} webhookID - the id of the existing webhook to create a hook for 647 | * @param {String} url - the url of the hook to create 648 | * @returns {Promise} 649 | */ 650 | createWebhookHook: function(username, name, webhookID, url) { 651 | return new Promise( 652 | function(resolve, reject) { 653 | if (!username || typeof username !== 'string') { 654 | return reject(new Error('Username must be provided!')); 655 | } 656 | 657 | if (!name || typeof name !== 'string') { 658 | return reject(new Error('Repository name must be provided!')); 659 | } 660 | 661 | if (!webhookID || typeof webhookID !== 'number') { 662 | return reject(new Error('Webhook ID must be provided!')); 663 | } 664 | 665 | if (!url || typeof url !== 'string') { 666 | return reject(new Error('URL must be provided!')); 667 | } 668 | 669 | // Make sure the username is all lowercase as per Docker Hub requirements 670 | username = username.toLowerCase(); 671 | 672 | return this.makePostRequest( 673 | `repositories/${username}/${name}/webhooks/${webhookID}/hooks/`, 674 | { hook_url: url }, 675 | ) 676 | .then(resolve) 677 | .catch(reject); 678 | }.bind(this), 679 | ); 680 | }, 681 | /** 682 | * Deletes a build link for a given repository. 683 | * 684 | * @param {String} username - the username to delete a build link for 685 | * @param {String} name - the name of the repository to delete a build link for 686 | * @param {Number} id - the id of the build link to delete 687 | * @returns {Promise} 688 | */ 689 | deleteBuildLink: function(username, name, id) { 690 | return new Promise( 691 | function(resolve, reject) { 692 | if (!username || typeof username !== 'string') { 693 | return reject(new Error('Username must be provided!')); 694 | } 695 | 696 | if (!name || typeof name !== 'string') { 697 | return reject(new Error('Repository name must be provided!')); 698 | } 699 | 700 | if (!id) { 701 | return reject(new Error('Build link id must be provided!')); 702 | } 703 | 704 | if (typeof id !== 'number' || id < 0) { 705 | return reject(new Error('Build link id must be a number greater than 0!')); 706 | } 707 | 708 | // Make sure the username is all lowercase as per Docker Hub requirements 709 | username = username.toLowerCase(); 710 | 711 | this.makeDeleteRequest(`repositories/${username}/${name}/links/${id}`) 712 | .then(resolve) 713 | .catch(reject); 714 | }.bind(this), 715 | ); 716 | }, 717 | /** 718 | * Deletes a build tag for a given repository. 719 | * 720 | * @param {String} username - the username to delete a build tag for 721 | * @param {String} name - the name of the repository to delete a build tag for 722 | * @param {Number} id - the id of the build tag to delete 723 | * @returns {Promise} 724 | */ 725 | deleteBuildTag: function(username, name, id) { 726 | return new Promise( 727 | function(resolve, reject) { 728 | if (!username || typeof username !== 'string') { 729 | return reject(new Error('Username must be provided!')); 730 | } 731 | 732 | if (!name || typeof name !== 'string') { 733 | return reject(new Error('Repository name must be provided!')); 734 | } 735 | 736 | if (!id) { 737 | return reject(new Error('Build link id must be provided!')); 738 | } 739 | 740 | if (typeof id !== 'number' || id < 0) { 741 | return reject(new Error('Build link id must be a number greater than 0!')); 742 | } 743 | 744 | // Make sure the username is all lowercase as per Docker Hub requirements 745 | username = username.toLowerCase(); 746 | 747 | this.makeDeleteRequest(`repositories/${username}/${name}/autobuild/tags/${id}`) 748 | .then(resolve) 749 | .catch(reject); 750 | }.bind(this), 751 | ); 752 | }, 753 | /** 754 | * Deletes a build tag for a given repository. 755 | * 756 | * @param {String} username - the username to delete a collaborator for 757 | * @param {String} name - the name of the repository to delete a collaborator for 758 | * @param {String} collaborator - the username of the collaborator to delete 759 | * @returns {Promise} 760 | */ 761 | deleteCollaborator: function(username, name, collaborator) { 762 | return new Promise( 763 | function(resolve, reject) { 764 | if (!username || typeof username !== 'string') { 765 | return reject(new Error('Username must be provided!')); 766 | } 767 | 768 | if (!name || typeof name !== 'string') { 769 | return reject(new Error('Repository name must be provided!')); 770 | } 771 | 772 | if (!collaborator || typeof collaborator !== 'string') { 773 | return reject(new Error("Collaborator's username must be provided!")); 774 | } 775 | 776 | // Make sure the username and collaborator is all lowercase as per Docker Hub requirements 777 | username = username.toLowerCase(); 778 | collaborator = collaborator.toLowerCase(); 779 | 780 | this.makeDeleteRequest( 781 | `repositories/${username}/${name}/collaborators/${collaborator}`, 782 | ) 783 | .then(resolve) 784 | .catch(reject); 785 | }.bind(this), 786 | ); 787 | }, 788 | /** 789 | * Deletes a repository. 790 | * 791 | * @param {String} username - the username of the repository to delete 792 | * @param {String} name - the name of the repository to delete 793 | * @returns {Promise} 794 | */ 795 | deleteRepository: function(username, name) { 796 | return new Promise( 797 | function(resolve, reject) { 798 | if (!username || typeof username !== 'string') { 799 | return reject(new Error('Username must be provided!')); 800 | } 801 | 802 | if (!name || typeof name !== 'string') { 803 | return reject(new Error('Repository name must be provided!')); 804 | } 805 | 806 | // Make sure the username is all lowercase as per Docker Hub requirements 807 | username = username.toLowerCase(); 808 | 809 | return this.makeDeleteRequest(`repositories/${username}/${name}/`) 810 | .then(resolve) 811 | .catch(reject); 812 | }.bind(this), 813 | ); 814 | }, 815 | /** 816 | * Deletes a tag for the given username and repository. 817 | * 818 | * @param {String} username - the username of the repository to delete a tag for 819 | * @param {String} name - the name of the repository to delete a tag for 820 | * @param {String} tag - the tag to delete 821 | * @returns {Promise} 822 | */ 823 | deleteTag: function(username, name, tag) { 824 | return new Promise( 825 | function(resolve, reject) { 826 | if (!username || typeof username !== 'string') { 827 | return reject(new Error('Username must be provided!')); 828 | } 829 | 830 | if (!name || typeof name !== 'string') { 831 | return reject(new Error('Repository name must be provided!')); 832 | } 833 | 834 | if (!tag || typeof tag !== 'string') { 835 | return reject(new Error('Tag must be provided!')); 836 | } 837 | 838 | // Make sure the username is all lowercase as per Docker Hub requirements 839 | username = username.toLowerCase(); 840 | 841 | return this.makeDeleteRequest(`repositories/${username}/${name}/tags/${tag}/`) 842 | .then(resolve) 843 | .catch(reject); 844 | }.bind(this), 845 | ); 846 | }, 847 | /** 848 | * Deletes a webhook for the given username and repository. 849 | * 850 | * @param {String} username - the username of the repository to delete a webhook for 851 | * @param {String} name - the name of the repository to delete a webhook for 852 | * @param {Number} webhookID - the ID of webhook to delete 853 | * @returns {Promise} 854 | */ 855 | deleteWebhook: function(username, name, webhookID) { 856 | return new Promise( 857 | function(resolve, reject) { 858 | if (!username || typeof username !== 'string') { 859 | return reject(new Error('Username must be provided!')); 860 | } 861 | 862 | if (!name || typeof name !== 'string') { 863 | return reject(new Error('Repository name must be provided!')); 864 | } 865 | 866 | if (!webhookID || typeof webhookID !== 'number') { 867 | return reject(new Error('Webhook ID must be provided!')); 868 | } 869 | 870 | // Make sure the username is all lowercase as per Docker Hub requirements 871 | username = username.toLowerCase(); 872 | 873 | return this.makeDeleteRequest( 874 | `repositories/${username}/${name}/webhooks/${webhookID}/`, 875 | ) 876 | .then(resolve) 877 | .catch(reject); 878 | }.bind(this), 879 | ); 880 | }, 881 | /** 882 | * This gets the registry settings for the current logged in user containing information about the number of private repositories used/available. 883 | * 884 | * @returns {Promise} 885 | */ 886 | registrySettings: function() { 887 | return new Promise( 888 | function(resolve, reject) { 889 | this.loggedInUser() 890 | .then( 891 | function(user) { 892 | return this.makeGetRequest( 893 | `users/${user.username}/registry-settings`, 894 | ); 895 | }.bind(this), 896 | ) 897 | .then(resolve) 898 | .catch(reject); 899 | }.bind(this), 900 | ); 901 | }, 902 | /** 903 | * Gets the details about a repository. 904 | * 905 | * @param {String} [username] - the username of the repository to get information about. If left out or '_' is provided then it will query the official Docker repository with the given name 906 | * @param {String} name - the name of the repository to get information about 907 | * @returns {Promise} 908 | */ 909 | repository: function(username, name) { 910 | // If no name is passed in, then the user wants an official repository 911 | if (username && !name) { 912 | name = username; 913 | username = 'library'; 914 | } 915 | 916 | // If username is '_' then we're trying to get an official repository 917 | if (username === '_') { 918 | username = 'library'; 919 | } 920 | 921 | // Make sure the username is all lowercase as per Docker Hub requirements 922 | username = username.toLowerCase(); 923 | 924 | return this.makeGetRequest(`repositories/${username}/${name}`); 925 | }, 926 | /** 927 | * Gets the repositories for a user. 928 | * 929 | * @param {String} username - the username to get the repositories for 930 | * @returns {Promise} 931 | */ 932 | repositories: function(username) { 933 | return new Promise( 934 | function(resolve, reject) { 935 | if (!username) { 936 | return reject(new Error('Username must be provided!')); 937 | } 938 | 939 | // Make sure the username is all lowercase as per Docker Hub requirements 940 | username = username.toLowerCase(); 941 | 942 | this.makeGetRequest(`users/${username}/repositories`) 943 | .then(resolve) 944 | .catch(reject); 945 | }.bind(this), 946 | ); 947 | }, 948 | /** 949 | * Gets the starred repositories for a user. 950 | * 951 | * @param {String} username - the username to get the starred repositories for 952 | * @param {{page: Number, perPage: Number}} [options] - the options for this call 953 | * @returns {Promise} 954 | */ 955 | repositoriesStarred: function(username, options) { 956 | return new Promise( 957 | function(resolve, reject) { 958 | if (!username || typeof username !== 'string') { 959 | return reject(new Error('Username must be provided!')); 960 | } 961 | 962 | if (!options) { 963 | options = { page: 1, perPage: 100 }; 964 | } 965 | 966 | // Make sure the username is all lowercase as per Docker Hub requirements 967 | username = username.toLowerCase(); 968 | 969 | this.makeGetRequest( 970 | `users/${username}/repositories/starred?page_size=${options.perPage || 971 | 100}&page=${options.page || 1}`, 972 | 'results', 973 | ) 974 | .then(resolve) 975 | .catch(reject); 976 | }.bind(this), 977 | ); 978 | }, 979 | /** 980 | * This sets the details of a build tag for a given users repository. 981 | * 982 | * @param {String} username - the username 983 | * @param {String} name - the name of the repository 984 | * @param {Number} id - the id of the build tag to save 985 | * @param {Object} details - an object with a the new details of the build tag 986 | * @returns {Promise} 987 | */ 988 | saveBuildTag: function(username, name, id, details) { 989 | return new Promise( 990 | function(resolve, reject) { 991 | if (!username || typeof username !== 'string') { 992 | return reject(new Error('Username must be provided!')); 993 | } 994 | 995 | if (!name || typeof name !== 'string') { 996 | return reject(new Error('Repository name must be provided!')); 997 | } 998 | 999 | if (!id) { 1000 | return reject(new Error('id must be provided!')); 1001 | } 1002 | 1003 | if (typeof id !== 'number' || id <= 0) { 1004 | return reject(new Error('Provided id must be a number greater than 0!')); 1005 | } 1006 | 1007 | if (!details || typeof details !== 'object') { 1008 | return reject(new Error('Tag details must be provided!')); 1009 | } 1010 | 1011 | // Build our object to post 1012 | let obj = { 1013 | id, 1014 | name: details.name || 'latest', 1015 | dockerfile_location: details.dockerfile_location || '/', 1016 | source_type: details.source_type || 'Branch', 1017 | source_name: details.source_name || 'master', 1018 | }; 1019 | 1020 | return this.makePutRequest( 1021 | `repositories/${username}/${name}/autobuild/tags/${id}`, 1022 | obj, 1023 | ) 1024 | .then(resolve) 1025 | .catch(reject); 1026 | }.bind(this), 1027 | ); 1028 | }, 1029 | /** 1030 | * This sets the description (short, full, or both) for a given users repository. 1031 | * 1032 | * @param {String} username - the username 1033 | * @param {String} name - the name of the repository 1034 | * @param {Object} descriptions - an object with a full, short, or both properties 1035 | * @returns {Promise} 1036 | */ 1037 | setRepositoryDescription: function(username, name, descriptions) { 1038 | return new Promise( 1039 | function(resolve, reject) { 1040 | if (!username || !name || !descriptions) { 1041 | return reject( 1042 | new Error( 1043 | 'A username and repository name must be passed in as well as the data to set!', 1044 | ), 1045 | ); 1046 | } 1047 | 1048 | if ( 1049 | typeof descriptions !== 'object' || 1050 | (!descriptions.hasOwnProperty('full') && 1051 | !descriptions.hasOwnProperty('short')) 1052 | ) { 1053 | return reject( 1054 | new Error( 1055 | 'Passed in descriptions must be an object with full and/or short properties!', 1056 | ), 1057 | ); 1058 | } 1059 | 1060 | let obj = {}; 1061 | 1062 | if (descriptions.full) { 1063 | obj.full_description = descriptions.full; 1064 | } 1065 | 1066 | if (descriptions.short) { 1067 | obj.description = descriptions.short; 1068 | } 1069 | 1070 | return this.makePatchRequest(`repositories/${username}/${name}`, obj) 1071 | .then(resolve) 1072 | .catch(reject); 1073 | }.bind(this), 1074 | ); 1075 | }, 1076 | /** 1077 | * This sets the privacy (public or private) for a given repository. 1078 | * 1079 | * @param {String} username - the username 1080 | * @param {String} name - the name of the repository 1081 | * @param {Boolean} privacy - if the repository should be private 1082 | * @returns {Promise} 1083 | */ 1084 | setRepositoryPrivacy: function(username, name, privacy) { 1085 | return new Promise( 1086 | function(resolve, reject) { 1087 | if (!username || !name || !descriptions) { 1088 | return reject( 1089 | new Error( 1090 | 'A username and repository name must be passed in as well as the data to set!', 1091 | ), 1092 | ); 1093 | } 1094 | 1095 | if (typeof privacy !== 'boolean') { 1096 | return reject(new Error('Passed in privacy property must be a boolean!')); 1097 | } 1098 | 1099 | return this.makePostRequest(`repositories/${username}/${name}/privacy`, { 1100 | is_private: privacy, 1101 | }) 1102 | .then(resolve) 1103 | .catch(reject); 1104 | }.bind(this), 1105 | ); 1106 | }, 1107 | /** 1108 | * This stars a repository for the logged in user. 1109 | * 1110 | * @param {String} username - the username 1111 | * @param {String} name - the name of the repository 1112 | * @returns {Promise} 1113 | */ 1114 | starRepository: function(username, name) { 1115 | return new Promise( 1116 | function(resolve, reject) { 1117 | if (username && !name) { 1118 | name = username; 1119 | username = 'library'; 1120 | } 1121 | 1122 | // If username is '_' then we're trying to get an official repository 1123 | if (username === '_') { 1124 | username = 'library'; 1125 | } 1126 | 1127 | // Make sure the username is all lowercase as per Docker Hub requirements 1128 | username = username.toLowerCase(); 1129 | 1130 | return this.makePostRequest(`repositories/${username}/${name}/stars/`, {}) 1131 | .then(resolve) 1132 | .catch(reject); 1133 | }.bind(this), 1134 | ); 1135 | }, 1136 | /** 1137 | * Gets the tags for a repository. 1138 | * 1139 | * @param {String} [username] - the username of the repository to get tags for. If left out or '_' is provided then it will query the official Docker repository with the given name 1140 | * @param {String} name - the name of the repository to get tags for 1141 | * @param {{page: Number, perPage: Number}} [options] - the options for this call 1142 | * @returns {Promise} 1143 | */ 1144 | tags: function(username, name, options) { 1145 | // If no name is passed in, then the user wants an official repository 1146 | if (username && !name && !options) { 1147 | name = username; 1148 | username = 'library'; 1149 | options = { page: 1, perPage: 100 }; 1150 | } else if (username && name && !options) { 1151 | if (name instanceof Object) { 1152 | options = name; 1153 | name = username; 1154 | username = 'library'; 1155 | } else { 1156 | options = { page: 1, perPage: 100 }; 1157 | } 1158 | } 1159 | 1160 | // If username is '_' then we're trying to get an official repository 1161 | if (username === '_') { 1162 | username = 'library'; 1163 | } 1164 | 1165 | // Make sure the username is all lowercase as per Docker Hub requirements 1166 | username = username.toLowerCase(); 1167 | 1168 | return this.makeGetRequest( 1169 | `repositories/${username}/${name}/tags?page_size=${options.perPage || 1170 | 100}&page=${options.page || 1}`, 1171 | 'results', 1172 | ); 1173 | }, 1174 | /** 1175 | * Creates a build tag for a given repository. 1176 | * 1177 | * @param {String} username - the username to create a build tag for 1178 | * @param {String} name - the name of the repository to create a build tag for 1179 | * @param {Object} details - the details of the build tag 1180 | * @returns {Promise} 1181 | */ 1182 | triggerBuild: function(username, name, details) { 1183 | return new Promise( 1184 | function(resolve, reject) { 1185 | if (!username || typeof username !== 'string') { 1186 | return reject(new Error('Username must be provided!')); 1187 | } 1188 | 1189 | if (!name || typeof name !== 'string') { 1190 | return reject(new Error('Repository name must be provided!')); 1191 | } 1192 | 1193 | if (!details || typeof details !== 'object') { 1194 | return reject(new Error('Build details must be provided!')); 1195 | } 1196 | 1197 | // Make sure the username is all lowercase as per Docker Hub requirements 1198 | username = username.toLowerCase(); 1199 | 1200 | // Build our object to post 1201 | let obj = { 1202 | dockerfile_location: details.dockerfile_location || '/', 1203 | source_type: details.source_type || 'Branch', 1204 | source_name: details.source_name || 'master', 1205 | }; 1206 | 1207 | this.makePostRequest( 1208 | `repositories/${username}/${name}/autobuild/trigger-build`, 1209 | obj, 1210 | ) 1211 | .then(resolve) 1212 | .catch(reject); 1213 | }.bind(this), 1214 | ); 1215 | }, 1216 | /** 1217 | * This unstars a repository for the logged in user. 1218 | * 1219 | * @param {String} username - the username 1220 | * @param {String} name - the name of the repository 1221 | * @returns {Promise} 1222 | */ 1223 | unstarRepository: function(username, name) { 1224 | return new Promise( 1225 | function(resolve, reject) { 1226 | if (username && !name) { 1227 | name = username; 1228 | username = 'library'; 1229 | } 1230 | 1231 | // If username is '_' then we're trying to get an official repository 1232 | if (username === '_') { 1233 | username = 'library'; 1234 | } 1235 | 1236 | // Make sure the username is all lowercase as per Docker Hub requirements 1237 | username = username.toLowerCase(); 1238 | 1239 | return this.makeDeleteRequest(`repositories/${username}/${name}/stars/`) 1240 | .then(resolve) 1241 | .catch(reject); 1242 | }.bind(this), 1243 | ); 1244 | }, 1245 | /** 1246 | * Gets the details about a user. 1247 | * 1248 | * @param {String} username - the username to get information about 1249 | * @returns {Promise} 1250 | */ 1251 | user: function(username) { 1252 | return new Promise( 1253 | function(resolve, reject) { 1254 | if (!username) { 1255 | return reject(new Error('Username must be provided!')); 1256 | } 1257 | 1258 | // Make sure the username is all lowercase as per Docker Hub requirements 1259 | username = username.toLowerCase(); 1260 | 1261 | this.makeGetRequest(`users/${username}`) 1262 | .then(resolve) 1263 | .catch(reject); 1264 | }.bind(this), 1265 | ); 1266 | }, 1267 | /** 1268 | * Gets the webhooks for a repository you own. 1269 | * 1270 | * @param {String} username - the username to get the webhooks for 1271 | * @param {String} name - the name of the repository to get the webhooks for 1272 | * @param {{page: Number, perPage: Number}} [options] - the options for this call 1273 | * @returns {Promise} 1274 | */ 1275 | webhooks: function(username, name, options) { 1276 | return new Promise( 1277 | function(resolve, reject) { 1278 | if (!username || typeof username !== 'string') { 1279 | return reject(new Error('Username must be provided!')); 1280 | } 1281 | 1282 | if (!name || typeof name !== 'string') { 1283 | return reject(new Error('Repository name must be provided!')); 1284 | } 1285 | 1286 | if (!options) { 1287 | options = { page: 1, perPage: 100 }; 1288 | } 1289 | 1290 | // Make sure the username is all lowercase as per Docker Hub requirements 1291 | username = username.toLowerCase(); 1292 | 1293 | this.makeGetRequest( 1294 | `repositories/${username}/${name}/repositories/webhooks?page_size=${options.perPage || 1295 | 100}&page=${options.page || 1}`, 1296 | 'results', 1297 | ) 1298 | .then(resolve) 1299 | .catch(reject); 1300 | }.bind(this), 1301 | ); 1302 | }, 1303 | /** 1304 | * Checks the body response from a call to Docker Hub API to check if it's an error. 1305 | * 1306 | * @param {Object} body - the body response from the api 1307 | * @returns {Boolean} 1308 | */ 1309 | bodyHasError(body) { 1310 | let detailDefined = typeof body.detail !== 'undefined'; 1311 | 1312 | // if there is an error object in the body, then there is likely an error 1313 | if (typeof body.error !== 'undefined' && body.error === true) { 1314 | return true; 1315 | } 1316 | 1317 | if (!detailDefined || (typeof body.error !== 'undefined' && body.error === false)) { 1318 | return false; 1319 | } 1320 | 1321 | // if there is a detail message in the api, but the api specifically says no error 1322 | // then there is no error 1323 | if (detailDefined && typeof body.error !== 'undefined' && body.error === false) { 1324 | return false; 1325 | } 1326 | 1327 | // else fallback to behaviour of if there is a detail on the response, it's probably 1328 | // an error from what I've seen 1329 | return detailDefined; 1330 | }, 1331 | /** 1332 | * Makes a raw get request to the Docker Hub API. 1333 | * 1334 | * @param {String} path - the path to fetch 1335 | * @param {String} [extract] - the name of the property in the resulting JSON to extract. If left blank it will return the entire JSON 1336 | * @returns {Promise} 1337 | */ 1338 | makeGetRequest(path, extract) { 1339 | return new Promise( 1340 | function(resolve, reject) { 1341 | let params = this.makeRequestParams('get', path); 1342 | 1343 | if (cacheEnabled && cache.hasOwnProperty(params.url)) { 1344 | if (Date.now() >= cache[params.url].expires) { 1345 | delete cache[params.url]; 1346 | } else { 1347 | return resolve(cache[params.url].data); 1348 | } 1349 | } 1350 | 1351 | request( 1352 | params, 1353 | function(err, res, body) { 1354 | if (err) { 1355 | return reject(err); 1356 | } 1357 | 1358 | // Check for potential error messages 1359 | if (this.bodyHasError(body)) { 1360 | return reject(new Error(JSON.stringify(body))); 1361 | } 1362 | 1363 | if (cacheEnabled) { 1364 | cache[params.url] = { 1365 | expires: Date.now() + cacheTimeSeconds * 1000, 1366 | data: body, 1367 | }; 1368 | } 1369 | 1370 | if (extract && body.hasOwnProperty(extract)) { 1371 | return resolve(body[extract]); 1372 | } 1373 | 1374 | return resolve(body); 1375 | }.bind(this), 1376 | ); 1377 | }.bind(this), 1378 | ); 1379 | }, 1380 | /** 1381 | * Makes a raw delete request to the Docker Hub API. 1382 | * 1383 | * @param {String} path - the path to fetch 1384 | * @param {String} [extract] - the name of the property in the resulting JSON to extract. If left blank it will return the entire JSON 1385 | * @returns {Promise} 1386 | */ 1387 | makeDeleteRequest(path) { 1388 | return new Promise( 1389 | function(resolve, reject) { 1390 | request(this.makeRequestParams('delete', path), function(err) { 1391 | if (err) { 1392 | return reject(err); 1393 | } 1394 | 1395 | return resolve(); 1396 | }); 1397 | }.bind(this), 1398 | ); 1399 | }, 1400 | /** 1401 | * Makes a raw patch request to the Docker Hub API. 1402 | * 1403 | * @param {String} path - the path to fetch 1404 | * @param {Object} data - the data to send 1405 | * @param {String} [extract] - the name of the property in the resulting JSON to extract. If left blank it will return the entire JSON 1406 | * @returns {Promise} 1407 | */ 1408 | makePatchRequest(path, data, extract) { 1409 | return new Promise( 1410 | function(resolve, reject) { 1411 | if (!data || typeof data !== 'object') { 1412 | return reject( 1413 | new Error( 1414 | 'Data must be passed to all PATCH requests in the form of an object!', 1415 | ), 1416 | ); 1417 | } 1418 | 1419 | request( 1420 | this.makeRequestParams('patch', path, data), 1421 | function(err, res, body) { 1422 | if (err) { 1423 | return reject(err); 1424 | } 1425 | 1426 | // Check for potential error messages 1427 | if (this.bodyHasError(body)) { 1428 | return reject(new Error(JSON.stringify(body))); 1429 | } 1430 | 1431 | if (extract && body.hasOwnProperty(extract)) { 1432 | return resolve(body[extract]); 1433 | } 1434 | 1435 | return resolve(body); 1436 | }.bind(this), 1437 | ); 1438 | }.bind(this), 1439 | ); 1440 | }, 1441 | /** 1442 | * Makes a raw post request to the Docker Hub API. 1443 | * 1444 | * @param {String} path - the path to fetch 1445 | * @param {Object} data - the data to send 1446 | * @param {String} [extract] - the name of the property in the resulting JSON to extract. If left blank it will return the entire JSON 1447 | * @returns {Promise} 1448 | */ 1449 | makePostRequest(path, data, extract) { 1450 | return new Promise( 1451 | function(resolve, reject) { 1452 | request( 1453 | this.makeRequestParams('post', path, data), 1454 | function(err, res, body) { 1455 | if (err) { 1456 | return reject(err); 1457 | } 1458 | 1459 | // Some api calls don't return any data 1460 | if (!body) { 1461 | return resolve(); 1462 | } 1463 | 1464 | // Check for potential error messages 1465 | if (this.bodyHasError(body)) { 1466 | return reject(new Error(JSON.stringify(body))); 1467 | } 1468 | 1469 | if (extract && body.hasOwnProperty(extract)) { 1470 | return resolve(body[extract]); 1471 | } 1472 | 1473 | return resolve(body); 1474 | }.bind(this), 1475 | ); 1476 | }.bind(this), 1477 | ); 1478 | }, 1479 | /** 1480 | * Makes a raw put request to the Docker Hub API. 1481 | * 1482 | * @param {String} path - the path to fetch 1483 | * @param {Object} data - the data to send 1484 | * @param {String} [extract] - the name of the property in the resulting JSON to extract. If left blank it will return the entire JSON 1485 | * @returns {Promise} 1486 | */ 1487 | makePutRequest(path, data, extract) { 1488 | return new Promise( 1489 | function(resolve, reject) { 1490 | if (!data || typeof data !== 'object') { 1491 | return reject( 1492 | new Error( 1493 | 'Data must be passed to all PUT requests in the form of an object!', 1494 | ), 1495 | ); 1496 | } 1497 | 1498 | request( 1499 | this.makeRequestParams('put', path, data), 1500 | function(err, res, body) { 1501 | if (err) { 1502 | return reject(err); 1503 | } 1504 | 1505 | // Some api calls don't return any data 1506 | if (!body) { 1507 | return resolve(); 1508 | } 1509 | 1510 | // Check for potential error messages 1511 | if (this.bodyHasError(body)) { 1512 | return reject(new Error(JSON.stringify(body))); 1513 | } 1514 | 1515 | if (extract && body.hasOwnProperty(extract)) { 1516 | return resolve(body[extract]); 1517 | } 1518 | 1519 | return resolve(body); 1520 | }.bind(this), 1521 | ); 1522 | }.bind(this), 1523 | ); 1524 | }, 1525 | /** 1526 | * Generates and error checks a request object. 1527 | * 1528 | * @param {String} method - the method of the request 1529 | * @param {String} path - the path to fetch 1530 | * @param {Object} [data] - the data to send 1531 | * @returns {Object} 1532 | */ 1533 | makeRequestParams(method, path, data) { 1534 | // Normalize the path so it doesn't start with a slash 1535 | if (path.substr(0, 1) === '/') { 1536 | path = path.substr(1); 1537 | } 1538 | 1539 | // Also add a slash to the end of the path unless there is a ? in the path 1540 | if (path.substr(-1) !== '/' && path.indexOf('?') === -1) { 1541 | path = path + '/'; 1542 | } 1543 | 1544 | let url = `https://hub.docker.com/v${apiVersion}/${path}`; 1545 | 1546 | let headers = {}; 1547 | 1548 | if (loggedInToken) { 1549 | headers.Authorization = `JWT ${loggedInToken}`; 1550 | } 1551 | 1552 | let params = { url, method, json: true, headers }; 1553 | 1554 | if (data) { 1555 | params.body = data; 1556 | } 1557 | 1558 | return params; 1559 | }, 1560 | }; 1561 | })(); 1562 | --------------------------------------------------------------------------------