Skip to content

Commit

Permalink
add error protection and improve push logic
Browse files Browse the repository at this point in the history
  • Loading branch information
davecthomas committed Mar 10, 2024
1 parent 17df1d9 commit 81042e8
Showing 1 changed file with 25 additions and 31 deletions.
56 changes: 25 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,45 @@ import * as core from '@actions/core';
import * as github from '@actions/github';
import { context } from '@actions/github';

export default async function run(): Promise<void> {
async function run(): Promise<void> {
try {
// Explicitly type the 'token' variable
const token: string = core.getInput('GITHUB_TOKEN', { required: true });
const octokit = github.getOctokit(token);

// Correctly type the 'octokit' variable with the specific type for the authenticated Octokit instance
const octokit: ReturnType<typeof github.getOctokit> = github.getOctokit(token);
const eventName: string = context.eventName;
const repoOwner: string = context.repo.owner;
const repoName: string = context.repo.repo;

// Check if the event that triggered the action was a pull request
if (context.payload.pull_request) {
// Explicitly type 'prNumber' as 'number'
const prNumber: number = context.payload.pull_request.number;
const owner: string = context.repo.owner;
const repo: string = context.repo.repo;
if (eventName === 'pull_request') {
const prNumber = context.payload.pull_request!.number;

// Use the 'octokit' instance to call the GitHub REST API
const { data: pr } = await octokit.rest.pulls.get({
owner: owner,
repo: repo,
pull_number: prNumber,
});

// Log the PR number to the console
console.log(`Fetched PR: #${pr.number}`);

// Fetching details of files changed in the PR
const { data: files } = await octokit.rest.pulls.listFiles({
owner: owner,
repo: repo,
owner: repoOwner,
repo: repoName,
pull_number: prNumber,
});

console.log(`Files changed in PR #${prNumber}:`);
// Convert the list of files to a string
const fileList = files.map((file) => file.filename).join('\n');
const fileList: string = files.map(file => file.filename).join('\n');
console.log(`Files changed in PR #${prNumber}: ${fileList}`);

// Set the output for the workflow by assigning the list of files to the 'prFiles' output
core.setOutput('prFiles', fileList);
} else if (eventName === 'push') {
const ref: string = context.payload.after!;
const compare: string = context.payload.before!;

const { data: commitDiff } = await octokit.rest.repos.compareCommits({
owner: repoOwner,
repo: repoName,
base: compare,
head: ref,
});

const fileList: string = commitDiff.files ? commitDiff.files.map(file => file.filename).join('\n');
console.log(`Files changed in push: ${fileList}`);
core.setOutput('pushFiles', fileList);
} else {
// Use 'core.setFailed' to log an error if the action was not triggered by a pull request
core.setFailed('Action was not triggered by a pull request');
console.log('This action runs on pull_request and push events. Current event is neither.');
}
} catch (error) {
// Use 'core.setFailed' to log any caught errors
core.setFailed(`Action failed with error: ${error}`);
}
}
Expand Down

0 comments on commit 81042e8

Please sign in to comment.