-
Notifications
You must be signed in to change notification settings - Fork 379
Expand file tree
/
Copy pathmain.ts
More file actions
109 lines (93 loc) · 3.34 KB
/
main.ts
File metadata and controls
109 lines (93 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import * as core from '@actions/core'
import * as github from '@actions/github'
import { Octokit } from '@octokit/rest'
export async function run() {
core.info('Running actions/first-interaction!')
// Check if this is an issue or PR event.
const isIssue = github.context.payload.issue !== undefined
const isPullRequest = github.context.payload.pull_request !== undefined
// Skip if this is not an issue or PR event.
if (!isIssue && !isPullRequest)
return core.info('Skipping...Not an Issue/PR Event')
// Skip if this is not an issue/PR open event.
if (github.context.payload.action !== 'opened')
return core.info('Skipping...Not an Opened Event')
// Confirm the sender data is present.
if (!github.context.payload.sender)
return core.setFailed('Internal Error...No Sender Provided by GitHub')
// Confirm that only one of the two is present.
if (isIssue && isPullRequest)
return core.setFailed(
'Internal Error...Both Issue and PR Provided by GitHub'
)
// Get the action inputs.
const issueMessage: string = core.getInput('issue_message')
if (isIssue && !issueMessage)
return core.info('Skipping... No issue message configured')
const prMessage: string = core.getInput('pr_message')
if (!isIssue && !prMessage)
return core.info('Skipping... No PR message configured')
const octokit = new Octokit({
auth: core.getInput('repo_token', { required: true })
})
// Check if this is the user's first contribution.
if (!(await isFirstIssue(octokit)) && !(await isFirstPullRequest(octokit)))
return core.info('Skipping...Not First Contribution')
core.info(`Adding Message to #${github.context.issue.number}`)
await octokit.rest.issues.createComment({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: github.context.issue.number,
body: isIssue ? issueMessage : prMessage
})
}
/**
* Checks if this is the user's first issue.
*
* @param octokit Octokit instance
* @returns true if this is the user's first issue
*/
export async function isFirstIssue(octokit: Octokit): Promise<boolean> {
try {
const issues = await octokit.paginate(octokit.rest.issues.listForRepo, {
owner: github.context.repo.owner,
repo: github.context.repo.repo,
creator: github.context.payload.sender!.login,
state: 'all'
})
return (
issues
// Filter out PRs.
.filter((issue) => issue.pull_request === undefined)
// Filter out any issue that are newer than the current issue.
.filter((issue) => issue.number < github.context.issue.number)
.length === 0
)
} catch (error) {
core.setFailed((error as any).message)
return false
}
}
/**
* Checks if this is the user's first pull request.
*
* @param octokit Octokit instance
* @returns true if this is the user's first pull request
*/
export async function isFirstPullRequest(octokit: Octokit): Promise<boolean> {
try {
const pulls = await octokit.paginate(octokit.rest.pulls.list, {
owner: github.context.repo.owner,
repo: github.context.repo.repo,
state: 'all'
})
return (
// Filter out any PRs that are newer than the current one.
pulls.filter((pull) => pull.number < github.context.issue.number)
.length === 0
)
} catch (error) {
core.setFailed((error as any).message)
return false
}
}