Skip to content

Commit e965148

Browse files
committed
Avoid non-null assertion operators by asserting arguments
1 parent 9359b6b commit e965148

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

‎pr-checks/post-comment.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,27 @@ describe("parseOptions", async () => {
5454
});
5555
});
5656

57+
await it("rejects a missing --body", () => {
58+
assert.throws(
59+
() => parseOptions(withoutFlag("--body")),
60+
/Missing required flag --body/,
61+
);
62+
});
63+
64+
await it("rejects an empty --body", () => {
65+
assert.throws(
66+
() => parseOptions(withFlag("--body", "")),
67+
/The value for --body cannot be empty/,
68+
);
69+
});
70+
71+
await it("rejects a whitespace-only --body", () => {
72+
assert.throws(
73+
() => parseOptions(withFlag("--body", " \t\n")),
74+
/The value for --body cannot be empty/,
75+
);
76+
});
77+
5778
await it("rejects a missing --marker", () => {
5879
assert.throws(
5980
() => parseOptions(withoutFlag("--marker")),
@@ -74,6 +95,34 @@ describe("parseOptions", async () => {
7495
/The value for --marker cannot be empty/,
7596
);
7697
});
98+
99+
await it("rejects a missing --repository", () => {
100+
assert.throws(
101+
() => parseOptions(withoutFlag("--repository")),
102+
/Missing required flag --repository/,
103+
);
104+
});
105+
106+
await it("rejects an empty --repository", () => {
107+
assert.throws(
108+
() => parseOptions(withFlag("--repository", "")),
109+
/The value for --repository cannot be empty/,
110+
);
111+
});
112+
113+
await it("rejects a whitespace-only --repository", () => {
114+
assert.throws(
115+
() => parseOptions(withFlag("--repository", " \t\n")),
116+
/The value for --repository cannot be empty/,
117+
);
118+
});
119+
120+
await it("rejects an invalid --repository value", () => {
121+
assert.throws(
122+
() => parseOptions(withFlag("--repository", "github/codeql/action")),
123+
/Invalid --repository value/,
124+
);
125+
});
77126
});
78127

79128
describe("resolveToken", async () => {

‎pr-checks/post-comment.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,29 @@ export function parseOptions(argv?: string[]): Options {
5252
strict: true,
5353
});
5454

55+
assert(values.body !== undefined, "Missing required flag --body.");
56+
assert(values.body.trim() !== "", "The value for --body cannot be empty.");
5557
assert(values.marker !== undefined, "Missing required flag --marker.");
5658
assert(
5759
values.marker.trim() !== "",
5860
"The value for --marker cannot be empty.",
5961
);
60-
const [owner, repo] = values.repository!.split("/");
62+
assert(
63+
values.repository !== undefined,
64+
"Missing required flag --repository.",
65+
);
66+
assert(
67+
values.repository.trim() !== "",
68+
"The value for --repository cannot be empty.",
69+
);
70+
assert(
71+
/^[\w.-]+\/[\w.-]+$/.test(values.repository),
72+
"Invalid --repository value.",
73+
);
6174

75+
const [owner, repo] = values.repository.split("/");
6276
return {
63-
body: values.body!,
77+
body: values.body,
6478
marker: values.marker,
6579
issueId: Number(values["issue-id"]),
6680
repository: { owner, repo },

0 commit comments

Comments
 (0)