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