-
Notifications
You must be signed in to change notification settings - Fork 83
Previews in PR Conversation #224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
IstoraMandiri
wants to merge
14
commits into
twitter-together:main
Choose a base branch
from
IstoraMandiri:fix-previews
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0a34264
fix: PR previews for new frontmatter types
IstoraMandiri ab9c94a
Add required `name` to workflow docs
IstoraMandiri 217f4cb
fix: validate preview tweet reference #221
IstoraMandiri 41f43f7
Linting
IstoraMandiri f09f4e8
Merge fix previews, resolve conflicts
IstoraMandiri 1b4b829
Render image in media previews
IstoraMandiri eefe1b1
Fix nested media previews
IstoraMandiri 4216a99
Updating comment in PR thread, better previews
IstoraMandiri 4cc21bb
Skip redudant check run if using comments
IstoraMandiri d148b5d
Refactor, update comment footer
IstoraMandiri 80d559d
Update docs for PR comments
IstoraMandiri 76d3af7
Typo fix
IstoraMandiri ac64942
Do not report "null" for media without alt text
IstoraMandiri 8ba8ea1
Prettier formatting
IstoraMandiri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| module.exports = parseTweetId; | ||
|
|
||
| const TWEET_REGEX = /^https:\/\/twitter\.com\/[^/]+\/status\/(\d+)$/; | ||
|
|
||
| // TODO allow differently formatted URLs and tweet ids ? | ||
| // https://github.com/twitter-together/action/issues/221 | ||
|
|
||
| // TODO: Should we check if the referenced tweet actually exists? | ||
|
|
||
| function parseTweetId(tweetRef) { | ||
| const match = tweetRef.match(TWEET_REGEX); | ||
| if (!match) { | ||
| throw new Error(`Invalid tweet reference: ${tweetRef}`); | ||
| } | ||
| return match[1]; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,77 +1,25 @@ | ||
| module.exports = createCheckRun; | ||
|
|
||
| const { autoLink } = require("twitter-text"); | ||
|
|
||
| const parseTweetFileContent = require("../common/parse-tweet-file-content"); | ||
|
|
||
| async function createCheckRun( | ||
| { octokit, payload, startedAt, toolkit, dir }, | ||
| newTweets | ||
| { payload, startedAt, octokit, toolkit }, | ||
| summary | ||
| ) { | ||
| const parsedTweets = newTweets.map((rawTweet) => { | ||
| try { | ||
| return parseTweetFileContent(rawTweet, dir); | ||
| } catch (error) { | ||
| return { | ||
| error: error.message, | ||
| valid: false, | ||
| text: rawTweet, | ||
| }; | ||
| } | ||
| }); | ||
|
|
||
| const allTweetsValid = parsedTweets.every((tweet) => tweet.valid); | ||
|
|
||
| // Check runs cannot be created if the pull request was created by a fork, | ||
| // so we just log out the result. | ||
| // https://help.github.com/en/actions/automating-your-workflow-with-github-actions/authenticating-with-the-github_token#permissions-for-the-github_token | ||
| if (payload.pull_request.head.repo.fork) { | ||
| for (const tweet of parsedTweets) { | ||
| if (tweet.valid) { | ||
| toolkit.info(`### ✅ Valid\n\n${tweet.text}`); | ||
| } else { | ||
| toolkit.info( | ||
| `### ❌ Invalid\n\n${tweet.text}\n\n${tweet.error || "Unknown error"}` | ||
| ); | ||
| } | ||
| } | ||
| process.exit(allTweetsValid ? 0 : 1); | ||
| } | ||
|
|
||
| const response = await octokit.request( | ||
| "POST /repos/:owner/:repo/check-runs", | ||
| { | ||
| headers: { | ||
| accept: "application/vnd.github.antiope-preview+json", | ||
| }, | ||
| owner: payload.repository.owner.login, | ||
| repo: payload.repository.name, | ||
| name: "preview", | ||
| head_sha: payload.pull_request.head.sha, | ||
| started_at: startedAt, | ||
| completed_at: new Date().toISOString(), | ||
| status: "completed", | ||
| conclusion: allTweetsValid ? "success" : "failure", | ||
| conclusion: summary.valid ? "success" : "failure", | ||
| output: { | ||
| title: `${parsedTweets.length} tweet(s)`, | ||
| summary: parsedTweets.map(tweetToCheckRunSummary).join("\n\n---\n\n"), | ||
| title: `${summary.count} tweet(s)`, | ||
| summary: summary.body, | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| toolkit.info(`check run created: ${response.data.html_url}`); | ||
| } | ||
|
|
||
| function tweetToCheckRunSummary(tweet) { | ||
| let text = autoLink(tweet.text) | ||
| .replace(/(^|\n)/g, "$1> ") | ||
| .replace(/(^|\n)> (\n|$)/g, "$1>$2"); | ||
|
|
||
| if (!tweet.valid) | ||
| return `### ❌ Invalid\n\n${text}\n\n${tweet.error || "Unknown error"}`; | ||
|
|
||
| if (tweet.poll) | ||
| text += | ||
| "\n\nThe tweet includes a poll:\n\n> 🔘 " + tweet.poll.join("\n> 🔘 "); | ||
| return `### ✅ Valid\n\n${text}`; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| module.exports = createComment; | ||
|
|
||
| const BOT_LOGIN = "github-actions[bot]"; | ||
|
|
||
| const DIVIDER = "\n\n---\n\n*"; | ||
| const PREVIEW = `${DIVIDER}Preview using `; | ||
| const UPDATED = `${DIVIDER}**Updated** preview using `; | ||
| const SIGNATURE = | ||
| " generated by [Twitter, together!](https://github.com/twitter-together/action)*"; | ||
|
|
||
| async function createComment({ octokit, payload }, summary) { | ||
| const comment = `${summary.title}${summary.body}`; | ||
| // check for existing comments. | ||
| const comments = await octokit.request( | ||
| "GET /repos/{owner}/{repo}/issues/{issue_number}/comments", | ||
| { | ||
| owner: payload.repository.owner.login, | ||
| repo: payload.repository.name, | ||
| issue_number: payload.pull_request.number, | ||
| } | ||
| ); | ||
|
|
||
| const match = comments.data.find( | ||
| ({ user, body }) => user.login === BOT_LOGIN && body.endsWith(SIGNATURE) | ||
| ); | ||
|
|
||
| if (match) { | ||
| // update the existing comment | ||
| await octokit.request( | ||
| "PATCH /repos/{owner}/{repo}/issues/comments/{comment_id}", | ||
| { | ||
| owner: payload.repository.owner.login, | ||
| repo: payload.repository.name, | ||
| comment_id: match.id, | ||
| body: `${comment}${UPDATED}${payload.pull_request.head.sha}${SIGNATURE}`, | ||
| } | ||
| ); | ||
| } else { | ||
| // post a new comment | ||
| await octokit.request( | ||
| "POST /repos/{owner}/{repo}/issues/{issue_number}/comments", | ||
| { | ||
| owner: payload.repository.owner.login, | ||
| repo: payload.repository.name, | ||
| issue_number: payload.pull_request.number, | ||
| body: `${comment}${PREVIEW}${payload.pull_request.head.sha}${SIGNATURE}`, | ||
| } | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's a great idea, can you file a follow up issue?