From 2a202e9887c7a85d577e7a4bd81ef4af984ff9f8 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 10 Jan 2023 11:01:40 +0100 Subject: [PATCH] Fix indentation Signed-off-by: Johannes Schindelin --- .eslintrc.json | 3 + GitForWindowsHelper/azure-pipelines.js | 56 ++++++------ GitForWindowsHelper/check-runs.js | 90 +++++++++---------- .../get-collaborator-permissions.js | 32 +++---- .../github-api-request-as-app.js | 2 +- GitForWindowsHelper/https-request.js | 2 +- GitForWindowsHelper/slash-commands.js | 36 ++++---- .../trigger-workflow-dispatch.js | 68 +++++++------- 8 files changed, 146 insertions(+), 143 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 92c9741f..cc93bdb3 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -7,5 +7,8 @@ "parserOptions": { "ecmaVersion": 12, "sourceType": "module" + }, + "rules": { + "indent": ["error", 4] } } diff --git a/GitForWindowsHelper/azure-pipelines.js b/GitForWindowsHelper/azure-pipelines.js index 9a387a85..d9690b58 100644 --- a/GitForWindowsHelper/azure-pipelines.js +++ b/GitForWindowsHelper/azure-pipelines.js @@ -79,24 +79,24 @@ const getRelease = async (context, token, organization, project, releaseId) => { } const createRelease = async ( - context, - token, - organization, - project, - releaseDefinitionId, - artifactAlias, - artifactBuildRunId, - artifactBuildRunName, - artifactBuildDefinitionId, - artifactBuildDefinitionName, - sourceBranch, - sourceCommitId, - repo + context, + token, + organization, + project, + releaseDefinitionId, + artifactAlias, + artifactBuildRunId, + artifactBuildRunName, + artifactBuildDefinitionId, + artifactBuildDefinitionName, + sourceBranch, + sourceCommitId, + repo ) => { const auth = Buffer.from("PAT:" + token).toString("base64"); const headers = { - Accept: "application/json; api-version=7.0; excludeUrls=true", - Authorization: "Basic " + auth, + Accept: "application/json; api-version=7.0; excludeUrls=true", + Authorization: "Basic " + auth, }; const body = { definitionId: releaseDefinitionId, @@ -157,19 +157,19 @@ const releaseGitArtifacts = async (context, prNumber) => { const token = process.env['AZURE_PIPELINE_TRIGGER_TOKEN'] const answer2 = await createRelease( - context, - token, - 'git-for-windows', - 'git', - 1, - 'artifacts', - artifactBuildRunId, - artifactBuildRunName, - artifactBuildDefinitionId, - artifactBuildDefinitionName, - sourceBranch, - sourceCommitId, - 'git-for-windows/git' + context, + token, + 'git-for-windows', + 'git', + 1, + 'artifacts', + artifactBuildRunId, + artifactBuildRunName, + artifactBuildDefinitionId, + artifactBuildDefinitionName, + sourceBranch, + sourceCommitId, + 'git-for-windows/git' ) return { id: answer2.id, diff --git a/GitForWindowsHelper/check-runs.js b/GitForWindowsHelper/check-runs.js index b730b051..92314631 100644 --- a/GitForWindowsHelper/check-runs.js +++ b/GitForWindowsHelper/check-runs.js @@ -2,64 +2,64 @@ const queueCheckRun = async (context, token, owner, repo, ref, checkRunName, tit const githubApiRequest = require('./github-api-request') // is there an existing check-run we can re-use? const { check_runs } = await githubApiRequest( - context, - token, - 'GET', - `/repos/${owner}/${repo}/commits/${ref}/check-runs` + context, + token, + 'GET', + `/repos/${owner}/${repo}/commits/${ref}/check-runs` ) const filtered = check_runs - .filter(e => e.name === checkRunName && e.conclusion === null).map(e => { - return { - id: e.id, - status: e.status - } - }) + .filter(e => e.name === checkRunName && e.conclusion === null).map(e => { + return { + id: e.id, + status: e.status + } + }) if (filtered.length > 0) { - // ensure that the check_run is set to status "in progress" - if (filtered[0].status !== 'queued') { - console.log(await githubApiRequest( - context, - token, - 'PATCH', - `/repos/${owner}/${repo}/check-runs/${filtered[0].id}`, { - status: 'queued' - } - )) - } - process.stderr.write(`Returning existing ${filtered[0].id}`) - return filtered[0].id + // ensure that the check_run is set to status "in progress" + if (filtered[0].status !== 'queued') { + console.log(await githubApiRequest( + context, + token, + 'PATCH', + `/repos/${owner}/${repo}/check-runs/${filtered[0].id}`, { + status: 'queued' + } + )) + } + process.stderr.write(`Returning existing ${filtered[0].id}`) + return filtered[0].id } const { id } = await githubApiRequest( - context, - token, - 'POST', - `/repos/${owner}/${repo}/check-runs`, { - name: checkRunName, - head_sha: ref, - status: 'queued', - output: { - title, - summary + context, + token, + 'POST', + `/repos/${owner}/${repo}/check-runs`, { + name: checkRunName, + head_sha: ref, + status: 'queued', + output: { + title, + summary + } } - } ) return id - } +} - const updateCheckRun = async (context, token, owner, repo, checkRunId, parameters) => { +const updateCheckRun = async (context, token, owner, repo, checkRunId, parameters) => { const githubApiRequest = require('./github-api-request') - await githubApiRequest( - context, - token, - 'PATCH', - `/repos/${owner}/${repo}/check-runs/${checkRunId}`, - parameters + await githubApiRequest( + context, + token, + 'PATCH', + `/repos/${owner}/${repo}/check-runs/${checkRunId}`, + parameters ) - } +} - module.exports = { +module.exports = { queueCheckRun, updateCheckRun - } +} diff --git a/GitForWindowsHelper/get-collaborator-permissions.js b/GitForWindowsHelper/get-collaborator-permissions.js index 5b654b4d..cd9419f9 100644 --- a/GitForWindowsHelper/get-collaborator-permissions.js +++ b/GitForWindowsHelper/get-collaborator-permissions.js @@ -1,13 +1,13 @@ // Gets the permission level of a collaborator on the specified repository // Returns `ADMIN`, `MAINTAIN`, `READ`, `TRIAGE` or `WRITE`. module.exports = async (context, token, owner, repo, collaborator) => { - const gitHubAPIRequest = require('./github-api-request') - const answer = await gitHubAPIRequest( - context, - token, - 'POST', - '/graphql', { - query: `query CollaboratorPermission($owner: String!, $repo: String!, $collaborator: String) { + const gitHubAPIRequest = require('./github-api-request') + const answer = await gitHubAPIRequest( + context, + token, + 'POST', + '/graphql', { + query: `query CollaboratorPermission($owner: String!, $repo: String!, $collaborator: String) { repository(owner:$owner, name:$repo) { collaborators(query: $collaborator) { edges { @@ -16,13 +16,13 @@ module.exports = async (context, token, owner, repo, collaborator) => { } } }`, - variables: { - owner, - repo, - collaborator - } - } - ) - if (answer.error) throw answer.error - return answer.data.repository.collaborators.edges.map(e => e.permission.toString()) + variables: { + owner, + repo, + collaborator + } + } + ) + if (answer.error) throw answer.error + return answer.data.repository.collaborators.edges.map(e => e.permission.toString()) } \ No newline at end of file diff --git a/GitForWindowsHelper/github-api-request-as-app.js b/GitForWindowsHelper/github-api-request-as-app.js index dd55241b..53c78261 100644 --- a/GitForWindowsHelper/github-api-request-as-app.js +++ b/GitForWindowsHelper/github-api-request-as-app.js @@ -16,7 +16,7 @@ module.exports = async (context, requestMethod, requestPath, body) => { } const toBase64 = (obj) => Buffer.from(JSON.stringify(obj), "utf-8").toString("base64url") - const headerAndPayload = `${toBase64(header)}.${toBase64(payload)}` + const headerAndPayload = `${toBase64(header)}.${toBase64(payload)}` const privateKey = `-----BEGIN RSA PRIVATE KEY-----\n${process.env['GITHUB_APP_PRIVATE_KEY']}\n-----END RSA PRIVATE KEY-----\n` diff --git a/GitForWindowsHelper/https-request.js b/GitForWindowsHelper/https-request.js index 398cca19..78fef58e 100644 --- a/GitForWindowsHelper/https-request.js +++ b/GitForWindowsHelper/https-request.js @@ -28,7 +28,7 @@ module.exports = async (context, hostname, method, requestPath, body, headers) = statusCode: res.statusCode, statusMessage: res.statusMessage, headers: res.headers - }) + }) const chunks = [] res.on('data', data => chunks.push(data)) diff --git a/GitForWindowsHelper/slash-commands.js b/GitForWindowsHelper/slash-commands.js index f6eb5173..3fe12a4c 100644 --- a/GitForWindowsHelper/slash-commands.js +++ b/GitForWindowsHelper/slash-commands.js @@ -69,14 +69,14 @@ module.exports = async (context, req) => { if (alreadyOpenedPR.length > 0) { ({ html_url: commentURL, id: commentId } = await appendToIssueComment( - context, - await getToken(), - owner, - repo, - commentId, - `${ - packageType ? `${packageType} ` : "" - }PR [already exists](${alreadyOpenedPR[0].html_url})` + context, + await getToken(), + owner, + repo, + commentId, + `${ + packageType ? `${packageType} ` : "" + }PR [already exists](${alreadyOpenedPR[0].html_url})` )); return } @@ -111,14 +111,14 @@ module.exports = async (context, req) => { || !req.body.issue.pull_request || !['build-extra', 'MINGW-packages', 'MSYS2-packages'].includes(repo)) { return `Ignoring ${command} in unexpected repo: ${commentURL}` - } + } await checkPermissions() const { guessComponentUpdateDetails, isMSYSPackage } = require('./component-updates') const { package_name } = deployMatch[2] - ? { package_name: deployMatch[2] } - : guessComponentUpdateDetails(req.body.issue.title, req.body.issue.body) + ? { package_name: deployMatch[2] } + : guessComponentUpdateDetails(req.body.issue.title, req.body.issue.body) // The commit hash of the tip commit is sadly not part of the // "comment.created" webhook's payload. Therefore, we have to get it @@ -238,9 +238,9 @@ module.exports = async (context, req) => { if (owner !== 'git-for-windows' || repo !== 'git' || !req.body.issue.pull_request - ) { + ) { return `Ignoring ${command} in unexpected repo: ${commentURL}` - } + } await checkPermissions() await thumbsUp() @@ -255,11 +255,11 @@ module.exports = async (context, req) => { if (command == '/release') { if (owner !== 'git-for-windows' - || repo !== 'git' - || !req.body.issue.pull_request - ) { + || repo !== 'git' + || !req.body.issue.pull_request + ) { return `Ignoring ${command} in unexpected repo: ${commentURL}` - } + } await checkPermissions() await thumbsUp() @@ -277,7 +277,7 @@ module.exports = async (context, req) => { if (owner !== 'git-for-windows' || !['git', 'build-extra', 'MINGW-packages', 'MSYS2-packages'].includes(repo)) { return `Ignoring ${command} in unexpected repo: ${commentURL}` - } + } await checkPermissions() diff --git a/GitForWindowsHelper/trigger-workflow-dispatch.js b/GitForWindowsHelper/trigger-workflow-dispatch.js index 6026837f..eba07bb4 100644 --- a/GitForWindowsHelper/trigger-workflow-dispatch.js +++ b/GitForWindowsHelper/trigger-workflow-dispatch.js @@ -2,50 +2,50 @@ const githubApiRequest = require('./github-api-request') const githubApiRequestAsApp = require('./github-api-request-as-app') const sleep = async (milliseconds) => { - return new Promise((resolve) => { - setTimeout(resolve, milliseconds) - }) + return new Promise((resolve) => { + setTimeout(resolve, milliseconds) + }) } const getActorForToken = async (context, token) => { - try { - const { login } = await githubApiRequest(context, token, 'GET', '/user') - return login - } catch (e) { - if (e.statusCode !== 403 || e.json?.message !== 'Resource not accessible by integration') throw e - const answer = await githubApiRequestAsApp(context, 'GET', '/app') - return `${answer.slug}[bot]` - } + try { + const { login } = await githubApiRequest(context, token, 'GET', '/user') + return login + } catch (e) { + if (e.statusCode !== 403 || e.json?.message !== 'Resource not accessible by integration') throw e + const answer = await githubApiRequestAsApp(context, 'GET', '/app') + return `${answer.slug}[bot]` + } } const waitForWorkflowRun = async (context, owner, repo, workflow_id, after, token, actor) => { - if (!actor) actor = await getActorForToken(context, token) - let counter = 0 - for (;;) { - const res = await githubApiRequest( - context, - token, - 'GET', - `/repos/${owner}/${repo}/actions/runs?actor=${actor}&event=workflow_dispatch&created=>${after}` - ) - const filtered = res.workflow_runs.filter(e => e.path === `.github/workflows/${workflow_id}`) - if (filtered.length > 0) return filtered - if (counter++ > 30) throw new Error(`Times out waiting for workflow?`) - await sleep(1000) - } + if (!actor) actor = await getActorForToken(context, token) + let counter = 0 + for (;;) { + const res = await githubApiRequest( + context, + token, + 'GET', + `/repos/${owner}/${repo}/actions/runs?actor=${actor}&event=workflow_dispatch&created=>${after}` + ) + const filtered = res.workflow_runs.filter(e => e.path === `.github/workflows/${workflow_id}`) + if (filtered.length > 0) return filtered + if (counter++ > 30) throw new Error(`Times out waiting for workflow?`) + await sleep(1000) + } } const triggerWorkflowDispatch = async (context, token, owner, repo, workflow_id, ref, inputs) => { - const { headers: { date } } = await githubApiRequest( - context, - token, - 'POST', - `/repos/${owner}/${repo}/actions/workflows/${workflow_id}/dispatches`, - { ref, inputs } - ) + const { headers: { date } } = await githubApiRequest( + context, + token, + 'POST', + `/repos/${owner}/${repo}/actions/workflows/${workflow_id}/dispatches`, + { ref, inputs } + ) - const runs = await waitForWorkflowRun(context, owner, repo, workflow_id, new Date(date).toISOString(), token) - return runs[0] + const runs = await waitForWorkflowRun(context, owner, repo, workflow_id, new Date(date).toISOString(), token) + return runs[0] } module.exports = triggerWorkflowDispatch \ No newline at end of file