Skip to content

Commit

Permalink
refactor: check update_pull_request_status in setStatus()
Browse files Browse the repository at this point in the history
  • Loading branch information
jamacku committed Feb 21, 2024
1 parent 3e64491 commit 422e1af
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
25 changes: 10 additions & 15 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,11 @@ async function action(pr: PullRequest): Promise<void> {
setOutput('request_url', `${tfInstance}/requests/${tfResponse.id}`);

// Create Pull Request status in state pending
const usePullRequestStatuses = getBooleanInput('update_pull_request_status');
if (usePullRequestStatuses) {
await pr.setStatus(
'pending',
'Build started',
`${tfArtifactUrl}/${tfResponse.id}`
);
}
await pr.setStatus(
'pending',
'Build started',
`${tfArtifactUrl}/${tfResponse.id}`
);

// Interval of 30 seconds in milliseconds
const interval = 30 * 1000;
Expand Down Expand Up @@ -215,13 +212,11 @@ async function action(pr: PullRequest): Promise<void> {
notice(`Infra state is: ${infraError ? 'Failed' : 'OK'}`);

// Switch Pull Request Status to final state
if (usePullRequestStatuses) {
await pr.setStatus(
finalState,
composeStatusDescription(infraError, getSummary(tfResult.result)),
`${tfArtifactUrl}/${tfResponse.id}`
);
}
await pr.setStatus(
finalState,
composeStatusDescription(infraError, getSummary(tfResult.result)),
`${tfArtifactUrl}/${tfResponse.id}`
);

// Add comment with Testing Farm request/result to Pull Request
if (getBooleanInput('create_issue_comment')) {
Expand Down
5 changes: 2 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getBooleanInput, getInput, setFailed } from '@actions/core';
import { getInput, getState, setFailed } from '@actions/core';
import { context } from '@actions/github';

import '@total-typescript/ts-reset';
Expand Down Expand Up @@ -28,8 +28,7 @@ try {
}

// Set the Pull Request status to error when error occurs
//? Note: getBooleanInput('update_pull_request_status') is used also in action(), there should be a better way to do this
if (pr && getBooleanInput('update_pull_request_status')) {
if (pr) {
const url = error instanceof TFError ? error.url : undefined;
await pr.setStatus('error', `${message}`, url);
}
Expand Down
12 changes: 11 additions & 1 deletion src/pull-request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { debug, getInput } from '@actions/core';
import { debug, getBooleanInput, getInput } from '@actions/core';
import { context } from '@actions/github';
import { Endpoints } from '@octokit/types';

Expand Down Expand Up @@ -31,6 +31,16 @@ export class PullRequest {
description: string,
url?: string
) {
const usePullRequestStatuses = getBooleanInput(
'update_pull_request_status'
);

// Don't set the statuses when they are disabled
if (!usePullRequestStatuses) {
debug('Skipping setting Pull Request Status');
return;
}

const { data } = await this.octokit.request(
'POST /repos/{owner}/{repo}/statuses/{sha}',
{
Expand Down

0 comments on commit 422e1af

Please sign in to comment.