generated from actions/typescript-action
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Akhil Mohan <akhil.mohan@mayadata.io>
- Loading branch information
Showing
7 changed files
with
214 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import * as release from '../src/release'; | ||
|
||
describe('isReleaseAlreadyExist', () => { | ||
it('multiple errors along with release already exists error', () => { | ||
const errorString = '{\n' + | ||
'"message": "Validation Failed",\n' + | ||
'"errors": [\n' + | ||
' {\n' + | ||
'"resource": "Release",\n' + | ||
'"code": "already_exists",\n' + | ||
'"field": "tag_name"\n' + | ||
'},\n' + | ||
' {\n' + | ||
'"resource": "Release",\n' + | ||
'"code": "invalid",\n' + | ||
'"field": "target_commitish"\n' + | ||
'}\n' + | ||
'],\n' + | ||
'"documentation_url": "https://docs.github.com/rest/reference/repos#create-a-release"\n' + | ||
'}' | ||
const errorObject = JSON.parse(errorString) | ||
expect(release.isReleaseAlreadyExist(errorObject)).toEqual(false); | ||
}); | ||
it('only release already exists error', () => { | ||
const errorString = '{\n' + | ||
'"message": "Validation Failed",\n' + | ||
'"errors": [\n' + | ||
' {\n' + | ||
'"resource": "Release",\n' + | ||
'"code": "already_exists",\n' + | ||
'"field": "tag_name"\n' + | ||
'}\n' + | ||
'],\n' + | ||
'"documentation_url": "https://docs.github.com/rest/reference/repos#create-a-release"\n' + | ||
'}' | ||
const errorObject = JSON.parse(errorString) | ||
expect(release.isReleaseAlreadyExist(errorObject)).toEqual(true); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,51 @@ | ||
import * as core from '@actions/core'; | ||
import csvparse from 'csv-parse/lib/sync'; | ||
import * as core from '@actions/core' | ||
import csvparse from 'csv-parse/lib/sync' | ||
|
||
export interface Inputs { | ||
tagName: string | ||
body: string; | ||
repositories: string[]; | ||
owner: string; | ||
githubToken: string; | ||
failFast: boolean; | ||
body: string | ||
repositories: string[] | ||
owner: string | ||
githubToken: string | ||
failFast: boolean | ||
} | ||
|
||
export async function getInputs(): Promise<Inputs> { | ||
return { | ||
tagName: getTag(), | ||
body: core.getInput('body'), | ||
repositories: await getRepositoryList(), | ||
owner: core.getInput('owner'), | ||
githubToken: core.getInput('github-token'), | ||
failFast: /true/i.test(core.getInput('fail-fast')) | ||
}; | ||
return { | ||
tagName: getTag(), | ||
body: core.getInput('body'), | ||
repositories: await getRepositoryList(), | ||
owner: core.getInput('owner'), | ||
githubToken: core.getInput('github-token'), | ||
failFast: /true/i.test(core.getInput('fail-fast')) | ||
} | ||
} | ||
|
||
export function getTag(): string { | ||
const tagName = core.getInput('tag-name'); | ||
// This removes the 'refs/tags' portion of the string if present, i.e. from 'refs/tags/v1.10.15' to 'v1.10.15' | ||
return tagName.replace('refs/tags/', ''); | ||
const tagName = core.getInput('tag-name') | ||
// This removes the 'refs/tags' portion of the string if present, i.e. from 'refs/tags/v1.10.15' to 'v1.10.15' | ||
return tagName.replace('refs/tags/', '') | ||
} | ||
|
||
export async function getRepositoryList(): Promise<string[]> { | ||
let res: Array<string> = []; | ||
const res: string[] = [] | ||
|
||
const items = core.getInput('repo'); | ||
if (items == '') { | ||
return res; | ||
} | ||
const items = core.getInput('repo') | ||
if (items == '') { | ||
return res | ||
} | ||
|
||
for (let output of (await csvparse(items, { | ||
columns: false, | ||
relaxColumnCount: true, | ||
skipLinesWithEmptyValues: true | ||
})) as Array<string[]>) { | ||
if (output.length == 1) { | ||
res.push(output[0]); | ||
} else { | ||
res.push(...output); | ||
} | ||
for (const output of (await csvparse(items, { | ||
columns: false, | ||
relaxColumnCount: true, | ||
skipLinesWithEmptyValues: true | ||
})) as string[][]) { | ||
if (output.length == 1) { | ||
res.push(output[0]) | ||
} else { | ||
res.push(...output) | ||
} | ||
} | ||
|
||
return res.filter(item => item).map(pat => pat.trim()); | ||
return res.filter(item => item).map(pat => pat.trim()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,14 @@ | ||
import * as core from '@actions/core'; | ||
import * as context from './context'; | ||
import {createRelease} from './release'; | ||
import * as core from '@actions/core' | ||
import * as context from './context' | ||
import {createRelease} from './release' | ||
|
||
async function run(): Promise<void> { | ||
try { | ||
const inputs: context.Inputs = await context.getInputs(); | ||
await createRelease(inputs); | ||
|
||
const inputs: context.Inputs = await context.getInputs() | ||
await createRelease(inputs) | ||
} catch (error) { | ||
core.setFailed(error.message); | ||
core.setFailed(error.message) | ||
} | ||
} | ||
|
||
|
||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,71 @@ | ||
import * as context from './context'; | ||
import * as github from '@actions/github'; | ||
import * as helper from './helper'; | ||
import * as core from '@actions/core'; | ||
import * as context from './context' | ||
import * as github from '@actions/github' | ||
import * as helper from './helper' | ||
import * as core from '@actions/core' | ||
|
||
export async function createRelease(ctx: context.Inputs) { | ||
const octokit = github.getOctokit(ctx.githubToken); | ||
const branchName = helper.getBranchName(ctx.tagName); | ||
const preRelease = helper.isRCBuild(ctx.tagName); | ||
const octokit = github.getOctokit(ctx.githubToken) | ||
const branchName = helper.getBranchName(ctx.tagName) | ||
const preRelease = helper.isRCBuild(ctx.tagName) | ||
|
||
let isSuccess = true | ||
let failureMessage = `Tagging failed for: ` | ||
|
||
for (const repo of ctx.repositories) { | ||
const result = await octokit.repos.createRelease({ | ||
owner: ctx.owner, | ||
repo: repo, | ||
tag_name: ctx.tagName, | ||
name: ctx.tagName, | ||
body: ctx.body, | ||
target_commitish: branchName, | ||
prerelease: preRelease, | ||
draft: false | ||
}); | ||
if (result.status != 201) { | ||
core.error(`Creating release failed for ${ctx.owner}/${repo}`); | ||
// when failFast is set, if tagging of one repository fails, all the further | ||
// repository tagging is cancelled | ||
if (ctx.failFast) { | ||
core.setFailed(`Aborting release tagging..`); | ||
try { | ||
const result = await octokit.repos.createRelease({ | ||
owner: ctx.owner, | ||
repo, | ||
tag_name: ctx.tagName, | ||
name: ctx.tagName, | ||
body: ctx.body, | ||
target_commitish: branchName, | ||
prerelease: preRelease, | ||
draft: false | ||
}) | ||
|
||
if (result.status === 201) { | ||
core.info(`Created release for ${ctx.owner}/${repo}`) | ||
} else { | ||
if (ctx.failFast) { | ||
core.setFailed( | ||
`Tagging failed for ${ctx.owner}/${repo}. Error: ${result}` | ||
) | ||
core.error(`Aborting tagging for further repositories`) | ||
return | ||
} else { | ||
isSuccess = false | ||
failureMessage = failureMessage.concat(`${repo}, `) | ||
} | ||
} | ||
} catch (error) { | ||
if (!isReleaseAlreadyExist(error)) { | ||
if (ctx.failFast) { | ||
core.setFailed( | ||
`Tagging failed for ${ctx.owner}/${repo}. Error: ${error}` | ||
) | ||
core.error(`Aborting tagging for further repositories`) | ||
return | ||
} else { | ||
isSuccess = false | ||
failureMessage = failureMessage.concat(`${repo}, `) | ||
} | ||
} else { | ||
core.info(`Release tag ${ctx.tagName} already exists for ${ctx.owner}/${repo}`) | ||
} | ||
} | ||
else { | ||
core.info(`Created release ${ctx.tagName} for ${ctx.owner}/${repo}`); | ||
} | ||
} | ||
|
||
if (!isSuccess) { | ||
core.setFailed(failureMessage) | ||
} | ||
} | ||
|
||
// checks if the release tag already exists | ||
export function isReleaseAlreadyExist(error: any): boolean { | ||
return ( | ||
error.errors.length === 1 && | ||
error.errors[0].resource === 'Release' && | ||
error.errors[0].code === 'already_exists' | ||
) | ||
} |