Skip to content

Commit

Permalink
chore: add better lock messages
Browse files Browse the repository at this point in the history
  • Loading branch information
FabiLo22 committed Nov 27, 2024
1 parent 124732f commit 7267504
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 40 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ inputs:
outputs:
deployment_lock:
description: 'If a lock is active, it returns the ID.'
lock_msg:
description: 'Message that should be send to slack.'
github_pr:
description: 'PR that set the lock.'

runs:
using: node20
Expand Down
62 changes: 45 additions & 17 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "typescript-action",
"description": "Givve Automatic Deployment",
"version": "v0.1.8",
"version": "v0.1.9",
"author": "PL Gutscheinsysteme GmbH",
"private": true,
"homepage": "https://github.com/givve/github_deployment_action",
Expand Down
15 changes: 15 additions & 0 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ export class GitHub {
})
}

async getPRUrl(): Promise<string> {
return new Promise(async (resolve, reject) => {
const { data: issue, error: error } = await this.requestWithAuth(
'GET /repos/{owner}/{repo}/issues/{pull_number}',
{
owner: 'givve',
repo: 'givve',
pull_number: core.getInput('pull_request')
}
)

resolve(issue.html_url)
})
}

async getLabels(): Promise<string[]> {
return new Promise(async (resolve, reject) => {
const { data: issue, error: error } = await this.requestWithAuth(
Expand Down
31 changes: 17 additions & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as core from '@actions/core'
import { checkOrWait } from './wait.js'
import { getLocks, setLock } from './semaphore.js'
import { check, checkOrWait } from './wait.js'
import { getLock, getLocks, setLock } from './semaphore.js'
import { GitHub } from './github.js'
import * as _ from 'lodash'

Expand All @@ -19,20 +19,33 @@ export async function run(): Promise<void> {
if (core.getInput('pull_request') != '') {
const github = new GitHub()
await github.performAuth()

const labels = await github.getLabels()
const url = await github.getPRUrl()

if (
!_.includes(labels, 'auto deploy') &&
_.includes(labels, 'manual deploy')
) {
let lock = await getLock()
let lock = await getLock(component + '_deploy')
// No lock, we need to lock deployment
if (!lock) {
const { data } = await setLock(component)
const { data } = await setLock(component, url)
lock = data.data
core.setOutput(
'lock_msg',
'Manual deployment enabled! Automatic deployment disabled!'
)
} else {
core.setOutput(
'lock_msg',
'Deployment already locked! Please check PR!'
)
}

// Manual deployment, so deployment is not permitted
core.setOutput('deployment_lock', lock.id)
core.setOutput('github_pr', url)
}
} else {
await checkOrWait()
Expand All @@ -42,13 +55,3 @@ export async function run(): Promise<void> {
if (error instanceof Error) core.setFailed(error.message)
}
}

export async function getLock() {
// Manual deployment, check locks
const locks = (await getLocks(component)).data.data

return _.find(locks, {
component: component,
unlocked_by: null
})
}
15 changes: 13 additions & 2 deletions src/semaphore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const axios = require('axios')
import * as core from '@actions/core'
import _ from 'lodash'

// TEMP: Will be implemented via action inputs
const semaphoreAPI = core.getInput('semaphoreAPI')
Expand All @@ -16,14 +17,14 @@ export async function getLocks(component: string): Promise<any> {
)
}

export async function setLock(component: string): Promise<any> {
export async function setLock(component: string, url: string): Promise<any> {
return axios.post(
semaphoreAPI +
`/api/products/5f7427d977b4b64aeabad92d/components/${component}/locks`,
{
lock: {
created_by: 'github_action',
purpose: 'manual deployment lock',
purpose: url,
expires_at: null
}
},
Expand All @@ -34,3 +35,13 @@ export async function setLock(component: string): Promise<any> {
}
)
}

export async function getLock(component: string) {
// Manual deployment, check locks
const locks = (await getLocks(component)).data.data

return _.find(locks, {
component: component,
unlocked_by: null
})
}
17 changes: 12 additions & 5 deletions src/wait.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import { getLock } from './main'
import * as core from '@actions/core'
import { getLock } from './semaphore'

export async function checkOrWait(): Promise<string> {
return new Promise(async (resolve, reject) => {
await check(resolve, reject)
})
}

async function check(resolve: any, reject: any) {
const lock = await getLock()
export async function check(resolve: any, reject: any) {
const component = core.getInput('component')

const lock = await getLock(component + '_deploy')
const capistranoLock = await getLock(component)

if (!lock) {
resolve('Done!')
} else {
if (lock.purpose === 'manual deployment lock') {
if (lock) {
// Some other deployment is running, so we wait
core.setOutput('deployment_lock', lock.id)
core.setOutput('github_pr', lock.purpose)
core.setOutput('lock_msg', 'Automatic deployment locked by manual lock.')
reject('Locked')
} else {
} else if (capistranoLock) {
setTimeout(() => {
check(resolve, reject)
}, 20000)
} else {
reject('Locked')
}
}
}

0 comments on commit 7267504

Please sign in to comment.