1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import {
|
18 | HandlerContext,
|
19 | RemoteRepoRef,
|
20 | } from "@atomist/automation-client";
|
21 | import {
|
22 | PushFields,
|
23 | } from "@atomist/sdm";
|
24 | import * as stringify from "json-stringify-safe";
|
25 | import {
|
26 | PushForCommit,
|
27 | RepoBranchTips,
|
28 | } from "../../typings/types";
|
29 |
|
30 | export 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 |
|
47 | export 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 |
|
61 | export 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 | }
|