UNPKG

4.91 kBPlain TextView Raw
1/*
2 * Copyright © 2019 Atomist, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {
18 configurationValue,
19 defaultHttpClientFactory,
20 HttpMethod,
21 logger,
22 RetryOptions,
23} from "@atomist/automation-client";
24
25export interface AtomistBuildRepository {
26 owner_name: string;
27 name: string;
28}
29
30export type AtomistBuildType = "cron" | "pull_request" | "push" | "tag" | "manual";
31
32export type AtomistBuildStatus = "started" | "failed" | "error" | "passed" | "canceled";
33
34export 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
50export type AtomistWebhookType = "application" | "build" | "link-image";
51
52const DefaultRetryOptions = {
53 retries: 10,
54 factor: 2,
55 minTimeout: 1 * 500,
56 maxTimeout: 5 * 1000,
57 randomize: true,
58};
59
60/**
61 * Post to the Atomist generic build webhook URL. It creates the payload
62 * then uses postWebhook.
63 *
64 * @param owner repository owner, i.e., user or organization
65 * @param repo name of repository
66 * @param branch commit branch
67 * @param commit commit SHA
68 * @param status "start", "success", or "fail"
69 * @param teamId Atomist team ID
70 * @param retryOptions change default retry options
71 * @return true if successful, false on failure after retries
72 */
73export 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
94export interface AtomistLinkImageGit {
95 owner: string;
96 repo: string;
97 sha: string;
98}
99
100export interface AtomistLinkImageDocker {
101 image: string;
102}
103
104export interface AtomistLinkImage {
105 git: AtomistLinkImageGit;
106 docker: AtomistLinkImageDocker;
107 type: "link-image";
108}
109
110/**
111 * Post to the Atomist link-image webhook URL. It creates the payload
112 * then uses postWebhook.
113 *
114 * @param owner repository owner, i.e., user or organization
115 * @param repo name of repository
116 * @param commit commit SHA
117 * @param image Docker image tag, e.g., registry.com/owner/repo:version
118 * @param teamId Atomist team ID
119 * @param retryOptions change default retry options
120 * @return true if successful, false on failure after retries
121 */
122export 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 * Post payload to the Atomist webhook URL. It will retry
147 * several times.
148 *
149 * @param webhook type of webhook
150 * @param payload object to post
151 * @param teamId Atomist team ID
152 * @param retryOptions change default retry options
153 * @return true if successful, false on failure after retries
154 */
155export 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}