2022-12-15 22:41:03 +00:00
|
|
|
// Note you'll need to install these dependencies as part of your workflow.
|
|
|
|
const { Octokit } = require( '@octokit/action' );
|
|
|
|
const core = require( '@actions/core' );
|
2022-10-09 22:34:52 +00:00
|
|
|
|
|
|
|
// Note that this script assumes you set GITHUB_TOKEN in env, if you don't
|
|
|
|
// this won't work.
|
|
|
|
const octokit = new Octokit();
|
|
|
|
|
2024-02-22 19:37:07 +00:00
|
|
|
const ignoredUsernames = [ 'dependabot[bot]' ];
|
|
|
|
const checkIfIgnoredUsername = ( username ) =>
|
|
|
|
ignoredUsernames.includes( username );
|
|
|
|
|
2022-12-15 22:41:03 +00:00
|
|
|
const getIssueAuthor = ( payload ) => {
|
|
|
|
return (
|
|
|
|
payload?.issue?.user?.login ||
|
|
|
|
payload?.pull_request?.user?.login ||
|
|
|
|
null
|
|
|
|
);
|
|
|
|
};
|
2022-10-09 22:34:52 +00:00
|
|
|
|
2022-12-15 22:41:03 +00:00
|
|
|
const isCommunityContributor = async ( owner, repo, username ) => {
|
2024-02-22 19:37:07 +00:00
|
|
|
if ( username && ! checkIfIgnoredUsername( username ) ) {
|
2022-12-15 22:41:03 +00:00
|
|
|
const {
|
|
|
|
data: { permission },
|
|
|
|
} = await octokit.rest.repos.getCollaboratorPermissionLevel( {
|
2022-10-09 22:34:52 +00:00
|
|
|
owner,
|
|
|
|
repo,
|
2022-08-23 19:01:12 +00:00
|
|
|
username,
|
2022-12-15 22:41:03 +00:00
|
|
|
} );
|
|
|
|
|
2022-10-09 22:34:52 +00:00
|
|
|
return permission === 'read' || permission === 'none';
|
2022-08-23 19:01:12 +00:00
|
|
|
}
|
|
|
|
|
2024-02-22 19:37:07 +00:00
|
|
|
console.log( 'Not a community contributor!' );
|
2022-12-15 22:41:03 +00:00
|
|
|
return false;
|
|
|
|
};
|
2022-10-09 22:34:52 +00:00
|
|
|
|
2022-12-15 22:41:03 +00:00
|
|
|
const addLabel = async ( label, owner, repo, issueNumber ) => {
|
|
|
|
await octokit.rest.issues.addLabels( {
|
2022-10-09 22:34:52 +00:00
|
|
|
owner,
|
|
|
|
repo,
|
|
|
|
issue_number: issueNumber,
|
2022-12-15 22:41:03 +00:00
|
|
|
labels: [ label ],
|
|
|
|
} );
|
|
|
|
};
|
2022-10-09 22:34:52 +00:00
|
|
|
|
|
|
|
const applyLabelToCommunityContributor = async () => {
|
2022-12-15 22:41:03 +00:00
|
|
|
const eventPayload = require( process.env.GITHUB_EVENT_PATH );
|
|
|
|
const username = getIssueAuthor( eventPayload );
|
|
|
|
const [ owner, repo ] = process.env.GITHUB_REPOSITORY.split( '/' );
|
2022-10-09 22:34:52 +00:00
|
|
|
const { number } = eventPayload?.issue || eventPayload?.pull_request;
|
2022-12-15 22:41:03 +00:00
|
|
|
|
|
|
|
const isCommunityUser = await isCommunityContributor(
|
|
|
|
owner,
|
|
|
|
repo,
|
|
|
|
username
|
|
|
|
);
|
|
|
|
|
|
|
|
core.setOutput( 'is-community', isCommunityUser ? 'yes' : 'no' );
|
|
|
|
|
|
|
|
if ( isCommunityUser ) {
|
|
|
|
console.log( 'Adding community contributor label' );
|
|
|
|
await addLabel( 'type: community contribution', owner, repo, number );
|
2022-08-23 19:01:12 +00:00
|
|
|
}
|
2022-12-15 22:41:03 +00:00
|
|
|
};
|
2022-08-23 19:01:12 +00:00
|
|
|
|
2022-10-09 22:34:52 +00:00
|
|
|
applyLabelToCommunityContributor();
|