Skip to content

Commit

Permalink
feat: [#38] Assign proper IDs to Jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
yeraydavidrodriguez committed May 16, 2022
1 parent ee9dd80 commit 9ccfeb8
Show file tree
Hide file tree
Showing 17 changed files with 239 additions and 162 deletions.
134 changes: 67 additions & 67 deletions __tests__/e2e/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,47 @@ function executeAction(env): string | Buffer {
return output
}

function createJob(gitRepo: GitRepo): string | Buffer {
const env = {
function createSampleJob(gitRepo: GitRepo, payload: String): string | Buffer {
return executeAction({
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'create-job',
INPUT_JOB_PAYLOAD: dummyPayload(),
INPUT_GIT_COMMIT_NO_GPG_SIGN: true
}
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true',
INPUT_JOB_PAYLOAD: payload
})
}

function nextSampleJob(gitRepo: GitRepo): string | Buffer {
return executeAction({
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'next-job',
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true'
})
}

return executeAction(env)
function startSampleJob(gitRepo: GitRepo, payload: String): string | Buffer {
return executeAction({
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'start-job',
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true',
INPUT_JOB_PAYLOAD: payload
})
}

function finishSampleJob(gitRepo: GitRepo, payload: String): string | Buffer {
return executeAction({
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'finish-job',
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true',
INPUT_JOB_PAYLOAD: payload
})
}

/**
Expand All @@ -72,16 +102,7 @@ describe('GitHub Action', () => {
it('should print the git repo dir at the beginning of the action execution', async () => {
const gitRepo = await createInitializedGitRepo()

const env = {
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'next-job',
INPUT_JOB_PAYLOAD: dummyPayload(),
INPUT_GIT_COMMIT_NO_GPG_SIGN: true
}

const output = executeAction(env)
const output = nextSampleJob(gitRepo)

expect(output).toEqual(
expect.stringContaining(`git_repo_dir: ${gitRepo.getDirPath()}`)
Expand Down Expand Up @@ -112,16 +133,7 @@ describe('GitHub Action', () => {
it('should create a new job', async () => {
const gitRepo = await createInitializedGitRepo()

const env = {
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'create-job',
INPUT_JOB_PAYLOAD: dummyPayload(),
INPUT_GIT_COMMIT_NO_GPG_SIGN: true
}

const output = executeAction(env)
const output = createSampleJob(gitRepo, dummyPayload())

expect(getOutputVariable('job_created', output.toString())).toBe('true')
expect(getOutputVariable('job_commit', output.toString())).toBe(
Expand All @@ -132,17 +144,8 @@ describe('GitHub Action', () => {
it('should get the next job', async () => {
const gitRepo = await createInitializedGitRepo()

createJob(gitRepo)

const env = {
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'next-job',
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true'
}

const output = executeAction(env)
createSampleJob(gitRepo, dummyPayload())
const output = nextSampleJob(gitRepo)

expect(getOutputVariable('job_payload', output.toString())).toBe(
dummyPayload()
Expand All @@ -155,18 +158,9 @@ describe('GitHub Action', () => {
it('should mark the pending job as started', async () => {
const gitRepo = await createInitializedGitRepo()

createJob(gitRepo)
createSampleJob(gitRepo, dummyPayload())

const env = {
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'start-job',
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true',
INPUT_JOB_PAYLOAD: 'test'
}

const output = executeAction(env)
const output = startSampleJob(gitRepo, dummyPayload())

expect(getOutputVariable('job_started', output.toString())).toBe('true')
expect(getOutputVariable('job_commit', output.toString())).toBe(
Expand All @@ -177,32 +171,38 @@ describe('GitHub Action', () => {
it('should mark the pending job as finished', async () => {
const gitRepo = await createInitializedGitRepo()

createJob(gitRepo)

executeAction({
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'start-job',
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true',
INPUT_JOB_PAYLOAD: 'test'
})

const output = executeAction({
...process.env,
INPUT_QUEUE_NAME: 'queue-name',
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
INPUT_ACTION: 'finish-job',
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true',
INPUT_JOB_PAYLOAD: 'test'
})
createSampleJob(gitRepo, dummyPayload())
startSampleJob(gitRepo, 'test')
const output = finishSampleJob(gitRepo, 'test')

expect(getOutputVariable('job_finished', output.toString())).toBe('true')
expect(getOutputVariable('job_commit', output.toString())).toBe(
getLatestCommitHash(gitRepo.getDir()).getHash()
)
})

it('should assign job id 0 to the first created job', async () => {
const gitRepo = await createInitializedGitRepo()

const output = createSampleJob(gitRepo, dummyPayload())

expect(getOutputVariable('job_id', output.toString())).toBe('0')
})

it('should assign job id 1 to the second created job', async () => {
const gitRepo = await createInitializedGitRepo()

const firstOutput = createSampleJob(gitRepo, dummyPayload())

expect(getOutputVariable('job_id', firstOutput.toString())).toBe('0')
startSampleJob(gitRepo, dummyPayload())
finishSampleJob(gitRepo, dummyPayload())

const output = createSampleJob(gitRepo, dummyPayload())

expect(getOutputVariable('job_id', output.toString())).toBe('1')
})

it('should allow to overwrite commit signing key', async () => {
const gitRepo = await createInitializedGitRepo()
const gnuPGHomeDir = await createInitializedTempGnuPGHomeDir()
Expand Down
2 changes: 1 addition & 1 deletion __tests__/unit/commit-body.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('CommitBody', () => {

it('could be built from a Message', () => {
const commitBody = CommitBody.fromMessage(
new NewJobMessage('message-payload')
new NewJobMessage('message-payload', 0)
)

const builtBody = JSON.stringify({
Expand Down
27 changes: 26 additions & 1 deletion __tests__/unit/commit-subject-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,35 @@ describe('CommitSubjectParser', () => {
})

it('should return a Null Commit when the job reference does not exist', () => {
const parser = new CommitSubjectParser('📝🈺: queue-name: NAME job.id.1 ')
const parser = new CommitSubjectParser('📝🈺: queue-name: job.id.1 ')
const jobRef = parser.getJobRef()

expect(jobRef).toBeInstanceOf(CommitHash)
expect(jobRef.isNull()).toBe(true)
})

it('should fail when the job Id does not exist', () => {
const fn = (): number => {
const parser = new CommitSubjectParser(
'📝🈺: queue-name: job.ref.f1a69d48a01cc130a64aeac5eaf762e4ba685de7'
)
return parser.getJobId()
}

expect(fn).toThrowError()
})

it('should parse the job id from a commit subject', () => {
const parser = new CommitSubjectParser(
'📝🈺: queue-name: job.id.42 job.ref.f1a69d48a01cc130a64aeac5eaf762e4ba685de7'
)

expect(parser.getJobId().toString()).toBe('42')
})

it('should parse the job id from a commit subject when there is no job ref', () => {
const parser = new CommitSubjectParser('📝🈺: queue-name: job.id.42')

expect(parser.getJobId().toString()).toBe('42')
})
})
2 changes: 1 addition & 1 deletion __tests__/unit/commit-subject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('CommitSubject', () => {
)

const commit3 = CommitSubjectParser.parseText(
'standard commit: not queue commit-no prefix'
'📝🈺: other-queue: not queue job.id.1 commit-no prefix'
)
expect(commit3.belongsToQueue(new QueueName('standard commit'))).toBe(false)
})
Expand Down
12 changes: 4 additions & 8 deletions __tests__/unit/job.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {CommitHash} from '../../src/commit-hash'
import {CommitInfo} from '../../src/commit-info'
import {Job} from '../../src/job'
import {Job, nullJob} from '../../src/job'
import {NewJobCommittedMessage} from '../../src/committed-message'
import {dummyCommitBodyText} from '../../src/__tests__/helpers'

Expand Down Expand Up @@ -35,13 +35,9 @@ describe('Job', () => {
})

it('should be nullable', () => {
const job = new Job(
'payload',
new CommitHash('a362802b98c78df052a78796a1a7cde60a5c1faf'),
0
)
const job = nullJob()

expect(job.getPayload()).toBe('payload')
expect(job.isNull()).toBe(true)
})

it('should compare two jobs', () => {
Expand Down Expand Up @@ -72,7 +68,7 @@ describe('Job', () => {
const commitInfo = new CommitInfo(
new CommitHash('ad5cea6308f69d7955d8de5f0da19f675d5ba75f'),
'date',
'message',
'📝🈺: queue-name: job.id.1 job.ref.f1a69d48a01cc130a64aeac5eaf762e4ba685de7',
'refs',
dummyCommitBodyText(),
'author name',
Expand Down
10 changes: 5 additions & 5 deletions __tests__/unit/message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@ import {CommitHash} from '../../src/commit-hash'

describe('Message', () => {
it('should have a payload', () => {
const message = new NewJobMessage('payload')
const message = new NewJobMessage('payload', 0)
expect(message.getPayload()).toBe('payload')
})

it('could have an optional reference (commit hash) to another job', () => {
const hash = new CommitHash('f1a69d48a01cc130a64aeac5eaf762e4ba685de7')

const message = new JobFinishedMessage('payload', hash)
const message = new JobFinishedMessage('payload', 0, hash)

expect(message.getJobRef().equalsTo(hash)).toBe(true)
})

it('should allow to omit the job reference', () => {
const message = new NewJobMessage('payload')
const message = new NewJobMessage('payload', 0)

expect(message.getJobRef().isNull()).toBe(true)
})

it('should indicate if it has a job ref', () => {
const messageWithoutJobRef = new NewJobMessage('payload')
const messageWithoutJobRef = new NewJobMessage('payload', 0)
expect(messageWithoutJobRef.hasJobRef()).toBe(false)

const hash = new CommitHash('f1a69d48a01cc130a64aeac5eaf762e4ba685de7')
const messageWithJobRef = new JobFinishedMessage('payload', hash)
const messageWithJobRef = new JobFinishedMessage('payload', 0, hash)
expect(messageWithJobRef.hasJobRef()).toBe(true)
})
})
25 changes: 14 additions & 11 deletions __tests__/unit/queue.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {Job, NO_JOB_ID} from '../../src/job'
import {
createInitializedGitRepo,
createInitializedTempGnuPGHomeDir,
Expand All @@ -12,6 +11,7 @@ import {CommitAuthor} from '../../src/commit-author'
import {CommitInfo} from '../../src/commit-info'
import {CommitOptions} from '../../src/commit-options'
import {GitRepo} from '../../src/git-repo'
import {Job} from '../../src/job'
import {Queue} from '../../src/queue'
import {QueueName} from '../../src/queue-name'
import {SigningKeyId} from '../../src/signing-key-id'
Expand Down Expand Up @@ -274,9 +274,9 @@ describe('Queue', () => {
const nextJob = queue.getNextJob()

const latestCommit = getLatestCommitHash(queue.getGitRepoDir())
expect(
nextJob.equalsTo(new Job(dummyPayload(), latestCommit, NO_JOB_ID))
).toBe(true)
expect(nextJob.equalsTo(new Job(dummyPayload(), latestCommit, 1))).toBe(
true
)
})
})

Expand All @@ -299,13 +299,16 @@ describe('Queue', () => {
const nextJob2 = queue2.getNextJob()

const newJob1Commit = getSecondToLatestCommitHash(queue1.getGitRepoDir())
expect(nextJob1.equalsTo(new Job(payload1, newJob1Commit, NO_JOB_ID))).toBe(
true
)
expect(nextJob1.equalsTo(new Job(payload1, newJob1Commit, 0))).toBe(true)

const latestCommit = getLatestCommitHash(queue2.getGitRepoDir())
expect(nextJob2.equalsTo(new Job(payload2, latestCommit, NO_JOB_ID))).toBe(
true
)
let latestCommit = getLatestCommitHash(queue2.getGitRepoDir())
expect(nextJob2.equalsTo(new Job(payload2, latestCommit, 0))).toBe(true)

await queue1.markJobAsStarted(payload1)
await queue1.markJobAsFinished(payload1)
await queue1.createJob(payload2)
const nextJob3 = queue1.getNextJob()
latestCommit = getLatestCommitHash(queue1.getGitRepoDir())
expect(nextJob3.equalsTo(new Job(payload2, latestCommit, 1))).toBe(true)
})
})
Loading

0 comments on commit 9ccfeb8

Please sign in to comment.