1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import {
|
18 | BitBucketServerRepoRef,
|
19 | GitHubRepoRef,
|
20 | GitlabRepoRef,
|
21 | RemoteRepoRef,
|
22 | } from "@atomist/automation-client";
|
23 | import {
|
24 | CoreRepoFieldsAndChannels,
|
25 | OnPushToAnyBranch,
|
26 | ProviderType,
|
27 | RepoRefResolver,
|
28 | SdmGoalEvent,
|
29 | } from "@atomist/sdm";
|
30 | import * as _ from "lodash";
|
31 |
|
32 | export class DefaultRepoRefResolver implements RepoRefResolver {
|
33 |
|
34 | |
35 |
|
36 |
|
37 |
|
38 |
|
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 |
|
94 |
|
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 |
|
138 |
|
139 |
|
140 |
|
141 |
|
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 |
|
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 | }
|