Skip to content

ahimanshu56/create_pr-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Create PR Plugin

A Drone/Harness CI plugin to automatically commit changes and create pull requests across multiple SCM providers using go-scm.

This plugin automates the complete workflow of committing local changes, pushing to a branch, and creating a pull request - replacing complex bash scripts with a single, portable plugin.

✨ Features

Feature GitHub GitLab Bitbucket Gitea Gogs Harness Code
🌿 Branch Creation βœ… βœ… βœ… βœ… βœ… βœ…
πŸ“ Commit Changes βœ… βœ… βœ… βœ… βœ… βœ…
πŸ”€ Create Pull Request βœ… βœ… βœ… βœ… βœ… βœ…
🎯 Smart Branch Naming βœ… βœ… βœ… βœ… βœ… βœ…
πŸ“€ Output Variables βœ… βœ… βœ… βœ… βœ… βœ…
πŸ’ͺ Single Commit βœ… βœ… βœ… βœ… βœ… βœ…

πŸš€ Quick Start

Basic Example - Harness CI

- step:
    type: Plugin
    name: Push and Create PR
    identifier: push_and_create_pr
    spec:
      connectorRef: account.harnessImage
      image: himanshuagrawal/create-pr-plugin
      settings:
        scm_provider: harness
        token: <+secrets.getValue("harness_api_key")>
        harness_account_id: <+account.identifier>
        harness_org_id: <+org.identifier>
        harness_project_id: <+project.identifier>
        repo: <+pipeline.properties.ci.codebase.repoName>
        source_branch: <+codebase.branch>
        netrc_machine: <+codebase.gitHttpUrl>
        netrc_username: <+codebase.build.spec.username>
        netrc_password: <+codebase.build.spec.password>
        create_pr: true
        pr_title: "Auto-fix by Harness AI"
        pr_description: "Automated fixes for your code"

Basic Example - Drone CI (GitHub)

kind: pipeline
type: docker
name: autofix

steps:
  - name: run-autofix
    image: node:18
    commands:
      - npm run lint:fix

  - name: push-and-create-pr
    image: himanshuagrawal/create-pr-plugin
    settings:
      scm_provider: github
      token:
        from_secret: github_token
      repo: ${DRONE_REPO}
      source_branch: ${DRONE_SOURCE_BRANCH}
      create_pr: true
      pr_title: "Automated fixes"

βš™οΈ Configuration

All parameters can be configured through the settings block in your pipeline or as environment variables with PLUGIN_ prefix.

Required Parameters

Parameter Environment Variable Description
scm_provider PLUGIN_SCM_PROVIDER SCM provider: github, gitlab, bitbucket, gitea, gogs, harness
token PLUGIN_TOKEN Authentication token (PAT, API key)
repo PLUGIN_REPO Repository (owner/repo or just repo-name for Harness)
source_branch PLUGIN_SOURCE_BRANCH Current branch name

Git Authentication (for git push)

Parameter Environment Variable Description
netrc_machine PLUGIN_NETRC_MACHINE Git server hostname (e.g., github.com)
netrc_username PLUGIN_NETRC_USERNAME Git username
netrc_password PLUGIN_NETRC_PASSWORD Git password/token

Branch Configuration

Parameter Environment Variable Default Description
branch_suffix PLUGIN_BRANCH_SUFFIX ai-autofix Suffix to add to branch name
force_push PLUGIN_FORCE_PUSH true Force push to branch
unique_per_execution PLUGIN_UNIQUE_PER_EXECUTION false Add timestamp for unique branch each run
commit_message PLUGIN_COMMIT_MESSAGE harness-auto-fix created this fix Commit message

Pull Request Configuration

Parameter Environment Variable Default Description
create_pr PLUGIN_CREATE_PR true Whether to create PR
pr_title PLUGIN_PR_TITLE Auto-fix by Harness AI PR title
pr_description PLUGIN_PR_DESCRIPTION Auto-generated fixes PR description
is_draft PLUGIN_IS_DRAFT false Create as draft PR
bypass_rules PLUGIN_BYPASS_RULES false Bypass branch protection (Harness only)

Control Flags

Parameter Environment Variable Default Description
push_changes PLUGIN_PUSH_CHANGES true Whether to commit and push changes
debug PLUGIN_DEBUG false Enable debug logging
dry_run PLUGIN_DRY_RUN false Log actions without executing

Harness Code Settings

