create-disposabl-app 0.7.1 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/dist/disposabl.js +44 -42
  2. package/dist/index.js +1 -1
  3. package/dist/templates/default/.github/workflows/pipeline.yml +4 -4
  4. package/dist/templates/default/packages/backend/AGENTS.md +2 -0
  5. package/dist/templates/default/packages/backend/README.md +2 -2
  6. package/dist/templates/default/packages/backend/convex/_generated/api.d.ts +10 -0
  7. package/dist/templates/default/packages/backend/convex/apps/actions/cleanup.ts +295 -0
  8. package/dist/templates/default/packages/backend/convex/apps/actions/delete.ts +15 -0
  9. package/dist/templates/default/packages/backend/convex/apps/actions/expo.ts +174 -0
  10. package/dist/templates/default/packages/backend/convex/apps/internal.ts +4 -21
  11. package/dist/templates/default/packages/backend/convex/apps/operations/internal.ts +213 -0
  12. package/dist/templates/default/packages/backend/convex/apps/validator.ts +23 -0
  13. package/dist/templates/default/packages/backend/convex/connections/actions/github.ts +1 -3
  14. package/dist/templates/default/packages/backend/convex/connections/index.ts +8 -8
  15. package/dist/templates/default/packages/backend/convex/connections/internal.ts +1 -2
  16. package/dist/templates/default/packages/backend/convex/github/actions/internal.ts +6 -0
  17. package/dist/templates/default/packages/backend/convex/github/actions/secrets.ts +35 -2
  18. package/dist/templates/default/packages/backend/convex/github/actions/tokens.ts +6 -2
  19. package/dist/templates/default/packages/backend/convex/github/internal.ts +5 -4
  20. package/dist/templates/default/packages/backend/convex/previews/internal.ts +5 -5
  21. package/dist/templates/default/packages/backend/convex/previews/token/actions/create.ts +30 -19
  22. package/dist/templates/default/packages/backend/convex/previews/token/actions/management.ts +2 -1
  23. package/dist/templates/default/packages/backend/convex/schema.ts +10 -0
  24. package/dist/templates/default/packages/backend/generated/openapi.json +5 -2
  25. package/dist/templates/default/packages/backend/generated/platforms.ts +14 -12
  26. package/dist/templates/default/packages/cli/README.md +3 -1
  27. package/dist/templates/default/packages/cli/commands.ts +23 -2
  28. package/dist/templates/default/packages/cli/delete.ts +39 -0
  29. package/dist/templates/default/packages/disposabl-kit/server/lib/platform-spec.ts +10 -2
  30. package/dist/templates/default/packages/sdk/src/generated/types.gen.ts +1 -0
  31. package/package.json +1 -1
