DAVID _
DAVID _

Reputation: 647

GitHub how prevent pushing commits with personal (non corporate) email

Is it possible to deny users to push commits to repo with a non corporate email?

for example only *@mycompany.com allowed

Upvotes: 2

Views: 822

Answers (1)

DAVID _
DAVID _

Reputation: 647

I solved it using a custom Github Actions job:

# .github/workflows/check_email.yml
jobs:
  validate_email:
    name: Validate email
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Extract author email
        id: author
        run: |
          git log -2
          echo "::set-output name=EMAIL::$(git show -s --format='%ae' HEAD~0)"
      # TODO: Fail the workflow in future instead of adding comment
      - name: Validate author email
        if: ${{ !endsWith(steps.author.outputs.EMAIL, '@mycompany.com')  }}
        uses: actions/github-script@v6
        env:
          EMAIL: ${{ steps.author.outputs.EMAIL }}
        with:
          script: |
            const { EMAIL } = process.env
            await github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: `⚠️ We detect that you are using a non-corporate email \`${EMAIL}\` address to contribute to the repo. :(
            Please update your repo config \`.git/config\`:
            \`\`\`
              [user]
                name = Your Name
                email = [email protected]
            \`\`\`
            > You may see \`<id>+<name>@users.noreply.github.com\` email, then you need to turn off [Keep my email addresses private](https://github.com/settings/emails) setting in your account.
            `})

Upvotes: 3

Related Questions