|
| 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