Skip to content

Commit

Permalink
issue action (#694)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustav-Eikaas committed Nov 17, 2023
1 parent 77a8554 commit 9c4bf76
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 1 deletion.
50 changes: 50 additions & 0 deletions .github/workflows/issue.yml
Original file line number Diff line number Diff line change
@@ -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}}
3 changes: 2 additions & 1 deletion github-action/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
124 changes: 124 additions & 0 deletions github-action/src/issue.ts
Original file line number Diff line number Diff line change
@@ -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 <token>', 'change the working directory')
.option('-C --ci <ci>', 'Fusion CI token')
.option('-F --fprd <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<IssueFusionApp[]> {
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;
};
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9c4bf76

Please sign in to comment.