November 04, 2021

Check formatting locally and on GitHub Actions

by Tyler Weaver

Transcript: Hello, I’m Tyler. And welcome to another episode of ROS developer notes. Today, I’m gonna tell you about something I’m really excited about. It’s a new way to do formatting or new to us, and I’m applying it to all the move it ecosystem of packages and all of our client projects. And I think it’d be really, you’d find it. You’ll find it really useful on any project you work on. Um, it’s a new way to run format matters and to test them. It’s called pre-commit and what’s really cool about it is it’s easy to run locally. It’s much more than just cling format and it’s easy to test and CI, like I, it’s a really simple config to get it working in CI and to show you about it. I am going to walk through applying it to a package that I haven’t applied it to yet and make a PR out of it.

We’re gonna do random numbers. Um, it’s one of the ROS planning packages I haven’t done yet. And I’m just gonna show you the config and move it too first. So this is all the stuff it does. Um, it checks that somebody’s not adding large files in their, in their thing. It checks that there’s, uh, not case conflicts and merge conflicts and that SIM links are good. And that yam Sy Texas is correct, and that they have good debug statements. And that the end of the file has, um, a new line and that there’s not mixed line endings and that there’s trail no trailing white space. And then this is like a really easy way to add black before I wanted to add black by writing an, a mint, uh, lint black formatter. Um, but I think that, uh, the linters that belong in a mint lint are very different from what sort of, uh, it it’s different from a formatter.

You wanna play a formatter over the whole repo and a mint lint works really well at and per package. And they’re, they’re good for other kinds of tests that output more detailed information formats basically pass fail, and oftentimes it fixes stuff for you. And that’s, that’s what you want from a format or so black is one. And then here is the rule we have for claim format. We use claim format 10 in all of our repos. Um, so we’ll walk you through using it, uh, first to learn to install it. I created a, in the Wiki for move it two. Um, I created this, I mostly created it so that I could comment it on people’s PRS when forgot to do it. And it would be like ready, made instructions for how do you install it locally? Um, something we should talk about when you use it locally, there’s sort of two ways to use it.

Why it’s called pre-commit is because it can install itself as a GI hook for when you commit and run automatically. And one thing that’s really cool about that is because it’s Ty into get really fast. It only runs the formatter over files that have changed. You’re like, well, how do I format the whole repo this way? Well, you can run it manually with pre-commit run back a and that’ll run it over everything. Um, affirmative and to install it in your local, um, the repo you’re working on, you use pre-commit install and then it installs itself as a GI hook it’s done per repo in your local file system. You can’t install the, um, like for your user account, which would’ve been really nice, but not one of the features. Um, and it’s really nice. Uh, I have a little note down here about 1804 using 1804 and using Python two, you really shouldn’t be Python two’s end of life, come over to Python three. Um, that’s all. Um, and then installing clean format 10. All right. Let’s get to doing it on random numbers. So here’s random numbers. Um, you’ll see, right now we have a Travis Yael. Uh, the other thing I’m doing is I’m migrating GitHub actions. I will eventually get this off of Travis, but for now we’re just gonna do the pre-commit thing. So I’m gonna copy that and then go over my terminal.

Uh let’s uh, get loan that, um, all right, so we’re here. Uh, the first thing we need is at file, so I’m just gonna use the one that I’m using and move it two, uh, recommit, uh, uh, I think there’s a check X amount, one that I want also, but I can deal with that later. Um, good enough. Uh, we just wanna turn it on. So the first thing I’m gonna do is just check to see, make sure it works. Uh, uh, it’s gonna find some stuff that is failing. Uh, there’s something wrong with a Yamo pile. That’s not really a surprise. Um, our cling format config for all of mod is when we copy past it, everywhere has the Yael errors in it, and this has helped us find them and fix it. Um, there’s also an end of file thing and, uh, then playing format actually modified some stuff.

That’s crazy. Anyway. Um, so if we look at the get status, this is what we see. Um, so I need to edit this to fix this. Um, you will see that this line and this line are the same. You’re not supposed to do that. Uh, that was an accident. Um, so I need to delete one of those. And then down here, um, this is Jason’s X, uh, which thel technically supports JSON, but it causes issues if you, yeah, so we want to use just actual J actually. Um, so then running it again. Um, they’re all passing. All right, cool. So I wanna add this as a GitHub action. So I’m gonna go back to my browser. Um, I’ll show you how to do that. So GitHub workflows, we have this format, date YAML. Um, it’s really simple. Just copy and paste it and really use the pre-commit thing.

So, uh, raw copy it. I’ll go back to the terminal. Um, right, so that’s gonna add it. Um, all right, now we need to check out a branch to do our thing on. I’m gonna, uh, call this one, add permit. Um, now here’s a point we can talk about and it’s that when you do formatting changes and you wanna submit ‘em as a PR, it’s often really helpful to have config changes separate from the actual formatting changes. So I’m gonna do that. I’m gonna so get status. Uh, I’m gonna add, add, um, a pre-commit YAML file, uh, the GitHub action, um, and only that, uh, the other things are to make it pass right to fix the formatting. Um, so we can see there’s, that’s what I’m adding, uh, uh, adding recommit checks.

