/
list.ts
116 lines (95 loc) · 2.94 KB
/
list.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { Flags, ux } from "@oclif/core";
import { AxiosResponse } from "axios";
import * as ApiV1 from "@/lib/api-v1";
import BaseCommand from "@/lib/base-command";
import { formatDate } from "@/lib/helpers/date";
import { merge } from "@/lib/helpers/object.isomorphic";
import {
maybePromptPageAction,
pageFlags,
paramsForPageAction,
} from "@/lib/helpers/page";
import { withSpinner } from "@/lib/helpers/request";
import { formatCommitAuthor } from "@/lib/marshal/commit";
export default class CommitList extends BaseCommand<typeof CommitList> {
static summary = "Display all commits in an environment";
static flags = {
environment: Flags.string({
default: "development",
summary: "The environment to use.",
}),
promoted: Flags.boolean({
summary:
"Show only promoted or unpromoted changes between the given environment and the subsequent environment.",
allowNo: true,
}),
...pageFlags,
};
static enableJsonFlag = true;
async run(): Promise<ApiV1.ListCommitResp | void> {
const resp = await this.request();
const { flags } = this.props;
if (flags.json) return resp.data;
this.render(resp.data);
}
async request(pageParams = {}): Promise<AxiosResponse<ApiV1.ListCommitResp>> {
const props = merge(this.props, { flags: { ...pageParams } });
return withSpinner<ApiV1.ListCommitResp>(() =>
this.apiV1.listCommits(props),
);
}
async render(data: ApiV1.ListCommitResp): Promise<void> {
const { entries } = data;
const { environment: env, promoted } = this.props.flags;
let qualifier = "";
if (promoted === true) {
qualifier = "(showing only promoted)";
}
if (promoted === false) {
qualifier = "(showing only unpromoted)";
}
this.log(
`‣ Showing ${entries.length} commits in \`${env}\` environment ${qualifier}\n`,
);
/*
* Commits table
*/
ux.table(entries, {
id: {
header: "ID",
},
resource: {
header: "Resource",
get: (entry) => entry.resource.type,
},
identifier: {
header: "Identifier",
get: (entry) => entry.resource.identifier,
},
author: {
header: "Author",
get: (entry) => formatCommitAuthor(entry),
},
commit_message: {
header: "Commit message",
get: (entry) =>
entry.commit_message ? entry.commit_message.trim() : "",
},
created_at: {
header: "Created at",
get: (entry) => formatDate(entry.created_at),
},
});
return this.prompt(data);
}
async prompt(data: ApiV1.ListCommitResp): Promise<void> {
const { page_info } = data;
const pageAction = await maybePromptPageAction(page_info);
const pageParams = pageAction && paramsForPageAction(pageAction, page_info);
if (pageParams) {
this.log("\n");
const resp = await this.request(pageParams);
return this.render(resp.data);
}
}
}