From 593c99c4782a94ea29096bc0841c0fdcdb6d3f4c Mon Sep 17 00:00:00 2001 From: Anirudh Varma Date: Sun, 13 Sep 2020 12:58:09 +0000 Subject: [PATCH] Add workflows and actions files (#38) --- .github/actions/action.yml | 5 +++++ .github/actions/index.js | 42 ++++++++++++++++++++++++++++++++++++++ .github/workflows/main.yml | 25 +++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 .github/actions/action.yml create mode 100644 .github/actions/index.js create mode 100644 .github/workflows/main.yml diff --git a/.github/actions/action.yml b/.github/actions/action.yml new file mode 100644 index 0000000..25dde25 --- /dev/null +++ b/.github/actions/action.yml @@ -0,0 +1,5 @@ +name: "Better Suggestions" +description: "A GitHub action to allow users to suggest changes alternative." +runs: + using: 'node12' + main: 'index.js' diff --git a/.github/actions/index.js b/.github/actions/index.js new file mode 100644 index 0000000..6cbf0d1 --- /dev/null +++ b/.github/actions/index.js @@ -0,0 +1,42 @@ +const fs = require("fs"); + +const PREFIX = "Suggestion:"; + +function parseURLFromIssueTitle(title) { + return title.replace(PREFIX, "").trim(); +} + +function findExistingMatchForUrl(url, list) { + return list.findIndex((listItem) => { + const match = url.match(new RegExp(listItem.urlPattern)); + return match; + }); +} + +fs.readFile(`${process.env.GITHUB_WORKSPACE}/defaultlist.json`, function (err, file) { + if (err) { + return console.log("Unable to scan directory: " + err); + } + const database = JSON.parse(file.toString()); + const gh_context = JSON.parse(process.env.gh_context); + const issueTitle = gh_context.event.issue.title; + if (!issueTitle.startsWith(PREFIX)) { + return; + } + const [suggestedUrl, name, ...descriptions] = gh_context.event.issue.body.split("\n"); + const url = parseURLFromIssueTitle(issueTitle); + const existingIndex = findExistingMatchForUrl(url, database); + console.log("Index for url", url, existingIndex); + const newItem = { url: suggestedUrl.trim(), name: name.trim(), desc: descriptions.join() }; + console.log("New Item", newItem); + if (existingIndex === -1) { + const item = { + urlPattern: url, + alternatives: [newItem], + }; + database.push(item); + } else { + database[existingIndex].alternatives.push(newItem); + } + fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/defaultlist.json`, JSON.stringify(database, null, 4)); +}); diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..169d3c8 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,25 @@ +name: Issues Workflow + +# Controls when the action will run. +on: + issues: + types: [opened] +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build" + build: + env: + gh_context: ${{ toJson(github) }} + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + - uses: actions/checkout@v2 + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: ./.github/actions/action + - uses: peter-evans/create-pull-request@v3 + name: Create Pull Request + with: + branch-suffix: random + labels: "Default List Suggestion"