Oh, one more thing. I wanna add it to the readme. Um, I wanna add a badge, the readme. So, um, this is the, the build status from Travis. Uh, we don’t care about that for now, but, um, affirmative we want to add the format one. Um, so I’m gonna go back to my browser and I’m gonna find the one I put and move it to for this. Um, so I can copy it and edit it. Uh, read me. So it’s right here, this thing, look at the raw format one there, back over here. All right. Um, so let’s go back to random numbers. Uh, branch is master that I’m adding this to, so plus one branch, I imagine that’s. Um, and all right, so master the branch, uh, in Ross planning, but it’s, um, both the badge and what the badge links to both of them there.

All right, hit commit. We’re gonna call this, uh, or three commit X and what, what do we have get, add that, get status, get six there. All right. Now we’re ready to submit ours. Um, normally you push through fork and maintainer so I can push to the actual refill. And the reason I’m doing this is there aren’t any GitHub actions running yet on this repo. And until there is the only way to get them to start running is for it to be on a branch. That’s actually late on the repo. So I’m gonna push to the actual repo and make a bar from that. Um, so, uh, we’re gonna push this. Oh, I gotta show you one other cool tool. Uh, that’s the, uh, the GitHub command line interface. So GitHub PR create, we’re gonna create a PR from this, um, uh, call it that, uh, that, uh, that’s title continue in the browser.

So if I go over here, it’ll do this recommit text, uh, creating a here on the, you get, um, uh, we from Robert, Robert’s been really helpful getting me getting these changes in it was him who showed me this pre-commit thing. He did it first on, uh, MTC. I think it’s awesome. It’s yeah, it’s great. Um, so here we check to see what we have. We have this pre-commit checks, um, test and fixed formatting errors, um, goal that’s changing. We’re adding this at, uh, the only like major formatting is a bunch of spaces in front of the license and the namespace thing wasn’t correct. This is because like you either thought cl format, wasn’t going to change anything that was changed by cling format and is because cling format had errors in its config that it was somehow silently letting us get away with. Um, and then just more, you know, faces where there shouldn’t be. Okay. Uh, so we create the poll request and here’s our new check format format, um, viewed, watch it run and you’ll see what it does. And then out gonna show you how you install it locally and like how it would run when you do a get commit. Uh, we’ll do a reveal change locally. Um, see what it does. All right.

So it’s gotta install the stuff. Uh, it failed what it, it fail for. Uh, okay. So there are errors. All right. Now I gotta show your interactive base. Uh, let’s go back and fix that. All right. So, and we’ll just anyway, so, uh, we’re gonna do Aase on master. Uh, we wanna edit this one cuz it’s actually in the files we care about. Um, and so I’m gonna run Bri commit from all, um, it, it changed our, those, it, it also changed the other, but we’re gonna ignore those changes because the fixed formatting error commit we have could already fix those. Um, so get ad GitHub, uh, workflows that get at recommit. Um, okay, so we’re gonna amend no edit. We’re not gonna change. Uh, commit message. Just so no edit means, uh, rebate. Can, uh, you missed, okay. Status, get, uh, or drop those, uh, get rebate in you. All right. Now we’re at the head. Um, just to make sure everything’s still kosher, uh, that okay. Every still passing there’s no diff now we need to push, uh, uh, it’s a force push because we are, uh, we’re in a force with peace, um, force push because we’re changing the history. We’re actually like writing a over, uh, an minute. That’s gonna push, uh, come back to a browser and it’s gonna run again and we’ll see what it does.

Oh, one cool feature. You hit shift F to log up. It’s a, so all right. Looks like, looks like everything passed. You know, some of them are skipped. Talk about that. There’s no Python files, so black isn’t even running. Uh, there are no Python files. The debug statements is a Python check and there are no SIM links, but all these other checks passed. Um, the total time was 36 seconds run really fast in CI and it runs in parallel to all the other things. So have to get back out full screen. Um, go back conversation, get passed. You were waiting on Travis, which is, um, it’s even passing on the official Ross build farm before Travis is queuing. That’s how bad Travis is. All right. Uh, let me show here. So if I do recommit install, uh, it adds it to the get hooks to this, uh, local copy of the repo. So shoot what that does. We’re gonna create a best brand.

We’ll add a change here. Uh, add something to read me. This is a text get and get commit. All right. Now it’s gonna run. It’s a commit hook. You didn’t find any errors if it had, it would stop the commit and we could change those and try the commit again. Um, which is great. It, it really is gonna go back to add, pre-commit get rid of one. All right. So that’s the whole story pre-commit is awesome. Uh, it runs really fast in CI. It fixes a lot more than just cl format. It fixes your cl format at config. When you have a broken click format config, how embarrassing is that? Uh, it is really easy to use locally. You can, once you’ve installed it. And once it’s up and working on your repo, then your developers just have to do pre-commit install. And then the checks automatically run that when they run a commit. So they won’t be pushing and they don’t have to learn how to use all these different tools, cuz they’re all used for them. It’s excellent. Uh, I hope this is raised your asset IQ, stop using a min claim format and come use this the rest of us, uh, black. It it’ll be great. Um, have a great day.