Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

bitbucketPullRequests.ts

115 lines3.9 KB

1import * as DateTime from "effect/DateTime";
2import * as Option from "effect/Option";
3import * as Schema from "effect/Schema";
4import { PositiveInt, TrimmedNonEmptyString } from "@t3tools/contracts";
5​
6export interface NormalizedBitbucketPullRequestRecord {
7 readonly number: number;
8 readonly title: string;
9 readonly url: string;
10 readonly baseRefName: string;
11 readonly headRefName: string;
12 readonly headSha?: string;
13 readonly state: "open" | "closed" | "merged";
14 readonly isDraft?: boolean;
15 readonly updatedAt: Option.Option<DateTime.Utc>;
16 readonly isCrossRepository?: boolean;
17 readonly headRepositoryNameWithOwner?: string | null;
18 readonly headRepositoryOwnerLogin?: string | null;
20​
21export const BitbucketRepositoryRefSchema = Schema.Struct({
22 full_name: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
23 workspace: Schema.optional(
24 Schema.NullOr(
25 Schema.Struct({
26 slug: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
27 }),
28 ),
29 ),
30});
31​
32export const BitbucketPullRequestBranchSchema = Schema.Struct({
33 repository: Schema.optional(Schema.NullOr(BitbucketRepositoryRefSchema)),
34 branch: Schema.Struct({
35 name: TrimmedNonEmptyString,
36 }),
37 commit: Schema.optional(
38 Schema.NullOr(Schema.Struct({ hash: Schema.optional(Schema.NullOr(Schema.String)) })),
39 ),
40});
41​
42export const BitbucketPullRequestSchema = Schema.Struct({
43 id: PositiveInt,
44 title: TrimmedNonEmptyString,
45 state: Schema.optional(Schema.NullOr(Schema.String)),
46 draft: Schema.optional(Schema.Boolean),
47 updated_on: Schema.optional(Schema.OptionFromNullOr(Schema.DateTimeUtcFromString)),
48 links: Schema.Struct({
49 html: Schema.Struct({
50 href: TrimmedNonEmptyString,
51 }),
52 }),
53 source: BitbucketPullRequestBranchSchema,
54 destination: BitbucketPullRequestBranchSchema,
55});
56​
57export const BitbucketPullRequestListSchema = Schema.Struct({
58 values: Schema.Array(BitbucketPullRequestSchema),
59 next: Schema.optional(TrimmedNonEmptyString),
60});
61​
62function trimOptionalString(value: string | null | undefined): string | null {
63 const trimmed = value?.trim() ?? "";
64 return trimmed.length > 0 ? trimmed : null;
66​
67function repositoryOwner(repository: Schema.Schema.Type<typeof BitbucketRepositoryRefSchema>) {
68 return (
69 trimOptionalString(repository.workspace?.slug) ??
70 (repository.full_name?.includes("/") ? (repository.full_name.split("/")[0] ?? null) : null)
71 );
73​
74function normalizeBitbucketPullRequestState(state: string | null | undefined) {
75 switch (state?.trim().toUpperCase()) {
76 case "MERGED":
77 return "merged" as const;
78 case "DECLINED":
79 case "SUPERSEDED":
80 return "closed" as const;
81 case "OPEN":
82 default:
83 return "open" as const;
84 }
86​
87export function normalizeBitbucketPullRequestRecord(
88 raw: Schema.Schema.Type<typeof BitbucketPullRequestSchema>,
89): NormalizedBitbucketPullRequestRecord {
90 const headRepositoryNameWithOwner = trimOptionalString(raw.source.repository?.full_name);
91 const baseRepositoryNameWithOwner = trimOptionalString(raw.destination.repository?.full_name);
92 const headSha = trimOptionalString(raw.source.commit?.hash);
93 const headRepositoryOwnerLogin = raw.source.repository
94 ? repositoryOwner(raw.source.repository)
95 : null;
96 const isCrossRepository =
97 headRepositoryNameWithOwner !== null &&
98 baseRepositoryNameWithOwner !== null &&
99 headRepositoryNameWithOwner !== baseRepositoryNameWithOwner;
100​
101 return {
102 number: raw.id,
103 title: raw.title,
104 url: raw.links.html.href,
105 baseRefName: raw.destination.branch.name,
106 headRefName: raw.source.branch.name,
107 ...(headSha ? { headSha } : {}),
108 state: normalizeBitbucketPullRequestState(raw.state),
109 ...(raw.draft === true ? { isDraft: true } : {}),
110 updatedAt: raw.updated_on ?? Option.none(),
111 ...(isCrossRepository ? { isCrossRepository: true } : {}),
112 ...(headRepositoryNameWithOwner ? { headRepositoryNameWithOwner } : {}),
113 ...(headRepositoryOwnerLogin ? { headRepositoryOwnerLogin } : {}),
114 };