1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import {
|
18 | GitProject,
|
19 | HandlerContext,
|
20 | MutationNoCacheOptions,
|
21 | QueryNoCacheOptions,
|
22 | Success,
|
23 | } from "@atomist/automation-client";
|
24 | import {
|
25 | ExecuteGoal,
|
26 | ExecuteGoalResult,
|
27 | GoalInvocation,
|
28 | ProgressLog,
|
29 | SdmGoalEvent,
|
30 | } from "@atomist/sdm";
|
31 | import { codeLine } from "@atomist/slack-messages";
|
32 | import * as _ from "lodash";
|
33 | import {
|
34 | SdmVersion,
|
35 | } from "../../../../ingesters/sdmVersionIngester";
|
36 | import {
|
37 | SdmVersionForCommit,
|
38 | UpdateSdmVersionMutation,
|
39 | UpdateSdmVersionMutationVariables,
|
40 | } from "../../../../typings/types";
|
41 |
|
42 | export type ProjectVersioner =
|
43 | (status: SdmGoalEvent, p: GitProject, log: ProgressLog) => Promise<string>;
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | export 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 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | export 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 |
|
101 | export interface GetGoalVersionArguments {
|
102 |
|
103 | context: HandlerContext;
|
104 |
|
105 | owner: string;
|
106 |
|
107 | providerId: string;
|
108 |
|
109 | repo: string;
|
110 |
|
111 | sha: string;
|
112 |
|
113 | branch?: string;
|
114 | }
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | export 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 |
|
140 | export 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 | }
|