Skip to content

Commit

Permalink
Create GitHub Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nvuillam committed Sep 11, 2023
1 parent 45907cf commit 1b31ed7
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 1 deletion.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"author": "NicolasVuillamy @nvuillam",
"bugs": "https://github.com/hardisgroupcom/sfdx-hardis/issues",
"dependencies": {
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1",
"@adobe/node-fetch-retry": "^1.1.2",
"@amplitude/node": "^1.3.2",
"@gitbeaker/node": "^35.8.0",
Expand Down
88 changes: 88 additions & 0 deletions src/common/gitProvider/github.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,99 @@
import * as core from '@actions/core';
import * as github from '@actions/github';
import * as c from 'chalk';
import { GitProviderRoot } from "./gitProviderRoot";
import { uxLog } from "../utils";
import { PullRequestMessageRequest, PullRequestMessageResult } from ".";
import { GitHub } from '@actions/github/lib/utils';

export class GithubProvider extends GitProviderRoot {
private octokit: InstanceType<typeof GitHub>;

constructor() {
super();
const tokenName =
process.env.CI_SFDX_HARDIS_GITHUB_TOKEN ? 'CI_SFDX_HARDIS_GITHUB_TOKEN'
: process.env.PAT ? 'PAT'
: 'GITHUB_TOKEN';
const token = core.getInput(tokenName);
this.octokit = github.getOctokit(token);
}

public getLabel(): string {
return "sfdx-hardis GitHub connector";
}

// Posts a note on the merge request
public async postPullRequestMessage(prMessage: PullRequestMessageRequest): Promise<PullRequestMessageResult> {
const { pull_request } = github.context.payload;

// Get CI variables
const repoOwner = github?.context?.repo?.owner || null;
const repoName = github?.context?.repo?.repo || null;
const pullRequestId = (pull_request?.number) || null;
if (repoName == null || pullRequestId == null) {
uxLog(this, c.grey("[GitHub integration] No project and merge request, so no note posted..."));
return { posted: false, providerResult: { info: "No related pull request" } };
}
const githubWorkflowName = github.context.workflow;
const githubJobUrl = `${github.context.serverUrl}/${github.context.repo.repo}/actions/runs/${github.context.runId}`
// Build note message
const messageKey = prMessage.messageKey + "-" + githubWorkflowName + "-" + pullRequestId;
let messageBody = `**${prMessage.title || ""}**
${prMessage.message}
_Provided by [sfdx-hardis](https://sfdx-hardis.cloudity.com) from job [${githubWorkflowName}](${githubJobUrl})_
<!-- sfdx-hardis message-key ${messageKey} -->
`;
// Add deployment id if present
if (globalThis.pullRequestDeploymentId) {
messageBody += `\n<!-- sfdx-hardis deployment-id ${globalThis.pullRequestDeploymentId} -->`;
}
// Check for existing note from a previous run
uxLog(this, c.grey("[Gitlab integration] Listing Notes of Merge Request..."));
const existingComments = await this.octokit.rest.issues.listComments({
owner: repoOwner,
repo: repoName,
issue_number: pullRequestId
});
let existingCommentId = null;
for (const existingComment of existingComments.data) {
if (existingComment?.body?.includes(`<!-- sfdx-hardis message-key ${messageKey} -->`)) {
existingCommentId = existingComment.id;
}
}

// Create or update MR note
if (existingCommentId) {
// Update existing note
uxLog(this, c.grey("[GitHub integration] Updating Merge Request Note on Gitlab..."));
const githubCommentEditResult = await this.octokit.rest.issues.updateComment({
owner: repoOwner,
repo: repoName,
issue_number: pullRequestId,
comment_id: existingCommentId,
body: messageBody
});
const prResult: PullRequestMessageResult = {
posted: githubCommentEditResult.data.id > 0,
providerResult: githubCommentEditResult,
};
return prResult;
} else {
// Create new note if no existing not was found
uxLog(this, c.grey("[GitHub integration] Adding Merge Request Note on Gitlab..."));
const githubCommentCreateResult = await this.octokit.rest.issues.createComment({
owner: repoOwner,
repo: repoName,
issue_number: pullRequestId,
body: messageBody
});
const prResult: PullRequestMessageResult = {
posted: githubCommentCreateResult.data.id > 0,
providerResult: githubCommentCreateResult,
};
return prResult;
}
}
}
145 changes: 144 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@
# yarn lockfile v1


"@actions/core@^1.10.0":
version "1.10.0"
resolved "https://registry.yarnpkg.com/@actions/core/-/core-1.10.0.tgz#44551c3c71163949a2f06e94d9ca2157a0cfac4f"
integrity sha512-2aZDDa3zrrZbP5ZYg159sNoLRb61nQ7awl5pSvIq5Qpj81vwDzdMRKzkWJGJuwVvWpvZKx7vspJALyvaaIQyug==
dependencies:
"@actions/http-client" "^2.0.1"
uuid "^8.3.2"

"@actions/github@^5.1.1":
version "5.1.1"
resolved "https://registry.yarnpkg.com/@actions/github/-/github-5.1.1.tgz#40b9b9e1323a5efcf4ff7dadd33d8ea51651bbcb"
integrity sha512-Nk59rMDoJaV+mHCOJPXuvB1zIbomlKS0dmSIqPGxd0enAXBnOfn4VWF+CGtRCwXZG9Epa54tZA7VIRlJDS8A6g==
dependencies:
"@actions/http-client" "^2.0.1"
"@octokit/core" "^3.6.0"
"@octokit/plugin-paginate-rest" "^2.17.0"
"@octokit/plugin-rest-endpoint-methods" "^5.13.0"

"@actions/http-client@^2.0.1":
version "2.1.1"
resolved "https://registry.yarnpkg.com/@actions/http-client/-/http-client-2.1.1.tgz#a8e97699c315bed0ecaeaaeb640948470d4586a0"
integrity sha512-qhrkRMB40bbbLo7gF+0vu+X+UawOvQQqNAA/5Unx774RS8poaOhThDOG6BGmxvAnxhQnDp2BG/ZUm65xZILTpw==
dependencies:
tunnel "^0.0.6"

"@adobe/node-fetch-retry@^1.1.2":
version "1.1.2"
resolved "https://registry.npmjs.org/@adobe/node-fetch-retry/-/node-fetch-retry-1.1.2.tgz"
Expand Down Expand Up @@ -588,6 +613,92 @@
dependencies:
fancy-test "^1.4.3"

"@octokit/auth-token@^2.4.4":
version "2.5.0"
resolved "https://registry.yarnpkg.com/@octokit/auth-token/-/auth-token-2.5.0.tgz#27c37ea26c205f28443402477ffd261311f21e36"
integrity sha512-r5FVUJCOLl19AxiuZD2VRZ/ORjp/4IN98Of6YJoJOkY75CIBuYfmiNHGrDwXr+aLGG55igl9QrxX3hbiXlLb+g==
dependencies:
"@octokit/types" "^6.0.3"

"@octokit/core@^3.6.0":
version "3.6.0"
resolved "https://registry.yarnpkg.com/@octokit/core/-/core-3.6.0.tgz#3376cb9f3008d9b3d110370d90e0a1fcd5fe6085"
integrity sha512-7RKRKuA4xTjMhY+eG3jthb3hlZCsOwg3rztWh75Xc+ShDWOfDDATWbeZpAHBNRpm4Tv9WgBMOy1zEJYXG6NJ7Q==
dependencies:
"@octokit/auth-token" "^2.4.4"
"@octokit/graphql" "^4.5.8"
"@octokit/request" "^5.6.3"
"@octokit/request-error" "^2.0.5"
"@octokit/types" "^6.0.3"
before-after-hook "^2.2.0"
universal-user-agent "^6.0.0"

"@octokit/endpoint@^6.0.1":
version "6.0.12"
resolved "https://registry.yarnpkg.com/@octokit/endpoint/-/endpoint-6.0.12.tgz#3b4d47a4b0e79b1027fb8d75d4221928b2d05658"
integrity sha512-lF3puPwkQWGfkMClXb4k/eUT/nZKQfxinRWJrdZaJO85Dqwo/G0yOC434Jr2ojwafWJMYqFGFa5ms4jJUgujdA==
dependencies:
"@octokit/types" "^6.0.3"
is-plain-object "^5.0.0"
universal-user-agent "^6.0.0"

"@octokit/graphql@^4.5.8":
version "4.8.0"
resolved "https://registry.yarnpkg.com/@octokit/graphql/-/graphql-4.8.0.tgz#664d9b11c0e12112cbf78e10f49a05959aa22cc3"
integrity sha512-0gv+qLSBLKF0z8TKaSKTsS39scVKF9dbMxJpj3U0vC7wjNWFuIpL/z76Qe2fiuCbDRcJSavkXsVtMS6/dtQQsg==
dependencies:
"@octokit/request" "^5.6.0"
"@octokit/types" "^6.0.3"
universal-user-agent "^6.0.0"

"@octokit/openapi-types@^12.11.0":
version "12.11.0"
resolved "https://registry.yarnpkg.com/@octokit/openapi-types/-/openapi-types-12.11.0.tgz#da5638d64f2b919bca89ce6602d059f1b52d3ef0"
integrity sha512-VsXyi8peyRq9PqIz/tpqiL2w3w80OgVMwBHltTml3LmVvXiphgeqmY9mvBw9Wu7e0QWk/fqD37ux8yP5uVekyQ==

"@octokit/plugin-paginate-rest@^2.17.0":
version "2.21.3"
resolved "https://registry.yarnpkg.com/@octokit/plugin-paginate-rest/-/plugin-paginate-rest-2.21.3.tgz#7f12532797775640dbb8224da577da7dc210c87e"
integrity sha512-aCZTEf0y2h3OLbrgKkrfFdjRL6eSOo8komneVQJnYecAxIej7Bafor2xhuDJOIFau4pk0i/P28/XgtbyPF0ZHw==
dependencies:
"@octokit/types" "^6.40.0"

"@octokit/plugin-rest-endpoint-methods@^5.13.0":
version "5.16.2"
resolved "https://registry.yarnpkg.com/@octokit/plugin-rest-endpoint-methods/-/plugin-rest-endpoint-methods-5.16.2.tgz#7ee8bf586df97dd6868cf68f641354e908c25342"
integrity sha512-8QFz29Fg5jDuTPXVtey05BLm7OB+M8fnvE64RNegzX7U+5NUXcOcnpTIK0YfSHBg8gYd0oxIq3IZTe9SfPZiRw==
dependencies:
"@octokit/types" "^6.39.0"
deprecation "^2.3.1"

"@octokit/request-error@^2.0.5", "@octokit/request-error@^2.1.0":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@octokit/request-error/-/request-error-2.1.0.tgz#9e150357831bfc788d13a4fd4b1913d60c74d677"
integrity sha512-1VIvgXxs9WHSjicsRwq8PlR2LR2x6DwsJAaFgzdi0JfJoGSO8mYI/cHJQ+9FbN21aa+DrgNLnwObmyeSC8Rmpg==
dependencies:
"@octokit/types" "^6.0.3"
deprecation "^2.0.0"
once "^1.4.0"

"@octokit/request@^5.6.0", "@octokit/request@^5.6.3":
version "5.6.3"
resolved "https://registry.yarnpkg.com/@octokit/request/-/request-5.6.3.tgz#19a022515a5bba965ac06c9d1334514eb50c48b0"
integrity sha512-bFJl0I1KVc9jYTe9tdGGpAMPy32dLBXXo1dS/YwSCTL/2nd9XeHsY616RE3HPXDVk+a+dBuzyz5YdlXwcDTr2A==
dependencies:
"@octokit/endpoint" "^6.0.1"
"@octokit/request-error" "^2.1.0"
"@octokit/types" "^6.16.1"
is-plain-object "^5.0.0"
node-fetch "^2.6.7"
universal-user-agent "^6.0.0"

"@octokit/types@^6.0.3", "@octokit/types@^6.16.1", "@octokit/types@^6.39.0", "@octokit/types@^6.40.0":
version "6.41.0"
resolved "https://registry.yarnpkg.com/@octokit/types/-/types-6.41.0.tgz#e58ef78d78596d2fb7df9c6259802464b5f84a04"
integrity sha512-eJ2jbzjdijiL3B4PrSQaSjuF2sPEQPVCPzBvTHJD9Nz+9dw2SGH4K4xeQJ77YfTq5bRQ+bD8wT11JbeDPmxmGg==
dependencies:
"@octokit/openapi-types" "^12.11.0"

"@salesforce/bunyan@^2.0.0":
version "2.0.0"
resolved "https://registry.npmjs.org/@salesforce/bunyan/-/bunyan-2.0.0.tgz"
Expand Down Expand Up @@ -1411,6 +1522,11 @@ bcrypt-pbkdf@^1.0.0:
dependencies:
tweetnacl "^0.14.3"

before-after-hook@^2.2.0:
version "2.2.3"
resolved "https://registry.yarnpkg.com/before-after-hook/-/before-after-hook-2.2.3.tgz#c51e809c81a4e354084422b9b26bad88249c517c"
integrity sha512-NzUnlZexiaH/46WDhANlyR2bXRopNg4F/zuSA3OpZnllCUgRaOF2znDioDWrmbNVsuZk6l9pMquQB38cfBZwkQ==

binary-extensions@^2.0.0:
version "2.2.0"
resolved "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz"
Expand Down Expand Up @@ -2335,6 +2451,11 @@ denque@^1.1.0:
resolved "https://registry.npmjs.org/denque/-/denque-1.5.1.tgz"
integrity sha512-XwE+iZ4D6ZUB7mfYRMb5wByE8L74HCn30FBN7sWnXksWc1LO1bPDl67pBR9o/kC4z/xSNAwkMYcGgqDV3BE3Hw==

deprecation@^2.0.0, deprecation@^2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/deprecation/-/deprecation-2.3.1.tgz#6368cbdb40abf3373b525ac87e4a260c3a700919"
integrity sha512-xmHIy4F3scKVwMsQ4WnVaS8bHOx0DmVwRywosKhaILI0ywMDWPtBSku2HNxRvF7jtwDRsoEwYQSfbxj8b7RlJQ==

detect-indent@^6.0.0:
version "6.0.0"
resolved "https://registry.npmjs.org/detect-indent/-/detect-indent-6.0.0.tgz"
Expand Down Expand Up @@ -4019,6 +4140,11 @@ is-plain-object@^2.0.3, is-plain-object@^2.0.4:
dependencies:
isobject "^3.0.1"

is-plain-object@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/is-plain-object/-/is-plain-object-5.0.0.tgz#4427f50ab3429e9025ea7d52e9043a9ef4159344"
integrity sha512-VRSzKkbMm5jMDoKLbltAkFQ5Qr7VDiTFGXxYFXXowVj387GeGNOCsOH6Msy00SGZ3Fp84b1Naa1psqgcCIEP5Q==

is-primitive@^3.0.1:
version "3.0.1"
resolved "https://registry.npmjs.org/is-primitive/-/is-primitive-3.0.1.tgz"
Expand Down Expand Up @@ -5079,6 +5205,13 @@ node-fetch@2.6.7, node-fetch@^2.6.0, node-fetch@^2.6.1:
dependencies:
whatwg-url "^5.0.0"

node-fetch@^2.6.7:
version "2.7.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==
dependencies:
whatwg-url "^5.0.0"

node-fetch@^3.2.10:
version "3.2.10"
resolved "https://registry.npmjs.org/node-fetch/-/node-fetch-3.2.10.tgz"
Expand Down Expand Up @@ -6950,7 +7083,7 @@ tunnel-agent@*, tunnel-agent@^0.6.0:
dependencies:
safe-buffer "^5.0.1"

tunnel@0.0.6:
tunnel@0.0.6, tunnel@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/tunnel/-/tunnel-0.0.6.tgz#72f1314b34a5b192db012324df2cc587ca47f92c"
integrity sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg==
Expand Down Expand Up @@ -7058,6 +7191,11 @@ unique-string@^2.0.0:
dependencies:
crypto-random-string "^2.0.0"

universal-user-agent@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/universal-user-agent/-/universal-user-agent-6.0.0.tgz#3381f8503b251c0d9cd21bc1de939ec9df5480ee"
integrity sha512-isyNax3wXoKaulPDZWHQqbmIx1k2tb9fb3GGDBRxCscfYV2Ch7WxPArBsFEG8s/safwXTT7H4QGhaIkTp9447w==

universalify@^0.1.0:
version "0.1.2"
resolved "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz"
Expand Down Expand Up @@ -7160,6 +7298,11 @@ uuid@^3.3.2, uuid@^3.3.3:
resolved "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz"
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==

uuid@^8.3.2:
version "8.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==

v8-compile-cache-lib@^3.0.0:
version "3.0.1"
resolved "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz"
Expand Down

0 comments on commit 1b31ed7

Please sign in to comment.