Add workflows and actions files (#38)

This commit is contained in:
Anirudh Varma 2020-09-13 12:58:09 +00:00 committed by GitHub
parent 1c4e6ffcfb
commit 593c99c478
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 0 deletions

5
.github/actions/action.yml vendored Normal file
View File

@ -0,0 +1,5 @@
name: "Better Suggestions"
description: "A GitHub action to allow users to suggest changes alternative."
runs:
using: 'node12'
main: 'index.js'

42
.github/actions/index.js vendored Normal file
View File

@ -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));
});

25
.github/workflows/main.yml vendored Normal file
View File

@ -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"