aboutsummarylogtreecommitdiffstats
path: root/script.js
blob: 1cf4997324804164fcd0834458de4873f4677e9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const { request } = require('@octokit/request')
const { stream: gotStream } = require('got')
const { createWriteStream } = require('fs')
const { pipeline } = require('stream/promises')
const { ParseOne: unzipOne } = require('unzipper')
const { writeFile } = require('fs/promises')
const { env } = process

const requestWithAuth = request.defaults({
  headers: {
    authorization: `token ${env.github_token}`
  },
  owner: 'FreeTubeApp',
  repo: 'FreeTube'
})

const f = async () => {
  let artifact_id = ''
  let name = 'freetube_0.18.0-nightly-3196_amd64.pacman.zip'
  let headSha = ''
  let build = ''
  let tag = ''
  let releaseTag = ''
  let workflowId = ''
  const res = await requestWithAuth('GET /repos/{owner}/{repo}/actions/artifacts')

  for (const artifact of res.data.artifacts) {
    if (artifact.name.endsWith('.pacman')) {
      artifact_id = artifact.id
      name = artifact.name
      headSha = artifact.workflow_run.head_sha
      workflowId = artifact.workflow_run.id
      build = name.split('-')[2].split('_')[0]
      tag = name.split('_')[1].split('-')[0]
      releaseTag = `${tag}.build${build}.${headSha.substring(0, 7)}`
      await writeFile('setenv.txt', `release_tag=${releaseTag}\nworkflow_id=${workflowId}\n`)
      break
    }
  }

  const dl = await requestWithAuth('GET /repos/{owner}/{repo}/actions/artifacts/{artifact_id}/zip', { artifact_id })

  await pipeline(
    gotStream(dl.url),
    unzipOne(),
    createWriteStream('freetube.pacman.tar.xz')
  )
}

f()