|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +const DIALECTS = [ |
| 19 | + { label: "BigQuery", stems: ["bigquery"], pattern: /\bbig\s?query\b/i }, |
| 20 | + { label: "ClickHouse", stems: ["clickhouse"], pattern: /\bclick\s?house\b/i }, |
| 21 | + { label: "Databricks", stems: ["databricks"], pattern: /\bdatabricks\b/i }, |
| 22 | + { label: "DuckDB", stems: ["duckdb"], pattern: /\bduck\s?db\b/i }, |
| 23 | + { label: "Hive", stems: ["hive"], pattern: /\bhive\b/i }, |
| 24 | + { label: "MySQL", stems: ["mysql"], pattern: /\b(mysql|maria\s?db)\b/i }, |
| 25 | + { label: "Oracle", stems: ["oracle"], pattern: /\boracle\b/i }, |
| 26 | + { label: "PostgreSQL", stems: ["postgresql", "postgres"], pattern: /\bpostgres/i }, |
| 27 | + { label: "Redshift", stems: ["redshift"], pattern: /\bredshift\b/i }, |
| 28 | + { label: "Snowflake", stems: ["snowflake"], pattern: /\bsnowflake\b/i }, |
| 29 | + { label: "Spark", stems: ["spark"], pattern: /\bspark\b/i }, |
| 30 | + { label: "SQL Server", stems: ["mssql"], pattern: /\b(ms\s?sql|sql server|t-?sql)\b/i }, |
| 31 | + { label: "SQLite", stems: ["sqlite"], pattern: /\bsqlite\b/i }, |
| 32 | + { label: "Teradata", stems: ["teradata"], pattern: /\bteradata\b/i }, |
| 33 | +]; |
| 34 | + |
| 35 | +const DECLARATION = /^[ \t]*dialects?[ \t]*:[ \t]*(.*?)[ \t]*$/im; |
| 36 | +const MARKER = "<!-- dialect-labeler -->"; |
| 37 | +const TRUSTED_ASSOCIATIONS = ["OWNER", "MEMBER", "COLLABORATOR"]; |
| 38 | + |
| 39 | +function matchDialects(text) { |
| 40 | + return DIALECTS.filter((d) => d.pattern.test(text)).map((d) => d.label); |
| 41 | +} |
| 42 | + |
| 43 | +// `undefined` when no `Dialects:` line exists, otherwise the labels it names (possibly none). |
| 44 | +function declaredDialects(text) { |
| 45 | + const value = DECLARATION.exec(text ?? "")?.[1]; |
| 46 | + return value ? matchDialects(value) : undefined; |
| 47 | +} |
| 48 | + |
| 49 | +function stripMarkup(text) { |
| 50 | + return text.replace(/<!--[\s\S]*?-->/g, "").replace(/```[\s\S]*?```/g, ""); |
| 51 | +} |
| 52 | + |
| 53 | +function resolve({ title, body }) { |
| 54 | + body ??= ""; |
| 55 | + const labels = new Set(matchDialects(title)); |
| 56 | + const declared = declaredDialects(body); |
| 57 | + for (const label of declared ?? []) labels.add(label); |
| 58 | + if (labels.size === 0 && declared === undefined) { |
| 59 | + for (const label of matchDialects(stripMarkup(body))) labels.add(label); |
| 60 | + } |
| 61 | + return { labels: [...labels], answered: labels.size > 0 || declared !== undefined }; |
| 62 | +} |
| 63 | + |
| 64 | +function touchedDialects(paths) { |
| 65 | + const stems = new Set(); |
| 66 | + for (const path of paths) { |
| 67 | + const stem = /^src\/dialect\/(\w+)\.rs$|^tests\/sqlparser_(\w+)\.rs$/.exec(path); |
| 68 | + if (stem) stems.add(stem[1] ?? stem[2]); |
| 69 | + } |
| 70 | + return DIALECTS.filter((d) => d.stems.some((s) => stems.has(s))).map((d) => d.label); |
| 71 | +} |
| 72 | + |
| 73 | +function isCodeChange(paths) { |
| 74 | + return paths.some((path) => path.startsWith("src/") || path.startsWith("tests/")); |
| 75 | +} |
| 76 | + |
| 77 | +function askComment(touched) { |
| 78 | + const lines = [ |
| 79 | + MARKER, |
| 80 | + "No dialect label was applied because the title does not name a SQL dialect.", |
| 81 | + "If this change targets one or more dialects, edit the title to `<Dialect>[, <Dialect>]: <description>`, add a `Dialects: <Dialect>[, <Dialect>]` line to the description, or reply with one. Reply `Dialects: none` if the change is dialect-agnostic.", |
| 82 | + ]; |
| 83 | + if (touched.length) lines.push(`Changed files touch ${touched.join(", ")}.`); |
| 84 | + return lines.join("\n\n"); |
| 85 | +} |
| 86 | + |
| 87 | +function trustedComment(comment, issue) { |
| 88 | + if (comment.user.type === "Bot") return false; |
| 89 | + return comment.user.id === issue.user.id || TRUSTED_ASSOCIATIONS.includes(comment.author_association); |
| 90 | +} |
| 91 | + |
| 92 | +async function run({ github, context }) { |
| 93 | + const { owner, repo } = context.repo; |
| 94 | + const { payload } = context; |
| 95 | + const issue = payload.pull_request ?? payload.issue; |
| 96 | + const issue_number = issue.number; |
| 97 | + let labels = []; |
| 98 | + let comment; |
| 99 | + |
| 100 | + if (context.eventName === "issue_comment") { |
| 101 | + if (!trustedComment(payload.comment, issue)) return; |
| 102 | + labels = declaredDialects(payload.comment.body) ?? []; |
| 103 | + } else { |
| 104 | + const resolved = resolve(issue); |
| 105 | + labels = resolved.labels; |
| 106 | + if (!resolved.answered && payload.pull_request && payload.action === "opened") { |
| 107 | + const files = await github.paginate(github.rest.pulls.listFiles, { |
| 108 | + owner, |
| 109 | + repo, |
| 110 | + pull_number: issue_number, |
| 111 | + per_page: 100, |
| 112 | + }); |
| 113 | + const paths = files.map((f) => f.filename); |
| 114 | + if (isCodeChange(paths)) comment = askComment(touchedDialects(paths)); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + const existing = new Set(issue.labels.map((l) => l.name)); |
| 119 | + const missing = labels.filter((l) => !existing.has(l)); |
| 120 | + if (missing.length) { |
| 121 | + await github.rest.issues.addLabels({ owner, repo, issue_number, labels: missing }); |
| 122 | + } |
| 123 | + |
| 124 | + if (comment) { |
| 125 | + const comments = await github.paginate(github.rest.issues.listComments, { |
| 126 | + owner, |
| 127 | + repo, |
| 128 | + issue_number, |
| 129 | + per_page: 100, |
| 130 | + }); |
| 131 | + if (comments.some((c) => c.body?.includes(MARKER))) return; |
| 132 | + await github.rest.issues.createComment({ owner, repo, issue_number, body: comment }); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +module.exports = run; |
0 commit comments