From 8c67e5d2763fb1610a1566122275b3423445338e Mon Sep 17 00:00:00 2001 From: realmarv Date: Tue, 11 Apr 2023 13:51:49 +0330 Subject: [PATCH 1/5] scripts/detectNotUsingGitPush1by1.fsx: fix In private repositories, this script needs to use an access token to be able to use GitHub api for accessing repository info. In this commit I added Authentication header to GitHub api requests. --- .github/workflows/CI.yml | 2 ++ scripts/detectNotUsingGitPush1by1.fsx | 34 ++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index efd23b7f3..eaefb5f6c 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -99,6 +99,8 @@ jobs: runs-on: ubuntu-22.04 container: image: "ubuntu:22.04" + env: + ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} steps: - name: Install required dependencies run: | diff --git a/scripts/detectNotUsingGitPush1by1.fsx b/scripts/detectNotUsingGitPush1by1.fsx index 9bebfb65a..03e860ec8 100644 --- a/scripts/detectNotUsingGitPush1by1.fsx +++ b/scripts/detectNotUsingGitPush1by1.fsx @@ -19,6 +19,26 @@ if String.IsNullOrEmpty githubEventPath then Environment.Exit 2 +let accessToken = Environment.GetEnvironmentVariable "ACCESS_TOKEN" +(* +To save your GitHub access token as an environment variable in GitHub +Actions, you can use the secrets feature in GitHub. In your GitHub Actions +workflow file, you can reference the secret as an environment variable using +the syntax ${{ secrets.GITHUB_TOKEN }}. For example: + +``` +name: My Workflow + +on: [push, pull_request] + +jobs: + my_job: + runs-on: ubuntu-latest + env: + ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} +``` +*) + type githubEventType = JsonProvider<""" { @@ -779,7 +799,7 @@ type PRCommitsType = ] """> -let GitHubApiCall(url: string) = +let GitHubApiCall (url: string) (accessToken: string) = let userAgent = ".NET App" let xGitHubApiVersion = "2022-11-28" @@ -794,11 +814,19 @@ let GitHubApiCall(url: string) = client.DefaultRequestHeaders.Add("User-Agent", userAgent) client.DefaultRequestHeaders.Add("X-GitHub-Api-Version", xGitHubApiVersion) + if not(String.IsNullOrEmpty accessToken) then + client.DefaultRequestHeaders.Add( + "Authorization", + $"token {accessToken}" + ) + (client.GetStringAsync url).Result let prCommits = let url = parsedJsonObj.PullRequest.Links.Commits.Href - let prCommitsJsonString = GitHubApiCall url + + let prCommitsJsonString = GitHubApiCall url accessToken + let parsedPrCommitsJsonObj = PRCommitsType.Parse prCommitsJsonString parsedPrCommitsJsonObj |> Seq.map(fun commit -> commit.Sha) @@ -812,7 +840,7 @@ let notUsingGitPush1by1 = gitRepo commit - let json = GitHubApiCall url + let json = GitHubApiCall url accessToken json.Contains "\"check_suites\":[]" ) From 346828c31baf4fa485c04079125b3839764cdadf Mon Sep 17 00:00:00 2001 From: realmarv Date: Thu, 13 Apr 2023 13:16:30 +0330 Subject: [PATCH 2/5] wip1 --- scripts/detectNotUsingGitPush1by1.fsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/detectNotUsingGitPush1by1.fsx b/scripts/detectNotUsingGitPush1by1.fsx index 03e860ec8..afedd697e 100644 --- a/scripts/detectNotUsingGitPush1by1.fsx +++ b/scripts/detectNotUsingGitPush1by1.fsx @@ -19,7 +19,6 @@ if String.IsNullOrEmpty githubEventPath then Environment.Exit 2 -let accessToken = Environment.GetEnvironmentVariable "ACCESS_TOKEN" (* To save your GitHub access token as an environment variable in GitHub Actions, you can use the secrets feature in GitHub. In your GitHub Actions @@ -38,6 +37,7 @@ jobs: ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} ``` *) +let accessToken = Environment.GetEnvironmentVariable "ACCESS_TOKEN" type githubEventType = JsonProvider<""" From 4c4b37f7e16e8e33e2c77d36e33d1a1bfbed0315 Mon Sep 17 00:00:00 2001 From: realmarv Date: Thu, 13 Apr 2023 13:19:43 +0330 Subject: [PATCH 3/5] wip2 --- .github/workflows/CI.yml | 135 --------------------------------------- 1 file changed, 135 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index eaefb5f6c..6b85496b9 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -3,99 +3,8 @@ name: CI on: [push, pull_request] jobs: - build: - name: Build - runs-on: ubuntu-22.04 - container: - image: "ubuntu:22.04" - steps: - - uses: actions/checkout@v2 - - name: Install required dependencies - run: | - apt update - apt install -y sudo - sudo apt install -y git - # workaround for https://github.com/actions/runner/issues/2033 - - name: ownership workaround - run: git config --global --add safe.directory '*' - - name: Install dotnet sdk - run: sudo apt install -y dotnet6 - - name: Compile the conventions solution - run: dotnet build --configuration Release conventions.sln - - name: Compile F# scripts - run: dotnet fsi scripts/compileFSharpScripts.fsx - - file-conventions-tests: - name: Run FileConventions-lib unit tests - needs: build - runs-on: ubuntu-22.04 - container: - image: "ubuntu:22.04" - steps: - - uses: actions/checkout@v2 - - name: Install required dependencies - run: | - apt update - apt install --yes sudo - - # We need to install curl otherwise we get these errors in the CI: - # Unable to load the service index for source https://api.nuget.org/v3/index.json. - # The SSL connection could not be established, see inner exception. - # The remote certificate is invalid because of errors in the certificate chain: UntrustedRoot - sudo apt install --yes curl - - - name: Setup .NET - run: apt install -y dotnet6 - - name: Run tests to validate F# scripts - run: dotnet test src/FileConventions.Test/FileConventions.Test.fsproj - - commitlint-plugins-tests: - name: Run commitlint-related tests - needs: build - runs-on: ubuntu-22.04 - container: - image: "ubuntu:22.04" - steps: - - uses: actions/checkout@v2 - - name: Install required dependencies - run: | - apt update - apt install --yes sudo - sudo apt install --yes git - - sudo apt install --yes curl - # can't install ubuntu's default nodejs version because we would get this error: - # error @jest/core@29.4.1: The engine "node" is incompatible with this module. Expected version "^14.15.0 || ^16.10.0 || >=18.0.0". Got "12.22.9" - curl --show-error --location https://deb.nodesource.com/setup_14.x | sudo --preserve-env bash - - sudo DEBIAN_FRONTEND=noninteractive apt install --yes nodejs - - name: Print versions - run: | - git --version - node --version - npm --version - npx commitlint --version - - name: Install yarn - run: | - npm install --global yarn - yarn add --dev jest typescript ts-jest @types/jest - - name: Install commitlint - run: | - npm install conventional-changelog-conventionalcommits - npm install commitlint@latest - - name: Print versions - run: | - git --version - node --version - npm --version - npx commitlint --version - - name: Run tests to validate our plugins - run: yarn jest - sanity-check: name: Sanity check - needs: - - file-conventions-tests - - commitlint-plugins-tests runs-on: ubuntu-22.04 container: image: "ubuntu:22.04" @@ -120,52 +29,8 @@ jobs: git --version node --version npm --version - - name: Validate current commit (last commit) with commitlint - if: github.event_name == 'push' - run: ./commitlint.sh --from HEAD~1 --to HEAD --verbose - - name: Validate PR commits with commitlint - if: github.event_name == 'pull_request' - run: | - ./commitlint.sh --verbose \ - --from ${{ github.event.pull_request.head.sha }}~${{ github.event.pull_request.commits }} \ - --to ${{ github.event.pull_request.head.sha }} - name: Install dotnet sdk run: sudo apt install --yes dotnet6 - - name: Check all files end with EOL - run: dotnet fsi scripts/eofConvention.fsx - - name: Check all .fsx scripts have shebang - run: dotnet fsi scripts/shebangConvention.fsx - - name: Check there are no mixed line-endings in any files - run: dotnet fsi scripts/mixedLineEndings.fsx - - name: Check there are no unpinned GitHubActions image versions - run: dotnet fsi scripts/unpinnedGitHubActionsImageVersions.fsx - - name: Check there are no unpinned dotnet package versions - run: dotnet fsi scripts/unpinnedDotnetPackageVersions.fsx - - name: Check there are no unpinned nuget package reference versions in F# scripts - run: dotnet fsi scripts/unpinnedNugetPackageReferenceVersions.fsx - name: Check if gitPush1by1 was used if: github.event_name == 'pull_request' run: dotnet fsi scripts/detectNotUsingGitPush1by1.fsx - - name: Install prettier - run: npm install prettier@2.8.3 - - name: Change file permissions - # We need this step so we can change the files using `npx prettier --write` in the next step. - # Otherwise we get permission denied error in the CI. - run: sudo chmod 777 -R . - - name: Run "prettier" to check the style of our TypeScript and YML code - run: | - sudo npx prettier --quote-props=consistent --write './**/*.ts' - sudo npx prettier --quote-props=consistent --write './**/*.yml' - # Since we changed file modes in the previous step we need the following command to - # make git ignore mode changes in files and doesn't include them in the git diff command. - git config core.fileMode false - # Since after installing commitlint dependencies package.json file changes, we need to - # run the following command to ignore package.json file - git restore package.json - git diff --exit-code - - name: fantomless - run: | - dotnet new tool-manifest - dotnet tool install fantomless-tool --version 4.7.997-prerelease - dotnet fantomless --recurse . - git diff --exit-code From 5c936a591530c8a58ae6c20e7f61024102e5e3ee Mon Sep 17 00:00:00 2001 From: realmarv Date: Thu, 13 Apr 2023 13:33:06 +0330 Subject: [PATCH 4/5] wip3 --- scripts/detectNotUsingGitPush1by1.fsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/detectNotUsingGitPush1by1.fsx b/scripts/detectNotUsingGitPush1by1.fsx index afedd697e..d49d43856 100644 --- a/scripts/detectNotUsingGitPush1by1.fsx +++ b/scripts/detectNotUsingGitPush1by1.fsx @@ -830,6 +830,8 @@ let prCommits = let parsedPrCommitsJsonObj = PRCommitsType.Parse prCommitsJsonString parsedPrCommitsJsonObj |> Seq.map(fun commit -> commit.Sha) +printfn "prCommits:%A" prCommits + let notUsingGitPush1by1 = prCommits |> Seq.map(fun commit -> From 7c8b4425fc1ed80007da3aee0a7e07e039f6b1ab Mon Sep 17 00:00:00 2001 From: realmarv Date: Thu, 13 Apr 2023 13:41:41 +0330 Subject: [PATCH 5/5] wip4 --- .github/workflows/CI.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 6b85496b9..1db099954 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -8,8 +8,6 @@ jobs: runs-on: ubuntu-22.04 container: image: "ubuntu:22.04" - env: - ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} steps: - name: Install required dependencies run: |