Skip to content

Commit

Permalink
Merge branch 'babel'
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Apr 4, 2024
2 parents f498354 + 4c63767 commit 759764a
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 47 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/npm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Node.js CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2 # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it

- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: "20"

- name: Install dependencies
run: npm ci # If you have a package-lock.json, use `npm ci` instead of `npm install` for faster installation

- name: Run tests
run: npm run lint

- name: Run tests
run: npm run test

6 changes: 3 additions & 3 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ export default function getCLI(context) {
.option('-p, --platform <platform...>', 'Platforms (multiple values allowed)')
.option('-f, --force-latest', 'Force the "latest" tag on the release')
.option('-v, --verbose', 'Print more info')
.action(function () {
const opts = context.processOptions(this, ['preset']);
.action(async function () {
const opts = context.processOptions(this, ['preset', 'repo']);
opts.platform = opts.platform || ['linux/arm64'];
const github = new Github({ context });
const latestRelease = github.getLatestRelease();
const latestRelease = await github.getLatestReleaseTag();
const command = docker.getDockerCommand({ ...opts, latestRelease });
context.log(command);
if (!opts.dryRun) {
Expand Down
18 changes: 13 additions & 5 deletions src/cli.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { spawnSync } from 'child_process';
import { runShellCommand } from './utils';

beforeEach(() => {
process.env.GITHUB_REPOSITORY = 'apache/superset';
});

afterEach(() => {
delete process.env.GITHUB_REPOSITORY;
});

describe('CLI Test', () => {
test.each([
['./src/supersetbot', ['docker', '--preset', 'dev', '--dry-run'], '--target dev'],
['./src/supersetbot', ['docker', '--dry-run'], '--target lean'],
])('returns %s for release %s', (command, arg, contains) => {
const result = spawnSync(command, arg);
['./src/supersetbot docker --preset dev --dry-run', '--target dev'],
['./src/supersetbot docker --dry-run', ' --target lean'],
])('returns %s for release %s', async (command, contains) => {
const result = await runShellCommand({ command, exitOnError: false });
const output = result.stdout.toString();
expect(output).toContain(contains);
});
Expand Down
3 changes: 1 addition & 2 deletions src/docker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { spawnSync } from 'child_process';
import GitHub from './github.js';

const REPO = 'apache/superset';
const CACHE_REPO = `${REPO}-cache`;
Expand Down Expand Up @@ -43,7 +42,7 @@ export function getDockerTags({
const tags = new Set();
const tagChunks = [];

const currentRelease = buildContext === "release" ? buildContextRef : null;
const currentRelease = buildContext === 'release' ? buildContextRef : null;
const isLatest = latestRelease === currentRelease;

if (preset !== 'lean') {
Expand Down
14 changes: 7 additions & 7 deletions src/docker.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { jest } from '@jest/globals';
import * as dockerUtils from './docker.js';

const SHA = '22e7c602b9aa321ec7e0df4bb0033048664dcdf0';
Expand All @@ -6,6 +7,8 @@ const OLD_REL = '2.1.0';
const NEW_REL = '2.1.1';
const REPO = 'apache/superset';

jest.mock('./github.js', () => jest.fn().mockImplementation(() => NEW_REL));

beforeEach(() => {
process.env.TEST_ENV = 'true';
});
Expand Down Expand Up @@ -186,7 +189,7 @@ describe('getDockerTags', () => {

])('returns expected tags', (preset, platforms, sha, buildContext, buildContextRef, expectedTags) => {
const tags = dockerUtils.getDockerTags({
preset, platforms, sha, buildContext, buildContextRef,
preset, platforms, sha, buildContext, buildContextRef, latestRelease: NEW_REL,
});
expect(tags).toEqual(expect.arrayContaining(expectedTags));
});
Expand All @@ -197,16 +200,14 @@ describe('getDockerCommand', () => {
[
'lean',
['linux/amd64'],
true,
SHA,
'push',
'master',
['--push', `-t ${REPO}:master `],
[`-t ${REPO}:master `],
],
[
'dev',
['linux/amd64'],
false,
SHA,
'push',
'master',
Expand All @@ -216,15 +217,14 @@ describe('getDockerCommand', () => {
[
'lean',
['linux/arm64', 'linux/amd64'],
true,
SHA,
'push',
'master',
['--platform linux/arm64,linux/amd64'],
],
])('returns expected docker command', (preset, platform, isAuthenticated, sha, buildContext, buildContextRef, contains) => {
])('returns expected docker command', (preset, platform, sha, buildContext, buildContextRef, contains) => {
const cmd = dockerUtils.getDockerCommand({
preset, platform, isAuthenticated, sha, buildContext, buildContextRef,
preset, platform, sha, buildContext, buildContextRef, latestRelease: NEW_REL,
});
contains.forEach((expectedSubstring) => {
expect(cmd).toContain(expectedSubstring);
Expand Down
20 changes: 15 additions & 5 deletions src/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,29 @@ class Github {
return { repo, owner };
}

async getLatestReleaseTag() {
const tags = await this.octokit.rest.repos.listTags({
async getAllTags() {
const options = this.octokit.rest.repos.listTags.endpoint.merge({
...this.unPackRepo(),
per_page: 100,
});

const tags = await this.octokit.paginate(options);

return tags;
}

async getLatestReleaseTag() {
const tags = await this.getAllTags();
const tagNames = tags.map((tag) => tag.name);

// Simple SemVer regex
const simpleSemverRegex = /^\d+\.\d+\.\d+$/;
// Date-like pattern regex to exclude (e.g., 2020.01.01)
const dateLikeRegex = /^\d{4}\.\d{2}\.\d{2}$/;

const validTags = tags.data.filter(
(tag) => simpleSemverRegex.test(tag.name) && !dateLikeRegex.test(tag.name),
).map((tag) => tag.name);
const validTags = tagNames.filter(
(tag) => simpleSemverRegex.test(tag) && !dateLikeRegex.test(tag),
);

// Sort tags in descending order (latest first)
validTags.sort((a, b) => {
Expand Down
1 change: 1 addition & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { jest } from '@jest/globals';
import Context from './context.js';
import Github from './github.js';

Expand Down
2 changes: 1 addition & 1 deletion src/supersetbot
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import getCLI from './cli.js';
import Context from './context.js';
import Github from './github.js';
import { runCommandFromGithubAction } from './utils.js';
import { runCommandFromGithubAction } from './index.js';

const envContext = new Context('CLI');
const cli = getCLI(envContext);
Expand Down
31 changes: 15 additions & 16 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,26 +113,25 @@ export function shuffleArray(originalArray) {
}

export function parsePinnedRequirements(requirements) {
const lines = requirements.split('\n');
const depsObject = {};
const lines = requirements
.split('\n')
.map((line) => line.trim().toLowerCase())
.filter((line) => !!line);

const depsObject = {};
let currentDep = null;

lines.forEach((line) => {
if (line.startsWith('# via')) {
const via = line.replace('# via', '').trim();
const vias = via.split(',').map((v) => v.trim());
vias.forEach((viaItem) => {
if (!depsObject[viaItem]) {
depsObject[viaItem] = [];
}
if (currentDep && !depsObject[viaItem].includes(currentDep)) {
depsObject[viaItem].push(currentDep);
const depMatch = line.match(/^(.+)==/);
if (depMatch) {
currentDep = depMatch[1].trim();
} else {
const viaLib = line.replace('# via', '').trim().replace('#', '').trim();
if (viaLib) {
if (!depsObject[viaLib]) {
depsObject[viaLib] = [];
}
});
} else if (line.trim()) {
const depMatch = line.match(/^(.+)==/);
if (depMatch) {
currentDep = depMatch[1].trim();
depsObject[viaLib].push(currentDep);
}
}
});
Expand Down
11 changes: 3 additions & 8 deletions src/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import { parsePinnedRequirements, mergeParsedRequirements } from './utils.js';

describe('parsePinnedRequirements', () => {
it('parses single dependency correctly', () => {
const requirements = `
const requirements = `\
alembic==1.13.1
# via flask-migrate
`;
# via flask-migrate`;
expect(parsePinnedRequirements(requirements)).toEqual({
'flask-migrate': ['alembic'],
});
Expand Down Expand Up @@ -38,12 +37,8 @@ describe('parsePinnedRequirements', () => {

it('ignores lines without dependencies or via comments', () => {
const requirements = `
# This is a comment
alembic==1.13.1
# via flask-migrate
# Another comment
`;
# via flask-migrate`;
expect(parsePinnedRequirements(requirements)).toEqual({
'flask-migrate': ['alembic'],
});
Expand Down

0 comments on commit 759764a

Please sign in to comment.