<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sashank Bhamidi</title>
    <description>The latest articles on DEV Community by Sashank Bhamidi (@sashankbhamidi).</description>
    <link>https://dev.to/sashankbhamidi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1235184%2F68c39e62-bb7e-4b34-ab2e-dfe8c33235c0.png</url>
      <title>DEV Community: Sashank Bhamidi</title>
      <link>https://dev.to/sashankbhamidi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sashankbhamidi"/>
    <language>en</language>
    <item>
      <title>How I Built a Self-Merging PR System for Git-Gang</title>
      <dc:creator>Sashank Bhamidi</dc:creator>
      <pubDate>Sat, 11 Oct 2025 11:28:10 +0000</pubDate>
      <link>https://dev.to/sashankbhamidi/building-a-self-merging-pr-system-for-github-4i79</link>
      <guid>https://dev.to/sashankbhamidi/building-a-self-merging-pr-system-for-github-4i79</guid>
      <description>&lt;p&gt;I wanted to build the world’s biggest open-source contributors list. A place where anyone could add their name with a pull request and become part of something massive.&lt;/p&gt;

&lt;p&gt;Sounds simple. It is, until you realize that “anyone” could mean thousands of people. Manually reviewing and merging every single PR? No thanks.&lt;/p&gt;

&lt;p&gt;So I built a self-merging PR system that validates, cleans, merges, and updates everything automatically. From submission to merge, the whole thing takes about fifteen seconds.&lt;/p&gt;

&lt;p&gt;Here’s how it works.&lt;/p&gt;




&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;When you open a repo to the internet, you can’t rely on manual moderation.&lt;br&gt;
People fork, change whatever they want, and send PRs. Most are fine. Some will break things. A few will test your patience.&lt;/p&gt;

&lt;p&gt;I needed a system that could:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Accept PRs from forks&lt;/li&gt;
&lt;li&gt;Validate what was changed&lt;/li&gt;
&lt;li&gt;Merge automatically if everything looked good&lt;/li&gt;
&lt;li&gt;Flag or reject anythign invalid&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Basically, I wanted to press zero buttons.&lt;/p&gt;


&lt;h2&gt;
  
  
  Figuring out the permissions
&lt;/h2&gt;

&lt;p&gt;Here’s where it got tricky.&lt;br&gt;
GitHub limits what workflows can do with PRs coming from forks. You can’t comment, label, or merge them directly using the default &lt;code&gt;pull_request&lt;/code&gt; trigger.&lt;/p&gt;

&lt;p&gt;The fix is to use &lt;code&gt;pull_request_target&lt;/code&gt;, which runs in the context of the base repository, giving the workflow the permissions it needs.&lt;/p&gt;

&lt;p&gt;The key part is to &lt;strong&gt;explicitly checkout the PR commit&lt;/strong&gt; so you never execute untrusted code from a fork.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;pull_request_target&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;types&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;opened&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;synchronize&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;reopened&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;ready_for_review&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
    &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;master&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;

&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Checkout code&lt;/span&gt;
  &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v4&lt;/span&gt;
  &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;ref&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ github.event.pull_request.head.sha }}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This setup runs securely while still giving the workflow full access.&lt;/p&gt;




&lt;h2&gt;
  
  
  Making the bot act like me
&lt;/h2&gt;

&lt;p&gt;The default &lt;code&gt;GITHUB_TOKEN&lt;/code&gt; works, but it’s limited.&lt;br&gt;
It can’t comment on fork PRs or bypass branch protection, and all its commits show up as &lt;strong&gt;github-actions[bot]&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That’s not what I wanted.&lt;/p&gt;

&lt;p&gt;So I created a personal access token (PAT) with full repo and workflow permissions, then configured git to use my credentials.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;GH_TOKEN&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.PAT_TOKEN || secrets.GITHUB_TOKEN }}&lt;/span&gt;

&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Setup Git&lt;/span&gt;
  &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
    &lt;span class="s"&gt;git config --global user.name "Sashank Bhamidi"&lt;/span&gt;
    &lt;span class="s"&gt;git config --global user.email "hello@sashank.wiki"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now every commit and comment looks like it came from me, but I’m not the one doing it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Stopping duplicate runs
&lt;/h2&gt;

