|
| 1 | +const { isNil, uniqBy, flatten } = require("lodash"); |
| 2 | +const pFilter = require("p-filter"); |
| 3 | +const issueParser = require("issue-parser"); |
| 4 | + |
| 5 | +// this file will be copied two folders deeper |
| 6 | +const resolveConfig = require("./resolve-config"); |
| 7 | +const getClient = require("./resolve-config"); |
| 8 | +const parseGithubUrl = require("./parse-github-url"); |
| 9 | +const getSearchQueries = require("./get-search-queries"); |
| 10 | +const success = require("./new-success"); |
| 11 | + |
| 12 | +module.exports = async (pluginConfig, context) => { |
| 13 | + const { |
| 14 | + options: { repositoryUrl }, |
| 15 | + commits, |
| 16 | + logger, |
| 17 | + } = context; |
| 18 | + const { |
| 19 | + githubToken, |
| 20 | + githubUrl, |
| 21 | + githubApiPathPrefix, |
| 22 | + proxy, |
| 23 | + successComment, |
| 24 | + } = resolveConfig(pluginConfig, context); |
| 25 | + const github = getClient({ |
| 26 | + githubToken, |
| 27 | + githubUrl, |
| 28 | + githubApiPathPrefix, |
| 29 | + proxy, |
| 30 | + }); |
| 31 | + const [owner, repo] = ( |
| 32 | + await github.repos.get(parseGithubUrl(repositoryUrl)) |
| 33 | + ).data.full_name.split("/"); |
| 34 | + |
| 35 | + if (successComment === false) { |
| 36 | + logger.log("Skipping old comment deletion."); |
| 37 | + } else { |
| 38 | + const parser = issueParser( |
| 39 | + "github", |
| 40 | + githubUrl ? { hosts: [githubUrl] } : {} |
| 41 | + ); |
| 42 | + const shas = commits.map(({ hash }) => hash); |
| 43 | + |
| 44 | + const searchQueries = getSearchQueries( |
| 45 | + `repo:${owner}/${repo}+type:pr+is:merged`, |
| 46 | + shas |
| 47 | + ).map( |
| 48 | + async (q) => (await github.search.issuesAndPullRequests({ q })).data.items |
| 49 | + ); |
| 50 | + |
| 51 | + const prs = await pFilter( |
| 52 | + uniqBy(flatten(await Promise.all(searchQueries)), "number"), |
| 53 | + async ({ number }) => |
| 54 | + ( |
| 55 | + await github.pulls.listCommits({ owner, repo, pull_number: number }) |
| 56 | + ).data.find(({ sha }) => shas.includes(sha)) || |
| 57 | + shas.includes( |
| 58 | + (await github.pulls.get({ owner, repo, pull_number: number })).data |
| 59 | + .merge_commit_sha |
| 60 | + ) |
| 61 | + ); |
| 62 | + |
| 63 | + // Parse the release commits message and PRs body to find resolved issues/PRs via comment keyworkds |
| 64 | + const issues = [ |
| 65 | + ...prs.map((pr) => pr.body), |
| 66 | + ...commits.map((commit) => commit.message), |
| 67 | + ].reduce((issues, message) => { |
| 68 | + return message |
| 69 | + ? issues.concat( |
| 70 | + parser(message) |
| 71 | + .actions.close.filter( |
| 72 | + (action) => |
| 73 | + isNil(action.slug) || action.slug === `${owner}/${repo}` |
| 74 | + ) |
| 75 | + .map((action) => ({ number: Number.parseInt(action.issue, 10) })) |
| 76 | + ) |
| 77 | + : issues; |
| 78 | + }, []); |
| 79 | + |
| 80 | + await Promise.all( |
| 81 | + uniqBy([...prs, ...issues], "number").map(async (issue) => { |
| 82 | + try { |
| 83 | + const comments = { owner, repo, issue_number: issue.number }; |
| 84 | + const foundComments = ( |
| 85 | + await github.issues.listComments(comments) |
| 86 | + ).filter((comment) => comment.user.login === "im-pipeline-bot"); |
| 87 | + |
| 88 | + for (let c in foundComments) { |
| 89 | + await github.issues.deleteComment({ |
| 90 | + owner, |
| 91 | + repo, |
| 92 | + comment_id: c.id, |
| 93 | + }); |
| 94 | + logger.log("Removed comment %c from issue #%d", c.id, issue.number); |
| 95 | + } |
| 96 | + } catch (error) { |
| 97 | + if (error.status === 403) { |
| 98 | + logger.error( |
| 99 | + "Not allowed to delete a comment to the issue #%d.", |
| 100 | + issue.number |
| 101 | + ); |
| 102 | + } else if (error.status === 404) { |
| 103 | + logger.error( |
| 104 | + "Failed to delete a comment to the issue #%d as it doesn't exist.", |
| 105 | + issue.number |
| 106 | + ); |
| 107 | + } else { |
| 108 | + logger.error( |
| 109 | + "Failed to delete a comment to the issue #%d.", |
| 110 | + issue.number |
| 111 | + ); |
| 112 | + } |
| 113 | + } |
| 114 | + }) |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + await success(pluginConfig, context); |
| 119 | +}; |
0 commit comments