@@ -0,0 +1,174 @@
1
+ "use node";
2
+
3
+ import { ConvexError } from "convex/values";
4
+
5
+ type Project = {
6
+ id: string;
7
+ slug: string;
8
+ ownerAccount: { id: string; name: string };
9
+ };
10
+ type Receipt = { id: string; state: string; willRetry: boolean };
11
+ const fields = "id slug ownerAccount { id name }";
12
+ export async function expoRequest<T>(
13
+ token: string,
14
+ query: string,
15
+ variables: Record<string, unknown>,
16
+ ): Promise<T> {
17
+ const response = await fetch("https://api.expo.dev/graphql", {
18
+ method: "POST",
19
+ redirect: "error",
20
+ signal: AbortSignal.timeout(30_000),
21
+ headers: {
22
+ Authorization: `Bearer ${token}`,
23
+ "Content-Type": "application/json",
24
+ },
25
+ body: JSON.stringify({ query, variables }),
26
+ });
27
+ if (!response.ok)
28
+ throw new ConvexError(`Expo request failed (${response.status}).`);
29
+ const result = (await response.json()) as {
30
+ data?: T;
31
+ errors?: { message?: string; extensions?: { errorCode?: string } }[];
32
+ };
33
+ if (
34
+ result.errors?.some(
35
+ (error) =>
36
+ error.extensions?.errorCode === "SUDO_MODE_REQUIRED" ||
37
+ error.message ===
38
+ "Only regular user and SSO user access to this API is supported",
39
+ )
40
+ )
41
+ throw new ExpoLoginRequired();
42
+ if (result.errors?.length) {
43
+ if (
44
+ result.errors.every(
45
+ (error) => error.extensions?.errorCode === "EXPERIENCE_NOT_FOUND",
46
+ )
47
+ )
48
+ throw new ExpoNotFound();
49
+ throw new ConvexError(
50
+ "Expo could not complete this request. Check your token and project permissions.",
51
+ );
52
+ }
53
+ if (!result.data) throw new ConvexError("Expo returned no project details.");
54
+ return result.data;
55
+ }
56
+ export class ExpoLoginRequired extends Error {}
57
+ class ExpoNotFound extends Error {}
58
+ export async function findExpo(
59
+ token: string,
60
+ fullName: string,
61
+ projectId?: string,
62
+ ) {
63
+ try {
64
+ const data = projectId
65
+ ? await expoRequest<{ app: { project: Project } }>(
66
+ token,
67
+ `query($id: String!) { app { project: byId(appId: $id) { ${fields} } } }`,
68
+ { id: projectId },
69
+ )
70
+ : await expoRequest<{ app: { project: Project } }>(
71
+ token,
72
+ `query($name: String!) { app { project: byFullName(fullName: $name) { ${fields} } } }`,
73
+ { name: fullName },
74
+ );
75
+ if (!data.app.project)
76
+ throw new ConvexError("Expo returned no project details.");
77
+ return data.app.project;
78
+ } catch (error) {
79
+ if (error instanceof ExpoNotFound) return null;
80
+ throw error;
81
+ }
82
+ }
83
+ export function verifyExpo(project: Project, accountId: string, appId: string) {
84
+ if (project.ownerAccount.id !== accountId || project.slug !== `app-${appId}`)
85
+ throw new ConvexError(
86
+ "Reconnect the Expo account that owns this app. The linked project must match this app's UUID.",
87
+ );
88
+ }
89
+ export async function ensureExpo(
90
+ token: string,
91
+ accountId: string,
92
+ owner: string,
93
+ appId: string,
94
+ saved?: string,
95
+ ) {
96
+ const fullName = `@${owner}/app-${appId}`;
97
+ let project = await findExpo(token, fullName, saved);
98
+ if (!project) {
99
+ if (saved)
100
+ throw new ConvexError(
101
+ "The linked Expo project is unavailable. Reconnect its owner before continuing.",
102
+ );
103
+ try {
104
+ const created = await expoRequest<{ app: { createApp: { id: string } } }>(
105
+ token,
106
+ "mutation($input: AppInput!) { app { createApp(appInput: $input) { id } } }",
107
+ { input: { accountId, projectName: `app-${appId}` } },
108
+ );
109
+ project = await findExpo(token, fullName, created.app.createApp.id);
110
+ } catch (error) {
111
+ project = await findExpo(token, fullName);
112
+ if (!project) throw error;
113
+ }
114
+ }
115
+ if (!project)
116
+ throw new ConvexError("Expo project creation is incomplete. Retry setup.");
117
+ verifyExpo(project, accountId, appId);
118
+ return project;
119
+ }
120
+ export async function deleteExpo(
121
+ token: string,
122
+ projectId: string,
123
+ savedReceipt: string | undefined,
124
+ saveReceipt: (id: string) => Promise<void>,
125
+ ) {
126
+ let receipt: Receipt;
127
+ if (savedReceipt) {
128
+ const data = await expoRequest<{
129
+ backgroundJobReceipt: { byId: Receipt | null };
130
+ }>(
131
+ token,
132
+ "query($id: ID!) { backgroundJobReceipt { byId(id: $id) { id state willRetry } } }",
133
+ { id: savedReceipt },
134
+ );
135
+ if (!data.backgroundJobReceipt.byId)
136
+ throw new ConvexError(
137
+ "Expo deletion receipt is unavailable. Reconnect the owning account.",
138
+ );
139
+ receipt = data.backgroundJobReceipt.byId;
140
+ } else {
141
+ receipt = (
142
+ await expoRequest<{ app: { scheduleAppDeletion: Receipt } }>(
143
+ token,
144
+ "mutation($id: ID!) { app { scheduleAppDeletion(appId: $id) { id state willRetry } } }",
145
+ { id: projectId },
146
+ )
147
+ ).app.scheduleAppDeletion;
148
+ await saveReceipt(receipt.id);
149
+ }
150
+ const deadline = Date.now() + 90_000;
151
+ while (receipt.state !== "SUCCESS") {
152
+ if (receipt.state === "FAILURE" && !receipt.willRetry)
153
+ throw new ConvexError(
154
+ "Expo project deletion failed. Check your Expo account and retry apps delete.",
155
+ );
156
+ if (Date.now() >= deadline)
157
+ throw new ConvexError(
158
+ "Expo is still deleting the project. Retry apps delete to finish cleanup.",
159
+ );
160
+ await new Promise((resolve) => setTimeout(resolve, 1500));
161
+ const data = await expoRequest<{
162
+ backgroundJobReceipt: { byId: Receipt | null };
163
+ }>(
164
+ token,
165
+ "query($id: ID!) { backgroundJobReceipt { byId(id: $id) { id state willRetry } } }",
166
+ { id: receipt.id },
167
+ );
168
+ if (!data.backgroundJobReceipt.byId)
169
+ throw new ConvexError(
170
+ "Expo deletion could not be confirmed. Retry apps delete.",
171
+ );
172
+ receipt = data.backgroundJobReceipt.byId;
173
+ }
174
+ }
@@ -29,6 +29,7 @@ export const insert = internalMutation({
29
29
  appId: args.appId,
30
30
  name,
31
31
  platforms,
32
+ expoAccountId: null,
32
33
  });
