|
1 | 1 | import { getBitBucketAccessToken } from './utils'; |
2 | | -import { getConfig } from './setup'; |
| 2 | +import { getConfigAsync } from './setup'; |
3 | 3 | import { logger, LogMessageType } from './logger'; |
4 | 4 | import * as request from 'request'; |
5 | 5 |
|
6 | 6 | export function sendSuccessStatus(build: any, buildId: number): Promise<void> { |
7 | | - let config: any = getConfig(); |
8 | | - if (build.repository && build.repository.access_token) { |
9 | | - if (build.repository.github_id) { |
10 | | - let sha = build.data.after || build.data.pull_request && build.data.pull_request.head.sha || |
11 | | - build.data.sha; |
12 | | - let name = build.repository.full_name; |
13 | | - let gitUrl = null; |
14 | | - if (build.repository.api_url && build.repository.api_url !== '') { |
15 | | - gitUrl = build.repository.api_url + `/repos/${name}/statuses/${sha}`; |
| 7 | + return getConfigAsync() |
| 8 | + .then(config => { |
| 9 | + if (build.repository && build.repository.access_token) { |
| 10 | + if (build.repository.github_id) { |
| 11 | + let sha = build.data.after || build.data.pull_request && build.data.pull_request.head.sha || |
| 12 | + build.data.sha; |
| 13 | + let name = build.repository.full_name; |
| 14 | + let gitUrl = null; |
| 15 | + if (build.repository.api_url && build.repository.api_url !== '') { |
| 16 | + gitUrl = build.repository.api_url + `/repos/${name}/statuses/${sha}`; |
| 17 | + } else { |
| 18 | + gitUrl = `https://api.github.com/repos/${name}/statuses/${sha}`; |
| 19 | + } |
| 20 | + let abstruseUrl = `${config.url}/build/${buildId}`; |
| 21 | + |
| 22 | + return setGitHubStatusSuccess(gitUrl, abstruseUrl, |
| 23 | + build.repository.access_token); |
| 24 | + } else if (build.repository.bitbucket_id) { |
| 25 | + let sha = build.data.sha; |
| 26 | + let name = build.data.repository.full_name; |
| 27 | + let gitUrl = `https://api.bitbucket.org/2.0/repositories` |
| 28 | + + `/${name}/commit/${sha}/statuses/build`; |
| 29 | + let abstruseUrl = `${config.url}/build/${buildId}`; |
| 30 | + |
| 31 | + return setBitbucketStatusSuccess(gitUrl, abstruseUrl, |
| 32 | + build.repository.access_token); |
| 33 | + } else if (build.repository.gitlab_id) { |
| 34 | + let id = build.data.project_id ? |
| 35 | + build.data.project_id : build.data.object_attributes.target_project_id; |
| 36 | + let sha = build.data.checkout_sha || build.data.object_attributes.last_commit.id; |
| 37 | + let gitUrl = `https://gitlab.com/api/v4/projects/${id}/statuses/${sha}`; |
| 38 | + let abstruseUrl = `${config.url}/build/${buildId}`; |
| 39 | + |
| 40 | + return setGitLabStatusSuccess(gitUrl, abstruseUrl, |
| 41 | + build.repository.access_token); |
| 42 | + } else { |
| 43 | + return Promise.resolve(); |
| 44 | + } |
16 | 45 | } else { |
17 | | - gitUrl = `https://api.github.com/repos/${name}/statuses/${sha}`; |
| 46 | + let name = build.repository && build.repository.full_name || |
| 47 | + build.data.repository.full_name; |
| 48 | + let msg: LogMessageType = { |
| 49 | + message: `[error]: repository: ${name} => access token is not set`, |
| 50 | + type: 'error', |
| 51 | + notify: true |
| 52 | + }; |
| 53 | + logger.next(msg); |
18 | 54 | } |
19 | | - let abstruseUrl = `${config.url}/build/${buildId}`; |
20 | | - |
21 | | - return setGitHubStatusSuccess(gitUrl, abstruseUrl, |
22 | | - build.repository.access_token); |
23 | | - } else if (build.repository.bitbucket_id) { |
24 | | - let sha = build.data.sha; |
25 | | - let name = build.data.repository.full_name; |
26 | | - let gitUrl = `https://api.bitbucket.org/2.0/repositories` |
27 | | - + `/${name}/commit/${sha}/statuses/build`; |
28 | | - let abstruseUrl = `${config.url}/build/${buildId}`; |
29 | | - |
30 | | - return setBitbucketStatusSuccess(gitUrl, abstruseUrl, |
31 | | - build.repository.access_token); |
32 | | - } else if (build.repository.gitlab_id) { |
33 | | - let id = build.data.project_id ? |
34 | | - build.data.project_id : build.data.object_attributes.target_project_id; |
35 | | - let sha = build.data.checkout_sha || build.data.object_attributes.last_commit.id; |
36 | | - let gitUrl = `https://gitlab.com/api/v4/projects/${id}/statuses/${sha}`; |
37 | | - let abstruseUrl = `${config.url}/build/${buildId}`; |
38 | | - |
39 | | - return setGitLabStatusSuccess(gitUrl, abstruseUrl, |
40 | | - build.repository.access_token); |
41 | | - } else { |
42 | | - return Promise.resolve(); |
43 | | - } |
44 | | - } else { |
45 | | - let name = build.repository && build.repository.full_name || |
46 | | - build.data.repository.full_name; |
47 | | - let msg: LogMessageType = { |
48 | | - message: `[error]: repository: ${name} => access token is not set`, |
49 | | - type: 'error', |
50 | | - notify: true |
51 | | - }; |
52 | | - logger.next(msg); |
53 | | - |
54 | | - return Promise.resolve(); |
55 | | - } |
| 55 | + }); |
56 | 56 | } |
57 | 57 |
|
58 | 58 | export function sendPendingStatus(buildData: any, buildId: number): Promise<void> { |
59 | | - let config: any = getConfig(); |
60 | | - if (buildData.repository && buildData.repository.access_token) { |
61 | | - if (buildData.repository.github_id) { |
62 | | - let sha = buildData.data.after || buildData.data.pull_request && |
63 | | - buildData.data.pull_request.head.sha || buildData.data.sha; |
64 | | - let name = buildData.repository.full_name; |
65 | | - let gitUrl = null; |
66 | | - if (buildData.repository.api_url && buildData.repository.api_url !== '') { |
67 | | - gitUrl = buildData.repository.api_url + `/repos/${name}/statuses/${sha}`; |
| 59 | + return getConfigAsync() |
| 60 | + .then(config => { |
| 61 | + if (buildData.repository && buildData.repository.access_token) { |
| 62 | + if (buildData.repository.github_id) { |
| 63 | + let sha = buildData.data.after || buildData.data.pull_request && |
| 64 | + buildData.data.pull_request.head.sha || buildData.data.sha; |
| 65 | + let name = buildData.repository.full_name; |
| 66 | + let gitUrl = null; |
| 67 | + if (buildData.repository.api_url && buildData.repository.api_url !== '') { |
| 68 | + gitUrl = buildData.repository.api_url + `/repos/${name}/statuses/${sha}`; |
| 69 | + } else { |
| 70 | + gitUrl = `https://api.github.com/repos/${name}/statuses/${sha}`; |
| 71 | + } |
| 72 | + let abstruseUrl = `${config.url}/build/${buildId}`; |
| 73 | + |
| 74 | + return setGitHubStatusPending(gitUrl, abstruseUrl, buildData.repository.access_token); |
| 75 | + } else if (buildData.repository.bitbucket_id) { |
| 76 | + let sha = buildData.data.sha; |
| 77 | + let name = buildData.data.repository.full_name; |
| 78 | + let gitUrl = `https://api.bitbucket.org/2.0/repositories` |
| 79 | + + `/${name}/commit/${sha}/statuses/build`; |
| 80 | + let abstruseUrl = `${config.url}/build/${buildId}`; |
| 81 | + |
| 82 | + return setBitbucketStatusPending(gitUrl, abstruseUrl, buildData.repository.access_token); |
| 83 | + } else if (buildData.repository.gitlab_id) { |
| 84 | + let id = buildData.data.project_id ? |
| 85 | + buildData.data.project_id : buildData.data.object_attributes.target_project_id; |
| 86 | + let sha = buildData.data.checkout_sha || buildData.data.object_attributes.last_commit.id; |
| 87 | + let gitUrl = `https://gitlab.com/api/v4/projects/${id}/statuses/${sha}`; |
| 88 | + let abstruseUrl = `${config.url}/build/${buildId}`; |
| 89 | + |
| 90 | + return setGitLabStatusPending(gitUrl, abstruseUrl, buildData.repository.access_token); |
| 91 | + } else { |
| 92 | + return Promise.resolve(); |
| 93 | + } |
68 | 94 | } else { |
69 | | - gitUrl = `https://api.github.com/repos/${name}/statuses/${sha}`; |
| 95 | + let name = buildData.repository && (buildData.repository.full_name || |
| 96 | + buildData.data.repository.full_name); |
| 97 | + let msg: LogMessageType = { |
| 98 | + message: `[error]: repository: ${name} => access token is not set`, |
| 99 | + type: 'error', |
| 100 | + notify: true |
| 101 | + }; |
| 102 | + logger.next(msg); |
70 | 103 | } |
71 | | - let abstruseUrl = `${config.url}/build/${buildId}`; |
72 | | - |
73 | | - return setGitHubStatusPending(gitUrl, abstruseUrl, buildData.repository.access_token); |
74 | | - } else if (buildData.repository.bitbucket_id) { |
75 | | - let sha = buildData.data.sha; |
76 | | - let name = buildData.data.repository.full_name; |
77 | | - let gitUrl = `https://api.bitbucket.org/2.0/repositories` |
78 | | - + `/${name}/commit/${sha}/statuses/build`; |
79 | | - let abstruseUrl = `${config.url}/build/${buildId}`; |
80 | | - |
81 | | - return setBitbucketStatusPending(gitUrl, abstruseUrl, buildData.repository.access_token); |
82 | | - } else if (buildData.repository.gitlab_id) { |
83 | | - let id = buildData.data.project_id ? |
84 | | - buildData.data.project_id : buildData.data.object_attributes.target_project_id; |
85 | | - let sha = buildData.data.checkout_sha || buildData.data.object_attributes.last_commit.id; |
86 | | - let gitUrl = `https://gitlab.com/api/v4/projects/${id}/statuses/${sha}`; |
87 | | - let abstruseUrl = `${config.url}/build/${buildId}`; |
88 | | - |
89 | | - return setGitLabStatusPending(gitUrl, abstruseUrl, buildData.repository.access_token); |
90 | | - } else { |
91 | | - return Promise.resolve(); |
92 | | - } |
93 | | - } else { |
94 | | - let name = buildData.repository && (buildData.repository.full_name || |
95 | | - buildData.data.repository.full_name); |
96 | | - let msg: LogMessageType = { |
97 | | - message: `[error]: repository: ${name} => access token is not set`, |
98 | | - type: 'error', |
99 | | - notify: true |
100 | | - }; |
101 | | - logger.next(msg); |
102 | | - |
103 | | - return Promise.resolve(); |
104 | | - } |
| 104 | + }); |
105 | 105 | } |
106 | 106 |
|
107 | 107 | export function sendFailureStatus(buildData: any, buildId: number): Promise<void> { |
108 | | - let config: any = getConfig(); |
109 | | - if (buildData.repository && buildData.repository.access_token) { |
110 | | - if (buildData.repository.github_id) { |
111 | | - let sha = buildData.data.after || buildData.data.pull_request && |
112 | | - buildData.data.pull_request.head.sha || buildData.data.sha; |
113 | | - let name = buildData.repository.full_name; |
114 | | - let gitUrl = null; |
115 | | - if (buildData.repository.api_url && buildData.repository.api_url !== '') { |
116 | | - gitUrl = buildData.repository.api_url + `/repos/${name}/statuses/${sha}`; |
| 108 | + return getConfigAsync() |
| 109 | + .then(config => { |
| 110 | + if (buildData.repository && buildData.repository.access_token) { |
| 111 | + if (buildData.repository.github_id) { |
| 112 | + let sha = buildData.data.after || buildData.data.pull_request && |
| 113 | + buildData.data.pull_request.head.sha || buildData.data.sha; |
| 114 | + let name = buildData.repository.full_name; |
| 115 | + let gitUrl = null; |
| 116 | + if (buildData.repository.api_url && buildData.repository.api_url !== '') { |
| 117 | + gitUrl = buildData.repository.api_url + `/repos/${name}/statuses/${sha}`; |
| 118 | + } else { |
| 119 | + gitUrl = `https://api.github.com/repos/${name}/statuses/${sha}`; |
| 120 | + } |
| 121 | + let abstruseUrl = `${config.url}/build/${buildId}`; |
| 122 | + |
| 123 | + return setGitHubStatusFailure(gitUrl, abstruseUrl, buildData.repository.access_token); |
| 124 | + } else if (buildData.repository.bitbucket_id) { |
| 125 | + let sha = buildData.data.sha; |
| 126 | + let name = buildData.data.repository.full_name; |
| 127 | + let gitUrl = `https://api.bitbucket.org/2.0/repositories` |
| 128 | + + `/${name}/commit/${sha}/statuses/build`; |
| 129 | + let abstruseUrl = `${config.url}/build/${buildId}`; |
| 130 | + |
| 131 | + return setBitbucketStatusFailure(gitUrl, abstruseUrl, buildData.repository.access_token); |
| 132 | + } else if (buildData.repository.gitlab_id) { |
| 133 | + let id = buildData.data.project_id ? |
| 134 | + buildData.data.project_id : buildData.data.object_attributes.target_project_id; |
| 135 | + let sha = buildData.data.checkout_sha || buildData.data.object_attributes.last_commit.id; |
| 136 | + let gitUrl = `https://gitlab.com/api/v4/projects/${id}/statuses/${sha}`; |
| 137 | + let abstruseUrl = `${config.url}/build/${buildId}`; |
| 138 | + |
| 139 | + return setGitLabStatusFailure(gitUrl, abstruseUrl, buildData.repository.access_token); |
| 140 | + } else { |
| 141 | + return Promise.resolve(); |
| 142 | + } |
117 | 143 | } else { |
118 | | - gitUrl = `https://api.github.com/repos/${name}/statuses/${sha}`; |
| 144 | + let name = buildData.repository && buildData.repository.full_name || |
| 145 | + buildData.data.repository.full_name; |
| 146 | + let msg: LogMessageType = { |
| 147 | + message: `[error]: repository: ${name} => access token is not set`, |
| 148 | + type: 'error', |
| 149 | + notify: true |
| 150 | + }; |
| 151 | + logger.next(msg); |
119 | 152 | } |
120 | | - let abstruseUrl = `${config.url}/build/${buildId}`; |
121 | | - |
122 | | - return setGitHubStatusFailure(gitUrl, abstruseUrl, buildData.repository.access_token); |
123 | | - } else if (buildData.repository.bitbucket_id) { |
124 | | - let sha = buildData.data.sha; |
125 | | - let name = buildData.data.repository.full_name; |
126 | | - let gitUrl = `https://api.bitbucket.org/2.0/repositories` |
127 | | - + `/${name}/commit/${sha}/statuses/build`; |
128 | | - let abstruseUrl = `${config.url}/build/${buildId}`; |
129 | | - |
130 | | - return setBitbucketStatusFailure(gitUrl, abstruseUrl, buildData.repository.access_token); |
131 | | - } else if (buildData.repository.gitlab_id) { |
132 | | - let id = buildData.data.project_id ? |
133 | | - buildData.data.project_id : buildData.data.object_attributes.target_project_id; |
134 | | - let sha = buildData.data.checkout_sha || buildData.data.object_attributes.last_commit.id; |
135 | | - let gitUrl = `https://gitlab.com/api/v4/projects/${id}/statuses/${sha}`; |
136 | | - let abstruseUrl = `${config.url}/build/${buildId}`; |
137 | | - |
138 | | - return setGitLabStatusFailure(gitUrl, abstruseUrl, buildData.repository.access_token); |
139 | | - } else { |
140 | | - return Promise.resolve(); |
141 | | - } |
142 | | - } else { |
143 | | - let name = buildData.repository && buildData.repository.full_name || |
144 | | - buildData.data.repository.full_name; |
145 | | - let msg: LogMessageType = { |
146 | | - message: `[error]: repository: ${name} => access token is not set`, |
147 | | - type: 'error', |
148 | | - notify: true |
149 | | - }; |
150 | | - logger.next(msg); |
151 | | - |
152 | | - return Promise.resolve(); |
153 | | - } |
| 153 | + }); |
154 | 154 | } |
155 | 155 |
|
156 | 156 | function setGitHubStatusSuccess( |
|
0 commit comments