11import * as core from '@actions/core'
22import * as github from '@actions/github'
33import { exec } from '@actions/exec'
4- import { execSync } from 'child_process'
54import sqlite3 from 'sqlite3'
65import { open } from 'sqlite'
6+ import { nanoid } from 'nanoid'
77
8- const dbfile = 'github-archive.db '
8+ const dbfile = 'github-archive.sqlite '
99const events = [
1010 'issues' ,
1111 'issue_comment' ,
@@ -14,6 +14,9 @@ const events = [
1414 'pull_request_review_comment' ,
1515]
1616async function run ( ) : Promise < void > {
17+ const now = new Date ( ) . toISOString ( )
18+ const id = nanoid ( )
19+
1720 core . info (
1821 '[INFO] Usage https://github.com/githubocto/github-archive-action#readme'
1922 )
@@ -27,69 +30,63 @@ async function run(): Promise<void> {
2730 `${ username } @users.noreply.github.com` ,
2831 ] )
2932 core . debug ( 'Configured git user.name/user.email' )
33+ core . endGroup ( )
3034
31- // Create the oprhan github-meta branch if it doesn't exist
32- const branch = core . getInput ( 'branch' )
33- const branchExists = await exec ( 'git' , [
34- 'fetch' ,
35- 'origin' ,
36- branch ,
37- '--depth' ,
38- '1' ,
39- ] )
40-
41- if ( branchExists !== 0 ) {
42- core . info ( `No ${ branch } branch exists, creating...` )
43- await exec ( 'git' , [ 'checkout' , '--orphan' , branch ] )
44- await exec ( 'git' , [ 'rm' , '-rf' , '.' ] )
45- await exec ( 'git' , [
46- 'commit' ,
47- '--allow-empty' ,
48- '-m' ,
49- `Creating ${ branch } branch` ,
50- ] )
51- } else {
52- core . info ( `Checking out ${ branch } ` )
53- await exec ( 'git' , [ 'checkout' , '-t' , `origin/${ branch } ` ] )
35+ const eventName = github . context . eventName
36+ if ( ! events . includes ( eventName ) ) {
37+ throw new Error ( `Unsupported event type: ${ eventName } ` )
5438 }
5539
56- // open the database
57- const db = await open ( {
58- filename : dbfile ,
59- driver : sqlite3 . Database ,
60- } )
40+ // Actions can be triggered in parallel
41+ // As a result, several invocations of this code might be
42+ // executing right now.
43+ // We could figure out how to merge sqlite databases; there
44+ // is even some prior art in https://github.com/cannadayr/git-sqlite
45+ // The simple approach of "if our push is refused, pull and try again"
46+ // is probably going to be sufficient.
47+ while ( true ) {
48+ // open the database
49+ const db = await open ( {
50+ filename : dbfile ,
51+ driver : sqlite3 . Database ,
52+ } )
6153
62- // create tables if they don't exist
63- await db . run ( `
54+ // create tables if they don't exist
55+ await db . run ( `
6456 CREATE TABLE IF NOT EXISTS events (
65- id INTEGER PRIMARY KEY,
66- timestamp TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP ,
57+ id TEXT PRIMARY KEY,
58+ timestamp TEXT NOT NULL,
6759 kind TEXT NOT NULL,
6860 event TEXT NOT NULL
6961 );` )
70- core . endGroup ( )
7162
72- core . startGroup ( 'Capture event' )
73- for await ( const e of events ) {
74- core . debug ( `Checking for "${ e } " event...` )
75- if ( github . context . eventName !== e ) {
76- // not this event
77- continue
63+ await db . run (
64+ 'INSERT INTO events (id, timestamp, kind, event) values (:id, :timestamp, :e, :payload)' ,
65+ {
66+ ':id' : id ,
67+ ':timestamp' : now ,
68+ ':e' : eventName ,
69+ ':payload' : JSON . stringify ( github . context . payload ) ,
70+ }
71+ )
72+ await db . close ( )
73+ await exec ( 'git' , [ 'add' , dbfile ] )
74+ await exec ( 'git' , [
75+ 'commit' ,
76+ '-m' ,
77+ `Capturing event ${ eventName } (id: ${ id } )` ,
78+ ] )
79+ const code = await exec ( 'git' , [ 'push' ] )
80+ if ( code === 0 ) {
81+ // success! We're finished.
82+ core . info ( 'Success!' )
83+ break
84+ } else {
85+ core . info ( 'Retrying because of conflicts...' )
86+ await exec ( 'git' , [ 'reset' , '--hard' , 'HEAD' ] )
87+ await exec ( 'git' , [ 'pull' ] )
7888 }
79- await db . run ( 'INSERT INTO events (kind, event) values (:e, :payload)' , {
80- ':e' : e ,
81- ':payload' : JSON . stringify ( github . context . payload ) ,
82- } )
83- core . info ( `Captured ${ e } event` )
8489 }
85- core . endGroup ( )
86-
87- core . startGroup ( 'Commit and close db' )
88- await db . close ( )
89- await exec ( 'git' , [ 'add' , dbfile ] )
90- await exec ( 'git' , [ 'commit' , '-m' , 'Adding data to repo' ] )
91- await exec ( 'git' , [ 'push' , 'origin' , branch ] )
92- core . endGroup ( )
9390}
9491
9592run ( ) . catch ( error => {
0 commit comments