Often when making a change, you want to verify your approach with the maintainer or ask them for help. In this lesson we’ll make a change and commit that WIP change. Then we’ll create a pull request requesting help from the project maintainer.
We're finally ready to start making our changes to add padLeft to the project. Let's review the contributing guidelines in the contributing.md file of the project. Here, we've already run NPM install, and run the tests and the build.
Now our next step is to run NPM run test watch. If we run "NPM run test watch," then we're going to see that it's running all the tests, and watching the file system for changes to rerun the test as we make changes to the files.
Let's just verify that this is working by opening up our editor. We'll open up one of the files, and we'll just add a new line here, and hit save. There, it reran the test. Everything is ready for us to start contributing to this project.
Now we're going to want to look at the acceptable contributions information to find out more information about how our contribution should be accepted. It's also wise for us to look at the other methods that have been implemented, and implement ours in a similar fashion.
We notice that flatten has an export default of the function. Then it has the function definition, and it has the documentation in here. We want our code contribution to have the same style as the other code throughout the project.
This is definitely something that we want to take note of for our implementation. I've skipped ahead a little bit here, because the actual implementation isn't really necessary for this lesson. If we look at the Git status, we'll see that we modified the source index.js, and added these two files.
If we look at the Git diff, we'll see the modification to the index.js was simply to import the padLeft function, and then export it. We can go ahead and take a look at padLeft. This is formatted just like we need.
It is using the function that we discussed with the project maintainer. Then we have this padLeft test that follows the standard for the other tests in the project. The problem is here there's one thing that we couldn't quite figure out for our specific scenario.
That is this project requires us to have 100 percent code coverage, and we didn't quite meet that standard. This is a fairly common scenario when you're contributing to an open source project. You have part of the implementation done, but you're not quite certain how to finish the entire implementation.
What we're going to do is we're going to commit what we have and create a pull request out of that so that we can iterate with the project maintainer to get a little bit of help, and a little bit of direction, to make sure that we're taking this solution in the direction that the project maintainer wants to accept.
To do this, we're going to Git add everything. Let's take a look at our Git status. Now we'll run Git commit with the message, "Work in progress. Cannot get coverage quite there." You'll notice that in this project, it's actually running a series of Git hooks.
Git hooks are some scripts that you can configure to run during certain parts of Git, like when you're creating a commit message, or when you're pushing to a repository, or committing some code. Many projects don't have Git hooks.
Don't be alarmed when you some random scripts running, when you're interacting with Git in a repository. If you're ever having trouble with a Git hook, and you still want to push your changes to ask for some help, just simply add the --no-verify flag to whatever command that you're running.
It will skip all the Git hooks. That way, you can get some help. Now we're going to push this up to our remote repository, to our fork. Then we'll create a pull request. To do this, we have to run Git push, but then Git needs to know where to push these changes to.
We're going to say origin, which is the name for the remote that is assigned to our fork. Then we need to tell it which branch on that remote to send our changes to. This will be the same as our local branch, PR/padLeft.
It can get cumbersome to specify the remote that you want to send your changes to, and what branch to send it to. To specify the upstream for a particular branch, you can say, "Git push --set upstream," and then specify the remote that you want this to be tracked against.
For us, that'll be origin. Then the branch would be PR padLeft. In the future, that's the first push you would specify the upstream. Then you wouldn't have to specify the remote branch that you're going to be sending these changes to.
As a pro tip, this set upstream can be shortened to -u. That's the command that you would use for the first time that you push to a remote branch. We can go ahead and do that now. Now that branch is set to track remote branch PR left from origin.
Now we no longer need to specify origin PR padLeft. We can simply say, "Git push" when we're on this branch, and it will automatically push to the right branch on the right remote. Now on the GitHub repository, we'll see that GitHub recognizes that we recently pushed changes to this branch.
We can instantly compare and create a pull request here. However, this only shows up for about an hour or so. If that's not showing up for you, the other way to do this is by clicking the "create new pull request" button.
Then you'll want to compare across forks, change your head fork to your fork, and then change the branch that you're comparing to the branch that you just pushed. Then you can create your pull request. You give a subject.
We can leave it as the default being the message of our commit. Then we can add a comment here. You'll notice that in the comment for this pull request, I've added as much information as I can about the help that I need.
This is really important for project maintainers. Many of them are really busy, and are doing this on their free time. You want to be as helpful as you possibly can. With this comment, we'll go ahead and click create pull request.
This will create our pull request. We can look at our files changed. This is the difference between the base branch, and our head branch that we want to have merged into this repository. Now we patiently wait until the project maintainer has a moment to look at our pull request, and give us some feedback.