Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit cfd79f5

Browse files
authored
Add workflow to label community contributors (#8995)
* Add workflow to label community contributors * Add action dependencies to package.json
1 parent f480e34 commit cfd79f5

File tree

4 files changed

+458
-5
lines changed

4 files changed

+458
-5
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Add Community Label
2+
3+
on:
4+
pull_request_target:
5+
types: [opened]
6+
issues:
7+
types: [opened]
8+
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.ref }}
11+
cancel-in-progress: true
12+
13+
permissions: {}
14+
15+
jobs:
16+
verify:
17+
name: Verify and add label
18+
runs-on: ubuntu-20.04
19+
permissions:
20+
contents: read
21+
pull-requests: write
22+
issues: write
23+
steps:
24+
- uses: actions/checkout@v3
25+
26+
- name: Setup Node.js
27+
uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c
28+
29+
- name: npm install
30+
run: npm install -D
31+
32+
- name: Check if user is a community contributor
33+
id: check
34+
run: node .github/workflows/scripts/is-community-contributor.js
35+
env:
36+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Note you'll need to install these dependencies as part of your workflow.
2+
const { Octokit } = require( '@octokit/action' );
3+
const core = require( '@actions/core' );
4+
5+
// Note that this script assumes you set GITHUB_TOKEN in env, if you don't
6+
// this won't work.
7+
const octokit = new Octokit();
8+
9+
const getIssueAuthor = ( payload ) => {
10+
return (
11+
payload?.issue?.user?.login ||
12+
payload?.pull_request?.user?.login ||
13+
null
14+
);
15+
};
16+
17+
const isCommunityContributor = async ( owner, repo, username ) => {
18+
if ( username ) {
19+
const {
20+
data: { permission },
21+
} = await octokit.rest.repos.getCollaboratorPermissionLevel( {
22+
owner,
23+
repo,
24+
username,
25+
} );
26+
27+
return permission === 'read' || permission === 'none';
28+
}
29+
30+
console.log( 'Not a community contributor!' );
31+
return false;
32+
};
33+
34+
const addLabel = async ( label, owner, repo, issueNumber ) => {
35+
await octokit.rest.issues.addLabels( {
36+
owner,
37+
repo,
38+
issue_number: issueNumber,
39+
labels: [ label ],
40+
} );
41+
};
42+
43+
const applyLabelToCommunityContributor = async () => {
44+
const eventPayload = require( process.env.GITHUB_EVENT_PATH );
45+
const username = getIssueAuthor( eventPayload );
46+
const [ owner, repo ] = process.env.GITHUB_REPOSITORY.split( '/' );
47+
const { number } = eventPayload?.issue || eventPayload?.pull_request;
48+
49+
const isCommunityUser = await isCommunityContributor(
50+
owner,
51+
repo,
52+
username
53+
);
54+
55+
core.setOutput( 'is-community', isCommunityUser ? 'yes' : 'no' );
56+
57+
if ( isCommunityUser ) {
58+
console.log( 'Adding community contributor label' );
59+
await addLabel( 'type: community contribution', owner, repo, number );
60+
}
61+
};
62+
63+
applyLabelToCommunityContributor();

0 commit comments

Comments
 (0)