Skip to content

Commit 60fa033

Browse files
feat:added new script
Signed-off-by: shiva <shiva_05@outlook.com>
1 parent 09d306a commit 60fa033

File tree

4 files changed

+346
-67
lines changed

4 files changed

+346
-67
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
const core = require('@actions/core');
3+
const github = require('@actions/github');
4+
5+
async function run() {
6+
const context = github.context;
7+
8+
const issueMessage = `Welcome to Accord. Thanks a lot for reporting your first issue. Please check out our [contributors guide](https://github.com/accordproject/techdocs/blob/main/CONTRIBUTING.md).<br />Keep in mind there are also other channels you can use to interact with the Accord community. For more details check out our [Discord](https://discord.com/invite/Zm99SKhhtA).`;
9+
10+
const prMessage = `Welcome to Accord. Thanks a lot for creating your first pull request. Please check out our [contributors guide](https://github.com/accordproject/techdocs/blob/main/CONTRIBUTING.md).<br />Keep in mind there are also other channels you can use to interact with the Accord community. For more details check out our [Discord](https://discord.com/invite/Zm99SKhhtA).`;
11+
let token=process.env.GITHUB_TOKEN;
12+
const octokit = github.getOctokit(token);
13+
const isIssue = !!context.payload.issue;
14+
let isFirstContribution;
15+
16+
if (isIssue) {
17+
const query = `query($owner:String!, $name:String!, $contributer:String!) {
18+
repository(owner:$owner, name:$name){
19+
issues(first: 1, filterBy: {createdBy:$contributer}){
20+
totalCount
21+
}
22+
}
23+
}`;
24+
const variables = {
25+
owner: context.repo.owner,
26+
name: context.repo.repo,
27+
contributer: context.payload.sender.login
28+
};
29+
const { repository: { issues: { totalCount } } } = await octokit.graphql(query, variables);
30+
isFirstContribution = totalCount === 1;
31+
} else {
32+
const query = `query($qstr: String!) {
33+
search(query: $qstr, type: ISSUE, first: 1) {
34+
issueCount
35+
}
36+
}`;
37+
const variables = {
38+
qstr: `repo:${context.repo.owner}/${context.repo.repo} type:pr author:${context.payload.sender.login}`,
39+
};
40+
const { search: { issueCount } } = await octokit.graphql(query, variables);
41+
isFirstContribution = issueCount === 1;
42+
}
43+
44+
if (!isFirstContribution) {
45+
console.log(`Not the user's first contribution.`);
46+
return;
47+
}
48+
49+
const message = isIssue ? issueMessage : prMessage;
50+
51+
if (isIssue) {
52+
const issueNumber = context.payload.issue.number;
53+
await octokit.rest.issues.createComment({
54+
owner: context.payload.repository.owner.login,
55+
repo: context.payload.repository.name,
56+
issue_number: issueNumber,
57+
body: message
58+
});
59+
} else {
60+
const pullNumber = context.payload.pull_request.number;
61+
await octokit.rest.pulls.createReview({
62+
owner: context.payload.repository.owner.login,
63+
repo: context.payload.repository.name,
64+
pull_number: pullNumber,
65+
body: message,
66+
event: 'COMMENT'
67+
});
68+
}
69+
}
70+
71+
run().catch(err => core.setFailed(err.message));
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
name: Welcome first time contributors
32

43
on:
@@ -13,70 +12,22 @@ jobs:
1312
welcome:
1413
name: Post welcome message
1514
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
node-version: [18.x]
1618
steps:
17-
- uses: actions/github-script@v7
18-
with:
19-
github-token: ${{ secrets.GITHUB_TOKEN }}
20-
script: |
21-
const issueMessage = `Welcome to Accord. Thanks a lot for reporting your first issue. Please check out our [contributors guide](https://github.com/accordproject/techdocs/blob/main/CONTRIBUTING.md) .<br />Keep in mind there are also other channels you can use to interact with Accord community. For more details check out [dicord](https://discord.com/invite/Zm99SKhhtA).`;
22-
const prMessage = `Welcome to Accord. Thanks a lot for reporting your first issue. Please check out our [contributors guide](https://github.com/accordproject/techdocs/blob/main/CONTRIBUTING.md) .<br />Keep in mind there are also other channels you can use to interact with Accord community. For more details check out [dicord](https://discord.com/invite/Zm99SKhhtA).`;
23-
if (!issueMessage && !prMessage) {
24-
throw new Error('Action must have at least one of issue-message or pr-message set');
25-
}
26-
const isIssue = !!context.payload.issue;
27-
let isFirstContribution;
28-
if (isIssue) {
29-
const query = `query($owner:String!, $name:String!, $contributer:String!) {
30-
repository(owner:$owner, name:$name){
31-
issues(first: 1, filterBy: {createdBy:$contributer}){
32-
totalCount
33-
}
34-
}
35-
}`;
36-
const variables = {
37-
owner: context.repo.owner,
38-
name: context.repo.repo,
39-
contributer: context.payload.sender.login
40-
};
41-
const { repository: { issues: { totalCount } } } = await github.graphql(query, variables);
42-
isFirstContribution = totalCount === 1;
43-
} else {
44-
const query = `query($qstr: String!) {
45-
search(query: $qstr, type: ISSUE, first: 1) {
46-
issueCount
47-
}
48-
}`;
49-
const variables = {
50-
"qstr": `repo:${context.repo.owner}/${context.repo.repo} type:pr author:${context.payload.sender.login}`,
51-
};
52-
const { search: { issueCount } } = await github.graphql(query, variables);
53-
isFirstContribution = issueCount === 1;
54-
}
55-
56-
if (!isFirstContribution) {
57-
console.log(`Not the users first contribution.`);
58-
return;
59-
}
60-
const message = isIssue ? issueMessage : prMessage;
61-
// Add a comment to the appropriate place
62-
if (isIssue) {
63-
const issueNumber = context.payload.issue.number;
64-
console.log(`Adding message: ${message} to issue #${issueNumber}`);
65-
await github.rest.issues.createComment({
66-
owner: context.payload.repository.owner.login,
67-
repo: context.payload.repository.name,
68-
issue_number: issueNumber,
69-
body: message
70-
});
71-
}
72-
else {
73-
const pullNumber = context.payload.pull_request.number;
74-
console.log(`Adding message: ${message} to pull request #${pullNumber}`);
75-
await github.rest.pulls.createReview({
76-
owner: context.payload.repository.owner.login,
77-
repo: context.payload.repository.name,
78-
pull_number: pullNumber,
79-
body: message,
80-
event: 'COMMENT'
81-
});
82-
}
19+
- name: Checkout code
20+
uses: actions/checkout@v2
21+
22+
- name: Use Node.js ${{ matrix.node-version }}
23+
uses: actions/setup-node@v1
24+
with:
25+
node-version: ${{ matrix.node-version }}
26+
- name: NPM Install
27+
run: npm install
28+
29+
- name: Run welcome script
30+
run: node .github/scripts/welcome-first-time-contributor.js
31+
env:
32+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
33+

0 commit comments

Comments
 (0)