Skip to content

Commit

Permalink
Clear on change (#67)
Browse files Browse the repository at this point in the history
*Close voting when a PR gets a new commit pushed to it or when it's close and reopened.
*Updated the format of the voting comment to be more clear
*Update Docs
*Updated package.json
** author
** version
** other metadata
  • Loading branch information
myyk committed Oct 6, 2020
1 parent 9942a4f commit abd120b
Show file tree
Hide file tree
Showing 6 changed files with 368 additions and 117 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ posted by the action on the PR automatically as a place for :thumbsup:/
:thumbsdown: votes to be cast.

Only configured voters are counted. Votes are counted when the action is rerun
either by an update to the PR or manual rerunning it. Failed votes will list
reasons why the vote failed in the action's error messaging.
by manual rerunning it. Failed votes will list reasons why the vote failed in
the action's error messaging.

When a Pull Request is update, it will clear the voting and restart the vote
with the default settings.

## Usage

Expand All @@ -33,14 +36,15 @@ Create a new workflow in `.github/workflows/` as a new `.yaml` file.
name: 'Voting'
on:
pull_request_target:
types: [opened, synchronize, reopened, closed]
jobs:
democracy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Evaluate vote
uses: myyk/git-democracy@main
uses: myyk/git-democracy@v1
```

The name of the workflow must be `Voting` to match the badge that will be
Expand Down
123 changes: 91 additions & 32 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "typescript-action",
"version": "0.0.0",
"name": "git-democracy",
"version": "1.1.0",
"private": true,
"description": "TypeScript template action",
"description": "Adds voting to github projects.",
"main": "lib/main.js",
"scripts": {
"build": "tsc",
Expand All @@ -15,14 +15,14 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/actions/typescript-action.git"
"url": "git+https://github.com/myyk/git-democracy.git"
},
"keywords": [
"actions",
"node",
"setup"
],
"author": "",
"author": "Myyk Seok",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.2.6",
Expand Down
48 changes: 40 additions & 8 deletions src/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ type Octokit = InstanceType<typeof GitHub>
export class Comment {
id: number
createdAt: Date
body: string

constructor(commentResponse: CommentResponse) {
this.id = commentResponse.id
this.createdAt = new Date(commentResponse.created_at)
this.body = commentResponse.body
}
}

interface CommentResponse {
id: number
created_at: string
body: string
}

export async function findVotingCommentId(
export async function findVotingComment(
octokit: Octokit,
owner: string,
repo: string,
Expand All @@ -47,27 +50,27 @@ export async function findVotingCommentId(
return Promise.reject(Error('commentId not a number'))
}

core.info(`reactions: ${inspect(comment)}`)
core.info(`comment: ${inspect(comment)}`)
return new Comment(comment)
}

export async function findOrCreateVotingCommentId(
export async function findOrCreateVotingComment(
octokit: Octokit,
owner: string,
repo: string,
issueNumber: number,
bodyIncludes: string,
createCommentBody: Promise<string>
): Promise<Comment> {
const comment = await findVotingCommentId(
const comment = await findVotingComment(
octokit,
owner,
repo,
issueNumber,
bodyIncludes
)
if (!comment) {
return createVotingCommentId(
return createVotingComment(
octokit,
owner,
repo,
Expand All @@ -79,7 +82,7 @@ export async function findOrCreateVotingCommentId(
return comment
}

export async function createVotingCommentId(
export async function createVotingComment(
octokit: Octokit,
owner: string,
repo: string,
Expand All @@ -99,7 +102,7 @@ export async function createVotingCommentId(
return new Comment(comment)
}

export async function updateVotingCommentId(
export async function updateVotingComment(
octokit: Octokit,
owner: string,
repo: string,
Expand All @@ -126,7 +129,7 @@ export async function createVotingCommentBody(
const votes = await votesPromise
const acceptanceCriteria = await acceptanceCriteriaPromise
let commentBody = `
![${bodyIncludes}](${serverURL}/${owner}/${repo}/workflows/Voting/badge.svg?branch=${ref})
**${bodyIncludes}** ![Voting](${serverURL}/${owner}/${repo}/workflows/Voting/badge.svg?branch=${ref})
Vote on this comment with 👍 or 👎.
Vote Summary:
Expand All @@ -143,6 +146,35 @@ Acceptance Criteria:
return commentBody
}

export async function closeVotingComment(
octokit: Octokit,
owner: string,
repo: string,
comment: Comment,
bodyIncludes: string,
closedVotingBodyTag: string
): Promise<void> {
if (bodyIncludes === closedVotingBodyTag) {
return Promise.reject(
Error(
'voting comment identifier and closed comment identifier cannot be equal'
)
)
}

const closedBody = comment.body.replace(bodyIncludes, closedVotingBodyTag)

await updateVotingComment(
octokit,
owner,
repo,
Promise.resolve(comment.id),
Promise.resolve(closedBody)
)

return
}

export async function commentToId(commit: Promise<Comment>): Promise<number> {
return (await commit).id
}
Expand Down
Loading

0 comments on commit abd120b

Please sign in to comment.