generated from actions/container-toolkit-action
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Get it working * Required token * Logging * Debug * Debug * Correct logging * No setNeutral * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * working * logging * logging * logging * logging * logging * logging * logging * logging * logging * logging * debug * debug
- Loading branch information
Danny McCormick
authored
Aug 2, 2019
1 parent
a10eefa
commit 5f06a65
Showing
7 changed files
with
180 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# 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/). | ||
An action for filtering pull requests and issues from first-time contributors |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
repoToken: | ||
description: 'Token for the repo. Can be passed in using {{ secrets.GITHUB_TOKEN }}' | ||
required: true | ||
issueMessage: | ||
description: 'Comment to post on an individuals first issue' | ||
prMessage: | ||
description: 'Comment to post on an individuals first pull request' | ||
runs: | ||
using: 'docker' | ||
image: 'Dockerfile' |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,103 @@ | ||
const core = require('@actions/core'); | ||
const github = require('@actions/github'); | ||
const fs = require('fs'); | ||
|
||
async function run() { | ||
try { | ||
const myInput = core.getInput('myInput'); | ||
core.debug(`Hello ${myInput} from inside a container`); | ||
|
||
// Get github context data | ||
// Get client and context | ||
const client = new github.GitHub(core.getInput('repoToken', { 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('Nothing was opened'); | ||
return; | ||
} | ||
|
||
// Do nothing if its not a pr or issue | ||
const isIssue = !!context.payload.issue; | ||
if (!isIssue && !context.payload.pull_request) { | ||
console.log('Not a pull request or issue'); | ||
return; | ||
} | ||
|
||
// Do nothing if its not their first contribution | ||
console.log('Checking if its the users first contribution'); | ||
const sender = context.payload.sender.login; | ||
const issue = context.issue; | ||
const firstContribution = isIssue ? await isFirstIssue(client, issue.owner, issue.repo, sender, issue.number) : 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 = isIssue ? core.getInput('issueMessage') : core.getInput('prMessage'); | ||
if (!message) { | ||
console.log('No message provided for this type of contribution'); | ||
return; | ||
} | ||
|
||
// Add a comment to the appropriate place | ||
console.log(`Adding message: ${message}`); | ||
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, owner, repo, sender, number): Promise<boolean> { | ||
console.log(`owner ${owner}, repo ${repo}, creator: ${sender}`); | ||
const {status, data: issues} = await client.issues.listForRepo({owner: owner, repo: repo, creator: sender, state: 'all'}); | ||
|
||
if (status !== 200) { | ||
throw new Error(`Received API status code ${status}`); | ||
} | ||
|
||
if (issues.length === 0) { | ||
return true; | ||
} | ||
|
||
for (const issue of issues) { | ||
const issueNumber = issue.number; | ||
console.log(issueNumber, number); | ||
if (issueNumber < number) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
// No way to filter pulls by creator | ||
async function isFirstPull(client, owner, repo, sender, number, page = 1): Promise<boolean> { | ||
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 API status code ${status}`); | ||
} | ||
|
||
if (pulls.length === 0) { | ||
return true; | ||
} | ||
|
||
for (const pull of pulls) { | ||
const login = pull.user.login; | ||
const pullNumber = pull.number; | ||
if (login === sender && pullNumber < number) { | ||
return false; | ||
} | ||
} | ||
|
||
return await isFirstPull(client, owner, repo, sender, number, page+1); | ||
} | ||
|
||
run(); |
Binary file not shown.