日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

dotnet 基于 dotnet format 的 GitHub Action 自动代码格式化机器人

發布時間:2023/12/4 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 dotnet 基于 dotnet format 的 GitHub Action 自动代码格式化机器人 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

是不是大家也會覺得代碼審查里面審查代碼格式化問題是無意義的,但是不審查又覺得過不去?是否有個專門的工具人,用來協助修復代碼格式化的問題?本文來安利大家一個特別好用的方法,使用 dotnet 完全開源的專業格式化工具 dotnet format 配合 GitHub 的自動構建 Action 做的自動代碼格式化機器人,這個機器人可以被指定到特定時機,如每天晚上或者每次代碼合并等,進行代碼格式化,格式化完成之后,可以選擇直接推送或者提代碼審查

這個方法將需要用到 dotnet 完全開源的專業格式化工具 dotnet format 工具,請看?https://github.com/dotnet/format

用法十分簡單,可以復制本文最后的 GitHub 的自動構建 Action 的腳本,放在倉庫的?.github\workflows?文件夾里面。現在請讓我告訴大家這個構建腳本的細節

在?.github\workflows?文件夾里面創建的所有?yml?文件都會當成構建腳本,每個腳本就應該給定一個名字,如下面代碼

name: Daily code format check

然后設置構建腳本的觸發時機,如下面代碼設置了在推送了 master 分支時,觸發構建腳本

on: push:branches: - master

其他觸發時機等,還請大家去閱讀官方文檔

下一步是指定運行在什么設備上,如下面代碼

jobs:dotnet-format:runs-on: windows-latest

接下來就是將代碼拉下來了,可以通過如下代碼將當前分支的最新代碼拉下來

steps:- name: Checkout repouses: actions/checkout@v2with:ref: $

本文的格式化方法是使用 dotnet format 工具格式化的,在使用這個工具之前,需要先安裝,請使用如下代碼進行安裝

- name: Install dotnet-formatrun: dotnet tool install -g dotnet-format

原本可以使用一句命令?dotnet format?就進行格式化,但是當前遇到的問題是,如果代碼格式化沒有任何文件更改,那么此時就不應該做創建新的分支和開啟代碼審查了,因此就需要用到 jfversluis 大佬的 dotnet-format 腳本。這個腳本可以輸出參數,用于在后續步驟判斷,如果沒有文件更改,也就是沒有代碼需要格式化就不需要開啟代碼審查了

- name: Run dotnet formatid: formatuses: jfversluis/dotnet-format@v1.0.5with:repo-token: $action: "fix"only-changed-files: true # only works for PRs# workspace: "Xamarin.Forms.sln" 默認根路徑只有一個 sln 文件,可以忽略這一行

如果自己的倉庫里面的根路徑,也就是放在和?.git?文件夾所在的相同的文件夾,存在了一個 sln 文件,那么可以忽略 workspace 參數

調用了上面代碼腳本之后,將會輸出,可以使用如下代碼判斷,是否有文件更改

if: steps.format.outputs.has-changes == 'true'

接下來是 commit 代碼,如果代碼文件有更改的話

- name: Commit filesif: steps.format.outputs.has-changes == 'true' # 如果有格式化,才繼續# 下面將使用機器人的賬號,你可以替換為你自己的賬號run: |git config --local user.name "github-actions-dotnet-formatter[bot]"git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"git commit -a -m 'Automated dotnet-format update'

上面代碼的郵件等是 GitHub 機器人的賬號,你可以替換為你的賬號

最后一步是開啟代碼審查,然后指定代碼審查者

- name: Create Pull Requestif: steps.format.outputs.has-changes == 'true' # 如果有格式化,才繼續uses: peter-evans/create-pull-request@v3with:title: '[Bot] Automated PR to fix formatting errors'body: |Automated PR to fix formatting errorscommitter: GitHub <noreply@github.com>author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com># 以下是給定代碼審查者,需要設置倉庫有權限的開發者assignees: lindexi,walterlvreviewers: lindexi,walterlv# 對應的上傳分支branch: t/bot/fix-codeformatting

