Skip to content

Commit

Permalink
CI内のスクリプトを別ファイルに分離する (#358)
Browse files Browse the repository at this point in the history
* CI内のスクリプトを別ファイルに分離する

* super-linterによる指摘事項修正
  • Loading branch information
massongit authored Nov 3, 2022
1 parent 6d70580 commit 15a0f90
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 50 deletions.
57 changes: 7 additions & 50 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,21 @@ runs:
- name: Increment version
id: increment_version
uses: actions/[email protected]
env:
SHA: ${{ github.sha }}
with:
github-token: ${{inputs.github-token}}
result-encoding: string
script: |
let latestReleaseVersion = '';
try {
const getLatestReleaseParams = {
owner: context.repo.owner,
repo: context.repo.repo
};
console.log("call repos.getLatestRelease:", getLatestReleaseParams);
const latestRelease = await github.rest.repos.getLatestRelease(getLatestReleaseParams);
latestReleaseVersion = latestRelease.data.tag_name;
} catch (e) {
if (e.status === 404) {
latestReleaseVersion = 'v0.0.0';
} else {
throw e;
}
}
const listPullRequestsAssociatedWithCommitParams = {
owner: context.repo.owner,
repo: context.repo.repo,
commit_sha: '${{ github.sha }}',
};
console.log("call repos.listPullRequestsAssociatedWithCommit:", listPullRequestsAssociatedWithCommitParams);
const pulls = await github.paginate(
github.rest.repos.listPullRequestsAssociatedWithCommit,
listPullRequestsAssociatedWithCommitParams
);
const labels = pulls.flatMap(p => p.labels.map(l => l.name));
const tagNames = latestReleaseVersion.split('.');
let version = [];
if (labels.includes('major release')) {
version = [`v${Number(tagNames[0].replace('v', '')) + 1}`, 0, 0];
} else if (labels.includes('minor release')) {
version = [tagNames[0], Number(tagNames[1]) + 1, 0];
} else {
version = [tagNames[0], tagNames[1], Number(tagNames[2]) + 1];
}
return version.join('.');
const script = require('${{ github.action_path }}/scripts/action/increment_version.js')
return await script({github, context})
- name: Create release
uses: actions/[email protected]
env:
GITHUB_REF: ${{env.GITHUB_REF}}
TAG_NAME: ${{ steps.increment_version.outputs.result }}
with:
github-token: ${{inputs.github-token}}
script: |
const createReleaseParams = {
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: '${{ steps.increment_version.outputs.result }}',
target_commitish: process.env['GITHUB_REF'],
generate_release_notes: true
};
console.log("call repos.createRelease:", createReleaseParams);
await github.rest.repos.createRelease(createReleaseParams);
const script = require('${{ github.action_path }}/scripts/action/create_release.js')
return await script({github, context})
11 changes: 11 additions & 0 deletions scripts/action/create_release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = async ({ github, context }) => {
const createReleaseParams = {
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: process.env.TAG_NAME,
target_commitish: process.env.GITHUB_REF,
generate_release_notes: true
}
console.log('call repos.createRelease:', createReleaseParams)
await github.rest.repos.createRelease(createReleaseParams)
}
43 changes: 43 additions & 0 deletions scripts/action/increment_version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module.exports = async ({ github, context }) => {
let latestReleaseVersion = ''

try {
const getLatestReleaseParams = {
owner: context.repo.owner,
repo: context.repo.repo
}
console.log('call repos.getLatestRelease:', getLatestReleaseParams)
const latestRelease = await github.rest.repos.getLatestRelease(getLatestReleaseParams)
latestReleaseVersion = latestRelease.data.tag_name
} catch (e) {
if (e.status === 404) {
latestReleaseVersion = 'v0.0.0'
} else {
throw e
}
}

const listPullRequestsAssociatedWithCommitParams = {
owner: context.repo.owner,
repo: context.repo.repo,
commit_sha: process.env.SHA
}
console.log('call repos.listPullRequestsAssociatedWithCommit:', listPullRequestsAssociatedWithCommitParams)
const pulls = await github.paginate(
github.rest.repos.listPullRequestsAssociatedWithCommit,
listPullRequestsAssociatedWithCommitParams
)
const labels = pulls.flatMap(p => p.labels.map(l => l.name))
const tagNames = latestReleaseVersion.split('.')
let version

if (labels.includes('major release')) {
version = [`v${Number(tagNames[0].replace('v', '')) + 1}`, 0, 0]
} else if (labels.includes('minor release')) {
version = [tagNames[0], Number(tagNames[1]) + 1, 0]
} else {
version = [tagNames[0], tagNames[1], Number(tagNames[2]) + 1]
}

return version.join('.')
}

0 comments on commit 15a0f90

Please sign in to comment.