Skip to content

Commit

Permalink
feat: Call make webhook on stable label added
Browse files Browse the repository at this point in the history
  • Loading branch information
rlch committed Feb 16, 2024
1 parent b0207b4 commit 0483a9d
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 46 deletions.
163 changes: 146 additions & 17 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"dependencies": {
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1",
"axios": "^1.4.0"
"axios": "^1.4.0",
"node-fetch": "^3.3.2"
},
"devDependencies": {
"@types/axios": "^0.14.0",
Expand Down
58 changes: 39 additions & 19 deletions src/core/github.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,54 @@
import * as github from '@actions/github'
import {GithubEvent} from '../types'
import fetch from 'node-fetch'

export default class Github {
export default class Action {
private octokit
private ghEvent: GithubEvent
readonly event: GithubEvent

constructor(token: string, jsonEvent: string) {
this.octokit = github.getOctokit(token)
const event = JSON.parse(jsonEvent)
this.ghEvent = {
owner: github.context.repo.owner,
repoName: github.context.repo.repo,
pr: event.pull_request.number,
title: event.pull_request.title,
branch: event.pull_request.head.ref
this.event = JSON.parse(jsonEvent)
}

async run(): Promise<void> {
if (this.event.label?.name == 'Released on @stable') {
await this.markAsReleased()
} else {
await this.addPrefixToPRTitle()
}
}
get githubEvent(): GithubEvent {
return this.ghEvent

// Mark the issue/task as released by invoking the release webhook.
private async markAsReleased(): Promise<void> {
const res = await fetch(
'https://hook.eu1.make.com/158u6i515f3xc12c2ah1uk0uyvirkdg4',
{
method: 'POST',
body: JSON.stringify({
id: this.event.pull_request.head.ref
})
}
)
if (!res.ok) {
throw new Error(await res.text())
}
}
async addPrefixToPRTitle(prefix: string): Promise<void> {

// Lets Notion manage the state of the issue/task by adding the [NOTION-ID] prefix to the PR title.
private async addPrefixToPRTitle(): Promise<void> {
const title = this.event.pull_request.title
const prefix = `[${this.event.pull_request.head.ref}]`
if (
!this.ghEvent.title.includes(prefix) &&
!this.ghEvent.title.includes('ISSUE-') &&
!this.ghEvent.title.includes('TASK-')
!title.includes(prefix) &&
!title.includes('ISSUE-') &&
!title.includes('TASK-')
) {
const newTitle = prefix + this.ghEvent.title
const newTitle = prefix + this.event.pull_request.title
await this.octokit.rest.pulls.update({
owner: this.ghEvent.owner,
pull_number: this.ghEvent.pr,
repo: this.ghEvent.repoName,
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: this.event.pull_request.number,
title: newTitle
})
}
Expand Down
6 changes: 2 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as core from '@actions/core'
import Github from './core/github'
import Action from './core/github'

async function run(): Promise<void> {
try {
Expand All @@ -10,9 +10,7 @@ async function run(): Promise<void> {
required: true
})

const gh = new Github(token, event)
const prefix = `[${gh.githubEvent.branch}]`
await gh.addPrefixToPRTitle(prefix)
await new Action(token, event).run()
} catch (error) {
core.info(JSON.stringify(error))
if (error instanceof Error) core.setFailed(error.message)
Expand Down
18 changes: 13 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
export type GithubEvent = {
owner: string
repoName: string
pr: number
title: string
branch: string
label:
| {
name: string
}
| undefined
pull_request: {
number: number
title: string
head: {
// Branch
ref: string
}
}
}

0 comments on commit 0483a9d

Please sign in to comment.