Skip to content

Auto-invite on invitation issue #14

Auto-invite on invitation issue

Auto-invite on invitation issue #14

Workflow file for this run

name: "Auto-invite on invitation issue"
on:
issues:
types: [opened]
jobs:
invite:
if: >
contains(github.event.issue.labels.*.name, 'welcome') &&
github.event.issue.title == 'Please invite me to RatLoopz'
runs-on: ubuntu-latest
steps:
- name: Invite user to organization
uses: actions/github-script@v7
with:
github-token: ${{ secrets.ORG_INVITE_TOKEN }}
script: |
const issue = context.payload.issue;
const body = issue.body || '';
function extractUsernameFromBody(text) {
const match = text.match(/###\s*GitHub username\s*\n+([A-Za-z0-9-]+)/i);
if (match && match[1]) return match[1].trim();
return null;
}
let username = extractUsernameFromBody(body);
if (!username && issue.user && issue.user.login) {
username = issue.user.login;
}
if (!username) {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
body: "⚠️ Could not determine your GitHub username. Please reply with your GitHub handle."
});
return;
}
try {
const { data: user } = await github.rest.users.getByUsername({ username });
const invite = await github.rest.orgs.createInvitation({
org: context.repo.owner,
invitee_id: user.id,
role: 'direct_member'
});
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
body: `✅ Invitation sent to @${username}. Check your email or GitHub notifications.`
});
} catch (error) {
// Handle the "already a member" case
if (error.message.includes("Invitee is already a part of this organization")) {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
body: `ℹ️ @${username} is **already a member** of RatLoopz. No invite needed.`
});
return; // Do NOT mark the workflow as failed
}
// Handle pending invite case
if (error.message.includes("has already been invited")) {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
body: `ℹ️ @${username} already has a **pending invite**. Check your email or notifications.`
});
return;
}
// Any other unknown error
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
body: `❌ Failed to invite \`@${username}\`. Error: \`${error.message}\``
});
core.setFailed(error.message);
}