這樣就能完成了在開發者將代碼合并或推送到主分支的時候,自動嘗試代碼格式化,如果代碼格式化有文件更改了,那么開啟一個代碼審查,如下圖

感謝?jfversluis?大佬的?dotnet-format?腳本,和?Peter Evans?的創建代碼審查的?create-pull-request?腳本

我比較推薦使用這個方法,盡管 dotnet format 工具是專業的代碼格式化工具,不會讓格式化前后的代碼的 IL 有變更。但是我依然推薦進行一次代碼審查

其實不使用?jfversluis?大佬的腳本也可以,因為?Peter Evans?的創建代碼審查的?create-pull-request?腳本會自動判斷如果沒有 commit 就不創建代碼審查,因此只需要跳過 commit 的失敗就可以了,如下面代碼

- name: Install dotnet-formatrun: dotnet tool install -g dotnet-format- name: Run dotnet formatrun: dotnet format- name: Commit files# 下面將使用機器人的賬號,你可以替換為你自己的賬號run: |git config --local user.name "github-actions-dotnet-formatter[bot]"git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"git commit -a -m 'Automated dotnet-format update'continue-on-error: true

可以看到代碼十分簡潔

而另外的方法是在每個開發者開啟代碼審查的時候,嘗試格式化他的代碼,這樣可以讓代碼審查者也許會更開森,代碼十分簡單,請看下面

name: Format check on pull request on: pull_request jobs:dotnet-format:runs-on: windows-lateststeps:- name: Install dotnet-formatrun: dotnet tool install -g dotnet-format- name: Checkout repouses: actions/checkout@v2with:ref: $- name: Run dotnet formatid: formatuses: jfversluis/dotnet-format@v1.0.5with:repo-token: $action: "fix"only-changed-files: true- name: Commit filesif: steps.format.outputs.has-changes == 'true'run: |git config --local user.name "github-actions[bot]"git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"git commit -a -m 'Automated dotnet-format updateCo-authored-by: $ <$+$@users.noreply.github.com>'- name: Push changesif: steps.format.outputs.has-changes == 'true'uses: ad-m/github-push-action@v0.5.0with:github_token: $branch: $

但是這個方法也許會讓開發者不開森,因為他下一次上傳代碼的時候需要先拉代碼,也許因為格式化給他了額外的改動。另外的,如 Xamarin 倉庫的注釋,其實代碼推送無法用在 fork 的倉庫上,也就是說如果這個代碼審查是另一個開發者在他 fork 的倉庫里面發起的,此時的這個方法將會失效

我現在在?dotnetCampus.Ipc?就接入這個自動代碼格式化機器人,用起來還不錯

構建腳本的全部代碼請看下面

name: Code format checkon: push:branches: - master jobs:dotnet-format:runs-on: windows-lateststeps:- name: Checkout repouses: actions/checkout@v2with:ref: $- name: Install dotnet-formatrun: dotnet tool install -g dotnet-format- name: Run dotnet formatrun: dotnet format- name: Commit files# 下面將使用機器人的賬號,你可以替換為你自己的賬號run: |git config --local user.name "github-actions-dotnet-formatter[bot]"git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"git commit -a -m 'Automated dotnet-format update'continue-on-error: true- name: Create Pull Request# if: steps.format.outputs.has-changes == 'true' # 如果有格式化,才繼續uses: peter-evans/create-pull-request@v3with:title: '[Bot] Automated PR to fix formatting errors'body: |Automated PR to fix formatting errorscommitter: GitHub <noreply@github.com>author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com># 以下是給定代碼審查者,需要設置倉庫有權限的開發者assignees: lindexi,walterlvreviewers: lindexi,walterlv# 對應的上傳分支branch: t/bot/fix-codeformatting

總結

以上是生活随笔為你收集整理的dotnet 基于 dotnet format 的 GitHub Action 自动代码格式化机器人的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。