Repository navigation
Expand file tree
/
Copy pathplay-internal-track.mjs
More file actions
281 lines (259 loc) · 11.9 KB
/
Copy pathplay-internal-track.mjs
File metadata and controls
281 lines (259 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { appendFile, readFile, writeFile } from "node:fs/promises";
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
// Google Play Android Publisher v3 로 internal track 에만 게시한다 (#852).
//
// production·open·closed track 은 이 파일 어디에도 없다. 승격은 사람이 Play Console 에서만
// 한다는 게 이슈의 비범위이자 완료 조건이라, 자동 승격 경로를 «지금은 안 쓴다» 로 두지 않고
// 아예 만들지 않는다. release-play-internal-policy.test.mjs 가 그 부재를 고정한다.
export const INTERNAL_TRACK = "internal";
export const RELEASE_NOTES_LANGUAGE = "ko-KR";
// Play 가 language 당 허용하는 release note 길이. 넘기면 track update 가 400 으로 떨어진다.
export const MAX_RELEASE_NOTES_LENGTH = 500;
const API_ROOT = "https://androidpublisher.googleapis.com";
function firstLine(value) {
return String(value ?? "").split("\n", 1)[0].slice(0, 500);
}
export function toVersionCode(rawValue) {
const value = typeof rawValue === "number" ? String(rawValue) : String(rawValue ?? "").trim();
if (!/^[1-9][0-9]*$/.test(value)) {
throw new Error(`versionCode 가 10진 양의 정수가 아니다: ${firstLine(rawValue)}`);
}
return Number(value);
}
/**
* 릴리스 PR 본문에서 이슈 번호만 남긴다. 순서는 본문 등장 순서, 중복은 첫 등장만 남긴다.
*/
export function parseIssueReferences(body) {
const seen = new Set();
for (const match of String(body ?? "").matchAll(/#([1-9][0-9]*)/g)) {
seen.add(`#${match[1]}`);
}
return [...seen];
}
/**
* Play Console 의 «What's new» 로 그대로 보이는 문구. source SHA 를 항상 먼저 적어 두어
* Console 에서 본 릴리스가 어느 commit 인지 사후에 되짚을 수 있게 한다.
*/
export function renderInternalReleaseNotes({ sourceRef, sourceSha, versionCode, issues = [] }) {
const ref = String(sourceRef ?? "").replace(/^refs\/heads\//, "") || "unknown";
const sha = String(sourceSha ?? "unknown");
const header = `Afternote 내부 테스트 ${versionCode}\n기준: ${ref} @ ${sha.slice(0, 7)}`;
const body = issues.length > 0 ? `\n포함 이슈: ${issues.join(", ")}` : "";
const notes = `${header}${body}`;
if (notes.length <= MAX_RELEASE_NOTES_LENGTH) {
return notes;
}
// 잘렸다는 사실이 Console 에서 보여야 한다 — 이슈 목록이 조용히 사라지면 릴리스 기록과
// Console 표시가 어긋난 걸 아무도 눈치채지 못한다.
return `${notes.slice(0, MAX_RELEASE_NOTES_LENGTH - 1)}…`;
}
/**
* Play 가 이미 알고 있는 가장 큰 versionCode. 업로드된 bundle 과 모든 track 의 release 를 함께
* 본다 — bundle 목록만 보면 사람이 Console 에서 직접 올린 산출물을 놓친다.
*/
export function maxVersionCode(bundlesResponse, tracksResponse) {
const codes = [];
for (const bundle of bundlesResponse?.bundles ?? []) {
codes.push(toVersionCode(bundle.versionCode));
}
for (const track of tracksResponse?.tracks ?? []) {
for (const release of track.releases ?? []) {
for (const code of release.versionCodes ?? []) {
codes.push(toVersionCode(code));
}
}
}
return codes.length === 0 ? 0 : Math.max(...codes);
}
export function createPlayClient({ accessToken, packageName, fetchImpl = globalThis.fetch }) {
if (!accessToken) {
throw new Error("PLAY_ACCESS_TOKEN 이 비어 있다.");
}
if (!packageName) {
throw new Error("PLAY_PACKAGE_NAME 이 비어 있다.");
}
const application = encodeURIComponent(packageName);
const editsRoot = `${API_ROOT}/androidpublisher/v3/applications/${application}/edits`;
const uploadRoot = `${API_ROOT}/upload/androidpublisher/v3/applications/${application}/edits`;
async function request(method, url, { body, contentType } = {}) {
const headers = { authorization: `Bearer ${accessToken}` };
if (contentType) {
headers["content-type"] = contentType;
}
const response = await fetchImpl(url, { method, headers, body });
const text = typeof response.text === "function" ? await response.text() : "";
if (!response.ok) {
// 토큰은 header 로만 흐르므로 URL·본문을 그대로 적어도 자격이 새지 않는다.
throw new Error(`Play API ${method} ${url} 실패 (${response.status}): ${firstLine(text)}`);
}
return text ? JSON.parse(text) : {};
}
return {
packageName,
insertEdit: async () => {
const edit = await request("POST", editsRoot);
if (!edit?.id) {
throw new Error("Play API 가 edit id 를 돌려주지 않았다.");
}
return edit.id;
},
deleteEdit: (editId) => request("DELETE", `${editsRoot}/${encodeURIComponent(editId)}`),
listBundles: (editId) => request("GET", `${editsRoot}/${encodeURIComponent(editId)}/bundles`),
listTracks: (editId) => request("GET", `${editsRoot}/${encodeURIComponent(editId)}/tracks`),
uploadBundle: (editId, bytes) =>
request("POST", `${uploadRoot}/${encodeURIComponent(editId)}/bundles?uploadType=media`, {
body: bytes,
contentType: "application/octet-stream",
}),
updateTrack: (editId, track, payload) =>
request(
"PUT",
`${editsRoot}/${encodeURIComponent(editId)}/tracks/${encodeURIComponent(track)}`,
{ body: JSON.stringify(payload), contentType: "application/json" },
),
commitEdit: (editId) => request("POST", `${editsRoot}/${encodeURIComponent(editId)}:commit`),
};
}
// edit 를 열어 둔 채 실패하면 Play 쪽에 미완료 edit 가 남고, 다음 실행이 같은 앱에서 충돌한다.
// 정리 실패 자체는 원인 오류를 덮지 않는다 — 그래서 여기서 삼키고 경고만 남긴다.
async function discardEdit(client, editId, log) {
try {
await client.deleteEdit(editId);
log(`미완료 edit ${editId} 를 정리했다.`);
} catch (error) {
log(`::warning::미완료 edit ${editId} 정리 실패: ${error.message}`);
}
}
export async function readLatestVersionCode(client, { log = console.log } = {}) {
const editId = await client.insertEdit();
try {
const bundles = await client.listBundles(editId);
const tracks = await client.listTracks(editId);
return maxVersionCode(bundles, tracks);
} finally {
// 읽기 전용 조회다. commit 대신 삭제를 시도한다.
await discardEdit(client, editId, log);
}
}
/**
* edit 생성 → bundle 업로드 → internal track 갱신 → commit. 각 단계의 응답을 다음 단계의 전제로
* 다시 확인한다. 포착한 예외에서는 edit 삭제를 시도하고 원인을 그대로 올린다.
* 취소·타임아웃으로 프로세스가 종료되면 이 catch 는 실행되지 않을 수 있다.
*/
export async function publishInternalBundle(
client,
{ bundle, versionCode, releaseNotes, log = console.log },
) {
const expected = toVersionCode(versionCode);
if (!bundle || bundle.length === 0) {
throw new Error("업로드할 AAB 가 비어 있다.");
}
const editId = await client.insertEdit();
try {
// 빌드 사이 Console 업로드·승격이 끼었을 수 있으므로 probe 의 값을 재사용하지 않는다.
// 새 edit 에서 bundle 과 모든 track 을 조회해 수십 MB 를 전송하기 전에 다시 막는다.
const bundles = await client.listBundles(editId);
const tracks = await client.listTracks(editId);
const latest = maxVersionCode(bundles, tracks);
if (expected <= latest) {
throw new Error(`versionCode ${expected} 는 Play 현재 최대 versionCode ${latest} 보다 커야 한다.`);
}
const uploaded = await client.uploadBundle(editId, bundle);
const uploadedCode = toVersionCode(uploaded?.versionCode);
if (uploadedCode !== expected) {
throw new Error(
`업로드된 versionCode ${uploadedCode} 가 빌드한 ${expected} 와 다르다.`,
);
}
const track = await client.updateTrack(editId, INTERNAL_TRACK, {
track: INTERNAL_TRACK,
releases: [
{
versionCodes: [String(expected)],
status: "completed",
releaseNotes: [{ language: RELEASE_NOTES_LANGUAGE, text: releaseNotes }],
},
],
});
if (track?.track !== INTERNAL_TRACK) {
throw new Error(`internal 이 아닌 track 응답을 받았다: ${firstLine(track?.track)}`);
}
const assigned = (track.releases ?? []).flatMap((release) => release.versionCodes ?? []);
if (!assigned.map(String).includes(String(expected))) {
throw new Error(`internal track 이 versionCode ${expected} 를 받지 않았다.`);
}
const commit = await client.commitEdit(editId);
if (commit?.id !== editId) {
throw new Error(`commit 응답의 edit id 가 다르다: ${firstLine(commit?.id)}`);
}
log(`internal track commit 완료 — edit ${editId}, versionCode ${expected}.`);
return { editId, versionCode: expected, track: INTERNAL_TRACK };
} catch (error) {
await discardEdit(client, editId, log);
throw error;
}
}
async function writeOutput(entries) {
const outputPath = process.env.GITHUB_OUTPUT;
if (!outputPath) {
throw new Error("GITHUB_OUTPUT 이 필요하다.");
}
const body = Object.entries(entries)
.map(([key, value]) => `${key}=${value}\n`)
.join("");
await appendFile(outputPath, body, "utf8");
}
function clientFromEnvironment() {
return createPlayClient({
accessToken: process.env.PLAY_ACCESS_TOKEN,
packageName: process.env.PLAY_PACKAGE_NAME,
});
}
async function main(argv) {
const command = argv[0];
if (command === "latest") {
const latest = await readLatestVersionCode(clientFromEnvironment());
await writeOutput({ latest_version_code: latest });
console.log(`Play 가 알고 있는 최대 versionCode: ${latest}.`);
return;
}
if (command === "notes") {
const outputFile = argv[1];
if (!outputFile) {
throw new Error("notes <출력 파일> 형식으로 호출한다.");
}
const bodyFile = process.env.RELEASE_PR_BODY_FILE;
const body = bodyFile ? await readFile(bodyFile, "utf8").catch(() => "") : "";
const notes = renderInternalReleaseNotes({
sourceRef: process.env.SOURCE_REF,
sourceSha: process.env.SOURCE_SHA,
versionCode: process.env.AFTERNOTE_VERSION_CODE,
issues: parseIssueReferences(body),
});
await writeFile(outputFile, notes, "utf8");
console.log(notes);
return;
}
if (command === "publish") {
const bundlePath = argv[1];
const notesPath = argv[2];
if (!bundlePath || !notesPath) {
throw new Error("publish <AAB 경로> <release note 경로> 형식으로 호출한다.");
}
const result = await publishInternalBundle(clientFromEnvironment(), {
bundle: await readFile(bundlePath),
versionCode: process.env.AFTERNOTE_VERSION_CODE,
releaseNotes: await readFile(notesPath, "utf8"),
});
await writeOutput({ edit_id: result.editId, version_code: result.versionCode });
return;
}
throw new Error(`알 수 없는 명령: ${firstLine(command)}`);
}
if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
main(process.argv.slice(2)).catch((error) => {
console.error(`::error::${error.message}`);
process.exitCode = 1;
});
}