Skip to content

Commit

Permalink
feat: update GitHub PR with Report info
Browse files Browse the repository at this point in the history
  • Loading branch information
cybersokari committed Dec 28, 2024
1 parent 3fe6518 commit 2ae2c68
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 28 deletions.
4 changes: 2 additions & 2 deletions apps/action/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ inputs:
description: 'The storage bucket path to back up Allure results and history files'
required: false
update_pr:
description: 'Update pull request with test report url and info'
default: 'false'
description: 'Add report info (url and status) as pull request comment or actions summary'
default: 'summary'

outputs:
report_url:
Expand Down
18 changes: 9 additions & 9 deletions apps/cli/coverage/coverage-final.json

Large diffs are not rendered by default.

15 changes: 12 additions & 3 deletions apps/cli/src/commands/deploy.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import fs from "fs/promises";
import path from "node:path";
import {KEY_BUCKET, KEY_PROJECT_ID, KEY_SLACK_CHANNEL, KEY_SLACK_TOKEN} from "../utilities/constants.js";
import chalk from "chalk";
import {ArgsInterface} from "../interfaces/args.interface.js";
import {ArgsInterface, GitHubPRUpdateType} from "../interfaces/args.interface.js";

const ERROR_MESSAGES = {
EMPTY_RESULTS: "Error: The specified results directory is empty.",
Expand Down Expand Up @@ -81,7 +81,16 @@ export function addDeployCommand(defaultProgram: Command, onCommand: (args: Args
.addOption(new Option("-sc, --slack-channel <channel>","Slack channel ID"))
.addOption(new Option("-st, --slack-token <token>","Slack token"))
.addOption(new Option("-p, --prefix <prefix>", "The storage bucket path to back up Allure results and history files"))
.addOption(new Option("--update-pr", "Update pull request with report url and info").hideHelp())
.addOption(new Option("--update-pr <type>", "Update pull request with report url and info")
.default(GitHubPRUpdateType.summary.toString(), 'summary/comment').hideHelp()
.argParser((value)=> {
if(Object.values(GitHubPRUpdateType).includes(value)){
return value
}
// Fallback to default if value is invalid
console.warn(`Invalid value "${value}" for --update-pr. Falling back to "summary".`);
return 'summary'
}))
.action(async (resultPath, reportName, options) => {
try {
if(!isJavaInstalled()){
Expand Down Expand Up @@ -116,7 +125,7 @@ export function addDeployCommand(defaultProgram: Command, onCommand: (args: Args
slack_channel: options.slackChannel || db.get(KEY_SLACK_CHANNEL, undefined),
slack_token: options.slackToken || db.get(KEY_SLACK_TOKEN, undefined),
buildUrl: getGitHubBuildUrl(),
updatePr: options.updatePr
updatePr: options.updatePr as GitHubPRUpdateType
};

await onCommand(cliArgs);
Expand Down
2 changes: 1 addition & 1 deletion apps/cli/src/commands/version.command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Command} from "commander";
// ./version-update.js updates this on prepublish
// The ./version-update.cjs file rewrites this file on npm prepublish
export const version = "1.0.0";
export function addVersionCommand(defaultProgram: Command) {
defaultProgram.version(version).description('Allure Deployer CLI');
Expand Down
15 changes: 9 additions & 6 deletions apps/cli/src/features/messaging/github-notifier.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Notifier} from "../../interfaces/notifier.interface.js";
import {NotificationData} from "../../models/notification.model.js";
import {ArgsInterface} from "../../interfaces/args.interface.js";
import {ArgsInterface, GitHubPRUpdateType} from "../../interfaces/args.interface.js";
import {GithubInterface} from "../../interfaces/github.interface.js";

export class GitHubNotifier implements Notifier {
Expand Down Expand Up @@ -29,15 +29,18 @@ export class GitHubNotifier implements Notifier {
|------------------------|------------------------|
| ${data.resultStatus.passed} | ${data.resultStatus.broken} |
`;

const promises: Promise<void>[] = [];
promises.push(this.client.updateSummary(markdown.trim()))
promises.push(this.client.updateOutput(`report_url=${data.reportUrl}`))

const githubToken = process.env.GITHUB_TOKEN;
if (githubToken) {
promises.push(this.client.updatePr({message: markdown, token: githubToken}))
if(this.args.updatePr === GitHubPRUpdateType.comment){
const githubToken = process.env.GITHUB_TOKEN;
if (githubToken) {
promises.push(this.client.updatePr({message: markdown, token: githubToken}))
}
} else {
promises.push(this.client.updateSummary(markdown.trim()))
}

await Promise.all(promises)
}
}
6 changes: 5 additions & 1 deletion apps/cli/src/interfaces/args.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ export interface ArgsInterface {
slack_channel?: string
slack_token?: string
buildUrl?: string;
updatePr?: boolean;
updatePr?: GitHubPRUpdateType;
}
export enum GitHubPRUpdateType {
comment,
summary
}
9 changes: 4 additions & 5 deletions apps/cli/src/services/github.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,21 @@ export class GitHubService implements GithubInterface {

async updatePr({message, token}: { message: string, token: string }): Promise<void> {
const {owner, repo} = github.context.repo
//TODO: remove this
console.log(`Repository Owner: ${owner}`);
console.log(`Repository Name: ${repo}`);
try {
const pr = github.context.payload.pull_request!
const octokit = github.getOctokit(token)
// Update the PR body
await octokit.rest.pulls.update({
await octokit.rest.issues.createComment({
owner,
repo,
pull_number: pr.number,
issue_number: pr.number,
body: message.trim(),
});
console.log(`Pull Request #${pr.number} updated successfully!`);
} catch (e) {
console.warn('Failed to update PR:', e);
console.log(`Repository Owner: ${owner}`);
console.log(`Repository Name: ${repo}`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion apps/cli/test/report-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as fs from "fs/promises";
import {fakeArgs} from "./mocks/fake-args.js";
import {jest} from "@jest/globals";
import {ExecutorInterface} from "../src/interfaces/executor.interface.js";
import {AllureService} from "../src/services/allure-service.js";
import {AllureService} from "../src/services/allure.service.js";
import {Allure} from "../src/features/allure.js";

const REPORTS_DIR = '/app/allure-reports';
Expand Down

0 comments on commit 2ae2c68

Please sign in to comment.