UNPKG

5.22 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 GitHubRepoRef,
19 logger,
20 RemoteRepoRef,
21} from "@atomist/automation-client";
22import {
23 Attachment,
24 url,
25} from "@atomist/slack-messages";
26import { listCommitsBetween } from "../github/ghub";
27import {
28 avatarUrl,
29 commitUrl,
30 RepoInfo,
31 truncateCommitMessage,
32 userUrl,
33} from "../lifecycleHelpers";
34
35/* tslint:disable:no-unused-variable */
36
37export function linkToDiff(id: RemoteRepoRef, start: string, end: string, endDescription?: string): string {
38 return url(diffUrl(id, start, end), `(Compare with ${endDescription || end.substr(0, 6)})`);
39}
40
41function diffUrl(id: RemoteRepoRef, start: string, end: string): string {
42 return `${id.url}/compare/${start}...${end}`;
43}
44
45export async function renderDiff(token: string, id: GitHubRepoRef, start: string, end: string, color: string): Promise<Attachment[]> {
46 const fromGitHub = await listCommitsBetween(token, id, start, end);
47
48 const commits: CommitForRendering[] = fromGitHub.commits.map(c => ({
49 message: c.commit.message,
50 sha: c.sha,
51 author: c.author,
52 }));
53
54 logger.info("Rendering %d commits in diff", commits.length);
55 return render({ owner: id.owner, name: id.repo }, commits, diffUrl(id, start, end), color);
56}
57
58// exported for testing
59export interface CommitForRendering {
60 sha: string;
61 message: string;
62 author: {
63 login: string,
64 };
65}
66
67function render(repo: RepoInfo, commits: CommitForRendering[], fullDiffLink: string, color: string): Promise<Attachment[]> {
68
69 const commitsGroupedByAuthor = [];
70
71 let author;
72 let commitsByAuthor: any = {};
73 // let unknownCommitter = false;
74 for (const commit of commits) {
75 const ca = (commit.author !== undefined && commit.author.login && commit.author.login !== ""
76 ? commit.author.login : "(unknown)");
77
78 // if (ca === "(unknown)") {
79 // unknownCommitter = true;
80 // }
81
82 if (author === undefined || author !== ca) {
83 commitsByAuthor = {
84 author: ca,
85 commits: [],
86 };
87 author = ca;
88 commitsGroupedByAuthor.push(commitsByAuthor);
89 }
90 if (ca === author) {
91 commitsByAuthor.commits.push(commit);
92 }
93 }
94
95 let attachments: Attachment[] = [];
96
97 commitsGroupedByAuthor
98 .forEach(cgba => {
99 const a = cgba.author;
100
101 const message = cgba.commits.map(c => renderCommitMessage(repo, c)).join("\n");
102
103 const fallback = `lots of commits`;
104
105 const attachment: Attachment = {
106 author_name: `@${a}`,
107 author_link: userUrl(repo, a),
108 author_icon: avatarUrl(repo, a),
109 text: message,
110 mrkdwn_in: ["text"],
111 color,
112 fallback,
113 actions: [],
114 };
115 attachments.push(attachment);
116 });
117
118 // Limit number of commits by author to 3
119 if (attachments.length > 3) {
120 attachments = attachments.slice(0, 3);
121 const fullDiffDescription = `... and more! (${commits.length} total commits)`;
122
123 const attachment: Attachment = {
124 title_link: fullDiffLink,
125 title: fullDiffDescription,
126 color,
127 fallback: fullDiffDescription,
128 actions: [],
129 };
130 attachments.push(attachment);
131 }
132
133 // if (attachments.length > 0) {
134 // const lastAttachment = attachments[attachments.length - 1];
135 // if (unknownCommitter) {
136 // lastAttachment.footer_icon = "https://images.atomist.com/rug/question.png";
137 // lastAttachment.footer = `Unrecognized author. Please use a known email address to commit.`;
138 // } else {
139 // lastAttachment.footer_icon = "https://images.atomist.com/rug/commit.png";
140 // if (lastAttachment.footer != null) {
141 // lastAttachment.footer = `${url(repoUrl(repo), repoSlug(repo))} - ${lastAttachment.footer}`;
142 // } else {
143 // lastAttachment.footer = url(repoUrl(repo), repoSlug(repo));
144 // }
145 // lastAttachment.ts = Math.floor(Date.parse(push.timestamp) / 1000);
146 // }
147
148 return Promise.resolve(attachments);
149}
150
151// exported for testing
152export function renderCommitMessage(repo: RepoInfo, commitNode: CommitForRendering): string {
153 // Cut commit to 50 chars of first line
154 const m = truncateCommitMessage(commitNode.message, repo);
155 return "`" + url(commitUrl(repo, commitNode), commitNode.sha.substring(0, 7)) + "` " + m;
156}