33
34
  return presentApp((await ctx.db.get("apps", id))!);
34
35
  },
@@ -59,27 +60,7 @@ export const get = internalQuery({
59
60
  handler: async (ctx, { userId, appId }) =>
60
61
  presentApp(await ownedApp(ctx, userId, appId)),
61
62
  });
62
- const remove = internalMutation({
63
- platforms: true,
64
- args: { ...identityFields, appId: v.string() },
65
- returns: v.null(),
66
- handler: async (ctx, { userId, appId }) => {
67
- const app = await ownedApp(ctx, userId, appId);
68
- for await (const preview of ctx.db
69
- .query("previews")
70
- .withIndex("by_appId_reference", (q) => q.eq("appId", appId))) {
71
- await ctx.db.delete("previews", preview._id);
72
- }
73
- const production = await ctx.db
74
- .query("appReleases")
75
- .withIndex("by_appId", (q) => q.eq("appId", appId))
76
- .unique();
77
- if (production) await ctx.db.delete("appReleases", production._id);
78
- await ctx.db.delete("apps", app._id);
79
- return null;
80
- },
81
- });
82
- export { remove as delete };
63
+ export { remove as delete } from "./actions/delete";
83
64
 
84
65
  function assertAppId(appId: string) {
85
66
  if (
@@ -120,6 +101,7 @@ export const githubApp = internalQuery({
120
101
  githubOwner: v.string(),
121
102
  githubRepo: v.string(),
122
103
  installationId: v.string(),
104
+ userId: v.string(),
123
105
  }),
124
106
  ),
125
107
  handler: async (ctx, { appId }) => {
@@ -141,6 +123,7 @@ export const githubApp = internalQuery({
141
123
  githubOwner: app.githubOwner,
142
124
  githubRepo: app.githubRepo,
143
125
  installationId,
126
+ userId: app.userId,
144
127
  }
145
128
  : null;
146
129
  },
@@ -0,0 +1,213 @@
1
+ import {
2
+ identityFields,
3
+ internalMutation,
4
+ internalQuery,
5
+ } from "@disposabl/disposabl-kit/server/lib/functions";
6
+ import { ConvexError, v } from "convex/values";
7
+ import type { Doc } from "../../_generated/dataModel";
8
+ import type { QueryCtx } from "../../_generated/server";
9
+ import { ownedApp } from "../internal";
10
+ import { pendingDelete, resources } from "../validator";
11
+
12
+ export function assertActive(app: Doc<"apps">) {
13
+ if (app.operation?.kind === "delete")
14
+ throw new ConvexError(
15
+ "App deletion is in progress. Retry apps delete to finish cleanup.",
16
+ );
17
+ }
18
+ async function assertPreviewOnly(ctx: Pick<QueryCtx, "db">, appId: string) {
19
+ const release = await ctx.db
20
+ .query("appReleases")
21
+ .withIndex("by_appId", (q) => q.eq("appId", appId))
22
+ .unique();
23
+ if (release)
24
+ throw new ConvexError("Apps with production releases cannot be deleted.");
25
+ }
26
+
27
+ // One lease prevents setup, deletion and production publishing from racing.
28
+ export const begin = internalMutation({
29
+ args: {
30
+ ...identityFields,
31
+ appId: v.string(),
32
+ kind: v.union(v.literal("setup"), v.literal("delete")),
33
+ },
34
+ returns: v.object({
35
+ until: v.number(),
36
+ resuming: v.boolean(),
37
+ pending: v.optional(pendingDelete),
38
+ }),
39
+ handler: async (ctx, { userId, appId, kind }) => {
40
+ const app = await ownedApp(ctx, userId, appId);
41
+ if (kind === "setup") assertActive(app);
42
+ else await assertPreviewOnly(ctx, appId);
43
+ if (app.operation && app.operation.until > Date.now())
44
+ throw new ConvexError(
45
+ "This app has an operation in progress. Try again when it finishes.",
46
+ );
47
+ // Longer than Convex's maximum action runtime, including nested actions.
48
+ const until = Date.now() + 15 * 60_000;
49
+ await ctx.db.patch("apps", app._id, {
50
+ operation: { ...app.operation, kind, until },
51
+ });
52
+ return {
53
+ until,
54
+ resuming: app.operation?.kind === "delete",
55
+ pending: app.operation?.pending,
56
+ };
57
+ },
58
+ });
59
+ export const finish = internalMutation({
60
+ args: {
61
+ ...identityFields,
62
+ appId: v.string(),
63
+ until: v.number(),
64
+ retryDelete: v.boolean(),
65
+ },
66
+ returns: v.null(),
67
+ handler: async (ctx, { userId, appId, until, retryDelete }) => {
68
+ const app = await ownedApp(ctx, userId, appId);
69
+ if (app.operation?.until === until)
70
+ await ctx.db.patch("apps", app._id, {
71
+ operation: retryDelete
72
+ ? { ...app.operation, kind: "delete", until: 0 }
73
+ : undefined,
74
+ });
75
+ return null;
76
+ },
77
+ });
78
+ export const linked = internalQuery({
79
+ args: { ...identityFields, appId: v.string() },
80
+ returns: resources,
81
+ handler: async (ctx, { userId, appId }) => {
82
+ const {
83
+ githubOwner,
84
+ githubRepo,
85
+ convexProjectId,
86
+ expoProjectId,
87
+ expoAccountId,
88
+ } = await ownedApp(ctx, userId, appId);
89
+ return {
90
+ githubOwner,
91
+ githubRepo,
92
+ convexProjectId,
93
+ expoProjectId,
94
+ expoAccountId,
95
+ };
96
+ },
97
+ });
98
+ export const saveExpo = internalMutation({
99
+ args: {
100
+ ...identityFields,
101
+ appId: v.string(),
102
+ accountId: v.string(),
103
+ projectId: v.optional(v.string()),
104
+ },
105
+ returns: v.null(),
106
+ handler: async (ctx, { userId, appId, accountId, projectId }) => {
107
+ const app = await ownedApp(ctx, userId, appId);
108
+ assertActive(app);
109
+ if (
110
+ (app.expoAccountId && app.expoAccountId !== accountId) ||
111
+ (projectId && app.expoProjectId && app.expoProjectId !== projectId)
112
+ )
113
+ throw new ConvexError("App is already linked to another Expo project.");
114
+ await ctx.db.patch("apps", app._id, {
115
+ expoAccountId: accountId,
116
+ ...(projectId ? { expoProjectId: projectId } : {}),
117
+ });
118
+ return null;
119
+ },
120
+ });
121
+ export const forget = internalMutation({
122
+ args: {
123
+ ...identityFields,
124
+ appId: v.string(),
125
+ until: v.number(),
126
+ provider: v.union(
127
+ v.literal("github"),
128
+ v.literal("convex"),
129
+ v.literal("expo"),
130
+ ),
131
+ },
132
+ returns: v.null(),
133
+ handler: async (ctx, { userId, appId, until, provider }) => {
134
+ const app = await ownedApp(ctx, userId, appId);
135
+ if (app.operation?.kind !== "delete" || app.operation.until !== until)
136
+ throw new ConvexError("Deletion must be restarted.");
137
+ await ctx.db.patch(
138
+ "apps",
139
+ app._id,
140
+ provider === "github"
141
+ ? { githubOwner: undefined, githubRepo: undefined }
142
+ : provider === "convex"
143
+ ? { convexProjectId: undefined }
144
+ : { expoProjectId: undefined, expoAccountId: null },
145
+ );
146
+ return null;
147
+ },
148
+ });
149
+ export const deletingResource = internalMutation({
150
+ args: {
151
+ ...identityFields,
152
+ appId: v.string(),
153
+ until: v.number(),
154
+ pending: pendingDelete,
155
+ },
156
+ returns: v.null(),
157
+ handler: async (ctx, { userId, appId, until, pending }) => {
158
+ const app = await ownedApp(ctx, userId, appId);
159
+ if (app.operation?.kind !== "delete" || app.operation.until !== until)
160
+ throw new ConvexError("Deletion must be restarted.");
161
+ await ctx.db.patch("apps", app._id, {
162
+ operation: { ...app.operation, pending },
163
+ });
164
+ return null;
165
+ },
166
+ });
167
+ export const erase = internalMutation({
168
+ args: { ...identityFields, appId: v.string(), until: v.number() },
169
+ returns: v.boolean(),
170
+ handler: async (ctx, { userId, appId, until }) => {
171
+ const app = await ownedApp(ctx, userId, appId);
172
+ await assertPreviewOnly(ctx, appId);
173
+ if (
174
+ app.operation?.kind !== "delete" ||
175
+ app.operation.until !== until ||
176
+ app.githubRepo ||
177
+ app.convexProjectId ||
178
+ app.expoProjectId ||
179
+ app.expoAccountId
180
+ )
181
+ throw new ConvexError("Linked projects must be deleted first.");
182
+ const previews = await ctx.db
183
+ .query("previews")
184
+ .withIndex("by_appId_reference", (q) => q.eq("appId", appId))
185
+ .take(100);
186
+ for (const preview of previews)
187
+ await ctx.db.delete("previews", preview._id);
188
+ if (previews.length === 100) return false;
189
+ await ctx.db.delete("apps", app._id);
190
+ return true;
191
+ },
192
+ });
193
+ export const workflow = internalMutation({
194
+ args: { ...identityFields, appId: v.string(), production: v.boolean() },
195
+ returns: v.null(),
196
+ handler: async (ctx, { userId, appId, production }) => {
197
+ const app = await ownedApp(ctx, userId, appId);
198
+ assertActive(app);
199
+ if (
200
+ production &&
201
+ !(await ctx.db
202
+ .query("appReleases")
203
+ .withIndex("by_appId", (q) => q.eq("appId", appId))
204
+ .unique())
205
+ )
206
+ await ctx.db.insert("appReleases", {
207
+ appId,
208
+ releases: {},
209
+ updatedAt: Date.now(),
210
+ });
211
+ return null;
212
+ },
213
+ });
@@ -0,0 +1,23 @@
1
+ import { v } from "convex/values";
2
+
3
+ export const deleteArgs = {
4
+ appId: v.string(),
5
+ dangerouslyDeleteApp: v.literal(true),
6
+ };
7
+ export const resources = v.object({
8
+ githubOwner: v.optional(v.string()),
9
+ githubRepo: v.optional(v.string()),
10
+ convexProjectId: v.optional(v.number()),
11
+ expoProjectId: v.optional(v.string()),
12
+ expoAccountId: v.optional(v.union(v.string(), v.null())),
13
+ });
14
+
15
+ export const pendingDelete = v.object({
16
+ provider: v.union(
17
+ v.literal("github"),
18
+ v.literal("convex"),
19
+ v.literal("expo"),
20
+ ),
21
+ accountId: v.string(),
22
+ receipt: v.optional(v.string()),
23
+ });
@@ -109,10 +109,8 @@ export const selectGithub = internalAction({
109
109
  installationId,
110
110
  permissions.map((p) => `${p}:write`),
111
111
  );
112
- const id = await ctx.runQuery(internal.users.internal.current, { userId });
113
- if (!id) throw new ConvexError("Sign in to connect GitHub.");
114
112
  await ctx.runMutation(internal.github.internal.connect, {
115
- userId: id.id,
113
+ userId,
116
114
  ...chosen,
117
115
  });
118
116
  return null;
@@ -11,7 +11,7 @@ import {
11
11
  provider,
12
12
  } from "./validator";
13
13
 
14
- async function userId(ctx: QueryCtx | MutationCtx | ActionCtx) {
14
+ async function requireUser(ctx: QueryCtx | MutationCtx | ActionCtx) {
15
15
  const id = await getAuthUserId(ctx);
16
16
  if (!id) throw new ConvexError("Sign in to manage your connections.");
17
17
  return id;
@@ -21,7 +21,7 @@ export const status = query({
21
21
  returns: connectionStatus,
22
22
  handler: async (ctx): Promise<Infer<typeof connectionStatus>> =>
23
23
  ctx.runQuery(internal.connections.internal.status, {
24
- userId: await userId(ctx),
24
+ userId: await requireUser(ctx),
25
25
  }),
26
26
  });
27
27
  export const start = action({
@@ -30,7 +30,7 @@ export const start = action({
30
30
  handler: async (ctx, args): Promise<string> =>
31
31
  ctx.runAction(internal.connections.actions.oauth.start, {
32
32
  ...args,
33
- userId: await userId(ctx),
33
+ userId: await requireUser(ctx),
34
34
  }),
35
35
  });
36
36
  export const complete = action({
@@ -39,7 +39,7 @@ export const complete = action({
39
39
  handler: async (ctx, args): Promise<Infer<typeof completion>> =>
40
40
  ctx.runAction(internal.connections.actions.oauth.complete, {
41
41
  ...args,
42
- userId: await userId(ctx),
42
+ userId: await requireUser(ctx),
43
43
  }),
44
44
  });
45
45
  export const selectGithub = action({
@@ -48,7 +48,7 @@ export const selectGithub = action({
48
48
  handler: async (ctx, args): Promise<null> =>
49
49
  ctx.runAction(internal.connections.actions.github.selectGithub, {
50
50
  ...args,
51
- userId: await userId(ctx),
51
+ userId: await requireUser(ctx),
52
52
  }),
53
53
  });
54
54
  export const expoAccounts = action({
@@ -57,7 +57,7 @@ export const expoAccounts = action({
57
57
  handler: async (ctx, args): Promise<Infer<typeof account>[]> =>
58
58
  ctx.runAction(internal.connections.actions.expo.expoAccounts, {
59
59
  ...args,
60
- userId: await userId(ctx),
60
+ userId: await requireUser(ctx),
61
61
  }),
62
62
  });
63
63
  export const connectExpo = action({
@@ -66,7 +66,7 @@ export const connectExpo = action({
66
66
  handler: async (ctx, args): Promise<null> =>
67
67
  ctx.runAction(internal.connections.actions.expo.connectExpo, {
68
68
  ...args,
69
- userId: await userId(ctx),
69
+ userId: await requireUser(ctx),
70
70
  }),
71
71
  });
72
72
  export const disconnect = mutation({
@@ -75,6 +75,6 @@ export const disconnect = mutation({
75
75
  handler: async (ctx, args): Promise<null> =>
76
76
  ctx.runMutation(internal.connections.internal.disconnect, {
77
77
  ...args,
78
- userId: await userId(ctx),
78
+ userId: await requireUser(ctx),
79
79
  }),
80
80
  });
@@ -116,8 +116,7 @@ export const begin = internalMutation({
116
116
  returns: v.null(),
117
117
  handler: async (ctx, { userId, ...state }) => {
118
118
  const id = ctx.db.normalizeId("users", userId);
119
- if (!id || !(await ctx.db.get("users", id)))
120
- throw new ConvexError("Sign in to connect an account.");
119
+ if (!id) throw new ConvexError("Invalid userId.");
121
120
  await ctx.db.patch("users", id, {
122
121
  connectionState: { ...state, expiresAt: Date.now() + 10 * 60_000 },
123
122
  });
@@ -96,6 +96,11 @@ async function authorize(ctx: ActionCtx, appId: string, token: string) {
96
96
  claims.head_ref !== workflowRun.head_branch
97
97
  )
98
98
  throw new Error("Unexpected preview branch.");
99
+ await ctx.runMutation(internal.apps.operations.internal.workflow, {
100
+ userId: app.userId,
101
+ appId,
102
+ production,
103
+ });
99
104
  return {
100
105
  app,
101
106
  repository,
@@ -178,6 +183,7 @@ export const report = internalAction({
178
183
  const { token: _token, ...result } = args;
179
184
  await ctx.runMutation(internal.previews.internal.report, {
180
185
  ...result,
186
+ userId: verified.app.userId,
181
187
  githubOwner: verified.app.githubOwner,
182
188
  githubRepo: verified.app.githubRepo,
183
189
  reference: verified.reference,
@@ -8,6 +8,7 @@ import {
8
8
  import { v } from "convex/values";
9
9
  import { internal } from "../../_generated/api";
10
10
  import { credential, unseal } from "../../connections/actions/credentials";
11
+ import { ensureExpo } from "../../apps/actions/expo";
11
12
  import { validateExpo } from "../../connections/actions/expo";
12
13
  import {
13
14
  management,
@@ -19,7 +20,7 @@ export const configure = internalAction({
19
20
  args: { ...identityFields, appId: v.string(), projectId: v.number() },
20
21
  returns: v.null(),
21
22
  handler: async (ctx, args) => {
22
- await ctx.runQuery(internal.apps.internal.get, {
23
+ const details = await ctx.runQuery(internal.apps.internal.get, {
23
24
  userId: args.userId,
24
25
  appId: args.appId,
25
26
  });
@@ -100,13 +101,45 @@ export const configure = internalAction({
100
101
  userId: args.userId,
101
102
  provider: "expo",
102
103
  });
103
- if (expo) {
104
+ if (
105
+ expo &&
106
+ details.platforms.some(
107
+ (platform) => platform === "android" || platform === "ios",
108
+ )
109
+ ) {
104
110
  const token = unseal(expo.token, `${args.userId}:expo`);
105
111
  const accounts = await validateExpo(token);
106
112
  if (!accounts.some((account) => account.id === expo.accountId))
107
113
  throw new Error(
108
114
  "Reconnect your Expo account before setting up mobile builds.",
109
115
  );
116
+ const linked = await ctx.runQuery(
117
+ internal.apps.operations.internal.linked,
118
+ {
119
+ userId: args.userId,
120
+ appId: args.appId,
121
+ },
122
+ );
123
+ await ctx.runMutation(internal.apps.operations.internal.saveExpo, {
124
+ userId: args.userId,
125
+ appId: args.appId,
126
+ accountId: expo.accountId,
127
+ });
128
+ const project = await ensureExpo(
129
+ token,
130
+ expo.accountId,
131
+ expo.name,
132
+ args.appId,
133
+ linked.expoProjectId,
134
+ );
135
+ await ctx.runMutation(internal.apps.operations.internal.saveExpo, {
136
+ userId: args.userId,
137
+ appId: args.appId,
138
+ projectId: project.id,
139
+ accountId: expo.accountId,
140
+ });
141
+ await save("EXPO_PROJECT_ID", project.id);
142
+ await save("EXPO_SLUG", project.slug);
110
143
  await save("EXPO_TOKEN", token);
111
144
  await save("EXPO_OWNER", expo.name);
112
145
  }
@@ -47,6 +47,11 @@ export async function vercelIdentity() {
47
47
  const { token } = (await response.json()) as { token: string };
48
48
  return token;
49
49
  }
50
+ export class GitHubError extends Error {
51
+ constructor(readonly status: number) {
52
+ super(`GitHub request failed (${status}).`);
53
+ }
54
+ }
50
55
  export async function githubRequest<T>(
51
56
  token: string,
52
57
  path: string,
@@ -65,8 +70,7 @@ export async function githubRequest<T>(
65
70
  },
66
71
  ...(body === undefined ? {} : { body: JSON.stringify(body) }),
67
72
  });
68
- if (!response.ok)
69
- throw new Error(`GitHub request failed (${response.status}).`);
73
+ if (!response.ok) throw new GitHubError(response.status);
70
74
  return (
71
75
  response.status === 204 || response.headers.get("content-length") === "0"
72
76
  ? undefined
@@ -1,17 +1,18 @@
1
- import { internalMutation } from "@disposabl/disposabl-kit/server/lib/functions";
1
+ import {
2
+ identityFields,
3
+ internalMutation,
4
+ } from "@disposabl/disposabl-kit/server/lib/functions";
2
5
  import { ConvexError, v } from "convex/values";
3
6
 
4
7
  /** A trusted installation callback supplies this verified binding, never a client API. */
5
8
  export const connect = internalMutation({
6
9
  args: {
7
- userId: v.id("users"),
10
+ ...identityFields,
8
11
  owner: v.string(),
9
12
  installationId: v.string(),
10
13
  },
11
14
  returns: v.null(),
12
15
  handler: async (ctx, { userId, owner, installationId }) => {
13
- if (!(await ctx.db.get("users", userId)))
14
- throw new ConvexError("User not found.");
15
16
  if (!/^[a-zA-Z0-9-]+$/.test(owner) || !/^\d+$/.test(installationId))
16
17
  throw new ConvexError("Invalid GitHub installation.");
17
18
  owner = owner.toLowerCase();