Skip to content

Commit

Permalink
fix(readme): decode base64 content from octokit before handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vnphanquang committed Mar 15, 2022
1 parent db81739 commit 53f7a8a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ You can display this badge somewhere in your readme as below, which will render
![!monkeytype.badge]

<!-- For clickable image that links to monkeytype website -->
[![!monkeytype.badge]](https://monkeytype.com/)
[![monkeytype.badge]](https://monkeytype.com/)
```

#### 2. Monkeytype API Key
Expand Down
7 changes: 4 additions & 3 deletions src/readme/readme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ jest.mock('@actions/github', () => ({
repos: {
getReadme: jest.fn(async () => ({
data: {
content,
content: Buffer.from(content).toString('base64'),
encoding: 'base64',
path: 'README.md',
sha: '802992c4220de19a90767f3000a79a31b98d0df7',
}
Expand All @@ -40,14 +41,14 @@ jest.mock('@actions/github', () => ({
test('Default input should update', async () => {
const coreInfoSpy = jest.spyOn(core, 'info');
const newContent = await updateReadme(defaultInput, badgeUrl);
expect(coreInfoSpy).toHaveBeenCalledTimes(1);
expect(coreInfoSpy).toHaveBeenCalled();
expect(newContent).toBe(content.replace(REGEX, `[monkeytype.badge]: ${badgeUrl}`));
});

test('Same content should skip', async () => {
const coreInfoSpy = jest.spyOn(core, 'info');
const newContent = await updateReadme(defaultInput, exampleUrl);
expect(coreInfoSpy).toHaveBeenCalledTimes(1);
expect(coreInfoSpy).toHaveBeenCalled();
expect(newContent).toBe(content);
});

Expand Down
8 changes: 6 additions & 2 deletions src/readme/readme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import * as github from '@actions/github';
import { BadgeResourceDeclarationNotFoundError } from '../error';
import type { Input } from '../input';

export const REGEX = /\[monkeytype\.badge\].*$/m;
export const REGEX = /(?<!!)\[monkeytype\.badge\].*$/m;

export async function updateReadme(input: Input, badgeUrl: string) {
const octokit = github.getOctokit(input.github_token);
const { repo, owner } = github.context.repo;

core.info(`Getting README.md content from ${owner}/${repo}`);
const readme = await octokit.rest.repos.getReadme({ owner, repo });
let content = readme.data.content;
core.info(readme.data.content);
let content = Buffer.from(readme.data.content, readme.data.encoding as BufferEncoding).toString('utf-8');

const newBadgeResource = `[monkeytype.badge]: ${badgeUrl}`;
const badgeResource = content.match(REGEX)?.[0];
if (!badgeResource) {
Expand Down

0 comments on commit 53f7a8a

Please sign in to comment.