UNPKG

4.19 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 HandlerContext,
19 MutationNoCacheOptions,
20 QueryNoCacheOptions,
21} from "@atomist/automation-client";
22import { PreferenceStoreFactory } from "@atomist/sdm";
23import {
24 DeleteTeamConfiguration,
25 SetTeamConfiguration,
26 TeamConfigurationByNamespace,
27} from "../../typings/types";
28import {
29 AbstractPreferenceStore,
30 Preference,
31} from "./AbstractPreferenceStore";
32
33/**
34 * Factory to create a new TeamConfigurationPreferenceStore instance
35 */
36export const TeamConfigurationPreferenceStoreFactory: PreferenceStoreFactory =
37 ctx => new TeamConfigurationPreferenceStore(ctx);
38
39/**
40 * PreferenceStore implementation that stores preferences in the backend GraphQL store.
41 */
42export 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, // ttl is handled in the backend store
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
112function normalizeNamespace(namespace: string): string {
113 if (!namespace || namespace.length === 0) {
114 return "@atomist.global";
115 }
116 // Backend doesn't allow / inside a namespace
117 return namespace.replace(/\//g, ".");
118}