Commit 13975335 by Stephanie Closson Committed by GitHub

Fixes for toolkit (#23379)

1. Typo in the default email address
2. Nested promises causing problems in some situations. Return a promise
and resolve in the outer function
3. If extension still have '.' in front remove it prior to matching.
parent 7f13b446
...@@ -10,8 +10,8 @@ entrypoint = () => { ...@@ -10,8 +10,8 @@ entrypoint = () => {
// THEN run everything in linked mode // THEN run everything in linked mode
if (fs.existsSync(toolkitDirectory)) { if (fs.existsSync(toolkitDirectory)) {
const tkStat = fs.lstatSync(toolkitDirectory); const tkStat = fs.lstatSync(toolkitDirectory);
if (fs.existsSync(`${process.env['HOME']}/.config/yarn/link/@grafana/toolkit`) && tkStat.isSymbolicLink()) { if (tkStat.isSymbolicLink()) {
console.log('Running in linked mode'); console.log('Running in linked mode', `${__dirname}/grafana-toolkit.js`);
return `${__dirname}/grafana-toolkit.js`; return `${__dirname}/grafana-toolkit.js`;
} }
} }
......
...@@ -10,11 +10,11 @@ import path = require('path'); ...@@ -10,11 +10,11 @@ import path = require('path');
import execa = require('execa'); import execa = require('execa');
interface Command extends Array<any> {} interface Command extends Array<any> {}
const DEFAULT_EMAIL_ADDRESS = 'eng@graafna.com'; const DEFAULT_EMAIL_ADDRESS = 'eng@grafana.com';
const DEFAULT_USERNAME = 'CircleCI Automation'; const DEFAULT_USERNAME = 'CircleCI Automation';
const releaseNotes = async (): Promise<string> => { const releaseNotes = async (): Promise<string> => {
const { stdout } = await execa.shell(`awk \'BEGIN {FS="##"; RS=""} FNR==3 {print; exit}\' CHANGELOG.md`); const { stdout } = await execa.shell(`awk 'BEGIN {FS="##"; RS="##"} FNR==3 {print "##" $1; exit}' CHANGELOG.md`);
return stdout; return stdout;
}; };
...@@ -138,14 +138,14 @@ const prepareRelease = useSpinner<any>('Preparing release', async ({ dryrun, ver ...@@ -138,14 +138,14 @@ const prepareRelease = useSpinner<any>('Preparing release', async ({ dryrun, ver
interface GithubPublishReleaseOptions { interface GithubPublishReleaseOptions {
commitHash?: string; commitHash?: string;
githubToken: string; githubToken: string;
githubEmail: string; githubUser: string;
gitRepoName: string; gitRepoName: string;
} }
const createRelease = useSpinner<GithubPublishReleaseOptions>( const createRelease = useSpinner<GithubPublishReleaseOptions>(
'Creating release', 'Creating release',
async ({ commitHash, githubEmail, githubToken, gitRepoName }) => { async ({ commitHash, githubUser, githubToken, gitRepoName }) => {
const gitRelease = new GitHubRelease(githubToken, githubEmail, gitRepoName, await releaseNotes(), commitHash); const gitRelease = new GitHubRelease(githubToken, githubUser, gitRepoName, await releaseNotes(), commitHash);
return gitRelease.release(); return gitRelease.release();
} }
); );
...@@ -159,10 +159,16 @@ export interface GithubPublishOptions { ...@@ -159,10 +159,16 @@ export interface GithubPublishOptions {
const githubPublishRunner: TaskRunner<GithubPublishOptions> = async ({ dryrun, verbose, commitHash }) => { const githubPublishRunner: TaskRunner<GithubPublishOptions> = async ({ dryrun, verbose, commitHash }) => {
if (!process.env['CIRCLE_REPOSITORY_URL']) { if (!process.env['CIRCLE_REPOSITORY_URL']) {
// Try and figure it out
const repo = await execa('git', ['config', '--local', 'remote.origin.url']);
if (repo && repo.stdout) {
process.env.CIRCLE_REPOSITORY_URL = repo.stdout;
} else {
throw new Error( throw new Error(
'The release plugin requires you specify the repository url as environment variable CIRCLE_REPOSITORY_URL' 'The release plugin requires you specify the repository url as environment variable CIRCLE_REPOSITORY_URL'
); );
} }
}
if (!process.env['GITHUB_ACCESS_TOKEN']) { if (!process.env['GITHUB_ACCESS_TOKEN']) {
// Try to use GITHUB_TOKEN, which may be set. // Try to use GITHUB_TOKEN, which may be set.
...@@ -183,7 +189,7 @@ const githubPublishRunner: TaskRunner<GithubPublishOptions> = async ({ dryrun, v ...@@ -183,7 +189,7 @@ const githubPublishRunner: TaskRunner<GithubPublishOptions> = async ({ dryrun, v
const parsedUrl = gitUrlParse(process.env['CIRCLE_REPOSITORY_URL']); const parsedUrl = gitUrlParse(process.env['CIRCLE_REPOSITORY_URL']);
const githubToken = process.env['GITHUB_ACCESS_TOKEN']; const githubToken = process.env['GITHUB_ACCESS_TOKEN'];
const githubEmail = process.env['GITHUB_USERNAME']; const githubUser = parsedUrl.owner;
await prepareRelease({ await prepareRelease({
dryrun, dryrun,
...@@ -192,7 +198,7 @@ const githubPublishRunner: TaskRunner<GithubPublishOptions> = async ({ dryrun, v ...@@ -192,7 +198,7 @@ const githubPublishRunner: TaskRunner<GithubPublishOptions> = async ({ dryrun, v
await createRelease({ await createRelease({
commitHash, commitHash,
githubEmail, githubUser,
githubToken, githubToken,
gitRepoName: parsedUrl.name, gitRepoName: parsedUrl.name,
}); });
......
...@@ -9,6 +9,9 @@ import GithubClient from './githubClient'; ...@@ -9,6 +9,9 @@ import GithubClient from './githubClient';
import { AxiosResponse } from 'axios'; import { AxiosResponse } from 'axios';
const resolveContentType = (extension: string): string => { const resolveContentType = (extension: string): string => {
if (extension.startsWith('.')) {
extension = extension.substr(1);
}
switch (extension) { switch (extension) {
case 'zip': case 'zip':
return 'application/zip'; return 'application/zip';
...@@ -42,27 +45,19 @@ class GitHubRelease { ...@@ -42,27 +45,19 @@ class GitHubRelease {
}); });
} }
async publishAssets(srcLocation: string, destUrl: string) { publishAssets(srcLocation: string, destUrl: string) {
// Add the assets. Loop through files in the ci/dist folder and upload each asset. // Add the assets. Loop through files in the ci/dist folder and upload each asset.
fs.readdir(srcLocation, (err: NodeJS.ErrnoException | null, files: string[]) => { const files = fs.readdirSync(srcLocation);
if (err) {
throw err;
}
files.forEach(async (file: string) => { return files.map(async (file: string) => {
const fileStat = fs.statSync(`${srcLocation}/${file}`); const fileStat = fs.statSync(`${srcLocation}/${file}`);
const fileData = fs.readFileSync(`${srcLocation}/${file}`); const fileData = fs.readFileSync(`${srcLocation}/${file}`);
try { return this.git.client.post(`${destUrl}?name=${file}`, fileData, {
await this.git.client.post(`${destUrl}?name=${file}`, fileData, {
headers: { headers: {
'Content-Type': resolveContentType(path.extname(file)), 'Content-Type': resolveContentType(path.extname(file)),
'Content-Length': fileStat.size, 'Content-Length': fileStat.size,
}, },
}); });
} catch (reason) {
console.log('Could not post', reason);
}
});
}); });
} }
...@@ -76,7 +71,7 @@ class GitHubRelease { ...@@ -76,7 +71,7 @@ class GitHubRelease {
const commitHash = this.commitHash || pluginInfo.build?.hash; const commitHash = this.commitHash || pluginInfo.build?.hash;
try { try {
const latestRelease: AxiosResponse<any> = await this.git.client.get('releases/latest'); const latestRelease: AxiosResponse<any> = await this.git.client.get(`releases/tags/v${pluginInfo.version}`);
// Re-release if the version is the same as an existing release // Re-release if the version is the same as an existing release
if (latestRelease.data.tag_name === `v${pluginInfo.version}`) { if (latestRelease.data.tag_name === `v${pluginInfo.version}`) {
...@@ -93,12 +88,13 @@ class GitHubRelease { ...@@ -93,12 +88,13 @@ class GitHubRelease {
prerelease: false, prerelease: false,
}); });
this.publishAssets( const publishPromises = this.publishAssets(
PUBLISH_DIR, PUBLISH_DIR,
`https://uploads.github.com/repos/${this.username}/${this.repository}/releases/${newReleaseResponse.data.id}/assets` `https://uploads.github.com/repos/${this.username}/${this.repository}/releases/${newReleaseResponse.data.id}/assets`
); );
await Promise.all(publishPromises);
} catch (reason) { } catch (reason) {
console.error(reason.data?.message ?? reason); console.error(reason.data?.message ?? reason.response.data ?? reason);
// Rethrow the error so that we can trigger a non-zero exit code to circle-ci // Rethrow the error so that we can trigger a non-zero exit code to circle-ci
throw reason; throw reason;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment