UNPKG

5.13 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 GitProject,
19 HandlerContext,
20 MutationNoCacheOptions,
21 QueryNoCacheOptions,
22 Success,
23} from "@atomist/automation-client";
24import {
25 ExecuteGoal,
26 ExecuteGoalResult,
27 GoalInvocation,
28 ProgressLog,
29 SdmGoalEvent,
30} from "@atomist/sdm";
31import { codeLine } from "@atomist/slack-messages";
32import * as _ from "lodash";
33import {
34 SdmVersion,
35} from "../../../../ingesters/sdmVersionIngester";
36import {
37 SdmVersionForCommit,
38 UpdateSdmVersionMutation,
39 UpdateSdmVersionMutationVariables,
40} from "../../../../typings/types";
41
42export type ProjectVersioner =
43 (status: SdmGoalEvent, p: GitProject, log: ProgressLog) => Promise<string>;
44
45/**
46 * Version the project with a build specific version number
47 * @param projectLoader used to load projects
48 * @param projectVersioner decides on the version string
49 */
50export function executeVersioner(projectVersioner: ProjectVersioner): ExecuteGoal {
51 return async (goalInvocation: GoalInvocation): Promise<ExecuteGoalResult> => {
52 const { configuration, goalEvent, credentials, id, context, progressLog } = goalInvocation;
53
54 return configuration.sdm.projectLoader.doWithProject({ credentials, id, context, readOnly: false }, async p => {
55 const version = await projectVersioner(goalEvent, p, progressLog);
56 const sdmVersion: SdmVersion = {
57 sha: goalEvent.sha,
58 branch: id.branch,
59 version,
60 repo: {
61 owner: goalEvent.repo.owner,
62 name: goalEvent.repo.name,
63 providerId: goalEvent.repo.providerId,
64 },
65 };
66 await context.graphClient.mutate<UpdateSdmVersionMutation, UpdateSdmVersionMutationVariables>({
67 name: "UpdateSdmVersion",
68 variables: {
69 version: sdmVersion,
70 },
71 options: MutationNoCacheOptions,
72 });
73 return {
74 ...Success,
75 description: `Versioned ${codeLine(version)}`,
76 };
77 });
78 };
79}
80
81/**
82 * Get prerelease, i.e., timestamped, version associated with the goal
83 * set for the provided goal invocation. The Version goal must be
84 * executed within the goal set prior to calling this function.
85 *
86 * @param gi Goal invocation
87 * @return Prerelease semantic version string
88 */
89export async function goalInvocationVersion(gi: GoalInvocation): Promise<string | undefined> {
90 return getGoalVersion({
91 branch: gi.id.branch,
92 context: gi.context,
93 owner: gi.goalEvent.repo.owner,
94 providerId: gi.goalEvent.repo.providerId,
95 repo: gi.goalEvent.repo.name,
96 sha: gi.goalEvent.sha,
97 });
98}
99
100/** Object wrapping [[getGoalVersion]] function arguments. */
101export interface GetGoalVersionArguments {
102 /** Context providing a graph client. */
103 context: HandlerContext;
104 /** Repository owner, i.e., user or organization. */
105 owner: string;
106 /** Git repository provider identifier. */
107 providerId: string;
108 /** Repository name. */
109 repo: string;
110 /** Commit SHA. */
111 sha: string;
112 /** Branch, "master" if not provided */
113 branch?: string;
114}
115
116/**
117 * Read and return prerelease version for the goal set associated with
118 * the provided commit.
119 *
120 * @param args Properties determining which version to retrieve
121 * @return Prerelease semantic version string
122 */
123export async function getGoalVersion(args: GetGoalVersionArguments): Promise<string | undefined> {
124 const branch = args.branch || "master";
125 const version = await args.context.graphClient.query<SdmVersionForCommit.Query, SdmVersionForCommit.Variables>({
126 name: "SdmVersionForCommit",
127 variables: {
128 name: [args.repo],
129 owner: [args.owner],
130 providerId: [args.providerId],
131 sha: [args.sha],
132 branch: [branch],
133 },
134 options: QueryNoCacheOptions,
135 });
136 return _.get(version, "SdmVersion[0].version");
137}
138
139/** See getGoalVersion. */
140export async function readSdmVersion(owner: string,
141 repo: string,
142 providerId: string,
143 sha: string,
144 branch: string,
145 context: HandlerContext): Promise<string | undefined> {
146 return getGoalVersion({ branch, context, owner, providerId, repo, sha });
147}