1
import { Config, Effect, Option, Path, Schema, Stdio, Stream } from 'effect';
2
import { Base64 } from 'effect/encoding';
3
-import { readReleasePull, verifyReleaseGate } from './approval';
3
+import { readReleasePull, trustedIssue, verifyReleaseGate } from './approval';
4
import {
5
CONTENT_CONCURRENCY,
6
CreatedSha,
77
readonly runId: string;
78
readonly sourceSha: string;
79
readonly issueNumber?: number;
80
+ /** The release App, whose commits alone may name a request issue. */
81
+ readonly appSlug: string;
82
}
83
84
/** A request either ends the request stage or plans a batch for the release PR jobs. */
85
export type RequestOutcome =
86
{ readonly _tag: 'Planned'; readonly batch: ReleaseBatch } | ReleaseState;
87
88
+/** The outcome, and the request issue the status job shows it on, when there is one. */
89
+export interface RequestResult {
90
+ readonly outcome: RequestOutcome;
91
+ readonly issue: Option.Option<number>;
92
+}
93
+
94
/** Opens the tracking issue for a dispatch-started release; issue-form requests already have one. */
95
const createRequestIssue = (run: string) =>
96
Effect.map(
116
return yield* parse.pipe(
117
Effect.matchEffect({
118
onFailure: reason =>
111
- Effect.succeed<RequestOutcome>(ReleaseState.make({ _tag: 'NeedsInput', reason })),
119
+ Effect.succeed<RequestResult>({
120
+ outcome: ReleaseState.make({ _tag: 'NeedsInput', reason }),
121
+ issue: Option.fromNullishOr(context.issueNumber),
122
+ }),
123
onSuccess: releaseRequest => decideRequest(releaseRequest, context, plan),
124
})
125
);
131
plan: (request: ReleaseRequest) => Effect.Effect<SdkReleasePlan, E, R>
132
) =>
133
Effect.gen(function* () {
134
+ const requestIssue = Option.fromNullishOr(context.issueNumber);
135
+ const decided = (outcome: ReleaseState, issue = requestIssue): RequestResult => ({
136
+ outcome,
137
+ issue,
138
+ });
139
const current = yield* activeRelease;
140
+ if (Option.isSome(current) && current.value.head.ref === releaseBranch(context.runId)) {
141
+ // A re-run of a dispatch has no issue in its event; its release PR's plan names it.
142
+ const issue = Option.isSome(requestIssue)
143
+ ? requestIssue
144
+ : yield* Effect.flatMap(readReleasePull(current.value.number), ({ pull, batch }) =>
145
+ trustedIssue(pull, batch, context.appSlug)
146
+ );
147
+ return decided(ReleaseState.make({ _tag: 'InReview', pull: current.value.html_url }), issue);
148
+ }
149
if (Option.isSome(current))
125
- return ReleaseState.make({
126
- _tag: current.value.head.ref === releaseBranch(context.runId) ? 'InReview' : 'Waiting',
127
- pull: current.value.html_url,
128
- });
150
+ return decided(ReleaseState.make({ _tag: 'Waiting', pull: current.value.html_url }));
151
const untagged = yield* untaggedRelease(context.sourceSha);
152
if (Option.isSome(untagged))
131
- return ReleaseState.make({ _tag: 'Blocked', release: untagged.value });
153
+ return decided(ReleaseState.make({ _tag: 'Blocked', release: untagged.value }));
154
const selected = yield* plan(releaseRequest);
133
- if (!selected.releases.length) return ReleaseState.make({ _tag: 'Empty' });
155
+ if (!selected.releases.length) return decided(ReleaseState.make({ _tag: 'Empty' }));
156
const issue = context.issueNumber ?? (yield* createRequestIssue(context.runId));
157
const batch = yield* Schema.decodeUnknownEffect(ReleaseBatch)({
158
id: context.runId,
167
new TextEncoder().encode(JSON.stringify(batch)).length < 120_000,
168
'Release batch is too large to pass between jobs'
169
);
148
- return { _tag: 'Planned', batch } as RequestOutcome;
170
+ return { outcome: { _tag: 'Planned', batch }, issue: Option.some(issue) } as RequestResult;
171
});
172
173
// 2. release PR --------------------------------------------------------------------------------
240
_tag: current.value.head.ref === releaseBranch(batch.id) ? 'InReview' : 'Waiting',
241
pull: current.value.html_url,
242
});
243
+ // Another release may have merged and failed its gate since this one was planned.
244
+ const next = yield* request('GET', `${REPOSITORY_PATH}/git/ref/heads/next`, TagRef);
245
+ const untagged = yield* untaggedRelease(next.object.sha);
246
+ if (Option.isSome(untagged))
247
+ return ReleaseState.make({ _tag: 'Blocked', release: untagged.value });
248
const frozen = yield* Effect.forEach(
249
patch.files,
250
file => fileAtOrNull(file.path, batch.inputBase),
352
envelope: typeof EventEnvelope.Type
353
) => Effect.Effect<void, unknown, GitHub | ReleaseServices>;
354
355
+/** Writes the request issue, when known, for the stage's status job. */
356
+const outputIssue = (env: Environment, issue: Option.Option<number>) =>
357
+ Option.match(issue, {
358
+ onNone: () => Effect.void,
359
+ onSome: number => appendFile(env.outputPath, `issue=${number}\n`),
360
+ });
361
+
362
/** Writes a stage outcome as a job output for the stage's status job. */
363
const outputState = (env: Environment, state: ReleaseState) =>
364
appendFile(env.outputPath, `state=${JSON.stringify(state)}\n`);
369
const cwd = (yield* Path.Path).resolve('.');
370
// Known before planning, so a failure is still reported on the request issue.
371
if (envelope.issue) yield* appendFile(env.outputPath, `issue=${envelope.issue.number}\n`);
338
- const outcome = yield* executeReleaseRequest(
372
+ const appSlug = yield* Config.String('SDK_RELEASE_APP_SLUG');
373
+ const { outcome, issue } = yield* executeReleaseRequest(
374
Effect.try({
375
try: () => parseReleaseRequest(env.eventName, event),
376
catch: error => (error instanceof Error ? error.message : 'Unreadable request'),
379
actor: envelope.sender.login,
380
runId: env.runId,
381
sourceSha: env.sha,
382
+ appSlug,
383
...(envelope.issue ? { issueNumber: envelope.issue.number } : {}),
384
},
385
choices => createSdkReleasePlan(cwd, choices)
386
);
387
yield* outcome._tag === 'Planned'
352
- ? appendFile(
353
- env.outputPath,
354
- `batch=${JSON.stringify(outcome.batch)}\nissue=${outcome.batch.issue}\n`
355
- )
388
+ ? appendFile(env.outputPath, `batch=${JSON.stringify(outcome.batch)}\n`)
389
: outputState(env, outcome);
390
+ yield* outputIssue(env, issue);
391
}),
392
'release-pr': (env: Environment) =>
393
Effect.gen(function* () {
405
if (!envelope.pull_request) return yield* fail('Expected a closed release PR event');
406
const appSlug = yield* Config.String('SDK_RELEASE_APP_SLUG');
407
const { pull, batch } = yield* readReleasePull(envelope.pull_request.number);
374
- yield* appendFile(env.outputPath, `issue=${batch.issue}\n`);
408
+ const issue = yield* trustedIssue(pull, batch, appSlug);
409
+ yield* outputIssue(env, issue);
410
yield* outputState(env, yield* authorizeSourceTag(pull, batch, appSlug));
411
}),
412
};