Skip to content

Commit

Permalink
Merge pull request #6 from soichisumi/add-test
Browse files Browse the repository at this point in the history
Add test
  • Loading branch information
soichisumi authored Dec 25, 2019
2 parents 323d1eb + 24586c0 commit 7a182d1
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 92 deletions.
14 changes: 2 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ on:
pull_request:
push:
branches:
# - master
- 'releases/*'
- master

jobs:
# unit tests
Expand All @@ -13,13 +12,4 @@ jobs:
steps:
- uses: actions/checkout@v1
- run: npm ci
- run: npm test

# test action works running from the graph
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: ./
with:
milliseconds: 1000
- run: npm test
60 changes: 4 additions & 56 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,5 @@
const core = require('@actions/core');
const github = require('@actions/github');

const getIssueNumber = () => {
const issue = github.context.payload.issue;
if (!issue) {
throw new Error("No issue provided");
}
return issue.number;
};

// getIssue from context
const getIssue = async (token) => {
let octocat = new github.GitHub(token);
const issueNum = getIssueNumber();

const repo = github.context.repo;
const issue = await octocat.issues.get({
owner: repo.owner,
repo: repo.repo,
issue_number: issueNum,
});

return issue;
};

const checkKeywords = (keywords, body) => {
const lowerBody = body.toLowerCase();
for(let k of keywords) {
if (lowerBody.toLowerCase().includes(k)){
return true;
}
}
return false;
};

const createNewIssue = async (token, owner, repoName, title, body, assignees, labels, fromIssue) => {
const octokit = new github.GitHub(token);
if (fromIssue) {
body = body + `\n\ncopiedFrom: ${fromIssue}`;
}

const res = await octokit.issues.create(
{
owner: owner,
repo: repoName,
title: title,
body: body,
assignees: assignees,
labels: labels,
}
);
return [res.id, res.number].join(':');
};
const lib = require('./lib');

async function run() {
try {
Expand All @@ -63,14 +11,14 @@ async function run() {
const repoName = splitted[1];

const token = core.getInput('githubToken');
const issue = await getIssue(token);
const issue = await lib.getIssue(token);

if (!checkKeywords([keyword], issue.data.body)){
if (!lib.checkKeywords([keyword], issue.data.body)){
console.log("Keyword not included");
return;
}

const created = await createNewIssue(token, owner, repoName, issue.data.title, 'this is body', ['soichisumi'], [], issue.data.html_url);
const created = await lib.createNewIssue(token, owner, repoName, issue.data.title, 'this is body', ['soichisumi'], [], issue.data.html_url);

core.setOutput('created', created);
}
Expand Down
23 changes: 0 additions & 23 deletions index.test.js

This file was deleted.

57 changes: 57 additions & 0 deletions lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const github = require('@actions/github');

const getIssueNumber = () => {
const issue = github.context.payload.issue;
if (!issue) {
throw new Error("No issue provided");
}
return issue.number;
};
module.exports.getIssueNumber = getIssueNumber;

// getIssue from context
const getIssue = async (token) => {
let octocat = new github.GitHub(token);
const issueNum = getIssueNumber();

const repo = github.context.repo;
const issue = await octocat.issues.get({
owner: repo.owner,
repo: repo.repo,
issue_number: issueNum,
});

return issue;
};
module.exports.getIssue = getIssue;

const checkKeywords = (keywords, body) => {
const lowerBody = body.toLowerCase();
for(let k of keywords) {
if (lowerBody.toLowerCase().includes(k.toLowerCase())){
return true;
}
}
return false;
};
module.exports.checkKeywords = checkKeywords;

const createNewIssue = async (token, owner, repoName, title, body, assignees, labels, fromIssue) => {
const octokit = new github.GitHub(token);
if (fromIssue) {
body = body + `\n\ncopiedFrom: ${fromIssue}`;
}

const res = await octokit.issues.create(
{
owner: owner,
repo: repoName,
title: title,
body: body,
assignees: assignees,
labels: labels,
}
);
return [res.id, res.number].join(':');
};
module.exports.createNewIssue = createNewIssue;
14 changes: 14 additions & 0 deletions lib.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const lib = require('./lib');


describe('checkKeyword', () => {
it('return true if any keyword is found in given string', () => {
expect(lib.checkKeywords(['/copy'], 'body of issue_comment. /copy')).toBe(true);
});
it('return false if any keyword is not found', ()=> {
expect(lib.checkKeywords(['/copy'], 'body of issue_comment.')).toBe(false);
});
it('multiple keyword', () => {
expect(lib.checkKeywords(['/copy', 'COPY'], 'body of issue_comment. copy')).toBe(true);
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "issue-copy-action",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"main": "index.js",
"scripts": {
"lint": "eslint index.js --fix",
"package": "ncc build index.js -o dist",
Expand Down

0 comments on commit 7a182d1

Please sign in to comment.