1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import {
|
18 | configurationValue,
|
19 | defaultHttpClientFactory,
|
20 | HttpMethod,
|
21 | logger,
|
22 | RetryOptions,
|
23 | } from "@atomist/automation-client";
|
24 |
|
25 | export interface AtomistBuildRepository {
|
26 | owner_name: string;
|
27 | name: string;
|
28 | }
|
29 |
|
30 | export type AtomistBuildType = "cron" | "pull_request" | "push" | "tag" | "manual";
|
31 |
|
32 | export type AtomistBuildStatus = "started" | "failed" | "error" | "passed" | "canceled";
|
33 |
|
34 | export interface AtomistBuild {
|
35 | repository: AtomistBuildRepository;
|
36 | number?: number;
|
37 | name?: string;
|
38 | compare_url?: string;
|
39 | type: AtomistBuildType;
|
40 | pull_request_number?: number;
|
41 | build_url?: string;
|
42 | status: AtomistBuildStatus;
|
43 | id?: string;
|
44 | commit: string;
|
45 | tag?: string;
|
46 | branch?: string;
|
47 | provider?: string;
|
48 | }
|
49 |
|
50 | export type AtomistWebhookType = "application" | "build" | "link-image";
|
51 |
|
52 | const DefaultRetryOptions = {
|
53 | retries: 10,
|
54 | factor: 2,
|
55 | minTimeout: 1 * 500,
|
56 | maxTimeout: 5 * 1000,
|
57 | randomize: true,
|
58 | };
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | export function postBuildWebhook(
|
74 | owner: string,
|
75 | repo: string,
|
76 | branch: string,
|
77 | commit: string,
|
78 | status: AtomistBuildStatus,
|
79 | teamId: string,
|
80 | retryOptions: RetryOptions = DefaultRetryOptions,
|
81 | ): Promise<boolean> {
|
82 |
|
83 | const payload: AtomistBuild = {
|
84 | repository: { owner_name: owner, name: repo },
|
85 | type: "push",
|
86 | status,
|
87 | commit,
|
88 | branch,
|
89 | provider: "GoogleContainerBuilder",
|
90 | };
|
91 | return postWebhook("build", payload, teamId, retryOptions);
|
92 | }
|
93 |
|
94 | export interface AtomistLinkImageGit {
|
95 | owner: string;
|
96 | repo: string;
|
97 | sha: string;
|
98 | }
|
99 |
|
100 | export interface AtomistLinkImageDocker {
|
101 | image: string;
|
102 | }
|
103 |
|
104 | export interface AtomistLinkImage {
|
105 | git: AtomistLinkImageGit;
|
106 | docker: AtomistLinkImageDocker;
|
107 | type: "link-image";
|
108 | }
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | export function postLinkImageWebhook(
|
123 | owner: string,
|
124 | repo: string,
|
125 | commit: string,
|
126 | image: string,
|
127 | teamId: string,
|
128 | retryOptions: RetryOptions = DefaultRetryOptions,
|
129 | ): Promise<boolean> {
|
130 |
|
131 | const payload: AtomistLinkImage = {
|
132 | git: {
|
133 | owner,
|
134 | repo,
|
135 | sha: commit,
|
136 | },
|
137 | docker: {
|
138 | image,
|
139 | },
|
140 | type: "link-image",
|
141 | };
|
142 | return postWebhook("link-image", payload, teamId, retryOptions);
|
143 | }
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | export async function postWebhook(
|
156 | webhook: AtomistWebhookType,
|
157 | payload: any,
|
158 | teamId: string,
|
159 | retryOptions: RetryOptions = DefaultRetryOptions,
|
160 | ): Promise<boolean> {
|
161 | logger.debug("Posting webhook: %j", payload);
|
162 |
|
163 | const baseUrl = process.env.ATOMIST_WEBHOOK_BASEURL || "https://webhook.atomist.com";
|
164 | const url = `${baseUrl}/atomist/${webhook}/teams/${teamId}`;
|
165 |
|
166 | const httpClient = configurationValue("http.client.factory", defaultHttpClientFactory()).create(url);
|
167 |
|
168 | try {
|
169 | await httpClient.exchange(
|
170 | url,
|
171 | {
|
172 | method: HttpMethod.Post,
|
173 | body: payload,
|
174 | options: {
|
175 | retry: retryOptions,
|
176 | },
|
177 | });
|
178 | return true;
|
179 | } catch (e) {
|
180 | return false;
|
181 | }
|
182 | }
|