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 = () => {
// THEN run everything in linked mode
if (fs.existsSync(toolkitDirectory)) {
const tkStat = fs.lstatSync(toolkitDirectory);
if (fs.existsSync(`${process.env['HOME']}/.config/yarn/link/@grafana/toolkit`) && tkStat.isSymbolicLink()) {
console.log('Running in linked mode');
if (tkStat.isSymbolicLink()) {
console.log('Running in linked mode', `${__dirname}/grafana-toolkit.js`);
return `${__dirname}/grafana-toolkit.js`;
}
}
......
......@@ -10,11 +10,11 @@ import path = require('path');
import execa = require('execa');
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 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;
};
......@@ -138,14 +138,14 @@ const prepareRelease = useSpinner<any>('Preparing release', async ({ dryrun, ver
interface GithubPublishReleaseOptions {
commitHash?: string;
githubToken: string;
githubEmail: string;
githubUser: string;
gitRepoName: string;
}
const createRelease = useSpinner<GithubPublishReleaseOptions>(
'Creating release',
async ({ commitHash, githubEmail, githubToken, gitRepoName }) => {
const gitRelease = new GitHubRelease(githubToken, githubEmail, gitRepoName, await releaseNotes(), commitHash);
async ({ commitHash, githubUser, githubToken, gitRepoName }) => {
const gitRelease = new GitHubRelease(githubToken, githubUser, gitRepoName, await releaseNotes(), commitHash);
return gitRelease.release();
}
);
......@@ -159,10 +159,16 @@ export interface GithubPublishOptions {
const githubPublishRunner: TaskRunner<GithubPublishOptions> = async ({ dryrun, verbose, commitHash }) => {
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(
'The release plugin requires you specify the repository url as environment variable CIRCLE_REPOSITORY_URL'
);
}
}
if (!process.env['GITHUB_ACCESS_TOKEN']) {
// Try to use GITHUB_TOKEN, which may be set.
......@@ -183,7 +189,7 @@ const githubPublishRunner: TaskRunner<GithubPublishOptions> = async ({ dryrun, v
const parsedUrl = gitUrlParse(process.env['CIRCLE_REPOSITORY_URL']);
const githubToken = process.env['GITHUB_ACCESS_TOKEN'];
const githubEmail = process.env['GITHUB_USERNAME'];
const githubUser = parsedUrl.owner;
await prepareRelease({
dryrun,
......@@ -192,7 +198,7 @@ const githubPublishRunner: TaskRunner<GithubPublishOptions> = async ({ dryrun, v
await createRelease({
commitHash,
githubEmail,
githubUser,
githubToken,
gitRepoName: parsedUrl.name,
});
......
......@@ -9,6 +9,9 @@ import GithubClient from './githubClient';
import { AxiosResponse } from 'axios';
const resolveContentType = (extension: string): string => {
if (extension.startsWith('.')) {
extension = extension.substr(1);
}
switch (extension) {
case 'zip':
return 'application/zip';
......@@ -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.
fs.readdir(srcLocation, (err: NodeJS.ErrnoException | null, files: string[]) => {
if (err) {
throw err;
}
const files = fs.readdirSync(srcLocation);
files.forEach(async (file: string) => {
return files.map(async (file: string) => {
const fileStat = fs.statSync(`${srcLocation}/${file}`);
const fileData = fs.readFileSync(`${srcLocation}/${file}`);
try {
await this.git.client.post(`${destUrl}?name=${file}`, fileData, {
return this.git.client.post(`${destUrl}?name=${file}`, fileData, {
headers: {
'Content-Type': resolveContentType(path.extname(file)),
'Content-Length': fileStat.size,
},
});
} catch (reason) {
console.log('Could not post', reason);
}
});
});
}
......@@ -76,7 +71,7 @@ class GitHubRelease {
const commitHash = this.commitHash || pluginInfo.build?.hash;
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
if (latestRelease.data.tag_name === `v${pluginInfo.version}`) {
......@@ -93,12 +88,13 @@ class GitHubRelease {
prerelease: false,
});
this.publishAssets(
const publishPromises = this.publishAssets(
PUBLISH_DIR,
`https://uploads.github.com/repos/${this.username}/${this.repository}/releases/${newReleaseResponse.data.id}/assets`
);
await Promise.all(publishPromises);
} 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
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