This Go library provides a set of functions to interact with GitHub Pull Requests, specifically for parsing and processing the contents of Git diffs associated with pull requests.
- Parse GitHub Pull Request URLs to extract owner, repository, and PR number.
- Retrieve the contents of a Pull Request's Git diff from GitHub.
- Parse combined Git diffs into individual file diffs.
- Filter out file diffs based on a list of ignored file extensions.
- Comprehensive regex-based file path matching for filtering file diffs.
- Robust and extensive unit testing to ensure reliability and functionality.
- Dependency injection support for GitHub API client, allowing for easier testing and flexibility.
To use this library, install it using go get
:
go get github.com/kmesiab/go-github-diff
prURL, err := github.ParsePullRequestURL(
"https://github.com/username/repository/pull/123",
)
if err != nil {
// Handle error
}
// Use prURL.Owner, prURL.Repo, and prURL.PRNumber
// Create a new github client
// (You can pass a HTTP Client, instead of nil)
client := github.NewClient(nil)
// Create a new GitHubClientWrapper using the github client
ghClient := ghdiff.GitHubClientWrapper{Client: client}
// Process the pull request using the new function
prString, err := ghdiff.GetPullRequestWithClient(context.TODO(), prURL, &ghClient)
if err != nil {
fmt.Printf("Error getting pull request: %s\n", err)
return
}
// Use diff as a string containing the Git diff
diff := "..." // Git diff string
ignoreList := []string{".md", ".txt"}
gitDiffs := github.ParseGitDiff(diff, ignoreList)
for _, gitDiff := range gitDiffs {
// Process each gitDiff
}
Contributions to this library are welcome! Here's how you can contribute:
-
Go to the GitHub repository page: [GitHub Repository URL]
-
Click on the 'Fork' button at the top right corner of the page. This creates a copy of the repository in your GitHub account.
-
Clone the forked repository to your local machine:
git clone https://github.com/yourusername/reponame.git
-
Navigate to the cloned directory:
cd reponame
-
Create a new branch for your changes:
git checkout -b feature-branch-name
-
Make your changes in the new branch. Be sure to follow the project's coding standards and guidelines.
-
Commit your changes with a descriptive message:
git commit -am "Add a brief description of your changes"
-
Push the changes to your fork:
git push origin feature-branch-name
-
Go to your forked repository on GitHub, and click 'New Pull Request'.
-
Ensure the 'base fork' points to the original repository, and the 'head fork' points to your fork.
-
Provide a clear and detailed description of your changes in the pull request. Reference any related issues.
-
Click 'Create Pull Request' to submit your changes for review.
- Wait for feedback or approval from the repository maintainers.
- If requested, make any necessary updates to your pull request.
- Once your pull request is merged, you can pull the changes from the original repository to keep your fork up to date.
Thank you for your contributions!