Skip to content

Commit

Permalink
WIP singleton octokit instance
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed May 29, 2023
1 parent fd4a18e commit e7f4f8f
Show file tree
Hide file tree
Showing 12 changed files with 306 additions and 1,382 deletions.
31 changes: 21 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import addChannelGitHub from "./lib/add-channel.js";
import publishGitHub from "./lib/publish.js";
import successGitHub from "./lib/success.js";
import failGitHub from "./lib/fail.js";
import { SemanticReleaseOctokit } from "./lib/octokit.js";
import { SemanticReleaseOctokit, getOctokitInstance } from "./lib/octokit.js";

let verified;
let octokit;

export async function verifyConditions(
pluginConfig,
Expand Down Expand Up @@ -44,7 +45,9 @@ export async function verifyConditions(
);
}

await verifyGitHub(pluginConfig, context, { Octokit });
octokit = octokit || getOctokitInstance(Octokit, pluginConfig, context);

await verifyGitHub(pluginConfig, context, { octokit });
verified = true;
}

Expand All @@ -53,49 +56,57 @@ export async function publish(
context,
{ Octokit = SemanticReleaseOctokit } = {}
) {
octokit = octokit || getOctokitInstance(Octokit, pluginConfig, context);

if (!verified) {
await verifyGitHub(pluginConfig, context, { Octokit });
await verifyGitHub(pluginConfig, context, { octokit });
verified = true;
}

return publishGitHub(pluginConfig, context, { Octokit });
return publishGitHub(pluginConfig, context, { octokit });
}

export async function addChannel(
pluginConfig,
context,
{ Octokit = SemanticReleaseOctokit } = {}
) {
octokit = octokit || getOctokitInstance(Octokit, pluginConfig, context);

if (!verified) {
await verifyGitHub(pluginConfig, context, { Octokit });
await verifyGitHub(pluginConfig, context, { octokit });
verified = true;
}

return addChannelGitHub(pluginConfig, context, { Octokit });
return addChannelGitHub(pluginConfig, context, { octokit });
}

export async function success(
pluginConfig,
context,
{ Octokit = SemanticReleaseOctokit } = {}
) {
octokit = octokit || getOctokitInstance(Octokit, pluginConfig, context);

if (!verified) {
await verifyGitHub(pluginConfig, context, { Octokit });
await verifyGitHub(pluginConfig, context, { octokit });
verified = true;
}

await successGitHub(pluginConfig, context, { Octokit });
await successGitHub(pluginConfig, context, { octokit });
}

export async function fail(
pluginConfig,
context,
{ Octokit = SemanticReleaseOctokit } = {}
) {
octokit = octokit || getOctokitInstance(Octokit, pluginConfig, context);

if (!verified) {
await verifyGitHub(pluginConfig, context, { Octokit });
await verifyGitHub(pluginConfig, context, { octokit });
verified = true;
}

await failGitHub(pluginConfig, context, { Octokit });
await failGitHub(pluginConfig, context, { octokit });
}
16 changes: 1 addition & 15 deletions lib/add-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,18 @@ import debugFactory from "debug";

import { RELEASE_NAME } from "./definitions/constants.js";
import parseGithubUrl from "./parse-github-url.js";
import resolveConfig from "./resolve-config.js";
import isPrerelease from "./is-prerelease.js";
import { toOctokitOptions } from "./octokit.js";

const debug = debugFactory("semantic-release:github");

