@@ -6,17 +6,14 @@ import process from 'node:process'
6
6
import * as core from '@actions/core'
7
7
import * as github from '@actions/github'
8
8
import isCI from 'is-ci'
9
- import { MongoClient } from 'mongodb'
10
9
import { getChangedActivities } from '../util/getActivities.js'
11
10
import { getFolderLetter } from '../util/getFolderLetter.js'
12
11
import { exit , MESSAGES , success } from '../util/log.js'
13
12
import { sanitazeFolderName } from '../util/sanitazeFolderName.js'
14
13
import { buildActivity } from './build/buildActivity.js'
15
14
16
- const NAME = 'pmd/release'
17
-
18
- interface DbData {
19
- name : string
15
+ interface ActivityData {
16
+ service : string
20
17
apiVersion ?: number
21
18
githubUrl : string
22
19
folderName : string
@@ -42,11 +39,13 @@ export async function release() {
42
39
return exit ( MESSAGES . noToken )
43
40
}
44
41
45
- const mongoUrl = process . env . MONGO_URL
46
- if ( ! mongoUrl ) {
47
- return exit ( MESSAGES . noMongoUrl )
42
+ const apiKey = process . env . ADMIN_API_KEY
43
+ if ( ! apiKey ) {
44
+ return exit ( 'No Admin API key provided' )
48
45
}
49
46
47
+ const apiUrl = process . env . API_URL || 'https://api.premid.app/v6'
48
+
50
49
const { changed, deleted } = await getChangedActivities ( )
51
50
52
51
core . info ( `Found ${ changed . length } changed activities, ${ deleted . length } deleted activities` )
@@ -56,34 +55,33 @@ export async function release() {
56
55
return success ( MESSAGES . noActivities )
57
56
}
58
57
59
- const client = new MongoClient ( mongoUrl , { appName : NAME } )
60
-
61
- try {
62
- await client . connect ( )
63
-
64
- core . info ( 'Connected to MongoDB' )
65
- }
66
- catch ( error ) {
67
- core . debug ( error as string )
68
- exit ( MESSAGES . noMongoConnection )
69
- }
70
-
71
- const connectToDev = process . env . CONNECT_TO_DEV === 'true'
72
- const database = client . db ( connectToDev ? 'PreMiD-DEV' : 'PreMiD' )
73
- const collection = database . collection < DbData > ( 'presences' )
74
-
75
58
if ( deleted . length ) {
76
- await collection . deleteMany ( {
77
- $or : deleted . map ( activity => ( {
78
- folderName : sanitazeFolderName ( activity . metadata . service ) ,
79
- apiVersion : activity . versionized ? activity . metadata . apiVersion : undefined ,
80
- } ) ) ,
81
- } )
59
+ for ( const activity of deleted ) {
60
+ const folderName = sanitazeFolderName ( activity . metadata . service )
61
+ const apiVersion = activity . versionized ? `/v${ activity . metadata . apiVersion } ` : ''
62
+
63
+ try {
64
+ const response = await fetch ( `${ apiUrl } /activities${ apiVersion } /${ encodeURIComponent ( folderName ) } ` , {
65
+ method : 'DELETE' ,
66
+ headers : {
67
+ 'Authorization' : `${ apiKey } ` ,
68
+ 'Content-Type' : 'application/json' ,
69
+ } ,
70
+ } )
71
+
72
+ if ( ! response . ok ) {
73
+ core . warning ( `Failed to delete activity ${ activity . metadata . service } : ${ response . statusText } ` )
74
+ }
75
+ }
76
+ catch ( error ) {
77
+ core . warning ( `Error deleting activity ${ activity . metadata . service } : ${ error } ` )
78
+ }
79
+ }
82
80
83
81
core . info ( `Deleted ${ deleted . length } activities` )
84
82
}
85
83
86
- const dbData : DbData [ ] = [ ]
84
+ let successCount = 0
87
85
88
86
for ( const activity of changed ) {
89
87
await buildActivity ( {
@@ -101,35 +99,40 @@ export async function release() {
101
99
const folderNameEncoded = encodeURIComponent ( folderName )
102
100
const apiVersion = activity . versionized ? `/v${ activity . metadata . apiVersion } ` : ''
103
101
104
- dbData . push ( {
105
- name : activity . metadata . service ,
102
+ const activityData : ActivityData = {
103
+ service : activity . metadata . service ,
106
104
apiVersion : activity . versionized ? activity . metadata . apiVersion : undefined ,
107
105
metadata : activity . metadata ,
108
106
folderName,
109
107
githubUrl : `https://github.com/PreMiD/Activities/tree/main/websites/${ folderLetter } /${ folderNameEncoded } ${ apiVersion } ` ,
110
- url : `https://api.premid.app/v6 /activities${ apiVersion } /${ folderNameEncoded } ` ,
108
+ url : `${ apiUrl } /activities${ apiVersion } /${ folderNameEncoded } ` ,
111
109
presenceJs : await readFile ( resolve ( activity . folder , 'dist' , 'presence.js' ) , 'utf8' ) ,
112
110
...( existsSync ( resolve ( activity . folder , 'dist' , 'iframe.js' ) ) && {
113
111
iframeJs : await readFile ( resolve ( activity . folder , 'dist' , 'iframe.js' ) , 'utf8' ) ,
114
112
} ) ,
115
- } )
116
- }
117
-
118
- if ( dbData . length ) {
119
- await collection . bulkWrite (
120
- dbData . map ( data => ( {
121
- updateOne : {
122
- filter : { name : data . name , apiVersion : data . apiVersion } ,
123
- update : {
124
- $set : data ,
125
- } ,
126
- upsert : true ,
113
+ }
114
+
115
+ try {
116
+ const response = await fetch ( `${ apiUrl } /activities` , {
117
+ method : 'POST' ,
118
+ headers : {
119
+ 'Authorization' : `${ apiKey } ` ,
120
+ 'Content-Type' : 'application/json' ,
127
121
} ,
128
- } ) ) ,
129
- )
130
-
131
- core . info ( `Updated ${ dbData . length } activities` )
122
+ body : JSON . stringify ( activityData ) ,
123
+ } )
124
+
125
+ if ( response . ok ) {
126
+ successCount ++
127
+ }
128
+ else {
129
+ core . warning ( `Failed to update activity ${ activity . metadata . service } : ${ response . statusText } ` )
130
+ }
131
+ }
132
+ catch ( error ) {
133
+ core . warning ( `Error updating activity ${ activity . metadata . service } : ${ error } ` )
134
+ }
132
135
}
133
136
134
- await client . close ( )
137
+ core . info ( `Successfully updated ${ successCount } activities` )
135
138
}
0 commit comments