From c2dd232564fb7cdde5f96d0b661020f199438e0b Mon Sep 17 00:00:00 2001 From: Nick Bolton Date: Fri, 6 Sep 2024 14:49:13 +0100 Subject: [PATCH] Move issue check logic to GitHub script (#7486) * Fixed new line strip regex * Move issue check logic to GitHub script * Fixed variable name issueBody --- .github/workflows/issue-check-support.yml | 43 ++++++++------- scripts/github.py | 66 ----------------------- 2 files changed, 24 insertions(+), 85 deletions(-) delete mode 100755 scripts/github.py diff --git a/.github/workflows/issue-check-support.yml b/.github/workflows/issue-check-support.yml index b7da9055b..af196b52b 100644 --- a/.github/workflows/issue-check-support.yml +++ b/.github/workflows/issue-check-support.yml @@ -16,24 +16,13 @@ jobs: runs-on: ubuntu-latest env: ISSUE_BODY: ${{ github.event.issue.body || github.event.inputs.issue_body }} - ISSUE_NUMBER: ${{ github.event.issue.number || github.event.inputs.issue_number }} + ISSUE_NUMBER: ${{ github.event.issue.number || github.event.inputs.issue_number }} steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Setup Python venv - uses: ./.github/actions/init-python - with: - cache-key: "issue-check-support" - - - name: Check issue - id: issue-check - run: ./scripts/github.py --issue-check-tech-support - - name: Add comment for customer - if: steps.issue-check.outputs.may-need-tech-support == 'true' env: + CUSTOMER_PATTERN: What type of Synergy user are you\?\s*Customer + NO_TICKET_PATTERN: Support ticket number \(for customers\)\s*_No response_ COMMENT_BODY: | Hello! :wave: As a customer, you're entitled to tech support which is included in your license at no extra cost. @@ -44,15 +33,31 @@ jobs: uses: actions/github-script@v6 with: script: | - issue_number = process.env.ISSUE_NUMBER - body = process.env.COMMENT_BODY + const issueNumber = process.env.ISSUE_NUMBER; + const issueBody = process.env.ISSUE_BODY; + const customerPattern = new RegExp(process.env.CUSTOMER_PATTERN); + const noTicketPattern = new RegExp(process.env.NO_TICKET_PATTERN); + + const isCustomer = customerPattern.test(issueBody); + const noTicket = noTicketPattern.test(issueBody); + + if (!isCustomer) { + console.log('Not a customer'); + return; + } + + if (!noTicket) { + console.log('Already has a ticket'); + return; + } // Strip newlines, since GitHub comments are not markdown. - body = body.replace(/\\n/g, ' ') + const commentBody = process.env.COMMENT_BODY.replace(/\n/g, ' '); + console.log('Adding comment to issue:', issueNumber); github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo, - issue_number, - body + issue_number: issueNumber, + body: commentBody }); diff --git a/scripts/github.py b/scripts/github.py deleted file mode 100755 index 4ac340d99..000000000 --- a/scripts/github.py +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env python3 - -# Synergy -- mouse and keyboard sharing utility -# Copyright (C) 2024 Symless Ltd. -# -# This package is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# found in the file LICENSE that should have accompanied this file. -# -# This package is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -import lib.env as env - -env.ensure_in_venv(__file__) - -import argparse -import sys -import re -import lib.github as github - -ISSUE_BODY_ENV = "ISSUE_BODY" - - -def main(): - parser = argparse.ArgumentParser() - parser.add_argument( - "--issue-check-tech-support", - action="store_true", - help="Check if an issue may need tech support", - ) - args = parser.parse_args() - - if args.issue_check_tech_support: - issue_check_tech_support() - else: - print("No command specified", file=sys.stderr) - sys.exit(1) - - -def issue_check_tech_support(): - print("Checking if issue may need tech support") - - issue_body = env.get_env(ISSUE_BODY_ENV) - print(f'Issue body...\n"{issue_body}"\n') - - customer_pattern = r"What type of Synergy user are you\?\s*Customer" - missing_ticket_pattern = r"Support ticket number \(for customers\)\s*_No response_" - - is_customer = re.search(customer_pattern, issue_body, re.DOTALL) - no_ticket = re.search(missing_ticket_pattern, issue_body, re.DOTALL) - - print(f"Is customer: {is_customer}") - print(f"No ticket: {no_ticket}") - - may_need_tech_support = bool(is_customer and no_ticket) - github.set_output("may-need-tech-support", str(may_need_tech_support).lower()) - - -if __name__ == "__main__": - main()