UNPKG

3.55 kBtext/coffeescriptView Raw
1_ = require 'lodash'
2request = require 'request'
3async = require 'async'
4url = require 'url'
5debug = require('debug')('deploy-state-util:quay-service')
6
7QUAY_BASE_URL='https://quay.io/api/v1'
8
9class QuayService
10 constructor: ({ config, @quayToken }) ->
11 throw new Error 'Missing config argument' unless config?
12 throw new Error 'Missing quayToken argument' unless @quayToken?
13 @webhookUrl = url.format {
14 hostname: config['deploy-state'].hostname,
15 protocol: 'https',
16 slashes: true,
17 pathname: '/deployments/quay.io'
18 auth: "#{config['deploy-state'].username}:#{config['deploy-state'].password}"
19 }
20
21 configure: ({ @repo, @owner, @isPrivate }, callback) =>
22 debug 'setting up quay'
23 @_createRepository (error) =>
24 return callback error if error?
25 @_createNotification callback
26
27 _getNotifications: (callback) =>
28 debug 'getting notifications'
29 options =
30 method: 'GET'
31 uri: "/repository/#{@owner}/#{@repo}/notification/"
32 json: true
33
34 @_request options, (error, body) =>
35 return callback error if error?
36 debug 'got notifications', body.notifications
37 callback null, body.notifications
38
39 _deleteNotification: ({ uuid }, callback) =>
40 debug 'delete notification', { uuid }
41 options =
42 method: 'DELETE'
43 uri: "/repository/#{@owner}/#{@repo}/notification/#{uuid}"
44 json: true
45
46 @_request options, callback
47
48 _clearNotifications: (callback) =>
49 @_getNotifications (error, notifications) =>
50 return callback error if error?
51 async.each notifications, @_deleteNotification, callback
52
53 _createNotification: (callback) =>
54 options =
55 method: 'POST'
56 uri: "/repository/#{@owner}/#{@repo}/notification/"
57 json:
58 eventConfig: {}
59 title: "Deploy State"
60 config:
61 url: @webhookUrl
62 event: "repo_push"
63 method: "webhook"
64
65 @_clearNotifications (error) =>
66 return callback error if error?
67 debug 'create notification in quay', options
68 @_request options, (error, body) =>
69 return callback error if error?
70 callback null
71
72 _repositoryExists: (callback) =>
73 options =
74 method: 'GET'
75 uri: "/repository/#{@owner}/#{@repo}"
76 json: true
77
78 @_request options, (error, body, statusCode) =>
79 return callback error if error?
80 exists = statusCode != 404
81 debug 'repo exists', exists
82 callback null, exists
83
84 _createRepository: (callback) =>
85 visibility = 'public'
86 visibility = 'private' if @isPrivate
87 options =
88 method: 'POST'
89 uri: '/repository'
90 json:
91 namespace: @owner
92 visibility: visibility
93 repository: @repo
94 description: "Service #{@owner}/#{@repo}"
95
96 @_repositoryExists (error, exists) =>
97 return callback error if error?
98 return callback null if exists
99 debug 'create repository in quay', options
100 @_request options, (error, body) =>
101 return callback error if error?
102 callback null
103
104 _request: ({ method, uri, json }, callback) =>
105 options = {
106 method,
107 uri,
108 baseUrl: QUAY_BASE_URL
109 headers:
110 Authorization: "Bearer #{@quayToken}"
111 followAllRedirects: true
112 json
113 }
114 request options, (error, response, body) =>
115 return callback error, null, response.statusCode if error?
116 return callback body, null, response.statusCode if response.statusCode > 499
117 callback null, body, response.statusCode
118
119module.exports = QuayService