Skip to content

Commit

Permalink
Merge pull request #267 from balena-io/renovate/balena-lint-7.x
Browse files Browse the repository at this point in the history
Update dependency @balena/lint to v7
  • Loading branch information
klutchell authored Nov 24, 2023
2 parents e2cda87 + a17f6a6 commit 58eecda
Show file tree
Hide file tree
Showing 9 changed files with 1,957 additions and 359 deletions.
2,210 changes: 1,900 additions & 310 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@
"action:close-pr": "npm run action dotenv_config_path=events/close-pr/.env",
"action:push": "npm run action dotenv_config_path=events/push/.env",
"build": "npm run clean && tsc",
"lint": "balena-lint --typescript src tests",
"lint-fix": "balena-lint --typescript --fix src tests",
"lint": "balena-lint src tests",
"lint-fix": "balena-lint --fix src tests",
"test:fast": "TEST=1 mocha --config tests/.mocharc.fast.js",
"prepack": "npm run build",
"test": "npm run clean && npm run lint && npm run test:build && TEST=1 mocha --config tests/.mocharc.js",
"test:build": "tsc --project tsconfig.json && npm run testitems:copy",
"testitems:copy": "cp -r tests/data build/tests/"
},
"devDependencies": {
"@balena/lint": "^6.2.0",
"@balena/lint": "^7.0.0",
"@types/chai": "^4.3.0",
"@types/chai-as-promised": "^7.1.5",
"@types/chai-like": "^1.1.1",
Expand Down
57 changes: 29 additions & 28 deletions src/action.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
import * as core from "@actions/core";
import { context as contextType } from "@actions/github";
import * as core from '@actions/core';
import { context as contextType } from '@actions/github';

import * as versionbot from "./versionbot-utils";
import * as balena from "./balena-utils";
import * as git from "./git";
import * as github from "./github-utils";
import { Inputs, RepoContext } from "./types";
import * as versionbot from './versionbot-utils';
import * as balena from './balena-utils';
import * as git from './git';
import * as github from './github-utils';
import { Inputs, RepoContext } from './types';

const ALLOWED_EVENTS = ["pull_request_target", "pull_request"];
const ALLOWED_EVENTS = ['pull_request_target', 'pull_request'];

export async function run(
context: typeof contextType,
inputs: Inputs
inputs: Inputs,
): Promise<void> {
// If the payload does not have a repository object then fail early (the events we are interested in always have this)
if (!context.payload.repository) {
throw new Error("Workflow payload was missing repository object");
throw new Error('Workflow payload was missing repository object');
}

// Get the master branch so we can infer intent
const target =
inputs.defaultBranch || context.payload.repository.master_branch;
// Collect repo context
const repoContext: RepoContext = {
owner: context.payload.repository.owner.login || "",
name: context.payload.repository.name || "",
owner: context.payload.repository.owner.login || '',
name: context.payload.repository.name || '',
sha: context.payload.pull_request?.head.sha || context.sha,
pullRequest: context.payload.pull_request
? {
id: context.payload.pull_request.id,
// eslint-disable-next-line id-denylist
number: context.payload.pull_request.number,
merged: context.payload.pull_request.merged,
}
Expand All @@ -40,7 +41,7 @@ export async function run(
// Version of release built
let rawVersion: string | null = null;

if (context.payload.action === "closed") {
if (context.payload.action === 'closed') {
// If a pull request was closed and merged then just finalize the release!
if (repoContext.pullRequest && repoContext.pullRequest.merged) {
// Get the previous release built
Expand All @@ -50,10 +51,10 @@ export async function run(
});
if (!previousRelease) {
throw new Error(
"Action reached point of finalizing a release but did not find one"
'Action reached point of finalizing a release but did not find one',
);
} else if (previousRelease.isFinal) {
core.info("Release is already finalized so skipping.");
core.info('Release is already finalized so skipping.');
return;
}

Expand All @@ -63,25 +64,25 @@ export async function run(
rawVersion = await balena.getReleaseVersion(previousRelease.id);

// set outputs on finalize
core.setOutput("version", rawVersion);
core.setOutput("release_id", previousRelease.id);
core.setOutput('version', rawVersion);
core.setOutput('release_id', previousRelease.id);

if (inputs.createTag && rawVersion) {
try {
await github.createTag(repoContext, rawVersion);
} catch (e: any) {
if (e.message !== "Reference already exists") {
if (e.message !== 'Reference already exists') {
throw e;
}
core.info("Git reference already exists.");
core.info('Git reference already exists.');
return;
}
}

return;
} else {
// If the pull request was closed but not merged then do nothing
core.info("Pull request was closed but not merged, nothing to do.");
core.info('Pull request was closed but not merged, nothing to do.');
return;
}
}
Expand All @@ -98,9 +99,9 @@ export async function run(

// If we are pushing directly to the target branch then just build a release without draft flag
if (
context.eventName === "push" &&
context.eventName === 'push' &&
(context.ref === `refs/heads/${target}` ||
context.ref.startsWith("refs/tags/"))
context.ref.startsWith('refs/tags/'))
) {
// TODO: Update this to use ref_type & ref_name once that becomes available
// See: https://github.com/actions/toolkit/pull/935/files
Expand All @@ -115,9 +116,9 @@ export async function run(
};
} else if (!ALLOWED_EVENTS.includes(context.eventName)) {
// Make sure the only events now are ones we expect
if (context.eventName === "push") {
if (context.eventName === 'push') {
throw new Error(
`Push workflow only works with ${target} branch. Event tried pushing to: ${context.ref}`
`Push workflow only works with ${target} branch. Event tried pushing to: ${context.ref}`,
);
}
throw new Error(`Unsure how to proceed with event: ${context.eventName}`);
Expand Down Expand Up @@ -147,17 +148,17 @@ export async function run(

// Now that we built a release set the expected outputs
rawVersion = await balena.getReleaseVersion(releaseId);
core.setOutput("version", rawVersion);
core.setOutput("release_id", releaseId);
core.setOutput('version', rawVersion);
core.setOutput('release_id', releaseId);

if (inputs.createTag && buildOptions.draft === false) {
try {
await github.createTag(repoContext, rawVersion);
} catch (e: any) {
if (e.message !== "Reference already exists") {
if (e.message !== 'Reference already exists') {
throw e;
}
core.info("Git reference already exists.");
core.info('Git reference already exists.');
}
}
}
3 changes: 2 additions & 1 deletion src/balena-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const DEFAULT_BUILD_OPTIONS: Partial<BuildOptions> = {
noCache: false,
multiDockerignore: false,
debug: false,
note: "",
note: '',
};

let sdk: ReturnType<typeof balena.getSdk> | null = null;
Expand Down Expand Up @@ -270,6 +270,7 @@ export async function finalize(releaseId: number): Promise<void> {

function stripAnsi(logLine: string): string {
return logLine.replace(
// eslint-disable-next-line no-control-regex -- We need to check for ansi control codes specifically
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
'',
);
Expand Down
6 changes: 4 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ const inputs: Inputs = {
githubToken: core.getInput('github_token', { required: false }),
layerCache: core.getBooleanInput('layer_cache', { required: false }),
defaultBranch: core.getInput('default_branch', { required: false }),
multiDockerignore: core.getBooleanInput('multi_dockerignore', { required: false }),
multiDockerignore: core.getBooleanInput('multi_dockerignore', {
required: false,
}),
debug: core.getBooleanInput('debug', { required: false }),
note: core.getInput('note', { required: false }),
};

(async () => {
void (async () => {
try {
// Initialize github client
githubUtils.init(inputs.githubToken);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type RepoContext = {

export type PullRequest = {
id: number;
// eslint-disable-next-line id-denylist
number: number;
merged: boolean;
};
Expand Down
28 changes: 15 additions & 13 deletions tests/src/action.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ describe('src/action', () => {

it('exits early on missing data', async () => {
await expect(
// @ts-expect-error
// @ts-expect-error: not assignable to parameter of type 'Context'
action.run({ payload: {} }, inputs),
).to.be.rejectedWith('Workflow payload was missing repository object');
});

it('errors on unknown workflow', async () => {
await expect(
action.run(
// @ts-expect-error
// @ts-expect-error: not assignable to parameter of type 'Context'
{ ...context, eventName: 'pull_request_review_comment' },
inputs,
),
Expand All @@ -102,14 +102,14 @@ describe('src/action', () => {
});

it('gets versionbot branch', async () => {
// @ts-expect-error
// @ts-expect-error: not assignable to parameter of type 'Context'
await action.run(context, { ...inputs, versionbot: true });
expect(vbBranchStub).to.have.been.calledOnce;
expect(checkoutStub).to.have.been.calledWith('vb-branch-123');
});

it('set correct outputs', async () => {
// @ts-expect-error
// @ts-expect-error: not assignable to parameter of type 'Context'
await action.run(context, { ...inputs, versionbot: true });
expect(setOutputStub).to.have.been.calledTwice;
expect(setOutputStub.getCall(0)).to.have.been.calledWith(
Expand All @@ -123,15 +123,15 @@ describe('src/action', () => {
});

it('creates a tag', async () => {
// @ts-expect-error
// @ts-expect-error: not assignable to parameter of type 'Context'
await action.run(context, { ...inputs, createTag: true });
expect(createTagStub).to.have.been.called;
// Check that create tag value was passed
expect(createTagStub.lastCall.args[1]).to.equal('v0.5.6');
});

it('passes correct build parameters to balena-utils', async () => {
// @ts-expect-error
// @ts-expect-error: not assignable to parameter of type 'Context'
await action.run(context, { ...inputs, layerCache: false });
// Check that the right parameters were passed
expect(pushStub.lastCall.firstArg).to.equal('my-org/my-fleet');
Expand Down Expand Up @@ -173,6 +173,7 @@ describe('src/action', () => {
},
pull_request: {
id: 4423422,
// eslint-disable-next-line id-denylist
number: 44,
merged: false,
head: {
Expand All @@ -181,7 +182,7 @@ describe('src/action', () => {
},
},
};
// @ts-expect-error
// @ts-expect-error: not assignable to parameter of type 'Context'
await action.run(prContext, { ...inputs, createTag: true });
// Check that the last arg (buildOptions) does not contain draft: true
expect(pushStub.lastCall.lastArg).to.deep.equal({
Expand Down Expand Up @@ -210,6 +211,7 @@ describe('src/action', () => {
},
pull_request: {
id: 4423422,
// eslint-disable-next-line id-denylist
number: 44,
merged: true,
head: {
Expand All @@ -224,7 +226,7 @@ describe('src/action', () => {
id: 123456,
isFinal: false,
});
// @ts-expect-error
// @ts-expect-error: not assignable to parameter of type 'Context'
await action.run(prContext, { ...inputs, createTag: true });
// Check that the release was finalized
expect(finalizeStub).to.have.been.calledWith(123456);
Expand All @@ -237,7 +239,7 @@ describe('src/action', () => {

describe('Main workflow', () => {
it('builds a finalized release', async () => {
// @ts-expect-error
// @ts-expect-error: not assignable to parameter of type 'Context'
await action.run(context, { ...inputs, createTag: true });
// Check that the last arg (buildOptions) does not contain draft: true
expect(pushStub.lastCall.lastArg).to.deep.equal({
Expand All @@ -261,7 +263,7 @@ describe('src/action', () => {
defaultBranch: 'target_branch_123',
};
await action.run(
// @ts-expect-error
// @ts-expect-error: not assignable to parameter of type 'Context'
{ ...context, ref: 'refs/heads/target_branch_123' },
{ ...customInputs, createTag: true },
);
Expand All @@ -288,13 +290,13 @@ describe('src/action', () => {
};
let e: Error | null = null;
try {
// @ts-expect-error
// @ts-expect-error: not assignable to parameter of type 'Context'
await action.run(context, { ...customInputs, createTag: true });
} catch (err: any) {
e = err;
}
// @ts-expect-error
expect(e.message).to.equal(

expect(e?.message).to.equal(
'Push workflow only works with branch123 branch. Event tried pushing to: refs/heads/main',
);
});
Expand Down
4 changes: 2 additions & 2 deletions tests/src/github-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('src/github-utils', () => {
const requestStub = stub();
before(() => {
getOctokitStub.returns({
// @ts-expect-error
// @ts-expect-error: missing properties from type 'RequestInterface<object>'
request: requestStub,
});
githubUtils.init('123'); // initilize module so interal client uses requestStub
Expand Down Expand Up @@ -87,7 +87,7 @@ describe('src/github-utils', () => {
getOctokitStub.returns({
rest: {
git: {
// @ts-expect-error
// @ts-expect-error: 'SinonStub<any[], any>' is missing properties from git.createRef
createRef: createRefStub,
},
},
Expand Down
1 change: 1 addition & 0 deletions tests/src/versionbot-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('src/versionbot-utils', () => {
sha: '123',
pullRequest: {
id: prId,
// eslint-disable-next-line id-denylist
number: prNumber,
merged: false,
},
Expand Down

0 comments on commit 58eecda

Please sign in to comment.