DEV Community

Cover image for From Kotlin Scripting to Python
Nicolas Fränkel
Nicolas Fränkel

Posted on • Originally published at blog.frankel.ch

From Kotlin Scripting to Python

GitHub offers a way to customize one's profile by allowing one to create a README in a specific repository, named as your profile, e.g., nfrankel/nfrankel. A couple of years ago, I automated the update of my GitHub profile with up-to-date info: my latest blog posts, my upcoming talks, and the last recorded YouTube talk. I took the time to document how to do it on this blog.

At the time, I chose Kotlin scripting because I was proficient enough in Kotlin, but I wanted to learn the scripting part. Over the years, I became more and more dissatisfied with the solution. I recently moved away from Kotlin Scripting to Python. In this post, I want to explain my reasons and document the migration.

The previous situation

First things first, I'm a big proponent of Kotlin; my issues were in other areas.

When I first developed the code, I had the feeling that Kotlin scripting was an unloved child. The documentation is pretty straightforward about it:

Kotlin scripting is Experimental. It may be dropped or changed at any time. Use it only for evaluation purposes. We appreciate your feedback on it in YouTrack.

Get started with Kotlin custom scripting

I didn't pay much attention then, believing it was temporary. The problem is that the status has stayed the same over two years. At some point, an experiment either graduates or the product is dropped. But Kotlin scripting stays in an intermediate Schrödinger state, neither really living nor truly dead.

Kotlin scripting allows dependencies via in-file annotations. Here's a dependency to the Freemarker templating engine:

@file:DependsOn("org.freemarker:freemarker:2.3.32")
Enter fullscreen mode Exit fullscreen mode

Dependabot and Renovate are bots that can regularly check dependencies for updates. Renovate manages more ecosystems than Dependabot, , e.g.,, Docker Compose, but still doesn't handle Kotlin scripting. It's a vicious circle because since not many use Kotlin scripting, it doesn't support it, and since it doesn't, not many use it. The consequence is that I had to check dependencies by myself regularly.

The final problem in my setup was entirely unrelated to Kotlin scripting but was the push to change anyway. I had put the script inside the magic GitHub repo. For this reason, the GitHub history contained daily README commits sprinkled with dependency upgrades and my changes.

The target setup

I put the code in a different repo than the profile one. I now have two repositories:

  • nfrankel: the target repo with the README
  • nfrankel-update: the repo hosting the script

It fixes the latest issue I mentioned above.

I chose Python because I used it for simple scheduled jobs in the last couple of years, and I'm happy enough about the results. A simple search pointed me to the following dependencies:

I manage them via Poetry. Renovate manages Poetry; it takes care of my first concern.

What the code does is precisely the same. It's about reading posts from this blog's RSS feed, talks from my underlying blog repo, and videos from YouTube.

Challenge

During the migration, I had a couple of hiccups, but the biggest challenge was committing to another repository.

Most GitHub actions either don't touch the repository hosting the action at all or commit to it and it only. In my case, I'm decoupling the script and the repo it acts upon:

jobs:
  update:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo                                 #1
        uses: actions/checkout@v4
      - name: Checkout profile repo                         #2
        uses: actions/checkout@v4
        with:
          repository: nfrankel/nfrankel
          path: nfrankel
          token:  ${{ secrets.NFRANKEL_GITHUB_TOKEN }}      #3
# Set up and run the script here
      - name: Commit README and push                        #4
        uses: EndBug/add-and-commit@v9
        with:
          cwd: './nfrankel'
          add: README.adoc
          default_author: github_actions
          message: Automatically update README.adoc
Enter fullscreen mode Exit fullscreen mode
  1. Checkout current repo, the one hosting the script
  2. Checkout profile repo in a sub-directory
  3. Use an explicit GitHub token
  4. Commit the README and push it to the profile repo

The magic happens in step 3 above. By default, GitHub allows you to commit to the repo of the action. When one wants to commit to other repos, one needs to have a regular token, the same one you'd use outside of GitHub. Besides, you must set the token in the checkout step, not the commit step. The rest is usual.

Conclusion

In this post, I've explained why and how I migrated from Kotlin scripting to Python. In my context, the latter is a better fit than the former. It will be the case until JetBrains commits to Kotlin scripting.

The complete source code for this post can be found on GitHub:

Update my GitHub profile

Refreshing the token

GitLab doesn’t support unexpiring tokens anymore. Every now and then, you’ll need to create a new token. It’s a reminder of the fun to update such a token.

  1. Create a new token PAT in GitLab: https://gitlab.com/nfrankel/nfrankel.gitlab.io/-/settings/access_tokens

  2. Name it something relevant, e.g., GitHub Profile access

  3. Give it at least read_repository scope

  4. Connect to the Raspberry Pi

  5. Update the BLOG_REPO_TOKEN value in /opt/github-actions-runner/nfrankel-update/.env

  6. Restart the service:

    • sudo ./svc.sh stop

    • sudo ./svc.sh start




TODOs

  1. Add a script to update the token automatically

  2. Dockerize the client part







To go further:


Originally published at A Java Geek on March 3rd, 2024

Top comments (0)