Skip to content

Clean-up copilot commits/suggestions #8

Clean-up copilot commits/suggestions

Clean-up copilot commits/suggestions #8

Workflow file for this run

# This workflow checks for common PR submission issues and provides helpful feedback
# - PRs from fork's main branch (helpful workflow suggestion)
# - Organization account submissions (critical - blocks CI due to GitHub limitation)
#
# Security Note: This workflow uses pull_request_target which runs in the context
# of the base repository, not the fork. User-controlled data (PR titles, descriptions,
# branch names, usernames) must be sanitized before use to prevent script injection.
name: PR Submission Checks
on:
pull_request_target:
types: [opened, reopened, synchronize]
branches:
- main
permissions:
pull-requests: write
contents: read
jobs:
check-pr-submission:
name: Check PR submission best practices
runs-on: ubuntu-latest
steps:
- name: Check if PR is from main branch of fork
id: check-main-branch
uses: actions/github-script@v7
with:
script: |
const pr = context.payload.pull_request;
// Get PR details from context (these are all user-controlled)
const headRef = pr.head.ref;
const headRepo = pr.head.repo.full_name;
const baseRepo = pr.base.repo.full_name;
const isFork = headRepo !== baseRepo;
const isFromMain = headRef === 'main';
const isFromOrg = pr.head.repo.owner.type === 'Organization';
// Log details - no user input in template literals for security
console.log('PR number:', pr.number);
console.log('Head branch:', headRef);
console.log('Head repo:', headRepo);
console.log('Base repo:', baseRepo);
console.log('Is fork:', isFork);
console.log('From main branch:', isFromMain);
console.log('Owner type:', pr.head.repo.owner.type);
core.setOutput('is_fork', isFork);
core.setOutput('is_from_main', isFromMain);
core.setOutput('is_from_org', isFromOrg);
core.setOutput('head_ref', headRef);
core.setOutput('head_repo', headRepo);
return {
isFork,
isFromMain,
isFromOrg,
needsComment: isFork && isFromMain
};
- name: Post comment about main branch submission
if: steps.check-main-branch.outputs.is_fork == 'true' && steps.check-main-branch.outputs.is_from_main == 'true'
uses: actions/github-script@v7
with:
script: |
const pr = context.payload.pull_request;
// Check if we already posted this comment
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number
});
const botComment = comments.data.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('submitted from the main branch')
);
if (botComment) {
console.log('Comment about main branch already exists, skipping');
return;
}
// Sanitize username - GitHub usernames can only contain alphanumeric characters and hyphens
const username = pr.user.login.replace(/[^a-zA-Z0-9-]/g, '');
// Post helpful comment
const commentBody = `Hi @${username}! 👋
Thank you for your contribution to voc4cat!

Check failure on line 94 in .github/workflows/pr-checks.yml

View workflow run for this annotation

GitHub Actions / .github/workflows/pr-checks.yml

Invalid workflow file

You have an error in your yaml syntax on line 94
⚠️ We noticed that this pull request was submitted from the \`main\` branch of your fork.
While this works, it can cause issues
- It makes it harder to keep your fork updated with upstream changes
- You won't be able to work on multiple PRs at once
- Future contributions may be complicated by merge conflicts
This PR can still be merged, but please use feature branches going forward!
For more information, see our [How to contribute](https://nfdi4cat.github.io/voc4cat/docs_usage/how-to-contribute.html) guide.
*If you have any questions, please don't hesitate to ask!* 🚀`;
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
body: commentBody
});
- name: Post info about organization account
if: steps.check-main-branch.outputs.is_from_org == 'true'
uses: actions/github-script@v7
with:
script: |
const pr = context.payload.pull_request;
// Check if we already posted this comment
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number
});
const botComment = comments.data.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('organization account')
);
if (botComment) {
console.log('Comment about organization account already exists, skipping');
return;
}
// Sanitize username - GitHub usernames can only contain alphanumeric characters and hyphens
const username = pr.user.login.replace(/[^a-zA-Z0-9-]/g, '');
const commentBody = `Hi @${username}! 👋
⚠️ We noticed that this pull request comes from an organization account,
which will prevent our CI workflow from working correctly.
GitHub does not allow the "Allow edits from maintainers" option for forks in organizations (see [discussion](https://github.com/orgs/community/discussions/5634)).
Our CI needs this permission to commit turtle files and clean up Excel files.
**This PR cannot be merged as-is.** Please:
1. Fork voc4cat to your personal GitHub account
2. Create a feature branch with your changes
3. Submit a new PR from your personal fork
4. Close this PR
Sorry for the inconvenience - this is a GitHub limitation, not our choice!`;
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
body: commentBody
});