Skip to content

Commit

Permalink
Merge pull request #10 from BoyWithSilverWings/feature/markdown-comment
Browse files Browse the repository at this point in the history
Add fn for create comment
  • Loading branch information
agneym authored Sep 21, 2019
2 parents bf46c0e + 57b4b6c commit 0152919
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 10 deletions.
3 changes: 1 addition & 2 deletions demo/test-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ title: Just hack'n
description: Nothing to see here
ogImage:
title: Generating *open graph* images with Github Actions
subtitle: Works with Markdown files
subtitle: Images for your blog
imageUrl: "🥳"
background: "linear-gradient(to right, #000428, #004e92)"
filename: this-file
---

Expand Down
Binary file modified demo/this-file.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions src/comment-markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { USER_REPO, GITHUB_HEAD_REF } from "./constants";

function commentMarkdown(path: string) {
const [owner, repo] = USER_REPO;

return `Your open graph image is ready:
![](https://github.com/${owner}/${repo}/raw/${GITHUB_HEAD_REF}/${path}.jpg)
`;
}

export default commentMarkdown;
17 changes: 17 additions & 0 deletions src/create-comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import getPrNumber from "./get-pr-number";
import { USER_REPO } from "./constants";
import octokit from "./github-api";

const createComment = async body => {
const [owner, repo] = USER_REPO;
const prNumber = getPrNumber();

return octokit.issues.createComment({
owner,
repo,
issue_number: prNumber,
body
});
};

export default createComment;
12 changes: 4 additions & 8 deletions src/find-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ import { readFileSync } from "fs";
import { kebabCase } from "lodash";
import { PullsListFilesResponseItem } from "@octokit/rest";

import {
GITHUB_CONTEXT,
USER_REPO,
FORMATS,
REPO_DIRECTORY
} from "./constants";
import { USER_REPO, FORMATS, REPO_DIRECTORY } from "./constants";
import octokit from "./github-api";
import { IFrontMatter, IFileProps } from "./types";
import getPrNumber from "./get-pr-number";

/**
* Get name of the file if provided by the user or title in kebab case
Expand Down Expand Up @@ -56,8 +52,8 @@ function getAttributes(files: PullsListFilesResponseItem[]): IFileProps[] {
*/
async function findFile() {
const [owner, repo] = USER_REPO;
const githubCtx: any = JSON.parse(GITHUB_CONTEXT as string);
const pullNumber = githubCtx.event.number;
const pullNumber = getPrNumber();

const { data: filesList } = await octokit.pulls.listFiles({
owner,
repo,
Expand Down
9 changes: 9 additions & 0 deletions src/get-pr-number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { GITHUB_CONTEXT } from "./constants";

function getPrNumber(): number {
const githubCtx: any = JSON.parse(GITHUB_CONTEXT as string);
const pullNumber = githubCtx.event.number;
return pullNumber;
}

export default getPrNumber;
7 changes: 7 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import commitFile from "./commit-file";
import generateHtml from "./generate-html";
import findFile from "./find-file";
import getRepoProps from "./repo-props";
import commentMarkdown from "./comment-markdown";
import createComment from "./create-comment";

if (!GITHUB_TOKEN) {
console.log("You must enable the GITHUB_TOKEN secret");
Expand Down Expand Up @@ -42,6 +44,11 @@ async function run() {
);

commitFile(image, repoProps, property.filename);

const markdown = commentMarkdown(
`${repoProps.assetPath}${property.filename}`
);
await createComment(markdown);
});
}

Expand Down

0 comments on commit 0152919

Please sign in to comment.