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

bitbucketPullRequests.ts

104 lines3.5 KB

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