mirror of
https://github.com/marocchino/sticky-pull-request-comment.git
synced 2026-04-08 07:20:05 +00:00
feat: actual logic
This commit is contained in:
parent
29d48cbb29
commit
cb9470fc12
9 changed files with 108 additions and 147 deletions
49
src/main.ts
49
src/main.ts
|
|
@ -1,18 +1,43 @@
|
|||
import * as core from '@actions/core';
|
||||
import {wait} from './wait'
|
||||
import * as core from "@actions/core";
|
||||
import { context, GitHub } from "@actions/github";
|
||||
|
||||
async function run() {
|
||||
try {
|
||||
const ms = core.getInput('milliseconds');
|
||||
console.log(`Waiting ${ms} milliseconds ...`)
|
||||
|
||||
core.debug((new Date()).toTimeString())
|
||||
await wait(parseInt(ms, 10));
|
||||
core.debug((new Date()).toTimeString())
|
||||
|
||||
core.setOutput('time', new Date().toTimeString());
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
const repo = context.repo;
|
||||
const issue_number = context?.payload?.pull_request?.number;
|
||||
if (!issue_number) {
|
||||
core.setFailed("This action only works for pull_request");
|
||||
return;
|
||||
}
|
||||
const body = core.getInput("message");
|
||||
const githubToken = core.getInput("GITHUB_TOKEN");
|
||||
if (!body || !githubToken) {
|
||||
core.setFailed("invalid input: please check your workflow");
|
||||
return;
|
||||
}
|
||||
const octokit = new GitHub(githubToken);
|
||||
const { data: comments } = await octokit.issues.listComments({
|
||||
...repo,
|
||||
issue_number
|
||||
});
|
||||
const myComment = comments.find(
|
||||
comment => comment.user.login === "github-actions[bot]"
|
||||
);
|
||||
if (myComment) {
|
||||
await octokit.issues.updateComment({
|
||||
...repo,
|
||||
comment_id: myComment.id,
|
||||
body
|
||||
});
|
||||
} else {
|
||||
await octokit.issues.createComment({
|
||||
...repo,
|
||||
issue_number,
|
||||
body
|
||||
});
|
||||
}
|
||||
} catch ({ message }) {
|
||||
core.setFailed(message);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
10
src/wait.ts
10
src/wait.ts
|
|
@ -1,10 +0,0 @@
|
|||
export function wait(milliseconds: number) {
|
||||
return new Promise((resolve) => {
|
||||
if (isNaN(milliseconds)) {
|
||||
throw new Error('milleseconds not a number');
|
||||
}
|
||||
|
||||
setTimeout(() => resolve("done!"), milliseconds)
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue