From 51cc9e29e92f8e28133303eed1447fb579a03b11 Mon Sep 17 00:00:00 2001 From: Renato Vicente Date: Sat, 6 Apr 2024 18:38:04 +0200 Subject: [PATCH] feat: add missing files --- .github/FUNDING.yml | 1 + .../workflows/{unit-tests.yml => checks.yml} | 3 +- .github/workflows/codecov.yml | 6 + .prettierrc.json | 10 ++ .vscode/extensions.json | 6 + CONTRIBUTING.md | 30 +++++ LICENSE | 22 +++ README.md | 127 ++++++++++++++++-- bun.lockb | Bin 13382 -> 13794 bytes package.json | 8 +- src/domain/labelsNotAllowed.ts | 16 +-- src/scraper/text.ts | 4 +- src/service/github.ts | 38 +++--- 13 files changed, 232 insertions(+), 39 deletions(-) create mode 100644 .github/FUNDING.yml rename .github/workflows/{unit-tests.yml => checks.yml} (92%) create mode 100644 .prettierrc.json create mode 100644 .vscode/extensions.json create mode 100644 CONTRIBUTING.md create mode 100644 LICENSE diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..d18dffd --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: Renato66 diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/checks.yml similarity index 92% rename from .github/workflows/unit-tests.yml rename to .github/workflows/checks.yml index a150222..9d6e1f1 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/checks.yml @@ -1,4 +1,4 @@ -name: Unit test +name: Checks on: pull_request: @@ -17,4 +17,5 @@ jobs: restore-keys: | ${{ runner.os }}-bun-${{ github.ref }} - run: bun install + - run: bun tslint - run: bun test diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index e5be102..9659f23 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -13,6 +13,12 @@ jobs: steps: - uses: actions/checkout@v4 - uses: oven-sh/setup-bun@v1 + - uses: actions/cache@v3 + with: + path: ~/.bun + key: ${{ runner.os }}-bun-${{ hashFiles('**/bun.lock') }} + restore-keys: | + ${{ runner.os }}-bun-${{ github.ref }} - run: bun install - run: bun test --coverage # diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..c34bafc --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,10 @@ +{ + "printWidth": 80, + "tabWidth": 2, + "useTabs": false, + "semi": false, + "singleQuote": true, + "trailingComma": "none", + "bracketSpacing": false, + "arrowParens": "avoid" +} diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..dbdae76 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,6 @@ +{ + "recommendations": [ + "streetsidesoftware.code-spell-checker", + "esbenp.prettier-vscode" + ] +} \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..a68edfc --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,30 @@ +# Bun + +> [!NOTE] +> You will need to have [bun](https://bun.sh/docs/installation) installed + +To install dependencies: + +```bash +bun install +``` + +To run: + +```bash +bun run test +``` + +This project was created using `bun init` in bun v1.1.0. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. + +# Docker + +> [!NOTE] +> You will need to have [docker](https://www.docker.com/products/docker-desktop/) installed + + +To create a docker image: + +``` +docker build --pull -t auto-label . +``` diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6bd8851 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ + +The MIT License (MIT) + +Copyright (c) 2018-2024 Renato Vicente and contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md index 3f41059..5ccf4bd 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,126 @@ -# auto-label +# Auto-label -To install dependencies: +![Unit test](https://github.com/Renato66/auto-label/workflows/Unit%20test/badge.svg) +[![Auto Label](https://github.com/Renato66/auto-label/workflows/Labeling%20new%20issue/badge.svg)](https://github.com/Renato66/auto-label) +[![codecov](https://codecov.io/gh/Renato66/auto-label/branch/master/graph/badge.svg)](https://codecov.io/gh/Renato66/auto-label) + +![image](https://user-images.githubusercontent.com/9284273/79672530-57c1db80-81a9-11ea-900c-3b4f73984e0a.png) + +The Auto label action will check for every new issue and automatically adds a label based on the body of the issue. This means that finding specifc issues will be much more easy. + +> [!WARNING] +> The main branch is being refactored, use the stable one at [master](https://github.com/Renato66/auto-label/tree/master) subscribe to [v3](https://github.com/Renato66/auto-label/issues/75) to get the latest version when it's released + +## Creating + +Check out the app to make yml file +[https://renato66.github.io/auto-label/](https://renato66.github.io/auto-label/) + +or + +add a file to `.github/workflows/issue.yml` + +```yml +name: Labeling new issue +on: + issues: + types: ['opened'] +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: Renato66/auto-label@v2 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + ignore-comments: true + labels-synonyms: '{"bug":["error","need fix","not working"],"enhancement":["upgrade"],"question":["help"]}' + labels-not-allowed: '["good first issue"]' + default-labels: '["help wanted"]' +``` + +## Inputs + +| Name | Description | Required | Default | Examples | +| ------------------ | ----------------------------------- | -------- | ------- | :------------------------: | +| repo-token | Github token for the repository | true | - | [...](#repo-token) | +| ignore-comments | Ignore labels inside issue comments | false | true | [...](#ignore-comments) | +| labels-synonyms | Text synonyms for labels | false | - | [...](#labels-synonyms) | +| labels-not-allowed | Labels to ignore | false | - | [...](#labels-not-allowed) | +| default-labels | Labels that will always be set | false | - | [...](#default-labels) | + +### Repo Token + +Repo token is provided automaticly by github just need to add + +``` +repo-token: ${{ secrets.GITHUB_TOKEN }} +``` + +#### Change bot apperance + +If you want to change who added the labels, you can provide a user token + +![image](https://user-images.githubusercontent.com/9284273/79672221-678bf080-81a6-11ea-908e-fb875772121a.png) + +![image](https://user-images.githubusercontent.com/9284273/79672289-e123de80-81a6-11ea-9faa-237adc0873f0.png) + +The [token](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line) will only need the `public_repo` scope + +You will need to provide it as [secret](https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets) `GITHUB_USER_TOKEN` +and update the issue.yml repo-token with: +`repo-token: ${{ secrets.GITHUB_USER_TOKEN }}` + +### Ignore Comments + +If you prefer to leave as default (true) You can provide a list of labels in a [template](https://help.github.com/en/github/building-a-strong-community/configuring-issue-templates-for-your-repository#configuring-the-template-chooser) for the author to choose wich labels are avaliable to pick +But if you prefer to set it as false, the author can set the labels inside a comment so it doesn't appear on issue body + +### Scoped blocks + +You can set a custom part of your issue/pr to be labeled using this structure: + +``` +Some text that could have a label that wouldn't be set + +Another text that could have a label and will be set as a label + +``` + +thanks to [@dielduarte](https://github.com/dielduarte) and [@PauloGoncalvesBH](https://github.com/PauloGoncalvesBH) + +### Labels Synonyms + +Sometimes labels can be set uppon other texts, as an example, if you have a label like `C: VCombobox` it would be hard to match the label in a normal issue, but you can provide a JSON to set it synonyms: -```bash -bun install ``` +labels-synonyms: '{"C: VCombobox":["combobox","v-combobox","combo box"]}' +``` + +### Labels Not Allowed -To run: +Some labels are restricted to repo owners such as `needs priority` `testing` `won't fix` they can be set in an Array form: -```bash -bun run index.ts +``` +labels-not-allowed: '["needs priority","testing","won't fix"]' ``` -This project was created using `bun init` in bun v1.1.0. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. +### Default Labels + +Labels that will always be set when a issue is created/updated such as `triage`, they can be set in an Array form: + +``` +default-labels: '["triage"]' +``` + +## Badge + +To add a badge simple replace owner and repository + + + +[![Auto Label](https://github.com/Renato66/auto-label/workflows/Labeling%20new%20issue/badge.svg)](https://github.com/Renato66/auto-label) + +``` + +[![Auto Label](https://github.com///workflows/Labeling%20new%20issue/badge.svg)](https://github.com/Renato66/auto-label) +``` diff --git a/bun.lockb b/bun.lockb index 76e3b5fb76e11e48c2153eea8a290ce25cad6fb3..7183ea4497746bb7649b3d44af85d80ac7a62f20 100755 GIT binary patch delta 1416 zcmcgsZAepL6yAINXm>UdKek=BT5ea}rrmAMnU*@qC{6lM%uzvU(uWEe6!bw~Vi6I7 zcAzhc5m6NNBN;(u(g$Hh64bwleiRWJEDHKr*>mO$3;gSW=bra@&U?>$@8O(xWXH#@ ze$AcvX=K^w_6M6{H_LxCHy;?^-uvWk+Gk_iX4CT5?^+&iZVKw+<{SvyC9`v2-y_8U z0XVv|yS65TF1FTyT z`gG5EZ2;$;5AgmxfDQggJm3W34fy~kDgfA~dyc!}>c5^77A5rQp7RtZboR^_0-VR2 z;69um5BMRlO?h6=N@Z~VOr@EX%D87P&3~1NRv44@HGA61S|)5)H-3Bf@~6%D<;8Gi zR@vi5TkW;6`r*Z22VKnvJ3iPAklZllN;MV27&jCyT&kWDSS``mic0LYt z$PO&$0Tx>4JgZr;$^{gFhlB@Y0boz~Joq}!3|s1DCSbs9{l}WoFI2sj(lmnFobweD=EX-{ah`S@Vyly@K06-alV> zwzso~Vl8IItc;D+K^ylc{?=@CT@5&PAQX4%@m!_6GD3Mv5L)j;$b5_)%})5yp#=ei z5(N>mT&4MK+331bqOk7gDu?<4=P<2S5z4kh*9a=vhSK;y{r)iJ&er2E|NrlQAHQ44 zU|&H|$scQ=O^HSed?`-B`(hD}I8t1KDMuP69U}ZdPlkieG&DO!n0BUk39O;_4ZY*8 z6!*eCR~qiRL=dGEFVos3S%3%9GPHV(U^G?1ykLiALxncu37c#k{%kSWc|VjXjc`gS zwCa(70vSc!OD9QXszm67E07REr(cek5Nb!f5v&T?j?jry76r+N4~iV5Q>Ol*=CdQk z2sJJ>XDcm3+X6$1l2?W!53~m+R5y+!#D!3kQiFPtQeAzhjg%D47lP7YIWz~Gv^&8S zgItGO!iz8>KN0IceaCWOVaLq|9QSHe;EnEt|O jZTva!-}Lj1zU+QXb5};p@H8fC)6tUx9E$FTrudEDPdus< diff --git a/package.json b/package.json index 216faa6..9f83830 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,15 @@ "version": "3.0.0", "module": "src/index.ts", "type": "module", + "scripts": { + "test": "bun test", + "coverage": "bun test --coverage", + "tslint": "bun run node_modules/typescript/lib/tsc.js --noEmit" + }, "devDependencies": { "@types/bun": "latest", - "nock": "^13.5.4" + "nock": "^13.5.4", + "typescript": "^5.4.4" }, "peerDependencies": { "typescript": "^5.0.0" diff --git a/src/domain/labelsNotAllowed.ts b/src/domain/labelsNotAllowed.ts index 9264613..7c97db1 100644 --- a/src/domain/labelsNotAllowed.ts +++ b/src/domain/labelsNotAllowed.ts @@ -1,15 +1,15 @@ -import * as core from "@actions/core"; +import * as core from '@actions/core' export const removeLabelsNotAllowed = (labels: string[]): string[] => { - const labelsNotAllowed = core.getInput("labels-not-allowed"); + const labelsNotAllowed = core.getInput('labels-not-allowed') if (!labelsNotAllowed) { - return labels; + return labels } - const labelsToIgnore = JSON.parse(core.getInput("labels-not-allowed")); + const labelsToIgnore = JSON.parse(core.getInput('labels-not-allowed')) return labels.filter((elem: string) => { return ( labelsToIgnore.find((label: string) => { - return label.toLowerCase() === elem.toLowerCase(); + return label.toLowerCase() === elem.toLowerCase() }) === undefined - ); - }); -}; \ No newline at end of file + ) + }) +} diff --git a/src/scraper/text.ts b/src/scraper/text.ts index 52ade22..ac3e8c5 100644 --- a/src/scraper/text.ts +++ b/src/scraper/text.ts @@ -1,10 +1,10 @@ import * as core from '@actions/core' -const escapeRegExp: Function = (string: String): String => { +const escapeRegExp = (string: String): String => { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') // $& means the whole matched string } -const compareLabels = (labels: string[]): (line: string) => string[] => { +const compareLabels = (labels: string[]): ((line: string) => string[]) => { const labelsSynonyms = getLabelsSynonyms() const hasSynonyms = Object.keys(labelsSynonyms).length !== 0 if (hasSynonyms) { diff --git a/src/service/github.ts b/src/service/github.ts index d2ed360..7bc060d 100644 --- a/src/service/github.ts +++ b/src/service/github.ts @@ -1,28 +1,28 @@ -import * as github from "@actions/github"; +import * as github from '@actions/github' // TODO: get github type const getRepoLabels = async (client: any): Promise => { - let list: any = []; - let page: number = 1; - let hasMorePages: Boolean = false; + let list: any = [] + let page: number = 1 + let hasMorePages: Boolean = false do { - const { data: labelList } = await client.rest.issues.listLabelsForRepo({ + const {data: labelList} = await client.rest.issues.listLabelsForRepo({ owner: github.context.repo.owner, repo: github.context.repo.repo, per_page: 100, - page: page, - }); - list = [...list, ...labelList]; - hasMorePages = labelList.length === 100; - page++; - } while (hasMorePages); + page: page + }) + list = [...list, ...labelList] + hasMorePages = labelList.length === 100 + page++ + } while (hasMorePages) return list.map((elem: any) => { - return elem.name; - }); -}; + return elem.name + }) +} -const addLabels: Function = async ( +const addLabels = async ( client: any, issueNumber: number, labels: string[] @@ -31,7 +31,7 @@ const addLabels: Function = async ( owner: github.context.repo.owner, repo: github.context.repo.repo, issue_number: issueNumber, - labels: labels, - }); -}; -export { getRepoLabels, addLabels }; + labels: labels + }) +} +export {getRepoLabels, addLabels}