Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/github.d.mts.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/github.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import{t as e}from"./github-D91hw1gk.mjs";const t=`<!-- happy-commit -->`;function n(e){return`${t}\n${e}`}function r(e,t){return e?e.body===t?{type:`noop`}:{type:`update`,commentId:e.id,body:t}:{type:`create`,body:t}}function i(e,t){return t.lucky?r(e,n(t.body)):e?{type:`delete`,commentId:e.id}:{type:`noop`}}async function a(t,n,r){let i=e,a=await l(t,n,r);for(let e of a.slice(0,-1))await t.issues.deleteComment({...i.repo,comment_id:e.id});return a.length>0?a[a.length-1]:null}async function o(e,t,n,r){await s(e,t,n,i(await a(e,t,n),r))}async function s(t,n,r,i){let o=e;switch(i.type){case`update`:await t.issues.updateComment({...o.repo,comment_id:i.commentId,body:i.body});return;case`create`:await t.issues.createComment({...o.repo,issue_number:n,body:i.body}),await a(t,n,r);return;case`delete`:await t.issues.deleteComment({...o.repo,comment_id:i.commentId});return;case`noop`:return}}async function c(t,n){let r=e,i=[],a=1,o=!0;for(;o;){let{data:e}=await t.issues.listComments({...r.repo,issue_number:n,per_page:100,page:a});i.push(...e),o=e.length>=100,a++}return i}async function l(e,t,n){return(await c(e,t)).filter(e=>u(e,n)).map(e=>({id:e.id,body:e.body||e.body_text||``})).sort((e,t)=>e.id-t.id)}function u(e,n){return e.user?.login===n&&(e.body||e.body_text||``).includes(t)}async function d(t){let n=e;return(await t.paginate(t.pulls.listCommits,{...n.repo,pull_number:n.issue.number,per_page:100})).map(e=>e.sha)}async function f(t,n){let r=e,i=(await t.graphql(`
import{t as e}from"./github-D91hw1gk.mjs";const t=`<!-- happy-commit:v1 -->`,n=[t,...[`<!-- happy-commit -->`]];function r(e){return`${t}\n${e}`}function i(e){return n.some(t=>e.includes(t))}function a(e,t){return e?e.body===t?{type:`noop`}:{type:`update`,commentId:e.id,body:t}:{type:`create`,body:t}}function o(e,t){return t.lucky?a(e,r(t.body)):e?{type:`delete`,commentId:e.id}:{type:`noop`}}async function s(t,n,r){let i=e,a=await d(t,n,r);for(let e of a.slice(0,-1))await t.issues.deleteComment({...i.repo,comment_id:e.id});return a.length>0?a[a.length-1]:null}async function c(e,t,n,r){await l(e,t,n,o(await s(e,t,n),r))}async function l(t,n,r,i){let a=e;switch(i.type){case`update`:await t.issues.updateComment({...a.repo,comment_id:i.commentId,body:i.body});return;case`create`:await t.issues.createComment({...a.repo,issue_number:n,body:i.body}),await s(t,n,r);return;case`delete`:await t.issues.deleteComment({...a.repo,comment_id:i.commentId});return;case`noop`:return}}async function u(t,n){let r=e,i=[],a=1,o=!0;for(;o;){let{data:e}=await t.issues.listComments({...r.repo,issue_number:n,per_page:100,page:a});i.push(...e),o=e.length>=100,a++}return i}async function d(e,t,n){return(await u(e,t)).filter(e=>f(e,n)).map(e=>({id:e.id,body:e.body||e.body_text||``})).sort((e,t)=>e.id-t.id)}function f(e,t){return e.user?.login===t&&i(e.body||e.body_text||``)}async function p(t){let n=e;return(await t.paginate(t.pulls.listCommits,{...n.repo,pull_number:n.issue.number,per_page:100})).map(e=>e.sha)}async function m(t,n){let r=e,i=(await t.graphql(`
query ($owner: String!, $repo: String!, $expression: String!) {
repository(owner: $owner, name: $repo) {
object(expression: $expression) {
Expand All @@ -10,10 +10,10 @@ query ($owner: String!, $repo: String!, $expression: String!) {
}
}
}
`,{owner:r.repo.owner,repo:r.repo.repo,expression:`refs/heads/${n}`})).repository?.object?.history?.totalCount;if(typeof i!=`number`)throw Error(`Could not resolve commit count for ${n}`);return i}async function p(e){let t=(await e.graphql(`
`,{owner:r.repo.owner,repo:r.repo.repo,expression:`refs/heads/${n}`})).repository?.object?.history?.totalCount;if(typeof i!=`number`)throw Error(`Could not resolve commit count for ${n}`);return i}async function h(e){let t=(await e.graphql(`
query {
viewer {
login
}
}`)).viewer?.login;if(typeof t!=`string`||t.length===0)throw Error(`Could not resolve current user login`);return t}export{i as decideCommentAction,d as getCommitIds,f as getRepositoryCommitCount,p as getUserLogin,o as updateMessage};
}`)).viewer?.login;if(typeof t!=`string`||t.length===0)throw Error(`Could not resolve current user login`);return t}export{o as decideCommentAction,p as getCommitIds,m as getRepositoryCommitCount,h as getUserLogin,c as updateMessage};
//# sourceMappingURL=github.mjs.map
2 changes: 1 addition & 1 deletion dist/github.mjs.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"mustache": "^4.2.0"
},
"name": "happy-commit",
"version": "0.0.1",
"version": "0.9.0",
"main": "dist/index.js",
"repository": "git@github.com:kitsuyui/happy-commit.git",
"author": "kitsuyui <kitsuyui@kitsuyui.com>",
Expand Down
87 changes: 67 additions & 20 deletions src/github.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('decideCommentAction', () => {
})
).toEqual({
type: 'create',
body: '<!-- happy-commit -->\nhello',
body: '<!-- happy-commit:v1 -->\nhello',
})
})

