1 | _ = require 'lodash'
|
2 | request = require 'request'
|
3 | async = require 'async'
|
4 | url = require 'url'
|
5 | colors = require 'colors'
|
6 | debug = require('debug')('deploy-state-util:quay-service')
|
7 |
|
8 | QUAY_BASE_URL='https://quay.io/api/v1'
|
9 |
|
10 | class QuayService
|
11 | constructor: ({ config, @quayToken }) ->
|
12 | throw new Error 'Missing config argument' unless config?
|
13 | throw new Error 'Missing quayToken argument' unless @quayToken?
|
14 |
|
15 | configure: ({ @repo, @owner, @isPrivate }, callback) =>
|
16 | debug 'setting up quay'
|
17 | @_repositoryExists (error, exists) =>
|
18 | return callback error if error?
|
19 | return callback null if exists?
|
20 | @_clearNotifications callback
|
21 |
|
22 | _getNotifications: (callback) =>
|
23 | debug 'getting notifications'
|
24 | options =
|
25 | method: 'GET'
|
26 | uri: "/repository/#{@owner}/#{@repo}/notification/"
|
27 | json: true
|
28 |
|
29 | @_request options, (error, body) =>
|
30 | return callback error if error?
|
31 | debug 'got notifications', body.notifications
|
32 | callback null, body.notifications
|
33 |
|
34 | _deleteNotification: ({ uuid }, callback) =>
|
35 | debug 'delete notification', { uuid }
|
36 | options =
|
37 | method: 'DELETE'
|
38 | uri: "/repository/#{@owner}/#{@repo}/notification/#{uuid}"
|
39 | json: true
|
40 |
|
41 | console.log colors.magenta('NOTICE'), colors.white('deleting quay webook')
|
42 | @_request options, callback
|
43 |
|
44 | _clearNotifications: (callback) =>
|
45 | @_getNotifications (error, notifications) =>
|
46 | return callback error if error?
|
47 | async.each notifications, @_deleteNotification, callback
|
48 |
|
49 | _repositoryExists: (callback) =>
|
50 | options =
|
51 | method: 'GET'
|
52 | uri: "/repository/#{@owner}/#{@repo}"
|
53 | json: true
|
54 |
|
55 | @_request options, (error, body, statusCode) =>
|
56 | return callback error if error?
|
57 | exists = statusCode != 404
|
58 | debug 'repo exists', exists
|
59 | callback null, exists
|
60 |
|
61 | _request: ({ method, uri, json }, callback) =>
|
62 | options = {
|
63 | method,
|
64 | uri,
|
65 | baseUrl: QUAY_BASE_URL
|
66 | headers:
|
67 | Authorization: "Bearer #{@quayToken}"
|
68 | followAllRedirects: true
|
69 | json
|
70 | }
|
71 | request options, (error, response, body) =>
|
72 | return callback error, null, response.statusCode if error?
|
73 | return callback body, null, response.statusCode if response.statusCode > 499
|
74 | callback null, body, response.statusCode
|
75 |
|
76 | module.exports = QuayService
|