feat: add main branch check #6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: GitHub - Pull Request Convention | ||
on: | ||
pull_request_target: | ||
types: | ||
- opened | ||
- reopened | ||
# The "synchronize" type may not be used because code review commits, | ||
# from GitHub UI might be acceptable. Enable this if you want to block | ||
# all commits from GitHub UI. | ||
# | ||
# - synchronize | ||
env: PR_NUMBER:$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH") | ||
jobs: | ||
has-web-commits: | ||
runs-on: ubuntu-22.04 | ||
steps: | ||
- name: Check if PR author is allow-listed | ||
id: pr_author | ||
uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6 | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
script: | | ||
const prAuthor = context.payload.pull_request.user.login; | ||
const response = await github.rest.teams | ||
.getMembershipForUserInOrg({ | ||
org: context.repo.owner, | ||
team_slug: 'moderators', | ||
username: prAuthor | ||
}) | ||
.catch(() => ({ status: 404 })); | ||
let isAllowListed = false; | ||
if (prAuthor === 'renovate[bot]' || response.status === 200) { | ||
isAllowListed = true; | ||
} | ||
core.setOutput('is_allow_listed', isAllowListed); | ||
- name: Check if commits are made on GitHub Web UI | ||
id: check-commits | ||
if: steps.pr_author.outputs.result.is_allow_listed == 'false' | ||
run: | | ||
COMMITS_URL="https://api.github.com/repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/commits" | ||
IS_GITHUB_COMMIT=$(curl --header "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" "$COMMITS_URL" | jq '[.[] | .commit.committer.name] | any(.[]; . == "GitHub")') | ||
if [ "$IS_GITHUB_COMMIT" = "true" ]; then | ||
echo "IS_GITHUB_COMMIT=true" >> $GITHUB_ENV | ||
fi | ||
- name: Add comment on PR if commits are made on GitHub Web UI | ||
uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6 | ||
if: steps.pr_author.outputs.result.is_allow_listed == 'false' && env.IS_GITHUB_COMMIT == 'true' | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
script: | | ||
core.setFailed("Commits were added via the GitHub Web UI."); | ||
github.rest.issues.createComment({ | ||
issue_number: context.issue.number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body: "Thanks for your pull request.\n\n**Please do not add commits via the GitHub Web UI.**\n\nIt generally means you have yet to test these changes in a development setup or complete any prerequisites. We need you to follow the guides mentioned in the checklist. Please revalidate these changes in a developer environment and confirm how you validated your changes.\n\nHappy contributing!\n\n---\n_**Note:** This message was automatically generated by a bot. If you feel this message is in error or would like help resolving it, feel free to reach us [in our contributor chat](https://discord.gg/PRyKn3Vbay)._" | ||
}); | ||
branch-is-main: | ||
runs-on: ubuntu-22.04 | ||
uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6 | ||
if: ${{ github.head_ref === 'master' }} | ||
run: | | ||
PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH") | ||
echo "PR_NUMBER=PR_NUMBER" >> $GITHUB_ENV | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
script: | | ||
core.setFailed("PR was created from 'main'."); | ||
github.rest.issues.createComment({ | ||
issue_number: context.issue.number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body: "Thanks for your pull request.\n\n**Please do not create PRs from the `main` branch of your fork. The branch should be treated as a copy of the `freeCodeCamp` repo's `main` branch, and thus needs to be kept pristine.\n\nPlease create a new branch and bring the changes there, then open a new PR from it.\n\nHappy contributing!\n\n---\n_**Note:** This message was automatically generated by a bot. If you feel this message is in error or would like help resolving it, feel free to reach us [in our contributor chat](https://discord.gg/PRyKn3Vbay)._" | ||
}); | ||
github.rest.pulls.update({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
pull_number: env.PR_NUMBER | ||
}); |