Skip to content

Commit

Permalink
refactor: use ref from metadata and repo/owner from context
Browse files Browse the repository at this point in the history
  • Loading branch information
jamacku committed May 20, 2024
1 parent 279319f commit 7f94193
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 179 deletions.
2 changes: 1 addition & 1 deletion dist/action.d.ts

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

140 changes: 48 additions & 92 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions dist/schema/input.d.ts

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

10 changes: 5 additions & 5 deletions dist/util.d.ts

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

47 changes: 10 additions & 37 deletions src/action.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { debug, error, getInput, notice } from '@actions/core';
import { context } from '@actions/github';
import { z } from 'zod';

import { Bugzilla } from './bugzilla';
import { Config } from './config';
import { Controller, IssueDetails } from './controller';
import { Jira } from './jira';
import { CustomOctokit } from './octokit';

import { PullRequestMetadata } from './schema/input';
import {
getFailedMessage,
Expand All @@ -18,8 +20,6 @@ import {

async function action(
octokit: CustomOctokit,
owner: string,
repo: string,
prMetadata: PullRequestMetadata
): Promise<string> {
const trackerType = getInput('tracker-type', { required: true });
Expand Down Expand Up @@ -49,9 +49,7 @@ async function action(
break;

default:
setLabels(octokit, owner, repo, prMetadata.number, [
config.labels['missing-tracker'],
]);
setLabels(octokit, prMetadata.number, [config.labels['missing-tracker']]);
raise(
`🔴 Missing tracker or Unknown tracker type; type: '${trackerType}'`
);
Expand All @@ -68,8 +66,7 @@ async function action(
await octokit.request(
'GET /repos/{owner}/{repo}/issues/{issue_number}/labels',
{
owner,
repo,
...context.repo,
issue_number: prMetadata.number,
}
)
Expand All @@ -84,18 +81,10 @@ async function action(
issueDetails = await trackerController.adapter.getIssueDetails(tracker);

if (labelsFromPR.includes(config.labels['missing-tracker'])) {
removeLabel(
octokit,
owner,
repo,
prMetadata.number,
config.labels['missing-tracker']
);
removeLabel(octokit, prMetadata.number, config.labels['missing-tracker']);
}
} catch (e) {
setLabels(octokit, owner, repo, prMetadata.number, [
config.labels['missing-tracker'],
]);
setLabels(octokit, prMetadata.number, [config.labels['missing-tracker']]);

error(`getIssueDetails(${tracker}): ${e}`);
raise(
Expand All @@ -105,8 +94,6 @@ async function action(

const titleResult = await setTitle(
octokit,
owner,
repo,
prMetadata.number,
tracker,
trackerType
Expand All @@ -125,13 +112,7 @@ async function action(
);
} else {
if (labelsFromPR.includes(config.labels['invalid-product'])) {
removeLabel(
octokit,
owner,
repo,
prMetadata.number,
config.labels['invalid-product']
);
removeLabel(octokit, prMetadata.number, config.labels['invalid-product']);
}

// Set base branch as label if it is not main or master (rhel-9.0.0, rhel-8.5.0, rhel-7.9, etc.)
Expand Down Expand Up @@ -165,8 +146,6 @@ async function action(
if (labelsFromPR.includes(config.labels['invalid-component'])) {
removeLabel(
octokit,
owner,
repo,
prMetadata.number,
config.labels['invalid-component']
);
Expand All @@ -183,13 +162,7 @@ async function action(
);
} else {
if (labelsFromPR.includes(config.labels.unapproved)) {
removeLabel(
octokit,
owner,
repo,
prMetadata.number,
config.labels.unapproved
);
removeLabel(octokit, prMetadata.number, config.labels.unapproved);
}
message.push(
`🟢 Tracker ${trackerController.adapter.getMarkdownUrl()} has been approved`
Expand All @@ -199,7 +172,7 @@ async function action(
if (isMatchingProduct && isMatchingComponent) {
const linkMessage = await trackerController.adapter.addLink(
'https://github.com/',
`${owner}/${repo}/pull/${prMetadata.number}`
`${context.repo.owner}/${context.repo.repo}/pull/${prMetadata.number}`
);
notice(`🔗 ${linkMessage}`);

Expand All @@ -209,7 +182,7 @@ async function action(

// TODO: Once validated update Tracker status and add/update comment in PR

setLabels(octokit, owner, repo, prMetadata.number, labels.add);
setLabels(octokit, prMetadata.number, labels.add);

if (err.length > 0) {
raise(getFailedMessage(err) + '\n\n' + getSuccessMessage(message));
Expand Down
22 changes: 4 additions & 18 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getBooleanInput, getInput, setFailed, setOutput } from '@actions/core';
import { context } from '@actions/github';
import { Endpoints } from '@octokit/types';
import { z } from 'zod';

import '@total-typescript/ts-reset';

Expand All @@ -12,21 +12,12 @@ import { ValidationError } from './error';

const octokit = getOctokit(getInput('token', { required: true }));

const owner = z
.string()
.min(1)
.parse(process.env.GITHUB_REPOSITORY?.split('/')[0]);
const repo = z
.string()
.min(1)
.parse(process.env.GITHUB_REPOSITORY?.split('/')[1]);

const prMetadataUnsafe = JSON.parse(
getInput('pr-metadata', { required: true })
);

const prMetadata = pullRequestMetadataSchema.parse(prMetadataUnsafe);
const commitSha = prMetadata.commits[prMetadata.commits.length - 1].sha;
const commitSha = prMetadata.ref;

const setStatus = getBooleanInput('set-status', { required: true });
let checkRunID:
Expand All @@ -36,8 +27,7 @@ let checkRunID:
if (setStatus) {
checkRunID = (
await octokit.request('POST /repos/{owner}/{repo}/check-runs', {
owner,
repo,
...context.repo,
name: 'Tracker Validator',
head_sha: commitSha,
status: 'in_progress',
Expand All @@ -53,14 +43,12 @@ if (setStatus) {
const statusTitle = getInput('status-title', { required: true });

try {
let message = await action(octokit, owner, repo, prMetadata);
let message = await action(octokit, prMetadata);

if (setStatus && checkRunID) {
await updateStatusCheck(
octokit,
checkRunID,
owner,
repo,
'completed' as unknown as undefined,
'success',
message
Expand All @@ -85,8 +73,6 @@ try {
await updateStatusCheck(
octokit,
checkRunID,
owner,
repo,
'completed' as unknown as undefined,
'failure',
message
Expand Down
1 change: 1 addition & 0 deletions src/schema/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type CommitMetadata = z.infer<typeof commitMetadataSchema>;
export const pullRequestMetadataSchema = z.object({
number: z.number(),
base: z.string(),
ref: z.string(),
commits: commitMetadataSchema,
});

Expand Down
34 changes: 9 additions & 25 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { debug } from '@actions/core';
import { context } from '@actions/github';
import { Octokit } from '@octokit/core';

import { ValidationError } from './error';
// import { Endpoints } from '@octokit/types';

Expand All @@ -11,8 +13,6 @@ import { ValidationError } from './error';
export async function updateStatusCheck(
octokit: Octokit,
checkID: number,
owner: string,
repo: string,
// https://github.com/octokit/types.ts/issues/283#issuecomment-1579239229
// Endpoints['POST /repos/{owner}/{repo}/check-runs']['parameters']['status']
status: undefined,
Expand All @@ -33,8 +33,7 @@ export async function updateStatusCheck(
await octokit.request(
'PATCH /repos/{owner}/{repo}/check-runs/{check_run_id}',
{
owner,
repo,
...context.repo,
check_run_id: checkID,
status,
completed_at: new Date().toISOString(),
Expand Down Expand Up @@ -65,8 +64,6 @@ export function getSuccessMessage(message: string[]): string {

export async function setLabels(
octokit: Octokit,
owner: string,
repo: string,
issueNumber: number,
labels: string[]
) {
Expand All @@ -78,8 +75,7 @@ export async function setLabels(
await octokit.request(
'POST /repos/{owner}/{repo}/issues/{issue_number}/labels',
{
owner,
repo,
...context.repo,
issue_number: issueNumber,
labels,
}
Expand All @@ -88,16 +84,13 @@ export async function setLabels(

export async function removeLabel(
octokit: Octokit,
owner: string,
repo: string,
issueNumber: number,
label: string
) {
await octokit.request(
'DELETE /repos/{owner}/{repo}/issues/{issue_number}/labels/{name}',
{
owner,
repo,
...context.repo,
issue_number: issueNumber,
name: label,
}
Expand All @@ -108,16 +101,10 @@ export function raise(error: string): never {
throw new ValidationError(error);
}

export async function getTitle(
octokit: Octokit,
owner: string,
repo: string,
issueNumber: number
) {
export async function getTitle(octokit: Octokit, issueNumber: number) {
return (
await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}', {
owner,
repo,
...context.repo,
pull_number: issueNumber,
})
).data.title;
Expand All @@ -138,13 +125,11 @@ export function getCurrentTitle(title: string): string {

export async function setTitle(
octokit: Octokit,
owner: string,
repo: string,
issueNumber: number,
tracker: string,
trackerType: 'bugzilla' | 'jira'
): Promise<string> {
const currentTitle = await getTitle(octokit, owner, repo, issueNumber);
const currentTitle = await getTitle(octokit, issueNumber);

const hash = trackerType === 'bugzilla' ? '#' : '';
if (isTrackerInTitle(currentTitle, `${hash}${tracker}`)) {
Expand All @@ -154,8 +139,7 @@ export async function setTitle(
const newTitle = `(${hash}${tracker}) ${getCurrentTitle(currentTitle)}`;

await octokit.request('PATCH /repos/{owner}/{repo}/issues/{issue_number}', {
owner,
repo,
...context.repo,
issue_number: issueNumber,
title: newTitle,
});
Expand Down

0 comments on commit 7f94193

Please sign in to comment.