UNPKG

6.76 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 BitBucketServerRepoRef,
19 GitHubRepoRef,
20 GitlabRepoRef,
21 RemoteRepoRef,
22} from "@atomist/automation-client";
23import {
24 CoreRepoFieldsAndChannels,
25 OnPushToAnyBranch,
26 ProviderType,
27 RepoRefResolver,
28 SdmGoalEvent,
29} from "@atomist/sdm";
30import * as _ from "lodash";
31
32export class DefaultRepoRefResolver implements RepoRefResolver {
33
34 /**
35 * Obtain a RemoteRepoRef from the given push, correctly
36 * resolving provider.
37 * @param {OnPushToAnyBranch.Push} push
38 * @return {any}
39 */
40 public repoRefFromPush(push: OnPushToAnyBranch.Push): RemoteRepoRef {
41 const providerType = push.repo.org.provider.providerType;
42 switch (providerType) {
43 case ProviderType.github_com:
44 case ProviderType.ghe:
45 return GitHubRepoRef.from({
46 owner: push.repo.owner,
47 repo: push.repo.name,
48 sha: push.after.sha,
49 rawApiBase: push.repo.org.provider.apiUrl,
50 branch: push.branch,
51 });
52 case ProviderType.bitbucket:
53 const providerUrl = push.repo.org.provider.url;
54 return this.toBitBucketServerRepoRef({
55 providerUrl,
56 owner: push.repo.owner,
57 name: push.repo.name,
58 sha: push.after.sha,
59 branch: push.branch,
60 });
61 case ProviderType.gitlab:
62 return GitlabRepoRef.from({
63 owner: push.repo.owner,
64 repo: push.repo.name,
65 sha: push.after.sha,
66 rawApiBase: push.repo.org.provider.apiUrl,
67 gitlabRemoteUrl: push.repo.org.provider.url,
68 branch: push.branch,
69 });
70 case ProviderType.bitbucket_cloud:
71 throw new Error("BitBucket Cloud not yet supported");
72 default:
73 throw new Error(`Provider ${providerType} not currently supported in SDM`);
74 }
75 }
76
77 public toBitBucketServerRepoRef(params: {
78 providerUrl: string,
79 owner: string,
80 name: string,
81 sha: string,
82 branch?: string,
83 }): BitBucketServerRepoRef {
84 const url = params.providerUrl;
85 const id = new BitBucketServerRepoRef(
86 url,
87 params.owner,
88 params.name,
89 true,
90 params.sha,
91 );
92 id.branch = params.branch;
93 // id.cloneUrl = (creds: BasicAuthCredentials) =>
94 // `http://${encodeURIComponent(creds.username)}:${encodeURIComponent(creds.password)}@${id.remoteBase}${id.pathComponent}.git`;
95 return id;
96 }
97
98 public providerIdFromPush(push: OnPushToAnyBranch.Push): string {
99 return push.repo.org.provider.providerId;
100 }
101
102 public repoRefFromSdmGoal(sdmGoal: SdmGoalEvent): RemoteRepoRef {
103 const provider = sdmGoal.push.repo.org.provider;
104 switch (provider.providerType) {
105 case ProviderType.github_com:
106 case ProviderType.ghe:
107 return GitHubRepoRef.from({
108 owner: sdmGoal.push.repo.owner,
109 repo: sdmGoal.push.repo.name,
110 sha: sdmGoal.sha,
111 branch: sdmGoal.branch,
112 rawApiBase: provider.apiUrl,
113 });
114 case ProviderType.bitbucket:
115 return this.toBitBucketServerRepoRef({
116 providerUrl: provider.url,
117 owner: sdmGoal.push.repo.owner,
118 name: sdmGoal.push.repo.name,
119 sha: sdmGoal.sha,
120 branch: sdmGoal.branch,
121 });
122 case ProviderType.gitlab:
123 return GitlabRepoRef.from({
124 owner: sdmGoal.push.repo.owner,
125 repo: sdmGoal.push.repo.name,
126 sha: sdmGoal.sha,
127 rawApiBase: provider.apiUrl,
128 gitlabRemoteUrl: provider.url,
129 branch: sdmGoal.branch,
130 });
131 default:
132 throw new Error(`Provider ${provider.providerType} not currently supported in SDM`);
133 }
134 }
135
136 /**
137 * Convert GraphQL return to our remote repo ref, instantiating
138 * the correct type based on provider
139 * @param {CoreRepoFieldsAndChannels.Fragment} repo
140 * @param opts options - sha or branch
141 * @return {RemoteRepoRef}
142 */
143 public toRemoteRepoRef(repo: CoreRepoFieldsAndChannels.Fragment, opts: { sha?: string, branch?: string } = {}): RemoteRepoRef {
144 const providerType = _.get(repo, "org.provider.providerType");
145 const apiUrl = _.get(repo, "org.provider.apiUrl");
146 const url = _.get(repo, "org.provider.url");
147
148 switch (providerType) {
149 case undefined:
150 // tslint:disable-next-line:no-null-keyword
151 case null:
152 case ProviderType.github_com:
153 case ProviderType.ghe:
154 return GitHubRepoRef.from({
155 owner: repo.owner,
156 repo: repo.name,
157 sha: opts.sha,
158 branch: opts.branch,
159 rawApiBase: apiUrl,
160 });
161 case ProviderType.bitbucket:
162 return this.toBitBucketServerRepoRef({
163 owner: repo.owner,
164 name: repo.name,
165 sha: opts.sha,
166 branch: opts.branch,
167 providerUrl: apiUrl,
168 });
169 case ProviderType.gitlab:
170 return GitlabRepoRef.from({
171 owner: repo.owner,
172 repo: repo.name,
173 sha: opts.sha,
174 rawApiBase: apiUrl,
175 gitlabRemoteUrl: url,
176 branch: opts.branch,
177 });
178 default:
179 throw new Error(`Provider ${repo.org.provider.providerType} not currently supported in SDM`);
180 }
181 }
182}