From 329f79ad04fd6e1a48081423580ed57f33b8d709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Levi=20He=C3=9Fmann?= Date: Wed, 12 Jun 2024 13:56:33 +0200 Subject: [PATCH] feat: Remove NO JIRA badge when no JIRA instance has been set, chore: Reformat code, feat: Add workflows --- .github/dependabot.yml | 11 ++ .github/{labels-and-badges.yml => labels.yml} | 0 .github/workflows/format.yml | 27 +++ README.md | 12 +- app.yml | 28 +-- package-lock.json | 21 ++- package.json | 5 +- src/config/config.ts | 64 +++---- src/index.ts | 106 ++++++----- src/labels/index.ts | 4 +- src/labels/label-helpers.ts | 95 ++++++---- src/labels/labels.ts | 176 +++++++++--------- src/labels/pull-request-labels.ts | 156 +++++++++------- src/labels/review-labels.ts | 26 +-- src/summary/change-notes.ts | 73 ++++---- src/summary/index.ts | 2 +- 16 files changed, 463 insertions(+), 343 deletions(-) create mode 100644 .github/dependabot.yml rename .github/{labels-and-badges.yml => labels.yml} (100%) create mode 100644 .github/workflows/format.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..000d73e --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,11 @@ +# To get started with Dependabot version updates, you'll need to specify which +# package ecosystems to update and where the package manifests are located. +# Please see the documentation for all configuration options: +# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates + +version: 2 +updates: + - package-ecosystem: "npm" # See documentation for possible values + directory: "/" # Location of package manifests + schedule: + interval: "daily" diff --git a/.github/labels-and-badges.yml b/.github/labels.yml similarity index 100% rename from .github/labels-and-badges.yml rename to .github/labels.yml diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml new file mode 100644 index 0000000..153ebb1 --- /dev/null +++ b/.github/workflows/format.yml @@ -0,0 +1,27 @@ +name: Format + +on: + push: + branches: + - "**" + - +jobs: + formatting: + name: Code Formatting + runs-on: ubuntu-22.04 + timeout-minutes: 10 + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Setup Node.js 20 + uses: actions/setup-node@v4 + with: + node-version: 20 + cache: npm + + - name: Install Dependencies + run: npm install + + - name: Run Tests + run: npm run test:format diff --git a/README.md b/README.md index db5d67c..34562b3 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,13 @@ # GH App Labels and Badges ## Overview + Automatically label your Pull Requests based on various criteria to streamline your workflow and make it easier to manage and track changes. You can configure the labels, sizing criteria, and other behaviors to suit your project's needs. ## Sizing Labels | Label | Description | -|-----------------|-----------------------------------------------------| +| --------------- | --------------------------------------------------- | | **PR:size/XS** | Added when additions or deletions are less than 10. | | **PR:size/S** | Added when additions or deletions are 10-30. | | **PR:size/M** | Added when additions or deletions are 30-100. | @@ -17,7 +18,7 @@ Automatically label your Pull Requests based on various criteria to streamline y ## Other Labels | Label | Description | -|--------------------------|---------------------------------------------------------------------------| +| ------------------------ | ------------------------------------------------------------------------- | | **release** | Applied when the Pull Request is raised to the main branch. | | **feature** | Applied when the branch name starts with "feat." | | **bug** | Applied when the branch name starts with "fix" or "bug." | @@ -28,10 +29,13 @@ Automatically label your Pull Requests based on various criteria to streamline y | **PR:APPROVED** | Applied when a PR is approved. | ## Change Notes + Whenever a PR is raised or updated, a comment with all the changes, including JIRA hyperlinks, is automatically added as release notes. You have the option to disable this feature via configuration. ## Configuration + ### Default Configuration + To configure the behavior of the application, create a `.github/labels-and-badges.yml` file with the following YAML structure: ```yaml @@ -79,10 +83,10 @@ If you have suggestions for how gh-app-labels-and-badges could be improved, or w For more, check out the [Contributing Guide](CONTRIBUTING.md). - > A GitHub App built with [Probot](https://github.com/probot/probot) # ROADMAP + - [ ] Setup CI/CD - [ ] Setup Linters and git hooks -- [ ] +- [ ] diff --git a/app.yml b/app.yml index af1e9a8..d3f663d 100644 --- a/app.yml +++ b/app.yml @@ -25,23 +25,23 @@ default_events: # - issue_comment - issues - label -# - milestone -# - member -# - membership -# - org_block -# - organization -# - page_build -# - project -# - project_card -# - project_column -# - public + # - milestone + # - member + # - membership + # - org_block + # - organization + # - page_build + # - project + # - project_card + # - project_column + # - public - pull_request - pull_request_review - pull_request_review_comment -# - push -# - release -# - repository -# - repository_import + # - push + # - release + # - repository + # - repository_import - status # - team # - team_add diff --git a/package-lock.json b/package-lock.json index ceed5d2..3319299 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "gh-app-labels-and-badges", - "version": "1.0.0", + "version": "1.0.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "gh-app-labels-and-badges", - "version": "1.0.0", + "version": "1.0.2", "license": "ISC", "dependencies": { "probot": "^12.2.4" @@ -16,6 +16,7 @@ "@types/node": "^18.0.0", "jest": "^29.0.0", "nock": "^13.0.5", + "prettier": "^3.3.2", "smee-client": "^1.2.2", "ts-jest": "^29.0.0", "typescript": "^4.1.3" @@ -5504,6 +5505,22 @@ "node": ">=8" } }, + "node_modules/prettier": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.3.2.tgz", + "integrity": "sha512-rAVeHYMcv8ATV5d508CFdn+8/pHPpXeIid1DdrPwXnaAdH7cqjVbpJaT5eq4yRAFU/lsbwYwSF/n5iNrdJHPQA==", + "dev": true, + "license": "MIT", + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, "node_modules/pretty-format": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-29.7.0.tgz", diff --git a/package.json b/package.json index 86c069b..e886f38 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,9 @@ "build": "tsc", "start": "yarn build && probot run ./lib/index.js", "start:prod": "probot run ./lib/index.js", - "test": "jest" + "test": "jest", + "format": "prettier --write .", + "test:format": "prettier --check ." }, "dependencies": { "probot": "^12.2.4" @@ -25,6 +27,7 @@ "@types/node": "^18.0.0", "jest": "^29.0.0", "nock": "^13.0.5", + "prettier": "^3.3.2", "smee-client": "^1.2.2", "ts-jest": "^29.0.0", "typescript": "^4.1.3" diff --git a/src/config/config.ts b/src/config/config.ts index 58b07f3..9ad8036 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -1,41 +1,41 @@ interface SizeLabelConfig { - lines: number; + lines: number; } interface ConfigFile { - jira_url: string; - pr_size_config: { - xs: SizeLabelConfig; - s: SizeLabelConfig; - m: SizeLabelConfig; - l: SizeLabelConfig; - xl: SizeLabelConfig; - xxl: SizeLabelConfig; - } - release_branch: string; - enable_change_notes: boolean; + jira_url: string; + pr_size_config: { + xs: SizeLabelConfig; + s: SizeLabelConfig; + m: SizeLabelConfig; + l: SizeLabelConfig; + xl: SizeLabelConfig; + xxl: SizeLabelConfig; + }; + release_branch: string; + enable_change_notes: boolean; } export class Config { - readonly jiraUrl: string; - readonly xsSizeLines: number; - readonly sSizeLines: number; - readonly mSizeLines: number; - readonly lSizeLines: number; - readonly xlSizeLines: number; - readonly xxlSizeLines: number; - readonly releaseBranch: string; - readonly enableChangeNotes: boolean; + readonly jiraUrl: string; + readonly xsSizeLines: number; + readonly sSizeLines: number; + readonly mSizeLines: number; + readonly lSizeLines: number; + readonly xlSizeLines: number; + readonly xxlSizeLines: number; + readonly releaseBranch: string; + readonly enableChangeNotes: boolean; - constructor(configFile: ConfigFile) { - this.jiraUrl = configFile?.jira_url || ''; - this.xsSizeLines = configFile?.pr_size_config?.xs?.lines || 10; - this.sSizeLines = configFile?.pr_size_config?.s?.lines || 30; - this.mSizeLines = configFile?.pr_size_config?.m?.lines || 100; - this.lSizeLines = configFile?.pr_size_config?.l?.lines || 500; - this.xlSizeLines = configFile?.pr_size_config?.xl?.lines || 1000; - this.xxlSizeLines = configFile?.pr_size_config?.xxl?.lines || 10000; - this.releaseBranch = configFile?.release_branch || 'main'; - this.enableChangeNotes = configFile?.enable_change_notes || false; - } + constructor(configFile: ConfigFile) { + this.jiraUrl = configFile?.jira_url || ""; + this.xsSizeLines = configFile?.pr_size_config?.xs?.lines || 10; + this.sSizeLines = configFile?.pr_size_config?.s?.lines || 30; + this.mSizeLines = configFile?.pr_size_config?.m?.lines || 100; + this.lSizeLines = configFile?.pr_size_config?.l?.lines || 500; + this.xlSizeLines = configFile?.pr_size_config?.xl?.lines || 1000; + this.xxlSizeLines = configFile?.pr_size_config?.xxl?.lines || 10000; + this.releaseBranch = configFile?.release_branch || "main"; + this.enableChangeNotes = configFile?.enable_change_notes || false; + } } diff --git a/src/index.ts b/src/index.ts index 911768c..68abc24 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,60 +1,64 @@ -import {Probot} from 'probot'; +import { Probot } from "probot"; import assignPullRequestLabels from "./labels/pull-request-labels"; import assignReviewLabels from "./labels/review-labels"; -import {createLabelsIfNotExists} from "./labels/label-helpers"; -import {addChangeNotes} from "./summary"; -import {Config} from "./config/config"; +import { createLabelsIfNotExists } from "./labels/label-helpers"; +import { addChangeNotes } from "./summary"; +import { Config } from "./config/config"; -const preHook = async (context: any): Promise<{ - existingLabels: any; - config: Config; +const preHook = async ( + context: any, +): Promise<{ + existingLabels: any; + config: Config; }> => { - await createLabelsIfNotExists(context); - const config = await context.config('labels-and-badges.yml'); - return { - existingLabels: await context.octokit.issues.listLabelsOnIssue(context.issue()), - config: new Config(config) - } -} + await createLabelsIfNotExists(context); + const config = await context.config("labels.yml"); + return { + existingLabels: await context.octokit.issues.listLabelsOnIssue( + context.issue(), + ), + config: new Config(config), + }; +}; const pullRequestHandler = async (context: any, skipChangeNotes: boolean) => { - const {existingLabels, config} = await preHook(context); - await assignPullRequestLabels(context, existingLabels, config); - if (config.enableChangeNotes && !skipChangeNotes) { - await addChangeNotes(context, config); - } -} + const { existingLabels, config } = await preHook(context); + await assignPullRequestLabels(context, existingLabels, config); + if (config.enableChangeNotes && !skipChangeNotes) { + await addChangeNotes(context, config); + } +}; export = (app: Probot) => { - app.on('pull_request.opened', async (context) => { - context.log.info('pull_request.opened') - await pullRequestHandler(context, false); - }); - - app.on('pull_request.edited', async (context) => { - context.log.info('pull_request.edited') - await pullRequestHandler(context, true); - }); - - app.on('pull_request.reopened', async (context) => { - context.log.info('pull_request.reopened') - await pullRequestHandler(context, true); - }); - - app.on('pull_request.synchronize', async (context) => { - context.log.info('pull_request.synchronize') - await pullRequestHandler(context, false); - }); - - app.on('pull_request_review.submitted', async (context) => { - context.log.info('pull_request_review.submitted') - const { existingLabels } = await preHook(context); - await assignReviewLabels(context, existingLabels); - }); - - // For more information on building apps: - // https://probot.github.io/docs/ - - // To get your app running against GitHub, see: - // https://probot.github.io/docs/development/ + app.on("pull_request.opened", async (context) => { + context.log.info("pull_request.opened"); + await pullRequestHandler(context, false); + }); + + app.on("pull_request.edited", async (context) => { + context.log.info("pull_request.edited"); + await pullRequestHandler(context, true); + }); + + app.on("pull_request.reopened", async (context) => { + context.log.info("pull_request.reopened"); + await pullRequestHandler(context, true); + }); + + app.on("pull_request.synchronize", async (context) => { + context.log.info("pull_request.synchronize"); + await pullRequestHandler(context, false); + }); + + app.on("pull_request_review.submitted", async (context) => { + context.log.info("pull_request_review.submitted"); + const { existingLabels } = await preHook(context); + await assignReviewLabels(context, existingLabels); + }); + + // For more information on building apps: + // https://probot.github.io/docs/ + + // To get your app running against GitHub, see: + // https://probot.github.io/docs/development/ }; diff --git a/src/labels/index.ts b/src/labels/index.ts index f1935f7..ad4aa62 100644 --- a/src/labels/index.ts +++ b/src/labels/index.ts @@ -1,2 +1,2 @@ -export * from './review-labels'; -export * from './pull-request-labels'; +export * from "./review-labels"; +export * from "./pull-request-labels"; diff --git a/src/labels/label-helpers.ts b/src/labels/label-helpers.ts index aa9506e..13378cf 100644 --- a/src/labels/label-helpers.ts +++ b/src/labels/label-helpers.ts @@ -8,47 +8,74 @@ import { labels } from "./labels"; * @param relevantLabelsToDelete * @param existingLabels */ -export const addLabelsOnIssue = async (context: any, labels: string[], relevantLabelsToDelete: string[], existingLabels: any) => { - if (!existingLabels) { - existingLabels = await context.octokit.issues.listLabelsOnIssue(context.issue()); - } +export const addLabelsOnIssue = async ( + context: any, + labels: string[], + relevantLabelsToDelete: string[], + existingLabels: any, +) => { + if (!existingLabels) { + existingLabels = await context.octokit.issues.listLabelsOnIssue( + context.issue(), + ); + } - const labelsToAdd = labels.filter((label) => { - return !existingLabels.data.some((existingLabel: any) => existingLabel.name === label); - }) + const labelsToAdd = labels.filter((label) => { + return !existingLabels.data.some( + (existingLabel: any) => existingLabel.name === label, + ); + }); - const labelsToDelete = relevantLabelsToDelete.filter((label) => { - return existingLabels.data.some((existingLabel: any) => existingLabel.name === label); - }).filter((label) => { - return !labels.includes(label); + const labelsToDelete = relevantLabelsToDelete + .filter((label) => { + return existingLabels.data.some( + (existingLabel: any) => existingLabel.name === label, + ); + }) + .filter((label) => { + return !labels.includes(label); }); - if (labelsToAdd.length) { - await context.octokit.issues.addLabels(context.issue({labels: labelsToAdd})); - } + if (labelsToAdd.length) { + await context.octokit.issues.addLabels( + context.issue({ labels: labelsToAdd }), + ); + } - if (labelsToDelete.length) { - await Promise.allSettled(labelsToDelete.map(async (label) => { - await context.octokit.issues.removeLabel(context.issue({name: label})); - })); - } -} + if (labelsToDelete.length) { + await Promise.allSettled( + labelsToDelete.map(async (label) => { + await context.octokit.issues.removeLabel( + context.issue({ name: label }), + ); + }), + ); + } +}; // The CreateLabels defines the labels that should be created with color and description export const createLabelsIfNotExists = async (context: any) => { - const existingLabels = await context.octokit.issues.listLabelsForRepo(context.repo()); + const existingLabels = await context.octokit.issues.listLabelsForRepo( + context.repo(), + ); - const filteredLabels = labels.filter((label) => { - return !existingLabels.data.some((existingLabel: any) => existingLabel.name === label.name); - }); + const filteredLabels = labels.filter((label) => { + return !existingLabels.data.some( + (existingLabel: any) => existingLabel.name === label.name, + ); + }); - if (filteredLabels.length) { - await Promise.allSettled(filteredLabels.map(async (label) => { - await context.octokit.issues.createLabel(context.issue({ - name: label.name, - color: label.color, - description: label.description - })); - })); - } -} + if (filteredLabels.length) { + await Promise.allSettled( + filteredLabels.map(async (label) => { + await context.octokit.issues.createLabel( + context.issue({ + name: label.name, + color: label.color, + description: label.description, + }), + ); + }), + ); + } +}; diff --git a/src/labels/labels.ts b/src/labels/labels.ts index dca06ff..3c8035a 100644 --- a/src/labels/labels.ts +++ b/src/labels/labels.ts @@ -1,93 +1,93 @@ export enum Label { - PR_XS = 'PR:size/XS', - PR_S = 'PR:size/S', - PR_M = 'PR:size/M', - PR_L = 'PR:size/L', - PR_XL = 'PR:size/XL', - PR_XXL = 'PR:size/XXL', - RELEASE = 'release', - FEATURE = 'feature', - BUG = 'bug', - CHORE = 'chore', - NOJIRA = 'NO JIRA', - PR_CHANGES_REQUESTED = 'PR:CHANGES REQUESTED', - PR_APPROVED = 'PR:APPROVED', - PR_CONFLICT = 'PR:CONFLICT' + PR_XS = "PR:size/XS", + PR_S = "PR:size/S", + PR_M = "PR:size/M", + PR_L = "PR:size/L", + PR_XL = "PR:size/XL", + PR_XXL = "PR:size/XXL", + RELEASE = "release", + FEATURE = "feature", + BUG = "bug", + CHORE = "chore", + NOJIRA = "NO JIRA", + PR_CHANGES_REQUESTED = "PR:CHANGES REQUESTED", + PR_APPROVED = "PR:APPROVED", + PR_CONFLICT = "PR:CONFLICT", } export const labels: { - name: string; - color: string; - description: string; + name: string; + color: string; + description: string; }[] = [ - { - name: Label.PR_XS, - color: 'e99695', // Light Red - description: 'Denotes a Pull Request that changes 0-9 lines.' - }, - { - name: Label.PR_S, - color: 'f4a460', // Sandy Brown - description: 'Denotes a Pull Request that changes 10-29 lines.' - }, - { - name: Label.PR_M, - color: 'ffcc66', // Light Orange - description: 'Denotes a Pull Request that changes 30-99 lines.' - }, - { - name: Label.PR_L, - color: 'ffd700', // Gold - description: 'Denotes a Pull Request that changes 100-499 lines.' - }, - { - name: Label.PR_XL, - color: 'ffdf00', // Yellow - description: 'Denotes a Pull Request that changes 500-999 lines.' - }, - { - name: Label.PR_XXL, - color: 'ffea00', // Amber - description: 'Denotes a Pull Request that changes 1000+ lines.' - }, - { - name: Label.RELEASE, - color: '00994d', // Green - description: 'This PR is a release' - }, - { - name: Label.FEATURE, - color: '33cc33', // Lime Green - description: 'This PR is a feature' - }, - { - name: Label.BUG, - color: 'ff0000', // Bright Red - description: 'This PR is a bug' - }, - { - name: Label.CHORE, - color: '6666ff', // Blue - description: 'This PR is a chore' - }, - { - name: Label.NOJIRA, - color: '00bfff', // Deep Sky Blue - description: 'This PR does not have a Jira Ticket' - }, - { - name: Label.PR_CHANGES_REQUESTED, - color: 'ff4500', // Orange Red - description: 'Review Changes are requested' - }, - { - name: Label.PR_APPROVED, - color: '008000', // Green - description: 'Review is approved' - }, - { - name: Label.PR_CONFLICT, - color: 'ff0000', // Bright Red - description: 'This PR has a conflict' - } -] + { + name: Label.PR_XS, + color: "e99695", // Light Red + description: "Denotes a Pull Request that changes 0-9 lines.", + }, + { + name: Label.PR_S, + color: "f4a460", // Sandy Brown + description: "Denotes a Pull Request that changes 10-29 lines.", + }, + { + name: Label.PR_M, + color: "ffcc66", // Light Orange + description: "Denotes a Pull Request that changes 30-99 lines.", + }, + { + name: Label.PR_L, + color: "ffd700", // Gold + description: "Denotes a Pull Request that changes 100-499 lines.", + }, + { + name: Label.PR_XL, + color: "ffdf00", // Yellow + description: "Denotes a Pull Request that changes 500-999 lines.", + }, + { + name: Label.PR_XXL, + color: "ffea00", // Amber + description: "Denotes a Pull Request that changes 1000+ lines.", + }, + { + name: Label.RELEASE, + color: "00994d", // Green + description: "This PR is a release", + }, + { + name: Label.FEATURE, + color: "33cc33", // Lime Green + description: "This PR is a feature", + }, + { + name: Label.BUG, + color: "ff0000", // Bright Red + description: "This PR is a bug", + }, + { + name: Label.CHORE, + color: "6666ff", // Blue + description: "This PR is a chore", + }, + { + name: Label.NOJIRA, + color: "00bfff", // Deep Sky Blue + description: "This PR does not have a Jira Ticket", + }, + { + name: Label.PR_CHANGES_REQUESTED, + color: "ff4500", // Orange Red + description: "Review Changes are requested", + }, + { + name: Label.PR_APPROVED, + color: "008000", // Green + description: "Review is approved", + }, + { + name: Label.PR_CONFLICT, + color: "ff0000", // Bright Red + description: "This PR has a conflict", + }, +]; diff --git a/src/labels/pull-request-labels.ts b/src/labels/pull-request-labels.ts index 6beeb89..3b679ac 100644 --- a/src/labels/pull-request-labels.ts +++ b/src/labels/pull-request-labels.ts @@ -1,85 +1,103 @@ -import {addLabelsOnIssue} from "./label-helpers"; -import {Label} from "./labels"; -import {Config} from "../config/config"; +import { addLabelsOnIssue } from "./label-helpers"; +import { Label } from "./labels"; +import { Config } from "../config/config"; -const assignPullRequestLabels = async (context: any, existingLabels: any, config: Config) => { - // Get Pull Request Title from Context - const pullRequest = context.payload.pull_request; - const pullRequestTitle = pullRequest.title; - const pullRequestFromBranch = pullRequest.head.ref; - const pullRequestToBranch = pullRequest.base.ref; - // Get number of lines added from Context - const pullRequestAdditions = pullRequest.additions; - // Get number of lines deleted from Context - const pullRequestDeletions = pullRequest.deletions; +const assignPullRequestLabels = async ( + context: any, + existingLabels: any, + config: Config, +) => { + // Get Pull Request Title from Context + const pullRequest = context.payload.pull_request; + const pullRequestTitle = pullRequest.title; + const pullRequestFromBranch = pullRequest.head.ref; + const pullRequestToBranch = pullRequest.base.ref; + // Get number of lines added from Context + const pullRequestAdditions = pullRequest.additions; + // Get number of lines deleted from Context + const pullRequestDeletions = pullRequest.deletions; - const sizeLabel = pullRequestAdditions + pullRequestDeletions; - let sizeLabelName = ''; - if (sizeLabel < config.xsSizeLines) { - sizeLabelName = Label.PR_XS; - } else if (sizeLabel < config.sSizeLines) { - sizeLabelName = Label.PR_S; - } else if (sizeLabel < config.mSizeLines) { - sizeLabelName = Label.PR_M; - } else if (sizeLabel < config.lSizeLines) { - sizeLabelName = Label.PR_L; - } else if (sizeLabel < config.xlSizeLines) { - sizeLabelName = Label.PR_XL; - } else { - sizeLabelName = Label.PR_XXL; - } - - const labelsToAdd = [sizeLabelName]; + const sizeLabel = pullRequestAdditions + pullRequestDeletions; + let sizeLabelName = ""; + if (sizeLabel < config.xsSizeLines) { + sizeLabelName = Label.PR_XS; + } else if (sizeLabel < config.sSizeLines) { + sizeLabelName = Label.PR_S; + } else if (sizeLabel < config.mSizeLines) { + sizeLabelName = Label.PR_M; + } else if (sizeLabel < config.lSizeLines) { + sizeLabelName = Label.PR_L; + } else if (sizeLabel < config.xlSizeLines) { + sizeLabelName = Label.PR_XL; + } else { + sizeLabelName = Label.PR_XXL; + } - // To Main? Add the release label - if (pullRequestToBranch === config.releaseBranch) { - labelsToAdd.push(Label.RELEASE); - } + const labelsToAdd = [sizeLabelName]; - // If the PR from branch starts with feat add the feature label - if (pullRequestFromBranch.startsWith('feat')) { - labelsToAdd.push(Label.FEATURE); - } + // To Main? Add the release label + if (pullRequestToBranch === config.releaseBranch) { + labelsToAdd.push(Label.RELEASE); + } - // If the PR from branch starts with fix or bug add the bug label - if (pullRequestFromBranch.startsWith('fix') || pullRequestFromBranch.startsWith('bug')) { - labelsToAdd.push(Label.BUG); - } + // If the PR from branch starts with feat add the feature label + if (pullRequestFromBranch.startsWith("feat")) { + labelsToAdd.push(Label.FEATURE); + } - // If the PR from branch starts with chore add the chore label - if (pullRequestFromBranch.startsWith('chore')) { - labelsToAdd.push(Label.CHORE); - } + // If the PR from branch starts with fix or bug add the bug label + if ( + pullRequestFromBranch.startsWith("fix") || + pullRequestFromBranch.startsWith("bug") + ) { + labelsToAdd.push(Label.BUG); + } - // If the PR includes the Jira ticket number add the Jira label - // Jira ticket number is in the format ABC-123 and can be in the ticket title or the branch name - // Example: A-1 - This is a Jira Ticket - // Example: feat/AB-12-this-is-a-jira-ticket - // Example: fix/ABC-123/this-is-a-jira-ticket - // Example: ABCD-1234: This is a Jira Ticket - // Example: ABCD-12345:This is a Jira Ticket - if (!pullRequestTitle.match(/([A-Z]{1,4}-\d{1,5})/g) && !pullRequestFromBranch.match(/([A-Z]{1,4}-\d{1,5})/g)) { - labelsToAdd.push(Label.NOJIRA); - } + // If the PR from branch starts with chore add the chore label + if (pullRequestFromBranch.startsWith("chore")) { + labelsToAdd.push(Label.CHORE); + } - // If the PR Has Conflict add the conflict label - if (pullRequest.mergeable_state === 'dirty') { - labelsToAdd.push(Label.PR_CONFLICT); + // If the PR includes the Jira ticket number add the Jira label + // Jira ticket number is in the format ABC-123 and can be in the ticket title or the branch name + // Example: A-1 - This is a Jira Ticket + // Example: feat/AB-12-this-is-a-jira-ticket + // Example: fix/ABC-123/this-is-a-jira-ticket + // Example: ABCD-1234: This is a Jira Ticket + // Example: ABCD-12345:This is a Jira Ticket + if ( + !pullRequestTitle.match(/([A-Z]{1,4}-\d{1,5})/g) && + !pullRequestFromBranch.match(/([A-Z]{1,4}-\d{1,5})/g) + ) { + if (config.jiraUrl && config.jiraUrl.length > 0) { + labelsToAdd.push(Label.NOJIRA); } + } - const labelsToDelete = [Label.PR_XS, Label.PR_S, Label.PR_M, Label.PR_L, Label.PR_XL, Label.PR_XXL]; + // If the PR Has Conflict add the conflict label + if (pullRequest.mergeable_state === "dirty") { + labelsToAdd.push(Label.PR_CONFLICT); + } - if (pullRequest.mergeable_state === 'clean') { - labelsToDelete.push(Label.PR_CONFLICT); - } + const labelsToDelete = [ + Label.PR_XS, + Label.PR_S, + Label.PR_M, + Label.PR_L, + Label.PR_XL, + Label.PR_XXL, + ]; + if (pullRequest.mergeable_state === "clean") { + labelsToDelete.push(Label.PR_CONFLICT); + } - context.log.debug(`Pull Request Title: ${pullRequestTitle}`); - context.log.debug(`Adding Labels: ${labelsToAdd}`); - context.log.debug(`Deleting Labels: ${labelsToDelete}`); - context.log.debug(`Existing Labels: ${existingLabels}`); + context.log.debug(`Pull Request Title: ${pullRequestTitle}`); + context.log.debug(`Adding Labels: ${labelsToAdd}`); + context.log.debug(`Deleting Labels: ${labelsToDelete}`); + context.log.debug(`Existing Labels: ${existingLabels}`); - await addLabelsOnIssue(context, labelsToAdd, labelsToDelete, existingLabels) -} + await addLabelsOnIssue(context, labelsToAdd, labelsToDelete, existingLabels); +}; export default assignPullRequestLabels; diff --git a/src/labels/review-labels.ts b/src/labels/review-labels.ts index ae3f20d..446dfb2 100644 --- a/src/labels/review-labels.ts +++ b/src/labels/review-labels.ts @@ -1,19 +1,21 @@ -import {addLabelsOnIssue} from "./label-helpers"; -import {Label} from "./labels"; +import { addLabelsOnIssue } from "./label-helpers"; +import { Label } from "./labels"; const assignReviewLabels = async (context: any, existingLabels: any) => { - const labelsToDelete = [Label.PR_CHANGES_REQUESTED, Label.PR_APPROVED]; - const labelsToAdd = []; + const labelsToDelete = [Label.PR_CHANGES_REQUESTED, Label.PR_APPROVED]; + const labelsToAdd = []; - context.log.info("context.payload.review.state: " + context.payload.review.state); + context.log.info( + "context.payload.review.state: " + context.payload.review.state, + ); - if (context.payload.review.state === 'changes_requested') { - labelsToAdd.push(Label.PR_CHANGES_REQUESTED); - } else if (context.payload.review.state === 'approved') { - labelsToAdd.push(Label.PR_APPROVED); - } + if (context.payload.review.state === "changes_requested") { + labelsToAdd.push(Label.PR_CHANGES_REQUESTED); + } else if (context.payload.review.state === "approved") { + labelsToAdd.push(Label.PR_APPROVED); + } - await addLabelsOnIssue(context, labelsToAdd, labelsToDelete, existingLabels) -} + await addLabelsOnIssue(context, labelsToAdd, labelsToDelete, existingLabels); +}; export default assignReviewLabels; diff --git a/src/summary/change-notes.ts b/src/summary/change-notes.ts index 06b89c2..dfd89fb 100644 --- a/src/summary/change-notes.ts +++ b/src/summary/change-notes.ts @@ -1,35 +1,42 @@ -import {Config} from "../config/config"; +import { Config } from "../config/config"; export const addChangeNotes = async (context: any, config: Config) => { - const pullRequest = context.payload.pull_request; - - const commits = await context.octokit.pulls.listCommits({ - owner: context.payload.repository.owner.login, - repo: context.payload.repository.name, - pull_number: pullRequest.number - }); - - // Jira Ticket Number - const jiraTicketNumber = pullRequest.title.match(/([A-Z]{1,4}-\d{1,5})/g) || pullRequest.head.ref.match(/([A-Z]{1,4}-\d{1,5})/g); - const jiraTicketNumberString = jiraTicketNumber ? jiraTicketNumber[0] : ''; - - const changeNotes = commits.data.map((commit: any) => { - // Replace Jira Ticket Number with Link - return commit.commit.message.replace(/([A-Z]{1,4}-\d{1,5})/g, `[$1](${config.jiraUrl}/browse/$1)`); - }).join('\n'); - - let body = ''; - - if (jiraTicketNumberString) { - body = `## Jira Ticket \n [${jiraTicketNumberString}](${config.jiraUrl}/browse/${jiraTicketNumberString})\n\n`; - } - - body += `## Change Notes\n\n${changeNotes}`; - - await context.octokit.issues.createComment({ - owner: context.payload.repository.owner.login, - repo: context.payload.repository.name, - issue_number: pullRequest.number, - body - }); -} + const pullRequest = context.payload.pull_request; + + const commits = await context.octokit.pulls.listCommits({ + owner: context.payload.repository.owner.login, + repo: context.payload.repository.name, + pull_number: pullRequest.number, + }); + + // Jira Ticket Number + const jiraTicketNumber = + pullRequest.title.match(/([A-Z]{1,4}-\d{1,5})/g) || + pullRequest.head.ref.match(/([A-Z]{1,4}-\d{1,5})/g); + const jiraTicketNumberString = jiraTicketNumber ? jiraTicketNumber[0] : ""; + + const changeNotes = commits.data + .map((commit: any) => { + // Replace Jira Ticket Number with Link + return commit.commit.message.replace( + /([A-Z]{1,4}-\d{1,5})/g, + `[$1](${config.jiraUrl}/browse/$1)`, + ); + }) + .join("\n"); + + let body = ""; + + if (jiraTicketNumberString) { + body = `## Jira Ticket \n [${jiraTicketNumberString}](${config.jiraUrl}/browse/${jiraTicketNumberString})\n\n`; + } + + body += `## Change Notes\n\n${changeNotes}`; + + await context.octokit.issues.createComment({ + owner: context.payload.repository.owner.login, + repo: context.payload.repository.name, + issue_number: pullRequest.number, + body, + }); +}; diff --git a/src/summary/index.ts b/src/summary/index.ts index d5dbd0b..88f6d66 100644 --- a/src/summary/index.ts +++ b/src/summary/index.ts @@ -1 +1 @@ -export * from './change-notes'; +export * from "./change-notes";