1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import {
|
18 | configurationValue,
|
19 | HandlerContext,
|
20 | Parameters,
|
21 | ProjectOperationCredentials,
|
22 | RemoteRepoRef,
|
23 | } from "@atomist/automation-client";
|
24 | import { CredentialsResolver } from "@atomist/sdm";
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | @Parameters()
|
30 | export class ConfigurationBasedBasicCredentialsResolver implements CredentialsResolver {
|
31 |
|
32 | constructor(private readonly paths: { username: string, password: string } = {
|
33 | username: "sdm.git.user",
|
34 | password: "sdm.git.password",
|
35 | }) { }
|
36 |
|
37 | public eventHandlerCredentials(context: HandlerContext): ProjectOperationCredentials {
|
38 | return this.credentialsFromConfiguration();
|
39 | }
|
40 |
|
41 | public commandHandlerCredentials(context: HandlerContext, id: RemoteRepoRef): ProjectOperationCredentials {
|
42 | return this.credentialsFromConfiguration();
|
43 | }
|
44 |
|
45 | private credentialsFromConfiguration(): ProjectOperationCredentials & { username: string, password: string} {
|
46 | const creds = {
|
47 | username: configurationValue<string>(this.paths.username),
|
48 | password: configurationValue<string>(this.paths.password),
|
49 | };
|
50 | if (!creds.username) {
|
51 | throw new Error(`Git username missing at '${this.paths.username}'`);
|
52 | }
|
53 | if (!creds.password) {
|
54 | throw new Error(`Git password missing at '${this.paths.password}'`);
|
55 | }
|
56 | return creds;
|
57 | }
|
58 | }
|