Skip to content

Commit

Permalink
Merge pull request #490 from yashksaini-coder/repo-access
Browse files Browse the repository at this point in the history
Invite user request workflow
  • Loading branch information
yashksaini-coder authored Jan 9, 2025
2 parents 335c00e + 1b00717 commit 05ddde2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 17 deletions.
43 changes: 43 additions & 0 deletions .github/scripts/invite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import fetch from 'node-fetch';

let GITHUB_TOKEN = '';
const OWNER = 'recodehive';
const REPO = 'Job-Seeker';

async function sendInvite(username) {
const url = `https://api.github.com/repos/${OWNER}/${REPO}/collaborators/${username}`;

const response = await fetch(url, {
method: 'PUT',
headers: {
'Authorization': `token ${GITHUB_TOKEN}`,
'Accept': 'application/vnd.github.v3+json',
},
body: JSON.stringify({
permission: 'pull',
}),
});

if (response.ok) {
console.log(`Success: Invitation sent to ${username} for the repository ${OWNER}/${REPO}`);
} else {
const error = await response.json();
console.error(`Error: ${response.status} - ${error.message}`);
}
}

const username = process.argv[2];
const token = process.argv[3];
if (!username) {
console.error('Error: Please provide a username as an argument.');
process.exit(1);
}
else if (!token) {
console.error('Error: Please provide a token as an argument.');
process.exit(1);
}
else {
GITHUB_TOKEN = token;
}

sendInvite(username,token ).catch((err) => console.error(`Unexpected Error: ${err.message}`));
35 changes: 18 additions & 17 deletions .github/workflows/private-access.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
name: Process Access Requests
name: Send GitHub Collaborator Invite from Issue

on:
issues:
types:
- closed
types: [closed]

jobs:
invite-user:
send-invite:

if: contains(github.event.issue.labels.*.name, 'access-request')
runs-on: ubuntu-latest

steps:
- name: Invite User to Private Repo
env:
TOKEN: ${{ secrets.GITHUB_TOKEN }}
USERNAME: ${{ github.event.issue.user.login }}
run: |
curl -X PUT \
-H "Authorization: token $TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/recodehive/Opensource-practice/collaborators/${USERNAME} \
-d '{"permission": "pull"}'
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 16

- name: Handle Errors
if: failure()
run: echo "An error occurred while processing the request."
- name: Invite User to Private Repo
env:
TOKEN: ${{ secrets.GH_TOKEN }}
USERNAME: ${{ github.event.issue.user.login }}
run: |
npm install
node scripts/invite.js $USERNAME $TOKEN

0 comments on commit 05ddde2

Please sign in to comment.