Skip to content

Commit

Permalink
ci: 📌
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustav-Eikaas committed Dec 18, 2023
1 parent 3d044cc commit 1b471e0
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/helpers/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const RELEASE_FILE_NAME = 'release.json';
16 changes: 4 additions & 12 deletions .github/helpers/src/pre-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,9 @@ const program = new Command();

program.name('PR');

program
.command('publish')
// .option('-T, --token <token>', 'NPM auth token')
.action(async (args) => {
// if (!args.token) {
// throw new Error('Missing npm token');
// }
// setSecret(args.token);
// execSync(`pnpm config set '//registry.npmjs.org/:_authToken' "${args.token}"`, { stdio: 'inherit' });
createFusionApp();
});
program.command('publish').action(async () => {
createFusionApp();
});

await program.parseAsync();

Expand All @@ -48,7 +40,7 @@ export async function createFusionApp() {
}

function publishPrPackage() {
const releaseMessage = execSync('pnpm publish --tag pr --json --no-git-checks');
const releaseMessage = execSync('pnpm publish --tag pr --json --dry-run --no-git-checks');

const npmRelease: NpmRelease = JSON.parse(releaseMessage.toString('utf-8'));
logInfo(npmRelease.id, 'Green');
Expand Down
72 changes: 72 additions & 0 deletions .github/helpers/src/publish-comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env node

import { Command } from 'commander';
import { getOctokit, context } from '@actions/github';
import { logInfo } from './utils/logInfo.js';
import { readFileSync } from 'fs';
import { RELEASE_FILE_NAME } from './constants.js';

type Octo = ReturnType<typeof getOctokit>;

async function getPullRequestFromBranch(client: Octo) {
const branch = context.ref;

const pullRequests = await client.rest.pulls.list({ owner: context.issue.owner, repo: context.issue.repo });
const pullRequest = pullRequests.data.find((s) => s.head.ref === branch);

if (!pullRequest) {
const msg = `${branch} does not have any associated pull request`;
logInfo(msg, 'Red');
throw new Error(msg);
}
return pullRequest;
}

async function commentPullRequest(client: Octo, issueNumber: number) {
const body = parseReleaseJson();
const comment = await client.rest.issues.createComment({
issue_number: issueNumber,
body: body,
owner: context.issue.owner,
repo: context.issue.repo,
});
if (comment.status === 201) {
logInfo('Comment created successfully', 'Green');
}
}

const program = new Command();

program.name('PR');

program.command('comment').action(async (args) => {
if (!args.token) {
throw new Error('Missing github token');
}
const client = getOctokit(args.token);
const pr = await getPullRequestFromBranch(args.token);
commentPullRequest(client, pr.number);
});

await program.parseAsync();

function parseReleaseJson() {
const packages: string[] = JSON.parse(readFileSync(`./${RELEASE_FILE_NAME}`).toString('utf-8')).packages;

const isWorkspaceRelease = packages.find((s) => s.includes('workspace-fusion'));

const workspaceWarning = isWorkspaceRelease
? ''
: '@equinor/workspace-fusion was not published😕❓. Did you forget to bump the package📦?';

const packageLines = packages.map((s) => s).join('\n');

const prBody = `Packages published🚀:
\`\`\`
${packageLines}
\`\`\`
${workspaceWarning}
`;
return prBody;
}
4 changes: 4 additions & 0 deletions .github/workflows/pre-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ permissions:
contents: read
deployments: write
id-token: write
issues: write
statuses: write
jobs:
publish:
Expand All @@ -29,3 +30,6 @@ jobs:

- name: Publish PR version
run: pnpm pr-shipit

- name: Comment PR
run: tsx .\.github\helpers\src\publish-comment.ts comment --token ${{github.token}}

0 comments on commit 1b471e0

Please sign in to comment.