diff --git a/README.md b/README.md index cdd991c..b1b0efe 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,20 @@ -# Container Action Template +# First Interaction -To get started, click the `Use this template` button on this repository [which will create a new repository based on this template](https://github.blog/2019-06-06-generate-new-repositories-with-repository-templates/). \ No newline at end of file +An action for filtering pull requests and issues from first-time contributors. + +# Usage + +See [action.yml](action.yml) + +```yaml +steps: +- uses: actions/first-interaction@v1 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + issue-message: '# Mesage with markdown.\nThis is the message that will be displayed on users' first issue.' + pr-message: 'Message that will be displayed on users' first pr. Look, a `code block` for markdown.' +``` + +# License + +The scripts and documentation in this project are released under the [MIT License](LICENSE) diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts deleted file mode 100644 index 7df9bad..0000000 --- a/__tests__/main.test.ts +++ /dev/null @@ -1,4 +0,0 @@ -describe('TODO - Add a test suite', () => { - it('TODO - Add a test', async () => { - }); -}); diff --git a/action.yml b/action.yml index 84c30ab..ca3bfdd 100644 --- a/action.yml +++ b/action.yml @@ -1,10 +1,14 @@ -name: 'Container Action Template' +name: 'First interaction' description: 'Get started with Container actions' author: 'GitHub' -inputs: - myInput: - description: 'Input to use' - default: 'world' +inputs: + repo-token: + description: 'Token for the repo. Can be passed in using {{ secrets.GITHUB_TOKEN }}' + required: true + issue-message: + description: 'Comment to post on an individuals first issue' + pr-message: + description: 'Comment to post on an individuals first pull request' runs: using: 'docker' image: 'Dockerfile' \ No newline at end of file diff --git a/jest.config.js b/jest.config.js deleted file mode 100644 index 563d4cc..0000000 --- a/jest.config.js +++ /dev/null @@ -1,11 +0,0 @@ -module.exports = { - clearMocks: true, - moduleFileExtensions: ['js', 'ts'], - testEnvironment: 'node', - testMatch: ['**/*.test.ts'], - testRunner: 'jest-circus/runner', - transform: { - '^.+\\.ts$': 'ts-jest' - }, - verbose: true -} \ No newline at end of file diff --git a/lib/main.js b/lib/main.js index a72e6ac..32606ba 100644 --- a/lib/main.js +++ b/lib/main.js @@ -7,20 +7,135 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; -const core = require('@actions/core'); -const github = require('@actions/github'); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const core = __importStar(require("@actions/core")); +const github = __importStar(require("@actions/github")); function run() { return __awaiter(this, void 0, void 0, function* () { try { - const myInput = core.getInput('myInput'); - core.debug(`Hello ${myInput} from inside a container`); - // Get github context data + const issueMessage = core.getInput('issue-message'); + const prMessage = core.getInput('pr-message'); + if (!issueMessage && !prMessage) { + throw new Error('Action must have at least one of issue-message or pr-message set'); + } + // Get client and context + const client = new github.GitHub(core.getInput('repo-token', { required: true })); const context = github.context; - console.log(`We can even get context data, like the repo: ${context.repo.repo}`); + if (context.payload.action !== 'opened') { + console.log('No issue or PR was opened, skipping'); + return; + } + // Do nothing if its not a pr or issue + const isIssue = !!context.payload.issue; + if (!isIssue && !context.payload.pull_request) { + console.log('The event that triggered this action was not a pull request or issue, skipping.'); + return; + } + // Do nothing if its not their first contribution + console.log('Checking if its the users first contribution'); + if (!context.payload.sender) { + throw new Error('Internal error, no sender provided by GitHub'); + } + const sender = context.payload.sender.login; + const issue = context.issue; + let firstContribution = false; + if (isIssue) { + firstContribution = yield isFirstIssue(client, issue.owner, issue.repo, sender, issue.number); + } + else { + firstContribution = yield isFirstPull(client, issue.owner, issue.repo, sender, issue.number); + } + if (!firstContribution) { + console.log('Not the users first contribution'); + return; + } + // Do nothing if no message set for this type of contribution + const message = isIssue ? issueMessage : prMessage; + if (!message) { + console.log('No message provided for this type of contribution'); + return; + } + const issueType = isIssue ? 'issue' : 'pull request'; + // Add a comment to the appropriate place + console.log(`Adding message: ${message} to ${issueType} ${issue.number}`); + if (isIssue) { + yield client.issues.createComment({ + owner: issue.owner, + repo: issue.repo, + issue_number: issue.number, + body: message + }); + } + else { + yield client.pulls.createReview({ + owner: issue.owner, + repo: issue.repo, + pull_number: issue.number, + body: message, + event: 'COMMENT' + }); + } } catch (error) { core.setFailed(error.message); + return; + } + }); +} +function isFirstIssue(client, owner, repo, sender, curIssueNumber) { + return __awaiter(this, void 0, void 0, function* () { + const { status, data: issues } = yield client.issues.listForRepo({ + owner: owner, + repo: repo, + creator: sender, + state: 'all' + }); + if (status !== 200) { + throw new Error(`Received unexpected API status code ${status}`); + } + if (issues.length === 0) { + return true; + } + for (const issue of issues) { + if (issue.number < curIssueNumber && !issue.pull_request) { + return false; + } + } + return true; + }); +} +// No way to filter pulls by creator +function isFirstPull(client, owner, repo, sender, curPullNumber, page = 1) { + return __awaiter(this, void 0, void 0, function* () { + // Provide console output if we loop for a while. + console.log('Checking...'); + const { status, data: pulls } = yield client.pulls.list({ + owner: owner, + repo: repo, + per_page: 100, + page: page, + state: 'all' + }); + if (status !== 200) { + throw new Error(`Received unexpected API status code ${status}`); + } + if (pulls.length === 0) { + return true; + } + for (const pull of pulls) { + const login = pull.user.login; + if (login === sender && pull.number < curPullNumber) { + return false; + } } + return yield isFirstPull(client, owner, repo, sender, curPullNumber, page + 1); }); } run(); diff --git a/package.json b/package.json index 8190532..37ebbc0 100644 --- a/package.json +++ b/package.json @@ -26,9 +26,9 @@ "homepage": "https://github.com/actions/container-toolkit-template#readme", "dependencies": { "@actions/core": "file:toolkit/actions-core-0.0.0.tgz", - "@actions/io": "file:toolkit/actions-io-0.0.0.tgz", "@actions/exec": "file:toolkit/actions-exec-0.0.0.tgz", "@actions/github": "file:toolkit/actions-github-0.0.0.tgz", + "@actions/io": "file:toolkit/actions-io-0.0.0.tgz", "@actions/tool-cache": "file:toolkit/actions-tool-cache-0.0.0.tgz" }, "devDependencies": { diff --git a/src/main.ts b/src/main.ts index 757639d..f386399 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,17 +1,170 @@ -const core = require('@actions/core'); -const github = require('@actions/github'); +import * as core from '@actions/core'; +import * as github from '@actions/github'; async function run() { try { - const myInput = core.getInput('myInput'); - core.debug(`Hello ${myInput} from inside a container`); - - // Get github context data + const issueMessage: string = core.getInput('issue-message'); + const prMessage: string = core.getInput('pr-message'); + if (!issueMessage && !prMessage) { + throw new Error( + 'Action must have at least one of issue-message or pr-message set' + ); + } + // Get client and context + const client: github.GitHub = new github.GitHub( + core.getInput('repo-token', {required: true}) + ); const context = github.context; - console.log(`We can even get context data, like the repo: ${context.repo.repo}`) + + if (context.payload.action !== 'opened') { + console.log('No issue or PR was opened, skipping'); + return; + } + + // Do nothing if its not a pr or issue + const isIssue: boolean = !!context.payload.issue; + if (!isIssue && !context.payload.pull_request) { + console.log( + 'The event that triggered this action was not a pull request or issue, skipping.' + ); + return; + } + + // Do nothing if its not their first contribution + console.log('Checking if its the users first contribution'); + if (!context.payload.sender) { + throw new Error('Internal error, no sender provided by GitHub'); + } + const sender: string = context.payload.sender!.login; + const issue: {owner: string; repo: string; number: number} = context.issue; + let firstContribution: boolean = false; + if (isIssue) { + firstContribution = await isFirstIssue( + client, + issue.owner, + issue.repo, + sender, + issue.number + ); + } else { + firstContribution = await isFirstPull( + client, + issue.owner, + issue.repo, + sender, + issue.number + ); + } + if (!firstContribution) { + console.log('Not the users first contribution'); + return; + } + + // Do nothing if no message set for this type of contribution + const message: string = isIssue ? issueMessage : prMessage; + if (!message) { + console.log('No message provided for this type of contribution'); + return; + } + + const issueType: string = isIssue ? 'issue' : 'pull request'; + // Add a comment to the appropriate place + console.log(`Adding message: ${message} to ${issueType} ${issue.number}`); + if (isIssue) { + await client.issues.createComment({ + owner: issue.owner, + repo: issue.repo, + issue_number: issue.number, + body: message + }); + } else { + await client.pulls.createReview({ + owner: issue.owner, + repo: issue.repo, + pull_number: issue.number, + body: message, + event: 'COMMENT' + }); + } } catch (error) { core.setFailed(error.message); + return; + } +} + +async function isFirstIssue( + client: github.GitHub, + owner: string, + repo: string, + sender: string, + curIssueNumber: number +): Promise { + const {status, data: issues} = await client.issues.listForRepo({ + owner: owner, + repo: repo, + creator: sender, + state: 'all' + }); + + if (status !== 200) { + throw new Error(`Received unexpected API status code ${status}`); + } + + if (issues.length === 0) { + return true; } + + for (const issue of issues) { + if (issue.number < curIssueNumber && !issue.pull_request) { + return false; + } + } + + return true; +} + +// No way to filter pulls by creator +async function isFirstPull( + client: github.GitHub, + owner: string, + repo: string, + sender: string, + curPullNumber: number, + page: number = 1 +): Promise { + // Provide console output if we loop for a while. + console.log('Checking...'); + const {status, data: pulls} = await client.pulls.list({ + owner: owner, + repo: repo, + per_page: 100, + page: page, + state: 'all' + }); + + if (status !== 200) { + throw new Error(`Received unexpected API status code ${status}`); + } + + if (pulls.length === 0) { + return true; + } + + for (const pull of pulls) { + const login: string = pull.user.login; + if (login === sender && pull.number < curPullNumber) { + return false; + } + } + + return await isFirstPull( + client, + owner, + repo, + sender, + curPullNumber, + page + 1 + ); } run(); diff --git a/toolkit/actions-github-0.0.0.tgz b/toolkit/actions-github-0.0.0.tgz index 4113d11..6cbe36d 100644 Binary files a/toolkit/actions-github-0.0.0.tgz and b/toolkit/actions-github-0.0.0.tgz differ