Skip to content

Commit

Permalink
Merge pull request #7 from kamaz/feat/tear-down-pr
Browse files Browse the repository at this point in the history
feat: tear down and inactive status
  • Loading branch information
kamaz authored Feb 23, 2020
2 parents a4dcbfb + 7d0a137 commit e47c9a9
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 34 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/tear-down.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: 'tear-down-test'
on: # rebuild any PRs and main branch changes
pull_request:
types:
- closed

jobs:
tear-down:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./
with:
token: ${{secrets.GITHUB_TOKEN}}
state: inactive
9 changes: 8 additions & 1 deletion __tests__/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
CreateDeploymentStatusResponse,
CreateDeploymentResponse,
CreateDeploymentParams,
CreateDeploymentStatusParams
CreateDeploymentStatusParams,
ListDeploymentsResponse
} from '../src/github-client'
import {Context} from '@actions/github/lib/context'

Expand Down Expand Up @@ -122,6 +123,12 @@ const createMockAppContext = (
status: 200
} as CreateDeploymentStatusResponse
},
async listDeployments(params) {
return {} as ListDeploymentsResponse
},
async request<T>(url: string) {
return {} as T
},
gitHubContext
}
}
Expand Down
64 changes: 52 additions & 12 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4507,20 +4507,50 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
Object.defineProperty(exports, "__esModule", { value: true });
const deployment_payload_1 = __webpack_require__(792);
const deployment_status_payload_1 = __webpack_require__(666);
const deployment_context_1 = __webpack_require__(157);
const getDeploymentId = (context) => {
return parseInt(context.getInput('deploymentId'), 10);
};
const isInactive = (context) => {
const state = context.getInput('state');
const deploymentId = getDeploymentId(context);
return state === 'inactive' && isNaN(deploymentId);
};
const createDeploymentStatus = (deploymentId, context) => __awaiter(void 0, void 0, void 0, function* () {
const deploymentStatusPayload = deployment_status_payload_1.createDeploymentStatusPayload(deploymentId, context);
const deploymentStatus = yield context.createDeploymentStatus(deploymentStatusPayload);
context.info(`Created deployment status: ${deploymentStatus.data.id}`);
});
const hasInactiveStatus = (status) => status.some(d => d.state === 'inactive');
exports.app = (context) => __awaiter(void 0, void 0, void 0, function* () {
try {
let deploymentId = parseInt(context.getInput('deploymentId'), 10);
context.info(`Deployment id ${deploymentId}`);
if (isNaN(deploymentId)) {
const deploymentPayload = deployment_payload_1.createDeploymentPayload(context);
const deployment = yield context.createDeployment(deploymentPayload);
deploymentId = deployment.data.id;
context.info(`Created deployment id: ${deploymentId}`);
}
const deploymentStatusPayload = deployment_status_payload_1.createDeploymentStatusPayload(deploymentId, context);
const deploymentStatus = yield context.createDeploymentStatus(deploymentStatusPayload);
context.info(`Created deployment status: ${deploymentStatus.data.id}`);
context.setOutput('deploymentId', `${deploymentId}`);
let deploymentId;
if (isInactive(context)) {
const { owner, repo, ref } = deployment_context_1.deploymentContext(context);
const deployments = yield context.listDeployments({
owner,
repo,
ref
});
for (const deployment of deployments.data) {
const status = yield context.request(deployment.statuses_url);
if (!hasInactiveStatus(status.data)) {
yield createDeploymentStatus(deployment.id, context);
}
}
}
else {
deploymentId = getDeploymentId(context);
context.info(`Deployment id ${deploymentId}`);
if (isNaN(deploymentId)) {
const deploymentPayload = deployment_payload_1.createDeploymentPayload(context);
const deployment = yield context.createDeployment(deploymentPayload);
deploymentId = deployment.data.id;
context.info(`Created deployment id: ${deploymentId}`);
}
yield createDeploymentStatus(deploymentId, context);
context.setOutput('deploymentId', `${deploymentId}`);
}
}
catch (error) {
context.error(error.message);
Expand Down Expand Up @@ -5020,6 +5050,16 @@ exports.createGitHubClient = (context) => {
return __awaiter(this, void 0, void 0, function* () {
return octokit.repos.createDeploymentStatus(deploymentStatus);
});
},
listDeployments(params) {
return __awaiter(this, void 0, void 0, function* () {
return octokit.repos.listDeployments(params);
});
},
request(url) {
return __awaiter(this, void 0, void 0, function* () {
return octokit.request(url);
});
}
};
};
Expand Down
85 changes: 65 additions & 20 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,83 @@
import {createDeploymentPayload} from './deployment-payload'
import {createDeploymentStatusPayload} from './deployment-status-payload'
import {
createDeploymentStatusPayload,
DeploymentState
} from './deployment-status-payload'
import {Octokit} from '@octokit/rest'
import {GitHubClient} from './github-client'
import {GitHubContext} from './github-context'
import {Context} from '@actions/github/lib/context'
import {deploymentContext} from './deployment-context'

export type AppContext = GitHubContext &
GitHubClient & {
gitHubContext: Context
}

const getDeploymentId = (context: AppContext): number => {
return parseInt(context.getInput('deploymentId'), 10)
}

const isInactive = (context: AppContext): boolean => {
const state = context.getInput('state') as DeploymentState
const deploymentId = getDeploymentId(context)

return state === 'inactive' && isNaN(deploymentId)
}

const createDeploymentStatus = async (
deploymentId: number,
context: AppContext
): Promise<void> => {
const deploymentStatusPayload = createDeploymentStatusPayload(
deploymentId,
context
)

const deploymentStatus = await context.createDeploymentStatus(
deploymentStatusPayload
)

context.info(`Created deployment status: ${deploymentStatus.data.id}`)
}

const hasInactiveStatus = (
status: Octokit.ReposListDeploymentStatusesResponse
): boolean => status.some(d => d.state === 'inactive')

export const app = async (context: AppContext): Promise<void> => {
try {
let deploymentId = parseInt(context.getInput('deploymentId'), 10)
context.info(`Deployment id ${deploymentId}`)

if (isNaN(deploymentId)) {
const deploymentPayload = createDeploymentPayload(context)
const deployment = await context.createDeployment(deploymentPayload)
deploymentId = deployment.data.id
context.info(`Created deployment id: ${deploymentId}`)
}

const deploymentStatusPayload = createDeploymentStatusPayload(
deploymentId,
context
)
let deploymentId: number
if (isInactive(context)) {
const {owner, repo, ref} = deploymentContext(context)
const deployments = await context.listDeployments({
owner,
repo,
ref
})

const deploymentStatus = await context.createDeploymentStatus(
deploymentStatusPayload
)
for (const deployment of deployments.data) {
const status = await context.request<
Octokit.ReposListDeploymentStatusesResponse
>(deployment.statuses_url)
if (!hasInactiveStatus(status.data)) {
await createDeploymentStatus(deployment.id, context)
}
}
} else {
deploymentId = getDeploymentId(context)
context.info(`Deployment id ${deploymentId}`)

context.info(`Created deployment status: ${deploymentStatus.data.id}`)
if (isNaN(deploymentId)) {
const deploymentPayload = createDeploymentPayload(context)
const deployment = await context.createDeployment(deploymentPayload)
deploymentId = deployment.data.id
context.info(`Created deployment id: ${deploymentId}`)
}

context.setOutput('deploymentId', `${deploymentId}`)
await createDeploymentStatus(deploymentId, context)
context.setOutput('deploymentId', `${deploymentId}`)
}
} catch (error) {
context.error(error.message)
context.setFailed(error.message)
Expand Down
2 changes: 1 addition & 1 deletion src/deployment-status-payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Octokit} from '@octokit/rest'
import {deploymentContext} from './deployment-context'
import {AppContext} from './app'

type DeploymentState = Octokit.ReposCreateDeploymentStatusParams['state']
export type DeploymentState = Octokit.ReposCreateDeploymentStatusParams['state']

const logUrl = (context: AppContext): string => {
const {type, number, owner, repo} = deploymentContext(context)
Expand Down
17 changes: 17 additions & 0 deletions src/github-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ Octokit.plugin([throttling, retry])

export type CreateDeploymentParams = Octokit.ReposCreateDeploymentParams
export type CreateDeploymentStatusParams = Octokit.ReposCreateDeploymentStatusParams
export type ListDeploymentsParam = Octokit.ReposListDeploymentsParams

export type CreateDeploymentResponse = Octokit.Response<
Octokit.ReposCreateDeploymentResponse
>

export type ListDeploymentsResponse = Octokit.Response<
Octokit.ReposListDeploymentsResponse
>

export type CreateDeployment = (
params: CreateDeploymentParams
) => Promise<CreateDeploymentResponse>
Expand All @@ -25,9 +30,15 @@ export type CreateDeploymentStatus = (
params: CreateDeploymentStatusParams
) => Promise<CreateDeploymentStatusResponse>

export type ListDeployments = (
params: ListDeploymentsParam
) => Promise<ListDeploymentsResponse>

export type GitHubClient = {
createDeployment: CreateDeployment
createDeploymentStatus: CreateDeploymentStatus
listDeployments: ListDeployments
request<T>(url: string): Promise<Octokit.Response<T>>
}

export const createGitHubClient = (context: GitHubContext): GitHubClient => {
Expand All @@ -39,6 +50,12 @@ export const createGitHubClient = (context: GitHubContext): GitHubClient => {
},
async createDeploymentStatus(deploymentStatus) {
return octokit.repos.createDeploymentStatus(deploymentStatus)
},
async listDeployments(params) {
return octokit.repos.listDeployments(params)
},
async request<T>(url: string) {
return (octokit.request(url) as unknown) as Octokit.Response<T>
}
}
}

0 comments on commit e47c9a9

Please sign in to comment.