Skip to content

Assignment #1 - Unix, Shell, Git & GitHub #38

Assignment #1 - Unix, Shell, Git & GitHub

Assignment #1 - Unix, Shell, Git & GitHub #38

name: UofT-DSI Main Repository PR Workflow
on:
pull_request_target:
types: [opened, reopened]
jobs:
handle-pr:
if: github.repository_owner == 'UofT-DSI'
runs-on: ubuntu-latest
steps:
- name: Handle PR based on branch and author
uses: actions/github-script@v6
with:
script: |
const pr = context.payload.pull_request;
const branchName = pr.head.ref;
const issue_number = pr.number;
const repo = context.repo;
const sender = context.payload.sender.login;
let isMember = false;
let isContributor = false;
// Check if user is a member of the org
try {
const membership = await github.rest.orgs.getMembershipForUser({
org: repo.owner,
username: sender
});
isMember = membership && membership.status === "active";
} catch (error) {
// If not a member, GitHub API throws a 404
isMember = false;
}
// Check if user is a contributor to the repo
try {
const contributors = await github.paginate(
github.rest.repos.listContributors,
{
owner: repo.owner,
repo: repo.repo,
anon: false
}
);
isContributor = contributors.some(contributor => contributor.login === sender);
} catch (error) {
isContributor = false;
}
if ((!isMember && !isContributor) || branchName.startsWith('assignment')) {
const commentBody = `This pull request was made to the wrong repository. If you are a participant, please close it and open it in your own fork instead. Refer to the [Assignment Submission Guide](https://github.com/UofT-DSI/onboarding/blob/main/onboarding_documents/submissions.md) for detailed instructions.`;
await github.rest.issues.createComment({
owner: repo.owner,
repo: repo.repo,
issue_number: issue_number,
body: commentBody
});
await github.rest.pulls.update({
owner: repo.owner,
repo: repo.repo,
pull_number: issue_number,
state: "closed"
});
} else {
const commentBody = `Thanks for your contribution! 🎉\n\nPlease remember to tag or request a review from the DSI team. Give us up to 72 hours to review your pull request. We appreciate your patience and efforts.`;
await github.rest.issues.createComment({
owner: repo.owner,
repo: repo.repo,
issue_number: issue_number,
body: commentBody
});
}