Skip to content

Commit

Permalink
Added first timers 😄
Browse files Browse the repository at this point in the history
  • Loading branch information
ankur12-1610 committed Jan 4, 2022
1 parent 08522e9 commit f117dfa
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
10 changes: 10 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12554,18 +12554,28 @@ function run() {
if (typeof GITHUB_TOKEN !== 'string') {
throw new Error('Invalid GITHUB_TOKEN: did you forget to set it in your action config?');
}
//fetch GIF from GIPHY
const response = yield axios_1.default.get(`https://api.giphy.com/v1/gifs/random?api_key=${GIPHY_TOKEN}&tag=${GIPHY_TOPIC}`);
const gifUrl = response.data.data.images.fixed_height_small.url;
const sender = context.payload.sender.login;
const gif = `\n\n![thanks](${gifUrl})`;
const octokit = github.getOctokit(GITHUB_TOKEN);
const { pull_request } = context.payload;
const payload = context.payload.pull_request;
const author = payload.user.login;
const tag_text = (TAG_AUTHOR ? `@` + author + ` ` : null);
const assignee = (ASSIGN_TO_AUTHOR ? author : null);
//comment on PR
yield octokit.rest.issues.createComment(Object.assign(Object.assign({}, context.repo), { issue_number: pull_request.number, body: tag_text + COMMENT_TEXT + gif, id: payload.number.toString() }));
//assign PR to its author
yield octokit.rest.issues.addAssignees(Object.assign(Object.assign({}, context.repo), { issue_number: pull_request.number, assignees: assignee }));
//add reaction to PR
yield octokit.rest.reactions.createForIssue(Object.assign(Object.assign({}, context.repo), { repo: context.repo.repo, issue_number: pull_request.number, content: PR_REACTION, owner: context.repo.owner }));
//first contributor to PR gets a welcome message
const firstContribution = yield octokit.rest.pulls.list(Object.assign(Object.assign({}, context.repo), { state: 'open', body: 'Welcome first timers!', sort: 'created', direction: 'asc', per_page: 1 }));
if (firstContribution.data.length === 1) {
yield octokit.rest.issues.createComment(Object.assign(Object.assign({}, context.repo), { issue_number: pull_request.number, body: `:tada: This is the first time you contributed to this repo. Thanks for the first contribution!` }));
}
}
catch (e) {
core.error(e);
Expand Down
34 changes: 30 additions & 4 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const core = require('@actions/core')
const github = require('@actions/github')
const camelCase = require('camelcase')
import axios, { AxiosInstance, AxiosResponse } from 'axios'
import { report } from 'process'

declare module 'axios' {
interface AxiosResponse<T = any> extends Promise<T> {}
Expand All @@ -24,37 +25,62 @@ async function run() {
throw new Error('Invalid GITHUB_TOKEN: did you forget to set it in your action config?');
}

//fetch GIF from GIPHY
const response: AxiosResponse = await axios.get(`https://api.giphy.com/v1/gifs/random?api_key=${GIPHY_TOKEN}&tag=${GIPHY_TOPIC}`)
const gifUrl: string = response.data.data.images.fixed_height_small.url


const sender = context.payload.sender.login
const gif = `\n\n![thanks](${gifUrl})`
const octokit = github.getOctokit(GITHUB_TOKEN)
const { pull_request } = context.payload
const payload = context.payload.pull_request
const author = payload.user.login
const author = payload.user.login
const tag_text = (TAG_AUTHOR ? `@` + author + ` ` : null)
const assignee = (ASSIGN_TO_AUTHOR ? author : null)


//comment on PR
await octokit.rest.issues.createComment({
...context.repo,
issue_number: pull_request.number,
body: tag_text + COMMENT_TEXT + gif,
id: payload.number.toString()
})


//assign PR to its author
await octokit.rest.issues.addAssignees({
...context.repo,
issue_number: pull_request.number,
assignees: assignee
})


//add reaction to PR
await octokit.rest.reactions.createForIssue({
...context.repo,
repo: context.repo.repo,
issue_number: pull_request.number,
content: PR_REACTION,
owner: context.repo.owner
})

//first contributor to PR gets a welcome message
const firstPull = await octokit.rest.pulls.list({
...context.repo,
state: 'open',
sort: 'created',
direction: 'asc',
per_page: 1
})

if (firstPull.data.length === 1) {
await octokit.rest.issues.createComment({
...context.repo,
issue_number: pull_request.number,
body: `Hi @${author}, thanks for contributing to this repo!`
})
}


} catch (e) {
core.error(e)
core.setFailed((e as Error).message)
Expand Down

0 comments on commit f117dfa

Please sign in to comment.