1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import {
|
18 | HandlerContext,
|
19 | MutationNoCacheOptions,
|
20 | QueryNoCacheOptions,
|
21 | } from "@atomist/automation-client";
|
22 | import { PreferenceStoreFactory } from "@atomist/sdm";
|
23 | import {
|
24 | DeleteTeamConfiguration,
|
25 | SetTeamConfiguration,
|
26 | TeamConfigurationByNamespace,
|
27 | } from "../../typings/types";
|
28 | import {
|
29 | AbstractPreferenceStore,
|
30 | Preference,
|
31 | } from "./AbstractPreferenceStore";
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | export const TeamConfigurationPreferenceStoreFactory: PreferenceStoreFactory =
|
37 | ctx => new TeamConfigurationPreferenceStore(ctx);
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | export class TeamConfigurationPreferenceStore extends AbstractPreferenceStore {
|
43 |
|
44 | constructor(private readonly context: HandlerContext) {
|
45 | super(context);
|
46 | }
|
47 |
|
48 | protected async doGet(name: string, namespace: string): Promise<Preference | undefined> {
|
49 | const result = await this.context.graphClient.query<TeamConfigurationByNamespace.Query, TeamConfigurationByNamespace.Variables>({
|
50 | name: "TeamConfigurationByNamespace",
|
51 | variables: {
|
52 | namespace: normalizeNamespace(namespace),
|
53 | },
|
54 | options: QueryNoCacheOptions,
|
55 | });
|
56 | const teamConfiguration = (result.TeamConfiguration || []).find(t => t.name === name);
|
57 | if (!!teamConfiguration) {
|
58 | return {
|
59 | name,
|
60 | namespace,
|
61 | value: teamConfiguration.value,
|
62 | ttl: undefined,
|
63 | };
|
64 | }
|
65 | return undefined;
|
66 | }
|
67 |
|
68 | protected async doPut(pref: Preference): Promise<void> {
|
69 | await this.context.graphClient.mutate<SetTeamConfiguration.Mutation, SetTeamConfiguration.Variables>({
|
70 | name: "SetTeamConfiguration",
|
71 | variables: {
|
72 | name: pref.name,
|
73 | namespace: normalizeNamespace(pref.namespace),
|
74 | value: pref.value,
|
75 | ttl: typeof pref.ttl === "number" ? Math.floor(pref.ttl / 1000) : undefined,
|
76 | },
|
77 | options: MutationNoCacheOptions,
|
78 | });
|
79 | }
|
80 |
|
81 | protected async doList(namespace: string): Promise<Preference[]> {
|
82 | const result = await this.context.graphClient.query<TeamConfigurationByNamespace.Query, TeamConfigurationByNamespace.Variables>({
|
83 | name: "TeamConfigurationByNamespace",
|
84 | variables: {
|
85 | namespace: normalizeNamespace(namespace),
|
86 | },
|
87 | options: QueryNoCacheOptions,
|
88 | });
|
89 | if (!!result.TeamConfiguration) {
|
90 | return result.TeamConfiguration.map(t => ({
|
91 | name: t.name,
|
92 | namespace: t.namespace,
|
93 | value: t.value,
|
94 | ttl: undefined,
|
95 | }));
|
96 | }
|
97 | return [];
|
98 | }
|
99 |
|
100 | protected async doDelete(name: string, namespace: string): Promise<void> {
|
101 | await this.context.graphClient.mutate<DeleteTeamConfiguration.Mutation, DeleteTeamConfiguration.Variables>({
|
102 | name: "DeleteTeamConfiguration",
|
103 | variables: {
|
104 | name,
|
105 | namespace: normalizeNamespace(namespace),
|
106 | },
|
107 | options: MutationNoCacheOptions,
|
108 | });
|
109 | }
|
110 | }
|
111 |
|
112 | function normalizeNamespace(namespace: string): string {
|
113 | if (!namespace || namespace.length === 0) {
|
114 | return "@atomist.global";
|
115 | }
|
116 |
|
117 | return namespace.replace(/\//g, ".");
|
118 | }
|