Expand All @@ -68,7 +68,7 @@ describe('decideCommentAction', () => {
).toEqual({
type: 'update',
commentId: 1,
body: '<!-- happy-commit -->\nnew',
body: '<!-- happy-commit:v1 -->\nnew',
})
})

Expand All @@ -77,7 +77,7 @@ describe('decideCommentAction', () => {
decideCommentAction(
{
id: 1,
body: '<!-- happy-commit -->\nsame',
body: '<!-- happy-commit:v1 -->\nsame',
},
{
lucky: true,
Expand All @@ -89,6 +89,25 @@ describe('decideCommentAction', () => {
})
})

it('updates a legacy marker to the current marker', () => {
expect(
decideCommentAction(
{
id: 1,
body: '<!-- happy-commit -->\nsame',
},
{
lucky: true,
body: 'same',
}
)
).toEqual({
type: 'update',
commentId: 1,
body: '<!-- happy-commit:v1 -->\nsame',
})
})

it('deletes a past comment when message is no longer lucky', () => {
expect(
decideCommentAction(
Expand Down Expand Up @@ -144,7 +163,7 @@ describe('updateMessage', () => {
owner: 'kitsuyui',
repo: 'happy-commit',
issue_number: 123,
body: '<!-- happy-commit -->\nnew body',
body: '<!-- happy-commit:v1 -->\nnew body',
})
expect(octokit.issues.updateComment).not.toHaveBeenCalled()
expect(octokit.issues.deleteComment).not.toHaveBeenCalled()
Expand All @@ -157,7 +176,7 @@ describe('updateMessage', () => {
{
id: 55,
body_text: 'old body',
body: '<!-- happy-commit -->\nold body',
body: '<!-- happy-commit:v1 -->\nold body',
user: { login: 'github-actions[bot]' },
},
],
Expand All @@ -172,7 +191,7 @@ describe('updateMessage', () => {
owner: 'kitsuyui',
repo: 'happy-commit',
comment_id: 55,
body: '<!-- happy-commit -->\nnew body',
body: '<!-- happy-commit:v1 -->\nnew body',
})
expect(octokit.issues.createComment).not.toHaveBeenCalled()
expect(octokit.issues.deleteComment).not.toHaveBeenCalled()
Expand All @@ -185,7 +204,7 @@ describe('updateMessage', () => {
{
id: 55,
body_text: 'same body',
body: '<!-- happy-commit -->\nsame body',
body: '<!-- happy-commit:v1 -->\nsame body',
user: { login: 'github-actions[bot]' },
},
],
Expand All @@ -201,14 +220,42 @@ describe('updateMessage', () => {
expect(octokit.issues.deleteComment).not.toHaveBeenCalled()
})

it('updates a legacy managed comment to the current marker', async () => {
const octokit = createOctokitMock()
octokit.issues.listComments.mockResolvedValue({
data: [
{
id: 55,
body_text: '<!-- happy-commit -->\nsame body',
body: '<!-- happy-commit -->\nsame body',
user: { login: 'github-actions[bot]' },
},
],
})

await updateMessage(octokit as never, 123, 'github-actions[bot]', {
lucky: true,
body: 'same body',
})

expect(octokit.issues.updateComment).toHaveBeenCalledWith({
owner: 'kitsuyui',
repo: 'happy-commit',
comment_id: 55,
body: '<!-- happy-commit:v1 -->\nsame body',
})
expect(octokit.issues.createComment).not.toHaveBeenCalled()
expect(octokit.issues.deleteComment).not.toHaveBeenCalled()
})

it('deletes an existing comment when the message is no longer lucky', async () => {
const octokit = createOctokitMock()
octokit.issues.listComments.mockResolvedValue({
data: [
{
id: 55,
body_text: 'same body',
body: '<!-- happy-commit -->\nsame body',
body: '<!-- happy-commit:v1 -->\nsame body',
user: { login: 'github-actions[bot]' },
},
],
Expand Down Expand Up @@ -237,8 +284,8 @@ describe('updateMessage', () => {
},
{
id: 11,
body_text: '<!-- happy-commit -->\nstale body',
body: '<!-- happy-commit -->\nstale body',
body_text: '<!-- happy-commit:v1 -->\nstale body',
body: '<!-- happy-commit:v1 -->\nstale body',
user: { login: 'github-actions[bot]' },
},
],
Expand All @@ -253,7 +300,7 @@ describe('updateMessage', () => {
owner: 'kitsuyui',
repo: 'happy-commit',
issue_number: 123,
body: '<!-- happy-commit -->\nfresh body',
body: '<!-- happy-commit:v1 -->\nfresh body',
})
})

Expand All @@ -279,7 +326,7 @@ describe('updateMessage', () => {
owner: 'kitsuyui',
repo: 'happy-commit',
issue_number: 123,
body: '<!-- happy-commit -->\nfresh body',
body: '<!-- happy-commit:v1 -->\nfresh body',
})
expect(octokit.issues.updateComment).not.toHaveBeenCalled()
})
Expand All @@ -291,14 +338,14 @@ describe('updateMessage', () => {
data: [
{
id: 10,
body_text: '<!-- happy-commit -->\nstale body',
body: '<!-- happy-commit -->\nstale body',
body_text: '<!-- happy-commit:v1 -->\nstale body',
body: '<!-- happy-commit:v1 -->\nstale body',
user: { login: 'github-actions[bot]' },
},
{
id: 11,
body_text: '<!-- happy-commit -->\nstale body',
body: '<!-- happy-commit -->\nstale body',
body_text: '<!-- happy-commit:v1 -->\nstale body',
body: '<!-- happy-commit:v1 -->\nstale body',
user: { login: 'github-actions[bot]' },
},
],
Expand Down Expand Up @@ -331,14 +378,14 @@ describe('updateMessage', () => {
data: [
{
id: 20,
body_text: '<!-- happy-commit -->\nnew body',
body: '<!-- happy-commit -->\nnew body',
body_text: '<!-- happy-commit:v1 -->\nnew body',
body: '<!-- happy-commit:v1 -->\nnew body',
user: { login: 'github-actions[bot]' },
},
{
id: 21,
body_text: '<!-- happy-commit -->\nnew body',
body: '<!-- happy-commit -->\nnew body',
body_text: '<!-- happy-commit:v1 -->\nnew body',
body: '<!-- happy-commit:v1 -->\nnew body',
user: { login: 'github-actions[bot]' },
},
],
Expand Down
10 changes: 8 additions & 2 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import type { Octokit } from '@octokit/action'

import type { Comment, MessageContext } from './interfaces'

const COMMENT_MARKER = '<!-- happy-commit -->'
const COMMENT_MARKER = '<!-- happy-commit:v1 -->'
const LEGACY_COMMENT_MARKERS = ['<!-- happy-commit -->']
const COMMENT_MARKERS = [COMMENT_MARKER, ...LEGACY_COMMENT_MARKERS]

type CommentAction =
| { type: 'create'; body: string }
Expand All @@ -15,6 +17,10 @@ function buildManagedBody(body: string): string {
return `${COMMENT_MARKER}\n${body}`
}

function hasManagedCommentMarker(body: string): boolean {
return COMMENT_MARKERS.some((marker) => body.includes(marker))
}

function createLuckyCommentAction(
pastComment: Comment | null,
managedBody: string
Expand Down Expand Up @@ -192,7 +198,7 @@ function isManagedCommentByUser(
): boolean {
return (
comment.user?.login === userLogin &&
(comment.body || comment.body_text || '').includes(COMMENT_MARKER)
hasManagedCommentMarker(comment.body || comment.body_text || '')
)
}

Expand Down