From 9c4bf768ac85e10e0b0386538609036be6bffcd9 Mon Sep 17 00:00:00 2001 From: Gustav-Eikaas <89254170+Gustav-Eikaas@users.noreply.github.com> Date: Fri, 17 Nov 2023 10:58:05 +0100 Subject: [PATCH] issue action (#694) --- .github/workflows/issue.yml | 50 +++++++++++++++ github-action/package.json | 3 +- github-action/src/issue.ts | 124 ++++++++++++++++++++++++++++++++++++ pnpm-lock.yaml | 3 + 4 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/issue.yml create mode 100644 github-action/src/issue.ts diff --git a/.github/workflows/issue.yml b/.github/workflows/issue.yml new file mode 100644 index 000000000..a6b65a5dd --- /dev/null +++ b/.github/workflows/issue.yml @@ -0,0 +1,50 @@ +name: Update issue + +on: + workflow_dispatch: + schedule: + - cron: '* 7 * * *' +permissions: + issues: write + id-token: write +jobs: + update-issue: + name: Update issue + timeout-minutes: 15 + runs-on: ubuntu-latest + environment: fprd + + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 # Required to fetch range + + - name: Install Dependencies + run: npm i -g pnpm typescript && pnpm i + + - name: 'Login to Azure' + uses: azure/login@v1 + with: + client-id: ${{ secrets.AZURE_CLIENT_ID }} + tenant-id: ${{ secrets.AZURE_TENANT_ID }} + allow-no-subscriptions: true + + - name: 'Obtain fusion token' + shell: bash + run: echo "FUSION_TOKEN=$(az account get-access-token --resource '${{ secrets.AZURE_RESOURCE_ID }}' | jq '.accessToken')" >> $GITHUB_ENV + + - name: 'Login to Azure' + uses: azure/login@v1 + with: + client-id: ${{ secrets.AZURE_PROD_CLIENT_ID }} + tenant-id: ${{ secrets.AZURE_TENANT_ID }} + allow-no-subscriptions: true + + - name: 'Obtain fusion token' + shell: bash + run: echo "FUSION_PROD_TOKEN=$(az account get-access-token --resource '${{ secrets.AZURE_RESOURCE_PROD_ID }}' | jq '.accessToken')" >> $GITHUB_ENV + + - name: 'Update issue' + shell: bash + run: npx ts-node --esm ./github-action/src/issue.ts issue -T ${{ github.token }} -C ${{env.FUSION_TOKEN}} -F ${{env.FUSION_PROD_TOKEN}} diff --git a/github-action/package.json b/github-action/package.json index 7035f6801..3f53c3f4d 100644 --- a/github-action/package.json +++ b/github-action/package.json @@ -14,6 +14,7 @@ "@actions/core": "^1.10.0", "@actions/github": "^5.1.1", "adm-zip": "^0.5.10", - "commander": "11.0.0" + "commander": "11.0.0", + "markdown-table": "3.0.3" } } diff --git a/github-action/src/issue.ts b/github-action/src/issue.ts new file mode 100644 index 000000000..373b3949b --- /dev/null +++ b/github-action/src/issue.ts @@ -0,0 +1,124 @@ +#!/usr/bin/env node + +import { Command } from 'commander'; +import { setSecret } from '@actions/core'; +import { getOctokit, context } from '@actions/github'; +import { markdownTable } from 'markdown-table'; +import { logInfo } from './utils/logInfo.js'; +const prodUrl = 'https://fusion-s-portal-fprd.azurewebsites.net'; +const ciUrl = 'https://fusion-s-portal-ci.azurewebsites.net'; +const program = new Command(); + +program.name('Issue'); + +program + .command('issue') + .option('-T, --token ', 'change the working directory') + .option('-C --ci ', 'Fusion CI token') + .option('-F --fprd ', 'Fusion prod token') + .action(async (args) => { + if (!args.token) { + throw new Error('Missing github token'); + } + if (!args.ci) { + throw new Error('Missing ci token'); + } + if (!args.fprd) { + throw new Error('Missing fprd token'); + } + setSecret(args.token); + setSecret(args.ci); + setSecret(args.fprd); + release(args.token, args.ci, args.fprd); + }); + +await program.parseAsync(); + +type FusionAppStatus = { + key: string; + name: string; + publishedTest: string; + publishedProd: string; +}; + +export async function release(token: string, ciToken: string, fprdToken: string) { + const client = getOctokit(token); + + const ciApps = await getFusionApps(ciUrl, ciToken); + const fprdApps = await getFusionApps(prodUrl, fprdToken); + + const appStatus = ciApps + .map((s) => s.key) + .concat(fprdApps.map((s) => s.key)) + .filter((v, i, a) => a.indexOf(v) === i) + .map((x): FusionAppStatus => { + const maybeCiApp = ciApps.find((s) => s.key === x); + + const maybeProdApp = fprdApps.find((s) => s.key === x); + return { + key: x, + name: maybeCiApp?.name ?? maybeProdApp?.name ?? x, + publishedProd: !!maybeProdApp?.isPublished + ? `${new Date(maybeProdApp.publishedDate).toLocaleDateString('en-gb')} ✅` + : '❌', + publishedTest: !!maybeCiApp?.isPublished + ? `${new Date(maybeCiApp.publishedDate).toLocaleDateString('en-gb')} ✅` + : '❌', + }; + }); + + const table = markdownTable([ + ['Key', 'Name', 'Test', 'Prod'], + ...appStatus.map((s) => [s.key, s.name, s.publishedTest, s.publishedProd]), + ]); + + const res = await client.rest.issues.update({ + issue_number: 693, + owner: context.repo.owner, + repo: context.repo.repo, + title: 'Fusion app status', + body: table, + }); + + if (res.status !== 200) { + logInfo(res.status, 'Red'); + throw new Error('Failed to update issue'); + } +} + +export async function getFusionApps( + baseUrl: string, + token: string +): Promise { + const headers = { + ['content-type']: 'application/json', + ['Authorization']: `Bearer ${token}`, + }; + + const res = await fetch(`${baseUrl}/api/admin/apps`, { + headers: headers, + }); + + const data = (await res.json()).map( + (s: FusionApp): IssueFusionApp => ({ + name: s.name, + key: s.key, + isPublished: !!s.publishedDate, + publishedDate: s.publishedDate, + }) + ); + return data; +} + +type FusionApp = { + name: string; + key: string; + publishedDate: string | null; +}; + +type IssueFusionApp = { + name: string; + key: string; + isPublished: boolean; + publishedDate: string | null; +}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 161fd16d4..8ff4c857a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -281,6 +281,9 @@ importers: commander: specifier: 11.0.0 version: 11.0.0 + markdown-table: + specifier: 3.0.3 + version: 3.0.3 libs/electricalconsumersapp: dependencies: