How to programmatically access Github pull requests, commits and download artifacts?

GitHub Actions workflows can be used to automatically build and test firmware on each commit. They may or may not create Github Releases, if not, build files are downloadable only via Github Action details. By default, those binaries require manual download, but here I will show you how to automate it.
The goal of the project is to create an auto-updater for WiFi MCU firmware, like for BK7231, W800/W600, or anything else supported by OpenBeken.
The basic presentation of Github Actions Binaries used in
OpenBeken was shown here, today I am going to create small C# utility to automate that. Let's start.
Used tools
For this presentation, I will use following tools:
- Visual Studio as a C# IDE and compiler
- Octokit library from NuGet for easier Github API access
- in some later parts, I will also use HTTPClient to directly access Github API, as I had some issues with Octokit. Responses will be parsed via JSONSerializer
Octokit library
First you'll need the Octokit library. Octokit provides easy access to Github API and can be downloaded within Visual Studio via "Manage NuGet Packages":

Add it to your stock Console program and make sure you have correct usings:
Code: C#
Initializing GitHubClient
Octokit library is using asynchronous tasks to run, so if your main function is not a task, you'll need to await for the result. Here's basic GitHubClient initialization:
Code: C#
Github Access Token
This is not required for listing pull requests, but needed for artifacts. You will also obviously need it for the write access. Login to Github and go to Settings:

then to Developer settings:

and then to Tokens. I have used "Classic" mode:

Create a token, make sure to write it down. Choose the token scopes depending on what you are going to use to the token for. Don't give full write access to token if you don't want to use it to write to repository and use separate token for reach purpose you have.
Finally, add the authentication code to your sample:
Code: C#
Enumerating pull requests
Now it's time to get this list:
https://github.com/openshwprojects/OpenBK7231T_App/pulls
This can be done with PullRequest object:
Code: C#
Result:

Enumerating commit
Once you have Pull Request, you can get the latest commit. All commits can be listed in similiar way:
Code: C#
Result:

Accessing workflows
For workflows, you'll need a separate function that will get JSON information from Github.
The URL we want to access is the following:
https://api.github.com/repos/openshwprojects/OpenBK7231T_App/commits/d763b28c9a433217f3c95ef4c9bbd6777a32486d/check-runs
The commit Hash to be inserted there.
Returned JSON is very simple:
Code: JSON
It's basically a JSON representation of each build here:

We're very close!
So, let's prepare the HttpClient and do GET request.
Code: C#
Then, let's put resulting JSON data into the classes with JsonSerializer:
Code: C#
Iterating the runs
Once you have a list of workflows, you can iterate it:
Code: C / C++
Result:

Fetching artifacts
Now we need to get links to zip files. We will need to extract a numerical ID for that. The ID can be found in workflow run data:
Code: C#
Here is a sample run ID: 12328623947
You can now construct link like:
https://api.github.com/repos/openshwprojects/OpenBK7231T_App/actions/runs/12305496423/artifacts
Visiting this link yields following JSON:
Code: JSON
It's time to parse it. Here is function call:
Code: C#
And the implementation:
Code: C#
Again, the JSON data is deserialized into classes with JsonSerializer. Used classes:
Code: C#
Finally it's time to iterate them:
Code: C#
Result:

Downloading ZIP files
Now we finally have ZIP file links, there's not much left to do:
Code: C#
Download handler:
Code: C#
Finally, after a few moments of downloading, we're getting OBK build files on our drive:

Now it's time to futher process them, maybe send them to devices via OTA, but it's a task for another topic.
Warning!
Without authentication, you can easily hit API rate limit:
Error: API rate limit exceeded for 123.234.233.121. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)
Press any key to exit...
Summary
This was the first part of Github Artifacts downloading presentation. In this part we've learned how to access Github API to list pull requests, commits, workflows and artifacts. We've also managed to download built files.
In the next part I will present the tool that I based on the knowledge shown here:

This tool will be able to automatically keep OBK device updated to the latest build from given PR, thus making testing and development easier.
Stay tuned!
What would YOU have used Github API for?
Full demo code is in the description:
Comments
Repository with a very early and dirty draft of mentioned tool: https://github.com/openshwprojects/OBKotaTool My private TODO for upcoming days: - add field for Github key and store settings in JSON -... [Read more]
It does start. https://obrazki.elektroda.pl/2186961800_1734383027_thumb.jpg [Read more]
Now you need a Github API key, a PR link, and device IP. btw: Shall I do the same for https://github.com/openshwprojects/BK7231GUIFlashTool ? [Read more]
Test successful! https://obrazki.elektroda.pl/6735248300_1734383534_thumb.jpg https://obrazki.elektroda.pl/8129852400_1734383542_thumb.jpg https://obrazki.elektroda.pl/7681850... [Read more]
cool https://obrazki.elektroda.pl/7010300000_1734385182_thumb.jpg [Read more]
This will help new chip implementation and testing and speed up the process but a large margin!! Now I can't wait for my new modules to arrive :D [Read more]
I hope you guys realize that now, if you push a change to that pull request, the tool will automatically update your device. It queries Github PR in the background constantly, just to detect the change.... [Read more]
Suggestion, as you already have a mechanism to determine what chip type is on that IP, a log window would be nice where that info would be displayed. Such is current build, platform (maybe option for future... [Read more]
build, still running, already showing https://obrazki.elektroda.pl/9125516000_1734386421_thumb.jpg [Read more]
I think I've handled this issue. If commit is available, but there are no artifacts yet, it shall retry every 5 seconds with a new message in log: https://obrazki.elektroda.pl/9784584900_173438658... [Read more]
Another idea. What if we implement this tool in the Web App or like in a Web Page? So it's done online? A web utility that can be run anywhere, and can either update OBKs to given release, or to given... [Read more]
that sounds excellent [Read more]
If it's web (html/js), then count me out. But, maybe use .net8 and avalonia, then it will be cross-platform too. And it may work in browser with wasm. [Read more]
My 2 cents: Web App - very good, Web Page - even better [Read more]
For me it really does not matter if it is web based or an win application. [Read more]
I will try to make web version someday, but first I will focus on finishing C# one. @insmod you're doing great things with porting so focus on that for now if you can. We can do testing much faster... [Read more]
And it works like a charm... https://obrazki.elektroda.pl/5500997300_1734814706_thumb.jpg Added after 3 [hours] 13 [minutes]: Auto build PR for PROTA tool added. https://github.com/op... [Read more]
It helped me, Thank you so much. [Read more]
regarding the URL pattern matching https://obrazki.elektroda.pl/9288791700_1735895056_thumb.jpg Is there an API/technological reason an URL like this couldn't be used also to get artefacts... [Read more]