feat: move supersetbot out of repo (#27647)
This commit is contained in:
parent
9022f5c519
commit
8e3cecda9f
|
|
@ -1,11 +1,36 @@
|
|||
name: 'Setup supersetbot'
|
||||
description: 'Sets up supersetbot npm lib from the repo'
|
||||
description: 'Sets up supersetbot npm lib from the repo or npm'
|
||||
inputs:
|
||||
from-npm:
|
||||
description: 'Install from npm instead of local setup'
|
||||
required: false
|
||||
default: 'true' # Defaults to using the local setup
|
||||
runs:
|
||||
using: 'composite'
|
||||
steps:
|
||||
- name: Install dependencies
|
||||
|
||||
- name: Setup Node Env
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '20'
|
||||
|
||||
- name: Install supersetbot from npm
|
||||
if: ${{ inputs.from-npm == 'true' }}
|
||||
shell: bash
|
||||
run: npm install -g supersetbot
|
||||
|
||||
- name: "Checkout ${{ github.ref }} ( ${{ github.sha }} )"
|
||||
if: ${{ inputs.from-npm == 'false' }}
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
repository: apache-superset/supersetbot
|
||||
path: supersetbot
|
||||
|
||||
- name: Setup supersetbot from repo
|
||||
if: ${{ inputs.from-npm == 'false' }}
|
||||
shell: bash
|
||||
working-directory: supersetbot
|
||||
run: |
|
||||
cd .github/supersetbot
|
||||
npm install
|
||||
npm link
|
||||
# simple trick to install globally with dependencies
|
||||
npm pack
|
||||
npm install -g ./supersetbot*.tgz
|
||||
|
|
|
|||
|
|
@ -1,22 +0,0 @@
|
|||
{
|
||||
"extends": "airbnb-base",
|
||||
"rules": {
|
||||
"import/extensions": 0,
|
||||
"import/prefer-default-export": 0,
|
||||
"func-names": 0,
|
||||
"no-console": 0,
|
||||
"class-methods-use-this": 0
|
||||
},
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2020,
|
||||
"sourceType": "module"
|
||||
},
|
||||
"parserOptions": {
|
||||
"ecmaVersion": "latest",
|
||||
"sourceType": "module",
|
||||
"requireConfigFile": false
|
||||
},
|
||||
"env": {
|
||||
"jest": true
|
||||
}
|
||||
}
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
# supersetbot
|
||||
|
||||
supersetbot is a utility bot that can be used to help around GitHub, CI and beyond.
|
||||
|
||||
The bot can be used as a local CLI OR, for a subset of fitted use cases, can be invoked directly
|
||||
from GitHub comments.
|
||||
|
||||
Because it's its own npm app, it can be tested/deployed/used in isolation from the rest of
|
||||
Superset, and take on some of the complexity from GitHub actions and onto a nifty
|
||||
utility that can be used in different contexts.
|
||||
|
||||
## Features
|
||||
|
||||
```bash
|
||||
$ use nvm 20
|
||||
$ npm i -g supersetbot
|
||||
$ supersetbot
|
||||
Usage: supersetbot [options] [command]
|
||||
|
||||
Options:
|
||||
-v, --verbose Output extra debugging information
|
||||
-r, --repo <repo> The GitHub repo to use (ie: "apache/superset")
|
||||
-d, --dry-run Run the command in dry-run mode
|
||||
-a, --actor <actor> The actor
|
||||
-h, --help display help for command
|
||||
|
||||
Commands:
|
||||
label [options] <label> Add a label to an issue or PR
|
||||
unlabel [options] <label> Remove a label from an issue or PR
|
||||
release-label-pr [options] <prId> Figure out first release for PR and label it
|
||||
version Prints supersetbot's version number
|
||||
release-label-prs [options] Given a set of PRs, auto-release label them
|
||||
release-label [options] <release> Figure out first release for PR and label it
|
||||
orglabel [options] Add an org label based on the author
|
||||
docker [options] Generates/run docker build commands use in CI
|
||||
help [command] display help for command
|
||||
```
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
export default {
|
||||
transform: {
|
||||
},
|
||||
testEnvironment: 'node',
|
||||
moduleNameMapper: {
|
||||
'^(\\.{1,2}/.*)\\.js$': '$1',
|
||||
},
|
||||
};
|
||||
File diff suppressed because it is too large
Load Diff
|
|
@ -1,36 +0,0 @@
|
|||
{
|
||||
"name": "supersetbot",
|
||||
"version": "0.4.2",
|
||||
"description": "A bot for the Superset GitHub repo",
|
||||
"type": "module",
|
||||
"main": "src/index.js",
|
||||
"scripts": {
|
||||
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
|
||||
"eslint": "eslint",
|
||||
"supersetbot": "supersetbot"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@octokit/plugin-throttling": "^8.1.3",
|
||||
"@octokit/rest": "^20.0.2",
|
||||
"commander": "^11.0.0",
|
||||
"semver": "^7.6.0",
|
||||
"simple-git": "^3.22.0",
|
||||
"string-argv": "^0.3.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@jest/globals": "^29.7.0",
|
||||
"eslint": "^8.56.0",
|
||||
"eslint-config-airbnb": "^19.0.4",
|
||||
"eslint-plugin-import": "^2.29.1",
|
||||
"eslint-plugin-jsx-a11y": "^6.8.0",
|
||||
"eslint-plugin-react": "^7.33.2",
|
||||
"eslint-plugin-react-hooks": "^4.6.0",
|
||||
"jest": "^29.7.0"
|
||||
},
|
||||
"bin": {
|
||||
"supersetbot": "./src/supersetbot"
|
||||
}
|
||||
}
|
||||
|
|
@ -1,175 +0,0 @@
|
|||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
import { Command, Option } from 'commander';
|
||||
|
||||
import * as docker from './docker.js';
|
||||
import * as utils from './utils.js';
|
||||
import Github from './github.js';
|
||||
import Git from './git.js';
|
||||
|
||||
export default function getCLI(context) {
|
||||
const program = new Command();
|
||||
|
||||
// Some reusable options
|
||||
const issueOption = new Option('-i, --issue <issue>', 'The issue number', process.env.GITHUB_ISSUE_NUMBER);
|
||||
const excludeCherriesOption = new Option('-c, --exclude-cherries', 'Generate cherry labels point to each release where the PR has been cherried');
|
||||
|
||||
// Setting up top-level CLI options
|
||||
program
|
||||
.option('-v, --verbose', 'Output extra debugging information')
|
||||
.option('-r, --repo <repo>', 'The GitHub repo to use (ie: "apache/superset")', process.env.GITHUB_REPOSITORY)
|
||||
.option('-d, --dry-run', 'Run the command in dry-run mode')
|
||||
.option('-a, --actor <actor>', 'The actor', process.env.GITHUB_ACTOR);
|
||||
|
||||
program.command('label <label>')
|
||||
.description('Add a label to an issue or PR')
|
||||
.addOption(issueOption)
|
||||
.action(async function (label) {
|
||||
const opts = context.processOptions(this, ['issue', 'repo']);
|
||||
const github = new Github({ context, issue: opts.issue });
|
||||
await github.label(opts.issue, label, context, opts.actor, opts.verbose, opts.dryRun);
|
||||
});
|
||||
|
||||
program.command('unlabel <label>')
|
||||
.description('Remove a label from an issue or PR')
|
||||
.addOption(issueOption)
|
||||
.action(async function (label) {
|
||||
const opts = context.processOptions(this, ['issue', 'repo']);
|
||||
const github = new Github({ context, issueNumber: opts.issue });
|
||||
await github.unlabel(opts.issue, label, context, opts.actor, opts.verbose, opts.dryRun);
|
||||
});
|
||||
|
||||
program.command('release-label-pr <prId>')
|
||||
.description('Figure out first release for PR and label it')
|
||||
.addOption(excludeCherriesOption)
|
||||
.action(async function (prId) {
|
||||
const opts = context.processOptions(this, ['repo']);
|
||||
const git = new Git(context);
|
||||
await git.loadReleases();
|
||||
|
||||
let wrapped = context.commandWrapper({
|
||||
func: git.getReleaseLabels,
|
||||
verbose: opts.verbose,
|
||||
});
|
||||
const labels = await wrapped(parseInt(prId, 10), opts.verbose, opts.excludeCherries);
|
||||
const github = new Github({ context, issueNumber: opts.issue });
|
||||
wrapped = context.commandWrapper({
|
||||
func: github.syncLabels,
|
||||
verbose: opts.verbose,
|
||||
});
|
||||
await wrapped({labels, prId, actor: opts.actor, verbose: opts.verbose, dryRun: opts.dryRun});
|
||||
});
|
||||
|
||||
program.command('version')
|
||||
.description("Prints supersetbot's version number")
|
||||
.action(async () => {
|
||||
const version = await utils.currentPackageVersion();
|
||||
context.log(version);
|
||||
});
|
||||
|
||||
if (context.source === 'CLI') {
|
||||
program.command('release-label-prs')
|
||||
.description('Given a set of PRs, auto-release label them')
|
||||
.option('-s, --search <search>', 'extra search string to append using the GitHub mini-language')
|
||||
.option('-p, --pages <pages>', 'the number of pages (100 per page) to fetch and process', 10)
|
||||
.action(async function () {
|
||||
const opts = context.processOptions(this, ['repo']);
|
||||
|
||||
const github = new Github({ context, issueNumber: opts.issue });
|
||||
const prs = await github.searchMergedPRs({
|
||||
query: opts.search,
|
||||
onlyUnlabeled: true,
|
||||
verbose: opts.verbose,
|
||||
pages: opts.pages,
|
||||
});
|
||||
const prIdLabelMap = new Map(prs.map((pr) => [pr.number, pr.labels]));
|
||||
const git = new Git(context);
|
||||
await git.loadReleases();
|
||||
|
||||
const prsPromises = prs.map(async (pr) => {
|
||||
const labels = await git.getReleaseLabels(pr.number, opts.verbose);
|
||||
return { prId: pr.number, labels };
|
||||
});
|
||||
const prsTargetLabel = await Promise.all(prsPromises);
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const { prId, labels } of prsTargetLabel) {
|
||||
// Running sequentially to avoid rate limiting
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await github.syncLabels({
|
||||
labels,
|
||||
existingLabels: prIdLabelMap.get(prId).map(l => l.name),
|
||||
prId,
|
||||
...opts,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
program.command('release-label <release>')
|
||||
.description('Figure out first release for PR and label it')
|
||||
.addOption(excludeCherriesOption)
|
||||
.action(async function (release) {
|
||||
const opts = context.processOptions(this, ['repo']);
|
||||
const git = new Git(context);
|
||||
await git.loadReleases();
|
||||
const prs = await git.getPRsToSync(release, opts.verbose, opts.excludeCherries);
|
||||
|
||||
const github = new Github({ context });
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const { prId, labels } of prs) {
|
||||
// Running sequentially to avoid rate limiting
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await github.syncLabels({
|
||||
prId,
|
||||
labels,
|
||||
...opts,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
program.command('orglabel')
|
||||
.description('Add an org label based on the author')
|
||||
.addOption(issueOption)
|
||||
.action(async function () {
|
||||
const opts = context.processOptions(this, ['issue', 'repo']);
|
||||
const github = new Github({ context, issueNumber: opts.issue });
|
||||
await github.assignOrgLabel(opts.issue, opts.verbose, opts.dryRun);
|
||||
});
|
||||
|
||||
|
||||
program.command('docker')
|
||||
.description('Generates/run docker build commands use in CI')
|
||||
.option('-t, --preset <preset>', 'Build preset', /^(lean|dev|dockerize|websocket|py310|ci)$/i, 'lean')
|
||||
.option('-c, --context <context>', 'Build context', /^(push|pull_request|release)$/i, 'local')
|
||||
.option('-r, --context-ref <ref>', 'Reference to the PR, release, or branch')
|
||||
.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']);
|
||||
opts.platform = opts.platform || ['linux/arm64'];
|
||||
const cmd = docker.getDockerCommand({ ...opts });
|
||||
context.log(cmd);
|
||||
if (!opts.dryRun) {
|
||||
utils.runShellCommand(cmd, false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return program;
|
||||
}
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
import { spawnSync } from 'child_process';
|
||||
|
||||
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);
|
||||
const output = result.stdout.toString();
|
||||
expect(result.stdout.toString()).toContain(contains);
|
||||
});
|
||||
});
|
||||
|
|
@ -1,152 +0,0 @@
|
|||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
import { parseArgsStringToArgv } from 'string-argv';
|
||||
|
||||
class Context {
|
||||
constructor(source) {
|
||||
this.hasErrors = false;
|
||||
this.source = source;
|
||||
this.options = {};
|
||||
this.errorLogs = [];
|
||||
this.logs = [];
|
||||
this.repo = null;
|
||||
this.optToEnvMap = {
|
||||
issue: 'GITHUB_ISSUE_NUMBER',
|
||||
repo: 'GITHUB_REPOSITORY',
|
||||
};
|
||||
}
|
||||
|
||||
requireOption(optionName, options) {
|
||||
const optionValue = options[optionName];
|
||||
if (optionValue === undefined || optionValue === null) {
|
||||
this.logError(`option [${optionName}] is required`);
|
||||
this.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
parseArgs(s) {
|
||||
return parseArgsStringToArgv(s);
|
||||
}
|
||||
|
||||
requireOptions(optionNames, options) {
|
||||
optionNames.forEach((optionName) => {
|
||||
this.requireOption(optionName, options);
|
||||
});
|
||||
}
|
||||
|
||||
processOptions(command, requiredOptions = []) {
|
||||
const raw = command.parent?.rawArgs;
|
||||
this.command = '???';
|
||||
if (raw) {
|
||||
this.command = raw.map((s) => (s.includes(' ') ? `"${s}"` : s)).join(' ').replace('node ', '');
|
||||
}
|
||||
this.options = { ...command.opts(), ...command.parent.opts() };
|
||||
|
||||
// Runtime defaults for unit tests since commanders can't receive callables as default
|
||||
Object.entries(this.optToEnvMap).forEach(([k, v]) => {
|
||||
if (!this.options[k]) {
|
||||
this.options[k] = process.env[v];
|
||||
}
|
||||
});
|
||||
this.requireOptions(requiredOptions, this.options);
|
||||
this.issueNumber = this.options.issue;
|
||||
|
||||
if (this.source === 'GHA') {
|
||||
this.options.actor = process.env.GITHUB_ACTOR || 'UNKNOWN';
|
||||
this.options.repo = process.env.GITHUB_REPOSITORY;
|
||||
}
|
||||
this.repo = this.options.repo;
|
||||
|
||||
return this.options;
|
||||
}
|
||||
|
||||
log(msg) {
|
||||
console.log(msg);
|
||||
this.logs = [...this.logs, msg];
|
||||
}
|
||||
|
||||
logSuccess(msg) {
|
||||
const augMsg = `🟢 SUCCESS: ${msg}`;
|
||||
console.log(augMsg);
|
||||
this.logs.push(augMsg);
|
||||
}
|
||||
|
||||
logError(msg) {
|
||||
this.hasErrors = true;
|
||||
const augMsg = `🔴 ERROR: ${msg}`;
|
||||
console.error(augMsg);
|
||||
this.errorLogs.push(augMsg);
|
||||
}
|
||||
|
||||
exit(code = 0) {
|
||||
this.onDone();
|
||||
process.exit(code);
|
||||
}
|
||||
|
||||
commandWrapper({
|
||||
func, successMsg, errorMsg = null, verbose = false, dryRun = false,
|
||||
}) {
|
||||
return async (...args) => {
|
||||
let resp;
|
||||
let hasError = false;
|
||||
|
||||
try {
|
||||
if (!dryRun) {
|
||||
resp = await func(...args);
|
||||
}
|
||||
if (verbose && resp) {
|
||||
console.log(resp);
|
||||
}
|
||||
} catch (error) {
|
||||
hasError = true;
|
||||
if (errorMsg) {
|
||||
this.logError(errorMsg);
|
||||
} else {
|
||||
this.logError(error);
|
||||
}
|
||||
throw (error);
|
||||
}
|
||||
if (successMsg && !hasError) {
|
||||
this.logSuccess(successMsg);
|
||||
}
|
||||
return resp;
|
||||
};
|
||||
}
|
||||
|
||||
doneComment() {
|
||||
const msgs = [...this.logs, ...this.errorLogs];
|
||||
let comment = '';
|
||||
comment += `> \`${this.command}\`\n`;
|
||||
comment += '```\n';
|
||||
comment += msgs.join('\n');
|
||||
comment += '\n```';
|
||||
return comment;
|
||||
}
|
||||
|
||||
async onDone() {
|
||||
let msg;
|
||||
if (this.source === 'GHA') {
|
||||
msg = this.doneComment();
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
||||
export default Context;
|
||||
|
|
@ -1,142 +0,0 @@
|
|||
import { spawnSync } from 'child_process';
|
||||
|
||||
const REPO = 'apache/superset';
|
||||
const CACHE_REPO = `${REPO}-cache`;
|
||||
const BASE_PY_IMAGE = '3.10-slim-bookworm';
|
||||
|
||||
export function runCmd(command, raiseOnFailure = true) {
|
||||
const { stdout, stderr } = spawnSync(command, { shell: true, encoding: 'utf-8', env: process.env });
|
||||
|
||||
if (stderr && raiseOnFailure) {
|
||||
throw new Error(stderr);
|
||||
}
|
||||
return stdout;
|
||||
}
|
||||
|
||||
function getGitSha() {
|
||||
return runCmd('git rev-parse HEAD').trim();
|
||||
}
|
||||
|
||||
function getBuildContextRef(buildContext) {
|
||||
const event = buildContext || process.env.GITHUB_EVENT_NAME;
|
||||
const githubRef = process.env.GITHUB_REF || '';
|
||||
|
||||
if (event === 'pull_request') {
|
||||
const githubHeadRef = process.env.GITHUB_HEAD_REF || '';
|
||||
return githubHeadRef.replace(/[^a-zA-Z0-9]/g, '-').slice(0, 40);
|
||||
} if (event === 'release') {
|
||||
return githubRef.replace('refs/tags/', '').slice(0, 40);
|
||||
} if (event === 'push') {
|
||||
return githubRef.replace('refs/heads/', '').replace(/[^a-zA-Z0-9]/g, '-').slice(0, 40);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
export function isLatestRelease(release) {
|
||||
const output = runCmd(`../../scripts/tag_latest_release.sh ${release} --dry-run`, false) || '';
|
||||
return output.includes('SKIP_TAG::false');
|
||||
}
|
||||
|
||||
function makeDockerTag(parts) {
|
||||
return `${REPO}:${parts.filter((part) => part).join('-')}`;
|
||||
}
|
||||
|
||||
export function getDockerTags({
|
||||
preset, platforms, sha, buildContext, buildContextRef, forceLatest = false,
|
||||
}) {
|
||||
const tags = new Set();
|
||||
const tagChunks = [];
|
||||
|
||||
const isLatest = isLatestRelease(buildContextRef);
|
||||
|
||||
if (preset !== 'lean') {
|
||||
tagChunks.push(preset);
|
||||
}
|
||||
|
||||
if (platforms.length === 1) {
|
||||
const platform = platforms[0];
|
||||
const shortBuildPlatform = platform.replace('linux/', '').replace('64', '');
|
||||
if (shortBuildPlatform !== 'amd') {
|
||||
tagChunks.push(shortBuildPlatform);
|
||||
}
|
||||
}
|
||||
|
||||
tags.add(makeDockerTag([sha, ...tagChunks]));
|
||||
tags.add(makeDockerTag([sha.slice(0, 7), ...tagChunks]));
|
||||
|
||||
if (buildContext === 'release') {
|
||||
tags.add(makeDockerTag([buildContextRef, ...tagChunks]));
|
||||
if (isLatest || forceLatest) {
|
||||
tags.add(makeDockerTag(['latest', ...tagChunks]));
|
||||
}
|
||||
} else if (buildContext === 'push' && buildContextRef === 'master') {
|
||||
tags.add(makeDockerTag(['master', ...tagChunks]));
|
||||
} else if (buildContext === 'pull_request') {
|
||||
tags.add(makeDockerTag([`pr-${buildContextRef}`, ...tagChunks]));
|
||||
}
|
||||
|
||||
return [...tags];
|
||||
}
|
||||
|
||||
export function getDockerCommand({
|
||||
preset, platform, buildContext, buildContextRef, forceLatest = false,
|
||||
}) {
|
||||
const platforms = platform;
|
||||
|
||||
let buildTarget = '';
|
||||
let pyVer = BASE_PY_IMAGE;
|
||||
let dockerContext = '.';
|
||||
|
||||
if (preset === 'dev') {
|
||||
buildTarget = 'dev';
|
||||
} else if (preset === 'lean') {
|
||||
buildTarget = 'lean';
|
||||
} else if (preset === 'py310') {
|
||||
buildTarget = 'lean';
|
||||
pyVer = '3.10-slim-bookworm';
|
||||
} else if (preset === 'websocket') {
|
||||
dockerContext = 'superset-websocket';
|
||||
} else if (preset === 'ci') {
|
||||
buildTarget = 'ci';
|
||||
} else if (preset === 'dockerize') {
|
||||
dockerContext = '-f dockerize.Dockerfile .';
|
||||
} else {
|
||||
console.error(`Invalid build preset: ${preset}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
let ref = buildContextRef;
|
||||
if (!ref) {
|
||||
ref = getBuildContextRef(buildContext);
|
||||
}
|
||||
const sha = getGitSha();
|
||||
const tags = getDockerTags({
|
||||
preset, platforms, sha, buildContext, buildContextRef: ref, forceLatest,
|
||||
}).map((tag) => `-t ${tag}`).join(' \\\n ');
|
||||
const isAuthenticated = !!(process.env.DOCKERHUB_TOKEN);
|
||||
|
||||
const dockerArgs = isAuthenticated ? '--push' : '--load';
|
||||
const targetArgument = buildTarget ? `--target ${buildTarget}` : '';
|
||||
const cacheRef = `${CACHE_REPO}:${pyVer}`;
|
||||
const platformArg = `--platform ${platforms.join(',')}`;
|
||||
const cacheFromArg = `--cache-from=type=registry,ref=${cacheRef}`;
|
||||
const cacheToArg = isAuthenticated ? `--cache-to=type=registry,mode=max,ref=${cacheRef}` : '';
|
||||
const buildArg = pyVer ? `--build-arg PY_VER=${pyVer}` : '';
|
||||
const actor = process.env.GITHUB_ACTOR;
|
||||
|
||||
return `docker buildx build \\
|
||||
${dockerArgs} \\
|
||||
${tags} \\
|
||||
${cacheFromArg} \\
|
||||
${cacheToArg} \\
|
||||
${targetArgument} \\
|
||||
${buildArg} \\
|
||||
${platformArg} \\
|
||||
--label sha=${sha} \\
|
||||
--label target=${buildTarget} \\
|
||||
--label build_trigger=${ref} \\
|
||||
--label base=${pyVer} \\
|
||||
--label build_actor=${actor} \\
|
||||
${dockerContext}
|
||||
`;
|
||||
}
|
||||
|
|
@ -1,244 +0,0 @@
|
|||
import * as dockerUtils from './docker.js';
|
||||
|
||||
const SHA = '22e7c602b9aa321ec7e0df4bb0033048664dcdf0';
|
||||
const PR_ID = '666';
|
||||
const OLD_REL = '2.1.0';
|
||||
const NEW_REL = '2.1.1';
|
||||
const REPO = 'apache/superset';
|
||||
|
||||
beforeEach(() => {
|
||||
process.env.TEST_ENV = 'true';
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
delete process.env.TEST_ENV;
|
||||
});
|
||||
|
||||
describe('isLatestRelease', () => {
|
||||
test.each([
|
||||
['2.1.0', false],
|
||||
['2.1.1', true],
|
||||
['1.0.0', false],
|
||||
['3.0.0', true],
|
||||
])('returns %s for release %s', (release, expectedBool) => {
|
||||
expect(dockerUtils.isLatestRelease(release)).toBe(expectedBool);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getDockerTags', () => {
|
||||
test.each([
|
||||
// PRs
|
||||
[
|
||||
'lean',
|
||||
['linux/arm64'],
|
||||
SHA,
|
||||
'pull_request',
|
||||
PR_ID,
|
||||
[`${REPO}:22e7c60-arm`, `${REPO}:${SHA}-arm`, `${REPO}:pr-${PR_ID}-arm`],
|
||||
],
|
||||
[
|
||||
'ci',
|
||||
['linux/amd64'],
|
||||
SHA,
|
||||
'pull_request',
|
||||
PR_ID,
|
||||
[`${REPO}:22e7c60-ci`, `${REPO}:${SHA}-ci`, `${REPO}:pr-${PR_ID}-ci`],
|
||||
],
|
||||
[
|
||||
'lean',
|
||||
['linux/amd64'],
|
||||
SHA,
|
||||
'pull_request',
|
||||
PR_ID,
|
||||
[`${REPO}:22e7c60`, `${REPO}:${SHA}`, `${REPO}:pr-${PR_ID}`],
|
||||
],
|
||||
[
|
||||
'dev',
|
||||
['linux/arm64'],
|
||||
SHA,
|
||||
'pull_request',
|
||||
PR_ID,
|
||||
[
|
||||
`${REPO}:22e7c60-dev-arm`,
|
||||
`${REPO}:${SHA}-dev-arm`,
|
||||
`${REPO}:pr-${PR_ID}-dev-arm`,
|
||||
],
|
||||
],
|
||||
[
|
||||
'dev',
|
||||
['linux/amd64'],
|
||||
SHA,
|
||||
'pull_request',
|
||||
PR_ID,
|
||||
[`${REPO}:22e7c60-dev`, `${REPO}:${SHA}-dev`, `${REPO}:pr-${PR_ID}-dev`],
|
||||
],
|
||||
// old releases
|
||||
[
|
||||
'lean',
|
||||
['linux/arm64'],
|
||||
SHA,
|
||||
'release',
|
||||
OLD_REL,
|
||||
[`${REPO}:22e7c60-arm`, `${REPO}:${SHA}-arm`, `${REPO}:${OLD_REL}-arm`],
|
||||
],
|
||||
[
|
||||
'lean',
|
||||
['linux/amd64'],
|
||||
SHA,
|
||||
'release',
|
||||
OLD_REL,
|
||||
[`${REPO}:22e7c60`, `${REPO}:${SHA}`, `${REPO}:${OLD_REL}`],
|
||||
],
|
||||
[
|
||||
'dev',
|
||||
['linux/arm64'],
|
||||
SHA,
|
||||
'release',
|
||||
OLD_REL,
|
||||
[
|
||||
`${REPO}:22e7c60-dev-arm`,
|
||||
`${REPO}:${SHA}-dev-arm`,
|
||||
`${REPO}:${OLD_REL}-dev-arm`,
|
||||
],
|
||||
],
|
||||
[
|
||||
'dev',
|
||||
['linux/amd64'],
|
||||
SHA,
|
||||
'release',
|
||||
OLD_REL,
|
||||
[`${REPO}:22e7c60-dev`, `${REPO}:${SHA}-dev`, `${REPO}:${OLD_REL}-dev`],
|
||||
],
|
||||
// new releases
|
||||
[
|
||||
'lean',
|
||||
['linux/arm64'],
|
||||
SHA,
|
||||
'release',
|
||||
NEW_REL,
|
||||
[
|
||||
`${REPO}:22e7c60-arm`,
|
||||
`${REPO}:${SHA}-arm`,
|
||||
`${REPO}:${NEW_REL}-arm`,
|
||||
`${REPO}:latest-arm`,
|
||||
],
|
||||
],
|
||||
[
|
||||
'lean',
|
||||
['linux/amd64'],
|
||||
SHA,
|
||||
'release',
|
||||
NEW_REL,
|
||||
[`${REPO}:22e7c60`, `${REPO}:${SHA}`, `${REPO}:${NEW_REL}`, `${REPO}:latest`],
|
||||
],
|
||||
[
|
||||
'dev',
|
||||
['linux/arm64'],
|
||||
SHA,
|
||||
'release',
|
||||
NEW_REL,
|
||||
[
|
||||
`${REPO}:22e7c60-dev-arm`,
|
||||
`${REPO}:${SHA}-dev-arm`,
|
||||
`${REPO}:${NEW_REL}-dev-arm`,
|
||||
`${REPO}:latest-dev-arm`,
|
||||
],
|
||||
],
|
||||
[
|
||||
'dev',
|
||||
['linux/amd64'],
|
||||
SHA,
|
||||
'release',
|
||||
NEW_REL,
|
||||
[
|
||||
`${REPO}:22e7c60-dev`,
|
||||
`${REPO}:${SHA}-dev`,
|
||||
`${REPO}:${NEW_REL}-dev`,
|
||||
`${REPO}:latest-dev`,
|
||||
],
|
||||
],
|
||||
// merge on master
|
||||
[
|
||||
'lean',
|
||||
['linux/arm64'],
|
||||
SHA,
|
||||
'push',
|
||||
'master',
|
||||
[`${REPO}:22e7c60-arm`, `${REPO}:${SHA}-arm`, `${REPO}:master-arm`],
|
||||
],
|
||||
[
|
||||
'lean',
|
||||
['linux/amd64'],
|
||||
SHA,
|
||||
'push',
|
||||
'master',
|
||||
[`${REPO}:22e7c60`, `${REPO}:${SHA}`, `${REPO}:master`],
|
||||
],
|
||||
[
|
||||
'dev',
|
||||
['linux/arm64'],
|
||||
SHA,
|
||||
'push',
|
||||
'master',
|
||||
[
|
||||
`${REPO}:22e7c60-dev-arm`,
|
||||
`${REPO}:${SHA}-dev-arm`,
|
||||
`${REPO}:master-dev-arm`,
|
||||
],
|
||||
],
|
||||
[
|
||||
'dev',
|
||||
['linux/amd64'],
|
||||
SHA,
|
||||
'push',
|
||||
'master',
|
||||
[`${REPO}:22e7c60-dev`, `${REPO}:${SHA}-dev`, `${REPO}:master-dev`],
|
||||
],
|
||||
|
||||
])('returns expected tags', (preset, platforms, sha, buildContext, buildContextRef, expectedTags) => {
|
||||
const tags = dockerUtils.getDockerTags({
|
||||
preset, platforms, sha, buildContext, buildContextRef,
|
||||
});
|
||||
expect(tags).toEqual(expect.arrayContaining(expectedTags));
|
||||
});
|
||||
});
|
||||
|
||||
describe('getDockerCommand', () => {
|
||||
test.each([
|
||||
[
|
||||
'lean',
|
||||
['linux/amd64'],
|
||||
true,
|
||||
SHA,
|
||||
'push',
|
||||
'master',
|
||||
['--push', `-t ${REPO}:master `],
|
||||
],
|
||||
[
|
||||
'dev',
|
||||
['linux/amd64'],
|
||||
false,
|
||||
SHA,
|
||||
'push',
|
||||
'master',
|
||||
['--load', `-t ${REPO}:master-dev `],
|
||||
],
|
||||
// multi-platform
|
||||
[
|
||||
'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) => {
|
||||
const cmd = dockerUtils.getDockerCommand({
|
||||
preset, platform, isAuthenticated, sha, buildContext, buildContextRef,
|
||||
});
|
||||
contains.forEach((expectedSubstring) => {
|
||||
expect(cmd).toContain(expectedSubstring);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -1,120 +0,0 @@
|
|||
import simpleGit from 'simple-git';
|
||||
import semver from 'semver';
|
||||
|
||||
import GitRelease from './git_release.js';
|
||||
|
||||
export default class Git {
|
||||
#releaseTags;
|
||||
|
||||
constructor(context, mainBranch = 'master') {
|
||||
this.context = context;
|
||||
this.mainBranch = mainBranch;
|
||||
this.releases = new Map();
|
||||
this.git = simpleGit();
|
||||
this.mainBranchGitRelease = this.mainBranchGitRelease.bind(this);
|
||||
this.getReleaseLabels = this.getReleaseLabels.bind(this);
|
||||
}
|
||||
|
||||
async mainBranchGitRelease() {
|
||||
let rel = this.releases.get(this.mainBranch);
|
||||
if (!rel) {
|
||||
rel = await this.loadRelease(this.mainBranch);
|
||||
}
|
||||
return rel;
|
||||
}
|
||||
|
||||
async releaseTags() {
|
||||
if (!this.#releaseTags) {
|
||||
const tags = await this.git.tags();
|
||||
// Filter tags to include only those that match semver and are official releases
|
||||
const semverTags = tags.all.filter((tag) => semver.valid(tag) && !tag.includes('-') && !tag.includes('v'));
|
||||
semverTags.sort((a, b) => semver.compare(a, b));
|
||||
this.#releaseTags = semverTags;
|
||||
}
|
||||
return this.#releaseTags;
|
||||
}
|
||||
|
||||
async loadMainBranch() {
|
||||
await this.loadRelease(this.mainBranch);
|
||||
}
|
||||
|
||||
async loadReleases(tags = null) {
|
||||
const tagsToFetch = tags || await this.releaseTags();
|
||||
if (!tags) {
|
||||
await this.loadMainBranch();
|
||||
}
|
||||
const promises = [];
|
||||
tagsToFetch.forEach((tag) => {
|
||||
promises.push(this.loadRelease(tag));
|
||||
});
|
||||
await Promise.all(promises);
|
||||
}
|
||||
|
||||
async loadRelease(tag) {
|
||||
const release = new GitRelease(tag, this.context);
|
||||
await release.load();
|
||||
this.releases.set(tag, release);
|
||||
return release;
|
||||
}
|
||||
|
||||
static shortenSHA(sha) {
|
||||
return sha.substring(0, 7);
|
||||
}
|
||||
|
||||
async getReleaseLabels(prNumber, verbose, excludeCherries = false) {
|
||||
const labels = [];
|
||||
const main = await this.mainBranchGitRelease();
|
||||
const commit = main.prIdCommitMap.get(prNumber);
|
||||
if (commit) {
|
||||
const { sha } = commit;
|
||||
const shortSHA = Git.shortenSHA(sha);
|
||||
if (verbose) {
|
||||
console.log(`PR ${prNumber} is ${shortSHA} on branch ${this.mainBranch}`);
|
||||
}
|
||||
|
||||
let firstGitReleased = null;
|
||||
const tags = await this.releaseTags();
|
||||
tags.forEach((tag) => {
|
||||
const release = this.releases.get(tag);
|
||||
if (release.shaCommitMap.get(sha) && !firstGitReleased && release.tag !== this.mainBranch) {
|
||||
firstGitReleased = release.tag;
|
||||
labels.push(`🚢 ${release.tag}`);
|
||||
}
|
||||
const commitInGitRelease = release.prIdCommitMap.get(prNumber);
|
||||
if (!excludeCherries && commitInGitRelease && commitInGitRelease.sha !== sha) {
|
||||
labels.push(`🍒 ${release.tag}`);
|
||||
}
|
||||
});
|
||||
if (labels.length >= 1) {
|
||||
// using this emoji to show it's been labeled by the bot
|
||||
labels.push('🏷️ bot');
|
||||
}
|
||||
}
|
||||
return labels;
|
||||
}
|
||||
|
||||
async previousRelease(release) {
|
||||
const tags = await this.releaseTags();
|
||||
return tags[tags.indexOf(release) - 1];
|
||||
}
|
||||
|
||||
async getPRsToSync(release, verbose = false, excludeCherries = false) {
|
||||
const prevRelease = await this.previousRelease(release);
|
||||
const releaseRange = new GitRelease(release, this.context, prevRelease);
|
||||
await releaseRange.load();
|
||||
const prIds = releaseRange.prIdCommitMap.keys();
|
||||
|
||||
const prs = [];
|
||||
const promises = [];
|
||||
[...prIds].forEach(prId => {
|
||||
promises.push(
|
||||
this.getReleaseLabels(prId, verbose, excludeCherries)
|
||||
.then((labels) => {
|
||||
prs.push({ prId, labels });
|
||||
}),
|
||||
);
|
||||
});
|
||||
await Promise.all(promises);
|
||||
return prs;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,50 +0,0 @@
|
|||
import simpleGit from 'simple-git';
|
||||
|
||||
export default class GitRelease {
|
||||
constructor(tag, context, from = null) {
|
||||
this.tag = tag;
|
||||
this.context = context;
|
||||
this.prNumberRegex = /\(#(\d+)\)/;
|
||||
this.shaCommitMap = null;
|
||||
this.prIdCommitMap = null;
|
||||
this.prCommitMap = null;
|
||||
this.git = simpleGit();
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
extractPRNumber(commitMessage) {
|
||||
const match = (commitMessage || '').match(this.prNumberRegex);
|
||||
return match ? parseInt(match[1], 10) : null;
|
||||
}
|
||||
|
||||
async load() {
|
||||
let from = this.from || await this.git.firstCommit();
|
||||
if (from.includes('\n')) {
|
||||
[from] = from.split('\n');
|
||||
}
|
||||
const range = `${this.from || 'first'}..${this.tag}`;
|
||||
const commits = await this.git.log({ from, to: this.tag });
|
||||
this.context.log(`${range} - fetched ${commits.all.length} commits`);
|
||||
|
||||
this.shaCommitMap = new Map();
|
||||
commits.all.forEach((commit) => {
|
||||
const sha = commit.hash.substring(0, 7);
|
||||
this.shaCommitMap.set(
|
||||
sha,
|
||||
{
|
||||
prId: this.extractPRNumber(commit.message),
|
||||
message: commit.message,
|
||||
sha,
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
this.prIdCommitMap = new Map();
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const commit of this.shaCommitMap.values()) {
|
||||
if (commit.prId) {
|
||||
this.prIdCommitMap.set(commit.prId, commit);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,252 +0,0 @@
|
|||
import { Octokit } from '@octokit/rest';
|
||||
import { throttling } from '@octokit/plugin-throttling';
|
||||
|
||||
import { ORG_LIST, PROTECTED_LABEL_PATTERNS, COMMITTER_TEAM } from './metadata.js';
|
||||
|
||||
class Github {
|
||||
#userInTeamCache;
|
||||
|
||||
constructor({ context, issueNumber = null, token = null }) {
|
||||
this.context = context;
|
||||
this.issueNumber = issueNumber;
|
||||
const githubToken = token || process.env.GITHUB_TOKEN;
|
||||
if (!githubToken) {
|
||||
const msg = 'GITHUB_TOKEN is not set';
|
||||
this.context.logError(msg);
|
||||
}
|
||||
const throttledOctokit = Octokit.plugin(throttling);
|
||||
// eslint-disable-next-line new-cap
|
||||
this.octokit = new throttledOctokit({
|
||||
auth: githubToken,
|
||||
throttle: {
|
||||
id: 'supersetbot',
|
||||
onRateLimit: (retryAfter, options, octokit, retryCount) => {
|
||||
const howManyRetries = 10;
|
||||
octokit.log.warn(`Retry ${retryCount} out of ${howManyRetries} - retrying in ${retryAfter} seconds!`);
|
||||
if (retryCount < howManyRetries) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
onSecondaryRateLimit: (retryAfter, options, octokit) => {
|
||||
octokit.log.warn(`SecondaryRateLimit detected for request ${options.method} ${options.url}`);
|
||||
},
|
||||
},
|
||||
});
|
||||
this.syncLabels = this.syncLabels.bind(this);
|
||||
this.#userInTeamCache = new Map();
|
||||
}
|
||||
|
||||
unPackRepo() {
|
||||
const [owner, repo] = this.context.repo.split('/');
|
||||
return { repo, owner };
|
||||
}
|
||||
|
||||
async label(issueNumber, label, actor = null, verbose = false, dryRun = false) {
|
||||
let hasPerm = true;
|
||||
if (actor && Github.isLabelProtected(label)) {
|
||||
hasPerm = await this.checkIfUserInTeam(actor, COMMITTER_TEAM, verbose);
|
||||
}
|
||||
if (hasPerm) {
|
||||
const addLabelWrapped = this.context.commandWrapper({
|
||||
func: this.octokit.rest.issues.addLabels,
|
||||
successMsg: `label "${label}" added to issue ${issueNumber}`,
|
||||
verbose,
|
||||
dryRun,
|
||||
});
|
||||
await addLabelWrapped({
|
||||
...this.unPackRepo(),
|
||||
issue_number: issueNumber,
|
||||
labels: [label],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async createComment(body) {
|
||||
if (this.issueNumber) {
|
||||
await this.octokit.rest.issues.createComment({
|
||||
...this.unPackRepo(),
|
||||
body,
|
||||
issue_number: this.issueNumber,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async unlabel(issueNumber, label, actor = null, verbose = false, dryRun = false) {
|
||||
let hasPerm = true;
|
||||
if (actor && Github.isLabelProtected(label)) {
|
||||
hasPerm = await this.checkIfUserInTeam(actor, COMMITTER_TEAM, verbose);
|
||||
}
|
||||
if (hasPerm) {
|
||||
const removeLabelWrapped = this.context.commandWrapper({
|
||||
func: this.octokit.rest.issues.removeLabel,
|
||||
successMsg: `label "${label}" removed from issue ${issueNumber}`,
|
||||
verbose,
|
||||
dryRun,
|
||||
});
|
||||
await removeLabelWrapped({
|
||||
...this.unPackRepo(),
|
||||
issue_number: issueNumber,
|
||||
name: label,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async assignOrgLabel(issueNumber, verbose = false, dryRun = false) {
|
||||
const issue = await this.octokit.rest.issues.get({
|
||||
...this.unPackRepo(),
|
||||
issue_number: issueNumber,
|
||||
});
|
||||
const username = issue.data.user.login;
|
||||
const orgs = await this.octokit.orgs.listForUser({ username });
|
||||
const orgNames = orgs.data.map((v) => v.login);
|
||||
|
||||
// get list of matching github orgs
|
||||
const matchingOrgs = orgNames.filter((org) => ORG_LIST.includes(org));
|
||||
if (matchingOrgs.length) {
|
||||
const wrapped = this.context.commandWrapper({
|
||||
func: this.octokit.rest.issues.addLabels,
|
||||
successMsg: `added label(s) ${matchingOrgs} to issue ${issueNumber}`,
|
||||
errorMsg: "couldn't add labels to issue",
|
||||
verbose,
|
||||
dryRun,
|
||||
});
|
||||
wrapped({
|
||||
...this.unPackRepo(),
|
||||
issue_number: issueNumber,
|
||||
labels: matchingOrgs,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async searchMergedPRs({
|
||||
query = '',
|
||||
onlyUnlabeled = true,
|
||||
verbose = false,
|
||||
startPage = 0,
|
||||
pages = 5,
|
||||
}) {
|
||||
// look for PRs
|
||||
let q = `repo:${this.context.repo} is:merged ${query}`;
|
||||
if (onlyUnlabeled) {
|
||||
q = `${q} -label:"🏷️ bot"`;
|
||||
}
|
||||
if (verbose) {
|
||||
this.context.log(`Query: ${q}`);
|
||||
}
|
||||
let prs = [];
|
||||
for (let i = 0; i < pages; i += 1) {
|
||||
if (verbose) {
|
||||
this.context.log(`Fetching PRs to process page ${i + 1} out of ${pages}`);
|
||||
}
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const data = await this.octokit.search.issuesAndPullRequests({
|
||||
q,
|
||||
per_page: 100,
|
||||
page: startPage + i,
|
||||
});
|
||||
prs = [...prs, ...data.data.items];
|
||||
}
|
||||
if (verbose) {
|
||||
this.context.log(`Fetched ${prs.length}`);
|
||||
}
|
||||
return prs;
|
||||
}
|
||||
|
||||
async syncLabels({
|
||||
labels,
|
||||
prId,
|
||||
actor = null,
|
||||
verbose = false,
|
||||
dryRun = false,
|
||||
existingLabels = null,
|
||||
}) {
|
||||
if (verbose) {
|
||||
this.context.log(`[PR: ${prId}] - sync labels ${labels}`);
|
||||
}
|
||||
let hasPerm = true;
|
||||
if (actor) {
|
||||
hasPerm = await this.checkIfUserInTeam(actor, COMMITTER_TEAM, verbose);
|
||||
}
|
||||
if (!hasPerm) {
|
||||
return;
|
||||
}
|
||||
let targetLabels = existingLabels;
|
||||
if (targetLabels === null) {
|
||||
// No labels have been passed as an array, so checking against GitHub
|
||||
const resp = await this.octokit.issues.listLabelsOnIssue({
|
||||
...this.unPackRepo(),
|
||||
issue_number: prId,
|
||||
});
|
||||
targetLabels = resp.data.map((l) => l.name);
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
this.context.log(`[PR: ${prId}] - target release labels: ${labels}`);
|
||||
this.context.log(`[PR: ${prId}] - existing labels on issue: ${existingLabels}`);
|
||||
}
|
||||
|
||||
// Extract existing labels with the given prefixes
|
||||
const prefixes = ['🚢', '🍒', '🎯', '🏷️'];
|
||||
const existingPrefixLabels = targetLabels
|
||||
.filter((label) => prefixes.some((s) => typeof(label) === 'string' && label.startsWith(s)));
|
||||
|
||||
// Labels to add
|
||||
const labelsToAdd = labels.filter((label) => !existingPrefixLabels.includes(label));
|
||||
if (verbose) {
|
||||
this.context.log(`[PR: ${prId}] - labels to add: ${labelsToAdd}`);
|
||||
}
|
||||
// Labels to remove
|
||||
const labelsToRemove = existingPrefixLabels.filter((label) => !labels.includes(label));
|
||||
if (verbose) {
|
||||
this.context.log(`[PR: ${prId}] - labels to remove: ${labelsToRemove}`);
|
||||
}
|
||||
|
||||
// Add labels
|
||||
if (labelsToAdd.length > 0 && !dryRun) {
|
||||
await this.octokit.issues.addLabels({
|
||||
...this.unPackRepo(),
|
||||
issue_number: prId,
|
||||
labels: labelsToAdd,
|
||||
});
|
||||
}
|
||||
|
||||
// Remove labels
|
||||
if (labelsToRemove.length > 0 && !dryRun) {
|
||||
await Promise.all(labelsToRemove.map((label) => this.octokit.issues.removeLabel({
|
||||
...this.unPackRepo(),
|
||||
issue_number: prId,
|
||||
name: label,
|
||||
})));
|
||||
}
|
||||
this.context.logSuccess(`synched labels for PR ${prId} with labels ${labels}`);
|
||||
}
|
||||
|
||||
async checkIfUserInTeam(username, team, verbose = false) {
|
||||
let isInTeam = this.#userInTeamCache.get([username, team]);
|
||||
if (isInTeam !== undefined) {
|
||||
return isInTeam;
|
||||
}
|
||||
|
||||
const [org, teamSlug] = team.split('/');
|
||||
const wrapped = this.context.commandWrapper({
|
||||
func: this.octokit.teams.getMembershipForUserInOrg,
|
||||
errorMsg: `User "${username}" is not authorized to alter protected labels.`,
|
||||
verbose,
|
||||
});
|
||||
const resp = await wrapped({
|
||||
org,
|
||||
team_slug: teamSlug,
|
||||
username,
|
||||
});
|
||||
isInTeam = resp?.data?.state === 'active';
|
||||
this.#userInTeamCache.set([username, team], isInTeam);
|
||||
return isInTeam;
|
||||
}
|
||||
|
||||
static isLabelProtected(label) {
|
||||
return PROTECTED_LABEL_PATTERNS.some((pattern) => new RegExp(pattern).test(label));
|
||||
}
|
||||
}
|
||||
|
||||
export default Github;
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
import getCLI from './cli.js';
|
||||
import Context from './context.js';
|
||||
import Github from './github.js';
|
||||
|
||||
async function runCommandFromGithubAction(rawCommand) {
|
||||
const context = new Context('GHA');
|
||||
const cli = getCLI(context);
|
||||
const github = new Github(context);
|
||||
|
||||
// Make rawCommand look like argv
|
||||
const cmd = rawCommand.trim().replace('@supersetbot', 'supersetbot');
|
||||
const args = context.parseArgs(cmd);
|
||||
|
||||
await cli.parseAsync(['node', ...args]);
|
||||
const msg = await context.onDone();
|
||||
|
||||
github.createComment(msg);
|
||||
}
|
||||
|
||||
export { runCommandFromGithubAction };
|
||||
|
|
@ -1,51 +0,0 @@
|
|||
// import * as stringArgv from 'string-argv';
|
||||
import { jest } from '@jest/globals';
|
||||
|
||||
import Context from './context.js';
|
||||
import Github from './github.js';
|
||||
import * as index from './index.js';
|
||||
|
||||
describe('runCommandFromGithubAction', () => {
|
||||
const labelSpy = jest.spyOn(Github.prototype, 'label').mockImplementation(jest.fn());
|
||||
// mocking some of the Context object
|
||||
const onDoneSpy = jest.spyOn(Context.prototype, 'onDone');
|
||||
const doneCommentSpy = jest.spyOn(Context.prototype, 'doneComment');
|
||||
const parseArgsSpy = jest.spyOn(Context.prototype, 'parseArgs');
|
||||
jest.spyOn(Github.prototype, 'createComment').mockImplementation(jest.fn());
|
||||
|
||||
let originalEnv;
|
||||
|
||||
afterEach(() => {
|
||||
process.env = originalEnv;
|
||||
});
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
originalEnv = process.env;
|
||||
process.env.GITHUB_ISSUE_NUMBER = '666';
|
||||
process.env.GITHUB_REPOSITORY = 'apache/superset';
|
||||
});
|
||||
|
||||
it('should strip the command', async () => {
|
||||
await index.runCommandFromGithubAction(' @supersetbot label test-label ');
|
||||
expect(parseArgsSpy).toHaveBeenCalledWith('supersetbot label test-label');
|
||||
|
||||
await index.runCommandFromGithubAction(' \n @supersetbot label test-label \n \n \n');
|
||||
expect(parseArgsSpy).toHaveBeenCalledWith('supersetbot label test-label');
|
||||
|
||||
await index.runCommandFromGithubAction(' \n \t@supersetbot label test-label \t \n \n\t \n');
|
||||
expect(parseArgsSpy).toHaveBeenCalledWith('supersetbot label test-label');
|
||||
});
|
||||
|
||||
it('should parse the raw command correctly and call commands.label and context.onDone', async () => {
|
||||
await index.runCommandFromGithubAction('@supersetbot label test-label');
|
||||
|
||||
expect(labelSpy).toHaveBeenCalled();
|
||||
expect(onDoneSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should generate a good comment message', async () => {
|
||||
await index.runCommandFromGithubAction('@supersetbot label test-label');
|
||||
const comment = doneCommentSpy.mock.results[0].value;
|
||||
expect(comment).toContain('> `supersetbot label test-label`');
|
||||
});
|
||||
});
|
||||
|
|
@ -1,36 +0,0 @@
|
|||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
export const ORG_LIST = [
|
||||
'preset-io',
|
||||
'airbnb',
|
||||
'dropbox',
|
||||
'lyft',
|
||||
'Turing',
|
||||
'Superset-Community-Partners',
|
||||
'CybercentreCanada',
|
||||
'TechAudit-BI',
|
||||
];
|
||||
export const PROTECTED_LABEL_PATTERNS = [
|
||||
'protected.*',
|
||||
'released.*',
|
||||
'hold.*',
|
||||
'^v\\d+(\\.\\d+)*$',
|
||||
'(🚢|🍒|🎯).*',
|
||||
];
|
||||
export const COMMITTER_TEAM = 'apache/superset-committers';
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
#!/usr/bin/env node
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
import getCLI from './cli.js';
|
||||
import Context from './context.js';
|
||||
|
||||
const envContext = new Context('CLI');
|
||||
const cli = getCLI(envContext);
|
||||
|
||||
cli.parse();
|
||||
|
|
@ -1,78 +0,0 @@
|
|||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
import { spawn } from 'child_process';
|
||||
import { readFile } from 'fs/promises';
|
||||
import { fileURLToPath } from 'url';
|
||||
import path from 'path';
|
||||
|
||||
const dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
async function loadPackageJson() {
|
||||
try {
|
||||
const packageJsonPath = path.join(dirname, '../package.json');
|
||||
const data = await readFile(packageJsonPath, 'utf8');
|
||||
const packageJson = JSON.parse(data);
|
||||
return packageJson;
|
||||
} catch (error) {
|
||||
console.error('Error reading package.json:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
export async function currentPackageVersion() {
|
||||
const data = await loadPackageJson();
|
||||
return data.version;
|
||||
}
|
||||
|
||||
export function runShellCommand(command, raiseOnError = true) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Split the command string into an array of arguments
|
||||
const args = command.split(/\s+/).filter((s) => !!s && s !== '\\');
|
||||
const childProcess = spawn(args.shift(), args);
|
||||
let stdoutData = '';
|
||||
let stderrData = '';
|
||||
|
||||
// Capture stdout data
|
||||
childProcess.stdout.on('data', (data) => {
|
||||
stdoutData += data;
|
||||
console.log(`stdout: ${data}`);
|
||||
});
|
||||
|
||||
// Capture stderr data
|
||||
childProcess.stderr.on('data', (data) => {
|
||||
stderrData += data;
|
||||
console.error(`stderr: ${data}`);
|
||||
});
|
||||
|
||||
// Handle process exit
|
||||
childProcess.on('close', (code) => {
|
||||
if (code === 0) {
|
||||
resolve(stdoutData);
|
||||
} else {
|
||||
const msg = `Command failed with code ${code}: ${stderrData}`;
|
||||
if (raiseOnError) {
|
||||
reject(new Error(msg));
|
||||
} else {
|
||||
console.error(msg);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
@ -51,13 +51,10 @@ jobs:
|
|||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Setup Node Env
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '20'
|
||||
|
||||
- name: "Checkout ${{ github.ref }} ( ${{ github.sha }} )"
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Setup supersetbot
|
||||
uses: ./.github/actions/setup-supersetbot/
|
||||
|
|
|
|||
|
|
@ -52,11 +52,6 @@ jobs:
|
|||
username: ${{ secrets.DOCKERHUB_USER }}
|
||||
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||
|
||||
- name: Setup Node Env
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '20'
|
||||
|
||||
- name: "Checkout ${{ github.ref }} ( ${{ github.sha }} )"
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
|
|
|
|||
|
|
@ -10,19 +10,21 @@ on:
|
|||
jobs:
|
||||
superbot-orglabel:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
pull-requests: write
|
||||
issues: write
|
||||
steps:
|
||||
- name: Execute SupersetBot Command
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '20'
|
||||
|
||||
- name: "Checkout ${{ github.ref }} ( ${{ github.sha }} )"
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Setup supersetbot
|
||||
uses: ./.github/actions/setup-supersetbot/
|
||||
|
||||
- name: Execute custom Node.js script
|
||||
- name: Execute supersetbot orglabel command
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
|
|
|
|||
|
|
@ -19,8 +19,13 @@ jobs:
|
|||
if: >
|
||||
github.event_name == 'workflow_dispatch' ||
|
||||
(github.event_name == 'issue_comment' && contains(github.event.comment.body, '@supersetbot'))
|
||||
permissions:
|
||||
contents: read
|
||||
pull-requests: write
|
||||
issues: write
|
||||
steps:
|
||||
- name: Quickly add thumbs up!
|
||||
if: github.event_name == 'issue_comment' && contains(github.event.comment.body, '@supersetbot')
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
script: |
|
||||
|
|
@ -28,16 +33,14 @@ jobs:
|
|||
await github.rest.reactions.createForIssueComment({
|
||||
owner,
|
||||
repo,
|
||||
comment_id: ${{ github.event.comment.id }},
|
||||
comment_id: context.payload.comment.id,
|
||||
content: '+1'
|
||||
});
|
||||
- name: Execute SupersetBot Command
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '20'
|
||||
|
||||
- name: "Checkout ${{ github.ref }} ( ${{ github.sha }} )"
|
||||
- name: "Checkout ( ${{ github.sha }} )"
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Setup supersetbot
|
||||
uses: ./.github/actions/setup-supersetbot/
|
||||
|
|
@ -48,16 +51,6 @@ jobs:
|
|||
GITHUB_ACTOR: ${{ github.actor }}
|
||||
GITHUB_REPOSITORY: ${{ github.repository }}
|
||||
GITHUB_ISSUE_NUMBER: ${{ github.event.issue.number }}
|
||||
COMMENT_BODY: ${{ github.event.comment.body }}
|
||||
INPUT_COMMENT_BODY: ${{ github.event.inputs.comment_body }}
|
||||
COMMENT_BODY: ${{ github.event.comment.body }}${{ github.event.inputs.comment_body }}
|
||||
run: |
|
||||
cat <<EOF > script.js
|
||||
const run = async () => {
|
||||
const { runCommandFromGithubAction } = await import('supersetbot');
|
||||
const cmd = process.env.COMMENT_BODY || process.env.INPUT_COMMENT_BODY;
|
||||
console.log("Executing: ", cmd);
|
||||
await runCommandFromGithubAction(cmd);
|
||||
};
|
||||
run().catch(console.error);
|
||||
EOF
|
||||
node script.js
|
||||
supersetbot run "$COMMENT_BODY"
|
||||
|
|
|
|||
Loading…
Reference in New Issue