UNPKG

2.73 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 RemoteRepoRef,
20} from "@atomist/automation-client";
21import {
22 PushFields,
23} from "@atomist/sdm";
24import * as stringify from "json-stringify-safe";
25import {
26 PushForCommit,
27 RepoBranchTips,
28} from "../../typings/types";
29
30export async function fetchPushForCommit(context: HandlerContext, id: RemoteRepoRef, providerId: string): Promise<PushFields.Fragment> {
31 const commitResult = await context.graphClient.query<PushForCommit.Query, PushForCommit.Variables>({
32 name: "PushForCommit", variables: {
33 owner: id.owner, repo: id.repo, providerId, branch: id.branch, sha: id.sha,
34 },
35 });
36
37 if (!commitResult || !commitResult.Commit || commitResult.Commit.length === 0) {
38 throw new Error("Could not find commit for " + stringify(id));
39 }
40 const commit = commitResult.Commit[0];
41 if (!commit.pushes || commit.pushes.length === 0) {
42 throw new Error("Could not find push for " + stringify(id));
43 }
44 return commit.pushes[0];
45}
46
47export async function fetchBranchTips(ctx: HandlerContext,
48 repositoryId: { repo: string, owner: string, providerId: string }): Promise<RepoBranchTips.Repo> {
49 const result = await ctx.graphClient.query<RepoBranchTips.Query, RepoBranchTips.Variables>(
50 { name: "RepoBranchTips", variables: { name: repositoryId.repo, owner: repositoryId.owner } });
51 if (!result || !result.Repo || result.Repo.length === 0) {
52 throw new Error(`Repository not found: ${repositoryId.owner}/${repositoryId.repo}`);
53 }
54 const repo = result.Repo.find(r => r.org.provider.providerId === repositoryId.providerId);
55 if (!repo) {
56 throw new Error(`Repository not found: ${repositoryId.owner}/${repositoryId.repo} provider ${repositoryId.providerId}`);
57 }
58 return repo;
59}
60
61export function tipOfBranch(repo: RepoBranchTips.Repo, branchName: string): string {
62 const branchData = repo.branches.find(b => b.name === branchName);
63 if (!branchData) {
64 throw new Error("Branch not found: " + branchName);
65 }
66 return branchData.commit.sha;
67}