Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 220dea2

Browse files
committed
Add subscription_filter to run onTag and onPush
refs #39
1 parent 0c02535 commit 220dea2

File tree

4 files changed

+295
-755
lines changed

4 files changed

+295
-755
lines changed

lib/events/onPush.ts renamed to lib/events.ts

Lines changed: 145 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ import * as fs from "fs-extra";
3333
import * as os from "os";
3434
import * as pRetry from "p-retry";
3535
import * as path from "path";
36-
import { extractAnnotations } from "../annotation";
37-
import { gitBranchToNpmTag } from "../branch";
38-
import { Configuration } from "../configuration";
39-
import { nextPrereleaseTag } from "../tag";
36+
import { extractAnnotations } from "./annotation";
37+
import { gitBranchToNpmTag } from "./branch";
38+
import { Configuration } from "./configuration";
39+
import { nextPrereleaseTag } from "./tag";
4040

4141
interface NpmParameters {
4242
project: project.Project;
@@ -46,15 +46,22 @@ interface NpmParameters {
4646
}
4747

4848
type NpmStep = Step<
49-
EventContext<subscription.types.OnPushSubscription, Configuration>,
49+
EventContext<
50+
| subscription.types.OnPushSubscription
51+
| subscription.types.OnTagSubscription,
52+
Configuration
53+
>,
5054
NpmParameters
5155
>;
5256

5357
const LoadProjectStep: NpmStep = {
5458
name: "load",
5559
run: async (ctx, params) => {
56-
const push = ctx.data.Push[0];
57-
const repo = push.repo;
60+
const repo =
61+
(ctx.data as subscription.types.OnPushSubscription).Push?.[0]
62+
?.repo ||
63+
(ctx.data as subscription.types.OnTagSubscription).Tag?.[0]?.commit
64+
?.repo;
5865

5966
const credential = await ctx.credential.resolve(
6067
secret.gitHubAppToken({
@@ -93,21 +100,29 @@ const ValidateStep: NpmStep = {
93100

94101
const CommandStep: NpmStep = {
95102
name: "command",
96-
runWhen: async ctx => !!ctx.configuration?.[0]?.parameters?.command,
103+
runWhen: async ctx => !!ctx.configuration?.parameters?.command,
97104
run: async ctx => {
98-
const push = ctx.data.Push[0];
105+
const repo =
106+
(ctx.data as subscription.types.OnPushSubscription).Push?.[0]
107+
?.repo ||
108+
(ctx.data as subscription.types.OnTagSubscription).Tag?.[0]?.commit
109+
?.repo;
110+
const commit =
111+
(ctx.data as subscription.types.OnPushSubscription).Push?.[0]
112+
?.after ||
113+
(ctx.data as subscription.types.OnTagSubscription).Tag?.[0]?.commit;
99114
const result = await childProcess.spawnPromise(
100115
"bash",
101-
["-c", ctx.configuration?.[0]?.parameters?.command],
116+
["-c", ctx.configuration?.parameters?.command],
102117
{
103118
log: childProcess.captureLog(),
104119
},
105120
);
106121
if (result.status !== 0) {
107122
return status.failure(
108-
`Failed to run command on [${push.repo.owner}/${
109-
push.repo.name
110-
}/${push.after.sha.slice(0, 7)}](${push.after.url})`,
123+
`Failed to run command on [${repo.owner}/${
124+
repo.name
125+
}/${commit.sha.slice(0, 7)}](${commit.url})`,
111126
);
112127
}
113128
return status.success();
@@ -117,6 +132,10 @@ const CommandStep: NpmStep = {
117132
const PrepareStep: NpmStep = {
118133
name: "prepare",
119134
run: async (ctx, params) => {
135+
const commit =
136+
(ctx.data as subscription.types.OnPushSubscription).Push?.[0]
137+
?.after ||
138+
(ctx.data as subscription.types.OnTagSubscription).Tag?.[0]?.commit;
120139
// copy creds
121140
const npmRc = path.join(os.homedir(), ".npmrc");
122141
if (process.env.NPM_NPMJS_CREDENTIALS) {
@@ -125,10 +144,10 @@ const PrepareStep: NpmStep = {
125144

126145
// raise the check
127146
params.check = await github.createCheck(ctx, params.project.id, {
128-
sha: ctx.data.Push[0].after.sha,
147+
sha: commit.sha,
129148
title: "npm run",
130-
name: `${ctx.skill.name}/${ctx.configuration?.[0]?.name}/run`,
131-
body: `Running \`npm run --if-present ${ctx.configuration?.[0]?.parameters?.scripts.join(
149+
name: `${ctx.skill.name}/${ctx.configuration?.name}/run`,
150+
body: `Running \`npm run --if-present ${ctx.configuration?.parameters?.scripts.join(
132151
" ",
133152
)}\``,
134153
});
@@ -140,8 +159,16 @@ const PrepareStep: NpmStep = {
140159
const SetupNodeStep: NpmStep = {
141160
name: "setup node",
142161
run: async (ctx, params) => {
143-
const push = ctx.data.Push[0];
144-
const cfg = ctx.configuration?.[0]?.parameters;
162+
const repo =
163+
(ctx.data as subscription.types.OnPushSubscription).Push?.[0]
164+
?.repo ||
165+
(ctx.data as subscription.types.OnTagSubscription).Tag?.[0]?.commit
166+
?.repo;
167+
const commit =
168+
(ctx.data as subscription.types.OnPushSubscription).Push?.[0]
169+
?.after ||
170+
(ctx.data as subscription.types.OnTagSubscription).Tag?.[0]?.commit;
171+
const cfg = ctx.configuration?.parameters;
145172
// Set up node version
146173
let result = await params.project.spawn("bash", [
147174
"-c",
@@ -153,9 +180,9 @@ const SetupNodeStep: NpmStep = {
153180
body: "`nvm install` failed",
154181
});
155182
return status.failure(
156-
`\`nvm install\` failed on [${push.repo.owner}/${
157-
push.repo.name
158-
}/${push.after.sha.slice(0, 7)}](${push.after.url})`,
183+
`\`nvm install\` failed on [${repo.owner}/${
184+
repo.name
185+
}/${commit.sha.slice(0, 7)}](${commit.url})`,
159186
);
160187
}
161188
// set the unsafe-prem config
@@ -180,9 +207,9 @@ const SetupNodeStep: NpmStep = {
180207
body: "`nvm which` failed",
181208
});
182209
return status.failure(
183-
`\`nvm which\` failed on [${push.repo.owner}/${
184-
push.repo.name
185-
}/${push.after.sha.slice(0, 7)}](${push.after.url})`,
210+
`\`nvm which\` failed on [${repo.owner}/${
211+
repo.name
212+
}/${commit.sha.slice(0, 7)}](${commit.url})`,
186213
);
187214
}
188215
return status.success();
@@ -192,7 +219,15 @@ const SetupNodeStep: NpmStep = {
192219
const NpmInstallStep: NpmStep = {
193220
name: "npm install",
194221
run: async (ctx, params) => {
195-
const push = ctx.data.Push[0];
222+
const repo =
223+
(ctx.data as subscription.types.OnPushSubscription).Push?.[0]
224+
?.repo ||
225+
(ctx.data as subscription.types.OnTagSubscription).Tag?.[0]?.commit
226+
?.repo;
227+
const commit =
228+
(ctx.data as subscription.types.OnPushSubscription).Push?.[0]
229+
?.after ||
230+
(ctx.data as subscription.types.OnTagSubscription).Tag?.[0]?.commit;
196231
const opts = {
197232
env: {
198233
...process.env,
@@ -220,9 +255,9 @@ const NpmInstallStep: NpmStep = {
220255
body: "`npm install` failed",
221256
});
222257
return status.failure(
223-
`\`npm install\` failed on [${push.repo.owner}/${
224-
push.repo.name
225-
}/${push.after.sha.slice(0, 7)}](${push.after.url})`,
258+
`\`npm install\` failed on [${repo.owner}/${
259+
repo.name
260+
}/${commit.sha.slice(0, 7)}](${commit.url})`,
226261
);
227262
}
228263
return status.success();
@@ -232,8 +267,16 @@ const NpmInstallStep: NpmStep = {
232267
const NpmScriptsStep: NpmStep = {
233268
name: "npm run",
234269
run: async (ctx, params) => {
235-
const push = ctx.data.Push[0];
236-
const cfg = ctx.configuration?.[0]?.parameters;
270+
const repo =
271+
(ctx.data as subscription.types.OnPushSubscription).Push?.[0]
272+
?.repo ||
273+
(ctx.data as subscription.types.OnTagSubscription).Tag?.[0]?.commit
274+
?.repo;
275+
const commit =
276+
(ctx.data as subscription.types.OnPushSubscription).Push?.[0]
277+
?.after ||
278+
(ctx.data as subscription.types.OnTagSubscription).Tag?.[0]?.commit;
279+
const cfg = ctx.configuration?.parameters;
237280
const scripts = cfg.scripts;
238281

239282
// Run scripts
@@ -276,9 +319,9 @@ ${captureLog.log.trim()}
276319
})),
277320
});
278321
return status.failure(
279-
`\`npm run ${script}\` failed on [${push.repo.owner}/${
280-
push.repo.name
281-
}/${push.after.sha.slice(0, 7)}](${push.after.url})`,
322+
`\`npm run ${script}\` failed on [${repo.owner}/${
323+
repo.name
324+
}/${commit.sha.slice(0, 7)}](${commit.url})`,
282325
);
283326
} else {
284327
params.body.push(
@@ -295,28 +338,39 @@ ${captureLog.log.trim()}
295338
body: params.body.join("\n\n---\n\n"),
296339
});
297340
return status.success(
298-
`\`npm run ${scripts.join(" ")}\` passed on [${push.repo.owner}/${
299-
push.repo.name
300-
}/${push.after.sha.slice(0, 7)}](${push.after.url})`,
341+
`\`npm run ${scripts.join(" ")}\` passed on [${repo.owner}/${
342+
repo.name
343+
}/${commit.sha.slice(0, 7)}](${commit.url})`,
301344
);
302345
},
303346
};
304347

305348
const NpmVersionStep: NpmStep = {
306349
name: "version",
307350
runWhen: async ctx =>
308-
ctx.configuration?.[0]?.parameters.publish &&
309-
ctx.configuration?.[0]?.parameters.publish !== "no",
351+
ctx.configuration?.parameters.publish &&
352+
ctx.configuration?.parameters.publish !== "no",
310353
run: async (ctx, params) => {
311-
const push = ctx.data.Push[0];
354+
const repo =
355+
(ctx.data as subscription.types.OnPushSubscription).Push?.[0]
356+
?.repo ||
357+
(ctx.data as subscription.types.OnTagSubscription).Tag?.[0]?.commit
358+
?.repo;
359+
const commit =
360+
(ctx.data as subscription.types.OnPushSubscription).Push?.[0]
361+
?.after ||
362+
(ctx.data as subscription.types.OnTagSubscription).Tag?.[0]?.commit;
363+
const branch =
364+
(ctx.data as subscription.types.OnPushSubscription).Push?.[0]
365+
?.branch ||
366+
(ctx.data as subscription.types.OnTagSubscription).Tag?.[0]?.name;
312367
let pj = await fs.readJson(params.project.path("package.json"));
313368

314369
const pjVersion =
315370
pj.version ||
316371
(await github.nextTag(params.project.id, "patch")) ||
317372
"0.1.0";
318373

319-
const repo = push.repo;
320374
const credential = await ctx.credential.resolve(
321375
secret.gitHubAppToken({
322376
owner: repo.owner,
@@ -342,7 +396,7 @@ const NpmVersionStep: NpmStep = {
342396
response => response.data.map(t => t.name),
343397
);
344398
const tag = nextPrereleaseTag({
345-
branch: push.branch,
399+
branch,
346400
defaultBranch: repo.defaultBranch,
347401
nextReleaseVersion: pjVersion,
348402
tags,
@@ -391,9 +445,9 @@ const NpmVersionStep: NpmStep = {
391445
body: "`npm version` failed",
392446
});
393447
return status.failure(
394-
`\`npm version\` failed on [${push.repo.owner}/${
395-
push.repo.name
396-
}/${push.after.sha.slice(0, 7)}](${push.after.url})`,
448+
`\`npm version\` failed on [${repo.owner}/${
449+
repo.name
450+
}/${commit.sha.slice(0, 7)}](${commit.url})`,
397451
);
398452
}
399453
return status.success();
@@ -402,14 +456,38 @@ const NpmVersionStep: NpmStep = {
402456

403457
const NpmPublishStep: NpmStep = {
404458
name: "npm publish",
405-
runWhen: async ctx =>
406-
ctx.configuration?.[0]?.parameters.publish &&
407-
ctx.configuration?.[0]?.parameters.publish !== "no" &&
408-
(ctx.configuration?.[0]?.parameters.publish === "all" ||
409-
ctx.data.Push[0].branch === ctx.data.Push[0].repo.defaultBranch),
459+
runWhen: async ctx => {
460+
const repo =
461+
(ctx.data as subscription.types.OnPushSubscription).Push?.[0]
462+
?.repo ||
463+
(ctx.data as subscription.types.OnTagSubscription).Tag?.[0]?.commit
464+
?.repo;
465+
const branch =
466+
(ctx.data as subscription.types.OnPushSubscription).Push?.[0]
467+
?.branch ||
468+
(ctx.data as subscription.types.OnTagSubscription).Tag?.[0]?.name;
469+
return (
470+
ctx.configuration?.parameters.publish &&
471+
ctx.configuration?.parameters.publish !== "no" &&
472+
(ctx.configuration?.parameters.publish === "all" ||
473+
branch === repo.defaultBranch)
474+
);
475+
},
410476
run: async (ctx, params) => {
411-
const cfg = ctx.configuration?.[0]?.parameters;
412-
const push = ctx.data.Push[0];
477+
const cfg = ctx.configuration?.parameters;
478+
const repo =
479+
(ctx.data as subscription.types.OnPushSubscription).Push?.[0]
480+
?.repo ||
481+
(ctx.data as subscription.types.OnTagSubscription).Tag?.[0]?.commit
482+
?.repo;
483+
const commit =
484+
(ctx.data as subscription.types.OnPushSubscription).Push?.[0]
485+
?.after ||
486+
(ctx.data as subscription.types.OnTagSubscription).Tag?.[0]?.commit;
487+
const branch =
488+
(ctx.data as subscription.types.OnPushSubscription).Push?.[0]
489+
?.branch ||
490+
(ctx.data as subscription.types.OnTagSubscription).Tag?.[0]?.name;
413491
const pj = await fs.readJson(params.project.path("package.json"));
414492

415493
// add /.npm/ to the .npmignore file
@@ -425,19 +503,19 @@ const NpmPublishStep: NpmStep = {
425503
if (cfg.access) {
426504
args.push("--access", cfg.access);
427505
}
428-
args.push("--tag", gitBranchToNpmTag(push.branch));
506+
args.push("--tag", gitBranchToNpmTag(branch));
429507

430508
const check = await github.createCheck(ctx, params.project.id, {
431-
sha: ctx.data.Push[0].after.sha,
509+
sha: commit.sha,
432510
title: "npm publish",
433-
name: `${ctx.skill.name}/${ctx.configuration?.[0]?.name}/publish`,
511+
name: `${ctx.skill.name}/${ctx.configuration?.name}/publish`,
434512
body: `Running \`npm publish ${args.join(" ")}\``,
435513
});
436514
const id = guid();
437-
const channels = push.repo?.channels?.map(c => c.name);
438-
const header = `*${push.repo.owner}/${push.repo.name}/${
439-
push.branch
440-
}* at <${push.after.url}|\`${push.after.sha.slice(0, 7)}\`>\n`;
515+
const channels = repo?.channels?.map(c => c.name);
516+
const header = `*${repo.owner}/${repo.name}/${branch}* at <${
517+
commit.url
518+
}|\`${commit.sha.slice(0, 7)}\`>\n`;
441519
await ctx.message.send(
442520
slack.progressMessage(
443521
"npm publish",
@@ -490,16 +568,14 @@ Failed to publish ${pj.name}
490568
{ id },
491569
);
492570
return status.failure(
493-
`\`npm publish ${args.join(" ")}\` failed on [${
494-
push.repo.owner
495-
}/${push.repo.name}/${push.after.sha.slice(0, 7)}](${
496-
push.after.url
497-
})`,
571+
`\`npm publish ${args.join(" ")}\` failed on [${repo.owner}/${
572+
repo.name
573+
}/${commit.sha.slice(0, 7)}](${commit.url})`,
498574
);
499575
}
500576

501577
const tags = cfg.tag || [];
502-
if (push.branch === push.repo.defaultBranch) {
578+
if (branch === repo.defaultBranch) {
503579
tags.push("next");
504580
}
505581
for (const tag of tags) {
@@ -543,15 +619,16 @@ Successfully published ${pj.name} with version ${pj.version}
543619
{ id },
544620
);
545621
return status.success(
546-
`\`npm publish ${args.join(" ")}\` passed on [${push.repo.owner}/${
547-
push.repo.name
548-
}/${push.after.sha.slice(0, 7)}](${push.after.url})`,
622+
`\`npm publish ${args.join(" ")}\` passed on [${repo.owner}/${
623+
repo.name
624+
}/${commit.sha.slice(0, 7)}](${commit.url})`,
549625
);
550626
},
551627
};
552628

553629
export const handler: EventHandler<
554-
subscription.types.OnPushSubscription,
630+
| subscription.types.OnPushSubscription
631+
| subscription.types.OnTagSubscription,
555632
Configuration
556633
> = async ctx =>
557634
runSteps({

0 commit comments

Comments
 (0)