Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Feb 14, 2024
1 parent 875ac73 commit 97defd1
Show file tree
Hide file tree
Showing 10 changed files with 6,562 additions and 4,245 deletions.
4 changes: 4 additions & 0 deletions .github/supersetbot/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
"import/prefer-default-export": 0,
"func-names": 0,
"no-console": 0
},
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
}
}
10,669 changes: 6,491 additions & 4,178 deletions .github/supersetbot/package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion .github/supersetbot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "module",
"main": "src/index.js",
"scripts": {
"test": "jest",
"test": "node --experimental-vm-modules node_modules/.bin/jest",
"eslint": "eslint",
"supersetbot": "supersetbot"
},
Expand All @@ -18,6 +18,9 @@
"string-argv": "^0.3.2"
},
"devDependencies": {
"@babel/core": "^7.23.9",
"@babel/preset-env": "^7.23.9",
"babel-jest": "^29.7.0",
"eslint": "^8.56.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-plugin-import": "^2.29.1",
Expand Down
4 changes: 2 additions & 2 deletions .github/supersetbot/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ export default function getCLI(envContext) {
program.command('orglabel')
.description('Add an org label based on the author')
.option(...issueOptionParams)
.action(async function (commandOptions) {
.action(async function () {
const opts = envContext.processOptions(this, ['issue', 'repo']);
const wrapped = envContext.commandWrapper({
func: commands.unlabel,
func: commands.assignOrgLabel,
successMsg: 'SUCCESS: added the right labels',
errorMsg: 'FAILED at stuff',
verbose: opts.verbose,
Expand Down
61 changes: 33 additions & 28 deletions .github/supersetbot/src/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ function unPackRepo(longRepo) {
}

function isLabelProtected(label) {
return PROTECTED_LABEL_PATTERNS.some(pattern => new RegExp(pattern).test(label));
return PROTECTED_LABEL_PATTERNS.some((pattern) => new RegExp(pattern).test(label));
}

async function checkIfUserInTeam(username, team, context, verbose) {
const [org, team_slug] = team.split('/');
const [org, teamSlug] = team.split('/');
const wrapped = context.commandWrapper({
func: context.github.teams.getMembershipForUserInOrg,
errorMsg: "User is not authorized to alter protected labels.",
errorMsg: `User "${username}" is not authorized to alter protected labels.`,
verbose,
});
const resp = await wrapped({
org,
team_slug,
team_slug: teamSlug,
username,
});
return resp?.data?.state === 'active';
Expand All @@ -29,43 +29,48 @@ async function checkIfUserInTeam(username, team, context, verbose) {
// Individual commands
// -------------------------------------
export async function label(longRepo, issueNumber, label, context, actor = null, verbose = false) {
let hasPerm = true;
if (actor && isLabelProtected(label)) {
checkIfUserInTeam(actor, COMMITTER_TEAM, context, verbose);
hasPerm = await checkIfUserInTeam(actor, COMMITTER_TEAM, context, verbose);
}
if (hasPerm) {
const addLabelWrapped = context.commandWrapper({
func: context.github.rest.issues.addLabels,
successMsg: `SUCCESS: label "${label}" added to issue ${issueNumber}`,
verbose,
});
await addLabelWrapped({
...unPackRepo(longRepo),
issue_number: issueNumber,
labels: [label],
});
}
const addLabelWrapped = context.commandWrapper({
func: context.github.rest.issues.addLabels,
successMsg:`SUCCESS: label "${label}" added to issue ${issueNumber}`,
verbose,
});
await addLabelWrapped({
...unPackRepo(longRepo),
issue_number: issueNumber,
labels: [label],
});
}

export async function unlabel(longRepo, issueNumber, label, context, actor = null, verbose = false) {
export async function unlabel(repo, issueNumber, label, context, actor = null, verbose = false) {
let hasPerm = true;
if (actor && isLabelProtected(label)) {
checkIfUserInTeam(actor, COMMITTER_TEAM, context, verbose);
const hasPerm = await checkIfUserInTeam(actor, COMMITTER_TEAM, context, verbose);
}
if (hasPerm) {
const addLabelWrapped = context.commandWrapper({
func: context.github.rest.issues.removeLabel,
successMsg: `SUCCESS: label "${label}" removed from issue ${issueNumber}`,
verbose,
});
await addLabelWrapped({
...unPackRepo(repo),
issue_number: issueNumber,
name: label,
});
}
const addLabelWrapped = context.commandWrapper({
func: context.github.rest.issues.removeLabel,
successMsg:`SUCCESS: label "${label}" removed from issue ${issueNumber}`,
verbose,
});
await addLabelWrapped({
...unPackRepo(longRepo),
issue_number: issueNumber,
name: label,
});
}

export async function assignOrgLabel(repo, issueNumber, context) {
const issue = await context.github.rest.issues.get({
...unPackRepo(repo),
issue_number: issueNumber,
});

const username = issue.data.user.login;
const orgs = await context.github.orgs.listForUser({ username });
const orgNames = orgs.data.map((v) => v.login);
Expand Down
48 changes: 25 additions & 23 deletions .github/supersetbot/src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { Octokit } from '@octokit/rest';

class Context {
constructor(source) {

this.hasErrors = false;
if (!process.env.GITHUB_TOKEN) {
const msg = 'GITHUB_TOKEN is not set. Please set the GITHUB_TOKEN environment variable.';
this.logError(msg);
process.exit(1);
}
this.github = new Octokit({ auth: `token ${process.env.GITHUB_TOKEN}` });

Expand All @@ -15,28 +14,33 @@ class Context {
this.options = {};
this.errorLogs = [];
this.logs = [];
this.hasError = false;
}

requireOption(optionName, options) {
const optionValue = options[optionName];
if (optionValue === undefined || optionValue === null) {
this.logError(`🔴 ERROR: option [${optionName}] is required`);
process.exit(1);
this.hasError = true;
}
}

requireOptions(optionNames, options) {
optionNames.forEach((optionName) => {
this.requireOption(optionName, options);
});
}

log(msg) {
console.log(msg);
this.logs.push(msg);
}

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.command = raw.map((s) => (s.includes(' ') ? `"${s}"` : s)).join(' ').replace('node ', '');
}
this.options = { ...command.opts(), ...command.parent.opts() };
this.requireOptions(requiredOptions, this.options);
Expand All @@ -49,48 +53,47 @@ class Context {

return this.options;
}

logError(msg) {
this.hasErrors = true;
console.error(msg);
this.errorLogs.push(msg);
}

preCommand() {
}

commandWrapper({func, successMsg, errorMsg = null, verbose = false, exitOnError = true}) {
commandWrapper({
func, successMsg, errorMsg = null, verbose = false,
}) {
return async (...args) => {
this.preCommand();
let resp;
try {
resp = await func(...args);
if (verbose) {
console.log(resp);
}
} catch (error) {
if (errorMsg) {
this.logError(`🔴 ERROR: ${errorMsg}`);
} else {
this.logError(`🔴 ERROR: ${error}`);
}
if (exitOnError) {
process.exit(1);
}
}
if (successMsg) {
this.log(`🟢 ${successMsg}`);
}
await this.onDone();
return resp;
};
}
reconstructCommand() {
}
async onDone(msg) {

async onDone() {
if (this.source === 'GHA') {
const [owner, repo] = this.repo.split('/')
let body = '';
body += `> \`${this.command}\`\n\n`
body += "```\n"
body += this.logs.join('\n')
body += this.errorLogs.join('\n')
body += "```"
body += `> \`${this.command}\`\n\n`;
body += '```\n';
body += this.logs.join('\n');
body += this.errorLogs.join('\n');
body += '```';

const [owner, repo] = this.repo.split('/');
await this.github.rest.issues.createComment({
owner,
repo,
Expand All @@ -99,7 +102,6 @@ class Context {
});
}
}

}

export default Context;
3 changes: 2 additions & 1 deletion .github/supersetbot/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export async function runCommandFromGithubAction(rawCommand) {
// Make rawCommand look like argv
const cmd = rawCommand.trim().replace('@supersetbot', 'supersetbot');
const args = parseArgsStringToArgv(cmd);
const resp = await cli.parseAsync(['node', ...args]);
await cli.parseAsync(['node', ...args]);
envContext.onDone();
}

const supersetbot = {
Expand Down
2 changes: 1 addition & 1 deletion .github/supersetbot/src/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export const PROTECTED_LABEL_PATTERNS = [
'protected.*',
'^v\\d+(\\.\\d+)*$',
];
export const COMMITTER_TEAM = "apache/superset-committers";
export const COMMITTER_TEAM = 'apache/superset-committers';
5 changes: 0 additions & 5 deletions .github/supersetbot/src/run.js

This file was deleted.

6 changes: 0 additions & 6 deletions .github/supersetbot/tests/cli.js

This file was deleted.

0 comments on commit 97defd1

Please sign in to comment.