&lt;p&gt;At one point, every time the workflow added or removed a label, it triggered itself again.&lt;br&gt;
That meant duplicate comments and multiple runs per PR.&lt;/p&gt;

&lt;p&gt;The fix was simple: skip when the action is related to labels.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;if&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;github.event.action != 'labeled' &amp;amp;&amp;amp; github.event.action != 'unlabeled'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sometimes, one clean conditional saves hours of noise.&lt;/p&gt;




&lt;h2&gt;
  
  
  Validating contributions
&lt;/h2&gt;

&lt;p&gt;Each contributor edits a single file: &lt;code&gt;ADD_YOUR_NAME.md&lt;/code&gt;.&lt;br&gt;
Here’s what the format looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Name: Your Name
- Username: github-username
- Message: Optional message
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The workflow checks a few things before merging:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The structure is still intact&lt;/li&gt;
&lt;li&gt;The name and username are properly formatted&lt;/li&gt;
&lt;li&gt;The username hasn’t already been used&lt;/li&gt;
&lt;li&gt;There’s no profanity&lt;/li&gt;
&lt;li&gt;No other files were changed&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The profanity check uses the &lt;strong&gt;purgomalum.com&lt;/strong&gt; API.&lt;br&gt;
If the API goes down, the PR is flagged for manual review instead of being approved blindly. Always fail closed, not open.&lt;/p&gt;


&lt;h2&gt;
  
  
  Auto-merging PRs
&lt;/h2&gt;

&lt;p&gt;Once validation passes, the workflow comments, labels, approves, and merges automatically.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="s"&gt;gh pr merge ${{ github.event.number }} --squash --auto&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--auto&lt;/code&gt; merges the PR the moment all checks pass, and &lt;code&gt;--squash&lt;/code&gt; keeps the commit history clean.&lt;/p&gt;




&lt;h2&gt;
  
  
  What happens after a merge
&lt;/h2&gt;

&lt;p&gt;When a PR merges, another workflow kicks in. It:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Extracts the new entry from &lt;code&gt;ADD_YOUR_NAME.md&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Appends it to &lt;code&gt;CONTRIBUTORS.md&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Sorts contributors alphabetically&lt;/li&gt;
&lt;li&gt;Updates the contributor count in &lt;code&gt;README.md&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Resets the template&lt;/li&gt;
&lt;li&gt;Commits and pushes everything back&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If anything fails mid-process, it rolls back automatically to a backup commit.&lt;br&gt;
No human debugging. No broken files.&lt;/p&gt;




&lt;h2&gt;
  
  
  The result
&lt;/h2&gt;

&lt;p&gt;The entire pipeline, from pull request to merge to updated list, runs in around fifteen seconds.&lt;br&gt;
No reviews. No waiting. No stress.&lt;/p&gt;

&lt;p&gt;It handles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fork permissions&lt;/li&gt;
&lt;li&gt;Validation&lt;/li&gt;
&lt;li&gt;Duplicate prevention&lt;/li&gt;
&lt;li&gt;Profanity checks&lt;/li&gt;
&lt;li&gt;Auto-merging&lt;/li&gt;
&lt;li&gt;Rollbacks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It just works. Every time.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I learned
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;pull_request_target&lt;/code&gt; but you have to use it safely.&lt;/li&gt;
&lt;li&gt;PAT tokens unlock what &lt;code&gt;GITHUB_TOKEN&lt;/code&gt; can’t.&lt;/li&gt;
&lt;li&gt;Always fail closed when user input is involved.&lt;/li&gt;
&lt;li&gt;Validation saves time later.&lt;/li&gt;
&lt;li&gt;Rollbacks are essential, not optional.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Try it yourself
&lt;/h2&gt;

&lt;p&gt;You can see it live at &lt;a href="https://github.com/SashankBhamidi/git-gang" rel="noopener noreferrer"&gt;&lt;strong&gt;git-gang&lt;/strong&gt;&lt;/a&gt;.&lt;br&gt;
Add your name, open a PR, and watch it merge itself. It takes about fifteen seconds.&lt;/p&gt;

&lt;p&gt;Goal’s ten thousand contributors. Started with ten.&lt;br&gt;
Let’s see how far it goes.&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>automation</category>
      <category>devops</category>
      <category>githubactions</category>
    </item>
  </channel>
</rss>