Parameter Environment Variable Description
harness_account_id PLUGIN_HARNESS_ACCOUNT_ID Harness account ID
harness_org_id PLUGIN_HARNESS_ORG_ID Harness organization ID
harness_project_id PLUGIN_HARNESS_PROJECT_ID Harness project ID
harness_base_url PLUGIN_HARNESS_BASE_URL Harness base URL (default: https://app.harness.io)

πŸ“€ Output Variables

The plugin exports these variables for use in subsequent steps:

Variable Description
FIX_BRANCH Branch where changes were pushed
IS_DIFF_PRESENT true if changes were committed, false otherwise
PR_NUMBER Pull request number (if created)
AUTOFIX_PR_ID Alias for PR_NUMBER
PR_URL Pull request URL (if created)
COMMIT_SHA SHA of the new commit
ORIGINAL_COMMIT_SHA SHA before changes

Using Output Variables in Harness CI

- step:
    type: Run
    name: Next Step
    spec:
      shell: Sh
      command: |-
        echo "Branch: <+steps.push_and_create_pr.output.outputVariables.FIX_BRANCH>"
        echo "PR: <+steps.push_and_create_pr.output.outputVariables.PR_URL>"

🌿 Branch Naming Logic

The plugin uses smart branch naming based on your current branch:

Scenario 1: Regular Branch

Current: feature-login
Result: feature-login-ai-autofix

Scenario 2: Detached HEAD

Current: HEAD (detached at abc1234)
Result: commit-abc1234-ai-autofix

Scenario 3: Branch Already Has Suffix

Current: feature-login-ai-autofix
Result: feature-login-ai-autofix (reuses same branch)

Scenario 4: Unique Per Execution

UNIQUE_PER_EXECUTION=true
Current: feature-login
Result: feature-login-ai-autofix-1738054912 (unique each time)

πŸ“‹ Complete Examples

Example 1: Harness CI - Auto-fix Workflow

- step:
    type: Plugin
    name: Auto-fix and Create PR
    identifier: autofix_pr
    spec:
      connectorRef: account.harnessImage
      image: himanshuagrawal/create-pr-plugin
      settings:
        scm_provider: harness
        token: <+secrets.getValue("harness_api_key")>
        harness_account_id: <+account.identifier>
        harness_org_id: <+org.identifier>
        harness_project_id: <+project.identifier>
        repo: <+pipeline.properties.ci.codebase.repoName>
        source_branch: <+codebase.branch>
        
        # Git authentication
        netrc_machine: <+codebase.gitHttpUrl>
        netrc_username: <+codebase.build.spec.username>
        netrc_password: <+codebase.build.spec.password>
        
        # PR configuration
        create_pr: true
        pr_title: "Auto-fix by Harness AI"
        pr_description: "Automated fixes for your CI checks"
        bypass_rules: true

Example 2: GitHub with Drone CI

kind: pipeline
type: docker
name: autofix

steps:
  - name: lint-and-fix
    image: node:18
    commands:
      - npm install
      - npm run lint:fix

  - name: create-pr
    image: himanshuagrawal/create-pr-plugin
    settings:
      scm_provider: github
      token:
        from_secret: github_token
      repo: ${DRONE_REPO}
      source_branch: ${DRONE_SOURCE_BRANCH}
      commit_message: "chore: automated linting fixes"
      create_pr: true
      pr_title: "πŸ€– Automated linting fixes"
      pr_description: |
        Automated fixes from CI pipeline
        
        - Build: ${DRONE_BUILD_NUMBER}
        - Commit: ${DRONE_COMMIT_SHA}

Example 3: GitLab CI

autofix:
  image: himanshuagrawal/create-pr-plugin
  variables:
    PLUGIN_SCM_PROVIDER: gitlab
    PLUGIN_TOKEN: $GITLAB_TOKEN
    PLUGIN_REPO: $CI_PROJECT_PATH
    PLUGIN_SOURCE_BRANCH: $CI_COMMIT_REF_NAME
    PLUGIN_CREATE_PR: "true"
    PLUGIN_PR_TITLE: "Automated fixes"
  script:
    - /app/create-pr-plugin

Example 4: Push Only (No PR)

settings:
  scm_provider: github
  token:
    from_secret: github_token
  repo: owner/repo
  source_branch: main
  push_changes: true
  create_pr: false  # Just push, don't create PR

Example 5: Create PR from Existing Branch

settings:
  scm_provider: github
  token:
    from_secret: github_token
  repo: owner/repo
  source_branch: feature-branch
  push_changes: false  # Don't push new changes
  create_pr: true      # Just create PR from existing branch
  pr_title: "Merge feature to main"

πŸ”§ Local Development

Build

make build

Run Tests

make test

Build Docker Image

make build-docker

Local Testing

  1. Create a test repository
  2. Make some changes
  3. Run with environment variables:
export PLUGIN_SCM_PROVIDER=github
export PLUGIN_TOKEN=your_token
export PLUGIN_REPO=owner/repo
export PLUGIN_SOURCE_BRANCH=main
export PLUGIN_DEBUG=true
export PLUGIN_DRY_RUN=true
./create-pr-plugin

πŸ› Troubleshooting

"Resource not accessible by personal access token"

Solution: Ensure your token has the required permissions:

  • GitHub: repo scope
  • GitLab: api scope
  • Harness: API key with Code Repository access

"PR already exists"

Solution: This is normal! The plugin will return the existing PR. Options:

  • Set UNIQUE_PER_EXECUTION=true to create unique branches each time
  • Keep default to reuse the same branch and PR (recommended for production)

"No changes to commit"

Solution: This is expected when there are no changes. The plugin will exit successfully with IS_DIFF_PRESENT=false.

πŸ“š Documentation

  • README.md (this file) - Complete documentation
  • LICENSE - Apache 2.0 license

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

πŸ“„ License

Apache 2.0 License - see LICENSE file for details.

πŸ”— Related Projects

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors