diff --git a/CODEOWNERS b/CODEOWNERS index 2e08bd2..f6e9e5d 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -1,3 +1,3 @@ # Repository CODEOWNERS -* @actions/actions-oss-maintainers +* @stakwork diff --git a/__tests__/main.test.js b/__tests__/main.test.js deleted file mode 100644 index 021a7d1..0000000 --- a/__tests__/main.test.js +++ /dev/null @@ -1,96 +0,0 @@ -/** - * Unit tests for the action's main functionality, src/main.js - */ -const core = require('@actions/core') -const main = require('../src/main') - -// Mock the GitHub Actions core library -const debugMock = jest.spyOn(core, 'debug').mockImplementation() -const getInputMock = jest.spyOn(core, 'getInput').mockImplementation() -const setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation() -const setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation() - -// Mock the action's main function -const runMock = jest.spyOn(main, 'run') - -// Other utilities -const timeRegex = /^\d{2}:\d{2}:\d{2}/ - -describe('action', () => { - beforeEach(() => { - jest.clearAllMocks() - }) - - it('sets the time output', async () => { - // Set the action's inputs as return values from core.getInput() - getInputMock.mockImplementation(name => { - switch (name) { - case 'milliseconds': - return '500' - default: - return '' - } - }) - - await main.run() - expect(runMock).toHaveReturned() - - // Verify that all of the core library functions were called correctly - expect(debugMock).toHaveBeenNthCalledWith(1, 'Waiting 500 milliseconds ...') - expect(debugMock).toHaveBeenNthCalledWith( - 2, - expect.stringMatching(timeRegex) - ) - expect(debugMock).toHaveBeenNthCalledWith( - 3, - expect.stringMatching(timeRegex) - ) - expect(setOutputMock).toHaveBeenNthCalledWith( - 1, - 'time', - expect.stringMatching(timeRegex) - ) - }) - - it('sets a failed status', async () => { - // Set the action's inputs as return values from core.getInput() - getInputMock.mockImplementation(name => { - switch (name) { - case 'milliseconds': - return 'this is not a number' - default: - return '' - } - }) - - await main.run() - expect(runMock).toHaveReturned() - - // Verify that all of the core library functions were called correctly - expect(setFailedMock).toHaveBeenNthCalledWith( - 1, - 'milliseconds not a number' - ) - }) - - it('fails if no input is provided', async () => { - // Set the action's inputs as return values from core.getInput() - getInputMock.mockImplementation(name => { - switch (name) { - case 'milliseconds': - throw new Error('Input required and not supplied: milliseconds') - default: - return '' - } - }) - - await main.run() - expect(runMock).toHaveReturned() - - // Verify that all of the core library functions were called correctly - expect(setFailedMock).toHaveBeenNthCalledWith( - 1, - 'Input required and not supplied: milliseconds' - ) - }) -}) diff --git a/__tests__/wait.test.js b/__tests__/wait.test.js deleted file mode 100644 index d58edc5..0000000 --- a/__tests__/wait.test.js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Unit tests for src/wait.js - */ -const { wait } = require('../src/wait') -const { expect } = require('@jest/globals') - -describe('wait.js', () => { - it('throws an invalid number', async () => { - const input = parseInt('foo', 10) - expect(isNaN(input)).toBe(true) - - await expect(wait(input)).rejects.toThrow('milliseconds not a number') - }) - - it('waits with a valid number', async () => { - const start = new Date() - await wait(500) - const end = new Date() - - const delta = Math.abs(end.getTime() - start.getTime()) - - expect(delta).toBeGreaterThan(450) - }) -}) diff --git a/action.yml b/action.yml index 7f69e47..31dd9ed 100644 --- a/action.yml +++ b/action.yml @@ -1,18 +1,29 @@ -name: 'The name of your action here' -description: 'Provide a description here' -author: 'Your name or organization here' +name: 'Sphixn Bot' +description: 'Sphinx Bot Github Action' +author: 'Stakwork & Sphinx' # Define your inputs here. inputs: - milliseconds: - description: 'Your input description here' + tribe_url: + description: 'URL of the tribe you want to post to' + required: true + bot_id: + description: 'BOT ID of the tribe you want to post to' + required: true + bot_secret: + description: 'BOT Secret of the tribe you want to post to' + required: true + chat_uuid: + description: 'Chat UUID of the tribe you want to post to' + required: true + bot_message: + description: 'Message to post' required: true - default: '1000' # Define your outputs here. outputs: - time: - description: 'Your output description here' + response: + description: 'Response from the bot' runs: using: node20 diff --git a/src/main.js b/src/main.js index 1654b6c..ff469d4 100644 --- a/src/main.js +++ b/src/main.js @@ -1,5 +1,5 @@ const core = require('@actions/core') -const { wait } = require('./wait') +const { http_client } = require('@actions/http-client') /** * The main function for the action. @@ -7,18 +7,27 @@ const { wait } = require('./wait') */ async function run() { try { - const ms = core.getInput('milliseconds', { required: true }) + const tribe_url = core.getInput('tribe_url', { required: true }) + const bot_id = core.getInput('bot_id', { required: true }) + const bot_secret = core.getInput('bot_secret', { required: true }) + const chat_uuid = core.getInput('chat_uuid', { required: true }) + const bot_message = core.getInput('bot_message', { required: true }) // Debug logs are only output if the `ACTIONS_STEP_DEBUG` secret is true - core.debug(`Waiting ${ms} milliseconds ...`) + core.debug(`Posting to ${tribe_url} ...`) - // Log the current timestamp, wait, then log the new timestamp - core.debug(new Date().toTimeString()) - await wait(parseInt(ms, 10)) - core.debug(new Date().toTimeString()) + const req_params = { + action: 'broadcast', + bot_id: bot_id, + bot_secret: bot_secret, + chat_uuid: chat_uuid, + content: bot_message + } + + const restRes = await http_client.postJson(tribe_url, req_params) // Set outputs for other workflow steps to use - core.setOutput('time', new Date().toTimeString()) + core.setOutput('response', restRes.result.json) } catch (error) { // Fail the workflow run if an error occurs core.setFailed(error.message) diff --git a/src/wait.js b/src/wait.js deleted file mode 100644 index f68b87c..0000000 --- a/src/wait.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Wait for a number of milliseconds. - * - * @param {number} milliseconds The number of milliseconds to wait. - * @returns {Promise} Resolves with 'done!' after the wait is over. - */ -async function wait(milliseconds) { - return new Promise(resolve => { - if (isNaN(milliseconds)) { - throw new Error('milliseconds not a number') - } - - setTimeout(() => resolve('done!'), milliseconds) - }) -} - -module.exports = { wait }