Skip to content

Commit

Permalink
feat: add triage label on communtiy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
scopsy committed Dec 22, 2022
1 parent b94e88a commit c369e5f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 14 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/issue-label.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Add Triage Label

on:
issue:
types: [opened]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
check:
name: Verify
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3

- uses: actions/setup-node@v3
with:
node-version: 16
- name: Install Octokit
run: npm --prefix .github/workflows/scripts install @octokit/action

- name: Check if user is a community contributor
id: check
run: node .github/workflows/scripts/add-triage-label.js
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
38 changes: 38 additions & 0 deletions .github/workflows/scripts/add-triage-label.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { Octokit } = require('@octokit/action');
const { isCommunityContributor } = require('./is-community-contributor');

const octokit = new Octokit();

const getAuthor = (payload) => {
return payload?.issue?.user?.login || payload?.pull_request?.user?.login || null;
};

async function start() {
const payload = require(process.env.GITHUB_EVENT_PATH);
if (payload?.pull_request) {
console.log('Skipping pull request execution');
return;
}

const username = getAuthor(payload);
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/');
const { number } = payload?.issue || payload?.pull_request;

const isCommunityUser = await isCommunityContributor(owner, repo, username);
console.log('::set-output name=is-community::%s', isCommunityUser ? 'yes' : 'no');

if (isCommunityUser) {
await addLabel('triage', owner, repo, number);
}
}

const addLabel = async (label, owner, repo, issueNumber) => {
await octokit.rest.issues.addLabels({
owner,
repo,
issue_number: issueNumber,
labels: [label],
});
};

start();
15 changes: 1 addition & 14 deletions .github/workflows/scripts/community-contribution-label.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
const { Octokit } = require('@octokit/action');
const { isCommunityContributor } = require('./is-community-contributor');

const octokit = new Octokit();

const getAuthor = (payload) => {
return payload?.issue?.user?.login || payload?.pull_request?.user?.login || null;
};

const isCommunityContributor = async (owner, repo, username) => {
if (!username) return false;

const {
data: { permission },
} = await octokit.rest.repos.getCollaboratorPermissionLevel({
owner,
repo,
username,
});

return permission === 'read' || permission === 'none';
};

const addLabel = async (label, owner, repo, issueNumber) => {
await octokit.rest.issues.addLabels({
owner,
Expand Down
21 changes: 21 additions & 0 deletions .github/workflows/scripts/is-community-contributor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { Octokit } = require('@octokit/action');

const octokit = new Octokit();

const isCommunityContributor = async (owner, repo, username) => {
if (!username) return false;

const {
data: { permission },
} = await octokit.rest.repos.getCollaboratorPermissionLevel({
owner,
repo,
username,
});

return permission === 'read' || permission === 'none';
};

module.exports = {
isCommunityContributor,
};

0 comments on commit c369e5f

Please sign in to comment.