export default async function addChannel(pluginConfig, context, { Octokit }) {
export default async function addChannel(pluginConfig, context, { octokit }) {
const {
options: { repositoryUrl },
branch,
nextRelease: { name, gitTag, notes },
logger,
} = context;
const { githubToken, githubUrl, githubApiPathPrefix, proxy } = resolveConfig(
pluginConfig,
context
);
const { owner, repo } = parseGithubUrl(repositoryUrl);
const octokit = new Octokit(
toOctokitOptions({
githubToken,
githubUrl,
githubApiPathPrefix,
proxy,
})
);
let releaseId;

const release = {
Expand Down
5 changes: 1 addition & 4 deletions lib/fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import getFailComment from "./get-fail-comment.js";

const debug = debugFactory("semantic-release:github");

export default async function fail(pluginConfig, context, { Octokit }) {
export default async function fail(pluginConfig, context, { octokit }) {
const {
options: { repositoryUrl },
branch,
Expand All @@ -31,9 +31,6 @@ export default async function fail(pluginConfig, context, { Octokit }) {
if (failComment === false || failTitle === false) {
logger.log("Skip issue creation.");
} else {
const octokit = new Octokit(
toOctokitOptions({ githubToken, githubUrl, githubApiPathPrefix, proxy })
);
// In case the repo changed name, get the new `repo`/`owner` as the search API will not follow redirects
const { data: repoData } = await octokit.request(
"GET /repos/{owner}/{repo}",
Expand Down
12 changes: 12 additions & 0 deletions lib/octokit.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { HttpsProxyAgent } from "https-proxy-agent";

import { RETRY_CONF } from "./definitions/retry.js";
import { THROTTLE_CONF } from "./definitions/throttle.js";
import resolveConfig from "./resolve-config.js";

// NOTE: replace with import ... assert { type: 'json' } once supported
const require = createRequire(import.meta.url);
Expand Down Expand Up @@ -74,3 +75,14 @@ export function toOctokitOptions(options) {
},
};
}

export function getOctokitInstance(Octokit, pluginConfig, context) {
const { githubToken, githubUrl, githubApiPathPrefix, proxy } = resolveConfig(
pluginConfig,
context
);

return new Octokit(
toOctokitOptions({ githubToken, githubUrl, githubApiPathPrefix, proxy })
);
}
20 changes: 2 additions & 18 deletions lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,20 @@ import { RELEASE_NAME } from "./definitions/constants.js";
import parseGithubUrl from "./parse-github-url.js";
import globAssets from "./glob-assets.js";
import resolveConfig from "./resolve-config.js";
import { toOctokitOptions } from "./octokit.js";
import isPrerelease from "./is-prerelease.js";

const debug = debugFactory("semantic-release:github");

export default async function publish(pluginConfig, context, { Octokit }) {
export default async function publish(pluginConfig, context, { octokit }) {
const {
cwd,
options: { repositoryUrl },
branch,
nextRelease: { name, gitTag, notes },
logger,
} = context;
const {
githubToken,
githubUrl,
githubApiPathPrefix,
proxy,
assets,
draftRelease,
} = resolveConfig(pluginConfig, context);
const { assets, draftRelease } = resolveConfig(pluginConfig, context);
const { owner, repo } = parseGithubUrl(repositoryUrl);
const octokit = new Octokit(
toOctokitOptions({
githubToken,
githubUrl,
githubApiPathPrefix,
proxy,
})
);
const release = {
owner,
repo,
Expand Down
9 changes: 1 addition & 8 deletions lib/success.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import getReleaseLinks from "./get-release-links.js";

const debug = debugFactory("semantic-release:github");

export default async function success(pluginConfig, context, { Octokit }) {
export default async function success(pluginConfig, context, { octokit }) {
const {
options: { repositoryUrl },
commits,
Expand All @@ -24,21 +24,14 @@ export default async function success(pluginConfig, context, { Octokit }) {
logger,
} = context;
const {
githubToken,
githubUrl,
githubApiPathPrefix,
proxy,
successComment,
failComment,
failTitle,
releasedLabels,
addReleases,
} = resolveConfig(pluginConfig, context);

const octokit = new Octokit(
toOctokitOptions({ githubToken, githubUrl, githubApiPathPrefix, proxy })
);

// In case the repo changed name, get the new `repo`/`owner` as the search API will not follow redirects
const { data: repoData } = await octokit.request(
"GET /repos/{owner}/{repo}",
Expand Down
7 changes: 1 addition & 6 deletions lib/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import AggregateError from "aggregate-error";

import parseGithubUrl from "./parse-github-url.js";
import resolveConfig from "./resolve-config.js";
import { toOctokitOptions } from "./octokit.js";
import getError from "./get-error.js";

const isNonEmptyString = (value) => isString(value) && value.trim();
Expand Down Expand Up @@ -47,7 +46,7 @@ const VALIDATORS = {
draftRelease: isBoolean,
};

export default async function verify(pluginConfig, context, { Octokit }) {
export default async function verify(pluginConfig, context, { octokit }) {
const {
env,
options: { repositoryUrl },
Expand Down Expand Up @@ -83,10 +82,6 @@ export default async function verify(pluginConfig, context, { Octokit }) {
githubToken &&
!errors.find(({ code }) => code === "EINVALIDPROXY")
) {
const octokit = new Octokit(
toOctokitOptions({ githubToken, githubUrl, githubApiPathPrefix, proxy })
);

// https://github.com/semantic-release/github/issues/182
// Do not check for permissions in GitHub actions, as the provided token is an installation access token.
// octokit.request("GET /repos/{owner}/{repo}", {repo, owner}) does not return the "permissions" key in that case.
Expand Down

0 comments on commit e7f4f8f

Please sign in to comment.