Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(stryker): not authorized error in workflow #290

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
89c15cd
fix(stryker): not authorized error in workflow
stenjo Sep 16, 2024
3ef3db9
try without permissions
stenjo Sep 16, 2024
3fef91e
read-all
stenjo Sep 16, 2024
221177f
use github app for auth
stenjo Sep 17, 2024
c096a03
formatting
stenjo Sep 17, 2024
db97ffa
add owner
stenjo Sep 17, 2024
a5651cb
new action
stenjo Sep 17, 2024
c7832bf
ver
stenjo Sep 17, 2024
17afaeb
gist app token
stenjo Sep 17, 2024
0752b09
Fixup
stenjo Sep 17, 2024
d06ecb3
comment
stenjo Oct 9, 2024
86e418f
Merge branches '289-fixstryker-not-authorized-error-in-workflow' and …
stenjo Oct 9, 2024
9462bdc
wip
stenjo Oct 9, 2024
ec8cf37
wip
stenjo Oct 9, 2024
6a0b302
format
stenjo Oct 9, 2024
cf9cd4b
token
stenjo Oct 10, 2024
6978dc9
continue past badge generation
stenjo Oct 10, 2024
8208ba6
Update permissions and add badge generation for Stryker mutation score
stenjo Oct 10, 2024
2441097
Update permissions and streamline Stryker workflow
stenjo Oct 10, 2024
a9af9d5
Update permissions and streamline Stryker workflow
stenjo Oct 10, 2024
2af2e13
Checking token access
stenjo Oct 10, 2024
7a25d38
JWT check
stenjo Oct 10, 2024
ad6b4fa
Update Stryker workflow to check token access and add API call for gists
stenjo Oct 10, 2024
3cd3352
Update Stryker workflow to remove JWT debug and streamline token access
stenjo Oct 10, 2024
b96a79a
Update Stryker workflow to remove unnecessary permissions
stenjo Oct 10, 2024
0cb7d52
Update Stryker workflow to remove unnecessary permissions and streaml…
stenjo Oct 10, 2024
8706479
Update Stryker workflow to remove unnecessary permissions, streamline…
stenjo Oct 10, 2024
4c1f7f6
Update Stryker workflow to remove unnecessary transformIgnorePatterns…
stenjo Oct 10, 2024
b5be3b5
Update Stryker workflow to remove unnecessary transformIgnorePatterns…
stenjo Oct 10, 2024
2e16aee
build all
stenjo Oct 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions .github/workflows/stryker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ on:

permissions:
contents: write
issues: write
pull-requests: write
pages: write

jobs:
stryker-js:
Expand All @@ -34,16 +36,24 @@ jobs:
- name: Install dependencies
run: npm install

- uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ vars.GIST_APP_ID }}
private-key: ${{ secrets.GIST_APP_KEY }}
owner: ${{ github.repository_owner }}

- name: Get the user's personal access token
run: |
curl -H "Authorization: token ${{ steps.app-token.outputs.token }}" https://api.github.com/user
curl -H "Authorization: token ${{ steps.app-token.outputs.token }}" https://api.github.com/gists

- name: Run StrykerJS
continue-on-error: true
id: stryker
run: |
npm run stryker > out

- name: Print output
continue-on-error: true
run: cat out

- name: Save stryker JS log
if: failure()
continue-on-error: true
Expand All @@ -52,24 +62,45 @@ jobs:
name: stryker.log
path: stryker.log

- name: Print output
continue-on-error: true
run: cat out

- name: Extract stryker score
id: extract-stryker-score
run: |
grep "All files" out > line
awk '{print $4}' line > fullscore
awk '{split($0,a,"."); print a[1]}' fullscore > score
echo "stryker-score=$(tr -s '\n' < score)" >> "$GITHUB_ENV"

- name: Create the Badge for Deploy Rate
continue-on-error: true
uses: Schneegans/[email protected]
with:
auth: ${{ secrets.GIST_AUTH_TOKEN }}
auth: ${{ steps.app-token.outputs.token }}
gistID: 9ce1ad7d8e9db99796e782b244eefa4a
filename: dora-stryker.json
label: Stryker score
message: ${{ env.stryker-score }}%
label: Mutation Score
namedLogo: stryker
message: ${{ steps.extract-stryker-score.outputs.stryker-score }}%
minColorRange: 50
maxColorRange: 90
valColorRange: ${{ steps.extract-stryker-score.outputs.stryker-score }}

- name: Create the SVG Badge for Deploy Rate
continue-on-error: true
uses: Schneegans/[email protected]
with:
auth: ${{ steps.app-token.outputs.token }}
gistID: 9ce1ad7d8e9db99796e782b244eefa4a
filename: dora-stryker.svg
label: Mutation Score
namedLogo: stryker
message: ${{ steps.extract-stryker-score.outputs.stryker-score }}%
minColorRange: 50
maxColorRange: 90
valColorRange: ${{ env.stryker-score }}
valColorRange: ${{ steps.extract-stryker-score.outputs.stryker-score }}

- name: Upload results
uses: actions/upload-artifact@v4
Expand Down
129 changes: 0 additions & 129 deletions __tests__/CommitsAdapter.spec.ts

This file was deleted.

128 changes: 79 additions & 49 deletions __tests__/CommitsAdapter.test.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,96 @@
import {http, HttpResponse} from 'msw'
import {setupServer} from 'msw/node'
import {setFailed} from '@actions/core'
import {CommitsAdapter} from '../src/CommitsAdapter'
import fs from 'fs'
import {Commit} from '../src/types/Commit'
import type {Octokit} from '@octokit/rest'
import * as core from '@actions/core'
import {CommitsAdapter} from '../src/CommitsAdapter' // Adjust the import path as necessary

const commitsUrl =
'https://api.github.com/repos/stenjo/devops-metrics-action/pulls/69/commits'
const server = setupServer(
http.get(
commitsUrl,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
({request, params, cookies}) => {
const commits: Commit[] = JSON.parse(
fs.readFileSync('./__tests__/test-data/commits.json').toString()
) as Commit[]
return HttpResponse.json(commits)
}
)
)
jest.mock('@actions/core', () => ({
setFailed: jest.fn()
}))
jest.mock('@octokit/rest')
jest.mock('@actions/core')

describe('CommitsAdapter', () => {
let commitsAdapter: CommitsAdapter
let octokitMock: jest.Mocked<Octokit>

describe('Commit Adapter should', () => {
beforeEach(() => {
server.listen()
octokitMock = {
request: jest.fn()
} as unknown as jest.Mocked<Octokit>
commitsAdapter = new CommitsAdapter('fake-token')
commitsAdapter.octokit = octokitMock as never
})

afterEach(() => {
jest.clearAllMocks()
})

afterAll(() => server.close())
it('should return commits when the request is successful', async () => {
const mockCommits = [{sha: '123', commit: {message: 'test commit'}}]
octokitMock.request.mockResolvedValue({
data: mockCommits,
headers: {},
status: 200,
url: 'https://api.github.com/repos/user/repo/commits'
})

const result = await commitsAdapter.getCommitsFromUrl(
'https://api.github.com/repos/user/repo/commits'
)

it('ice breaker', () => {
expect(true).toBe(true)
expect(result).toEqual(mockCommits)
expect(octokitMock.request).toHaveBeenCalledWith(
'https://api.github.com/repos/user/repo/commits',
{
headers: {
'X-GitHub-Api-Version': '2022-11-28',
Authorization: 'token fake-token'
}
}
)
})

it('return unpaged values', async () => {
const r = new CommitsAdapter(undefined)
it('should throw an error and call core.setFailed when the request fails', async () => {
const errorMessage = 'Request failed'
octokitMock.request.mockRejectedValue(new Error(errorMessage))

const releases: Commit[] = (await r.getCommitsFromUrl(
commitsUrl
)) as Commit[]
await expect(
commitsAdapter.getCommitsFromUrl(
'https://api.github.com/repos/user/repo/commits'
)
).rejects.toThrow(errorMessage)
expect(core.setFailed).toHaveBeenCalledWith(errorMessage)
})

it('should throw an error and call core.setFailed when headers are incorrect', async () => {
const errorMessage = 'Bad headers'
octokitMock.request.mockRejectedValue(new Error(errorMessage))

expect(releases.length).toBe(30)
await expect(
commitsAdapter.getCommitsFromUrl(
'https://api.github.com/repos/user/repo/commits'
)
).rejects.toThrow(errorMessage)
expect(core.setFailed).toHaveBeenCalledWith(errorMessage)
})

it('handles access denied', async () => {
server.close()
const errorServer = setupServer(
http.get(
commitsUrl,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
({request, params, cookies}) => {
return new HttpResponse('Bad credentials', {status: 401})
it('should send the token as an authorization parameter', async () => {
const mockCommits = [{sha: '123', commit: {message: 'test commit'}}]
octokitMock.request.mockResolvedValue({
data: mockCommits,
headers: {},
status: 200,
url: 'https://api.github.com/repos/user/repo/commits'
})

await commitsAdapter.getCommitsFromUrl(
'https://api.github.com/repos/user/repo/commits'
)

expect(octokitMock.request).toHaveBeenCalledWith(
'https://api.github.com/repos/user/repo/commits',
{
headers: {
'X-GitHub-Api-Version': '2022-11-28',
Authorization: 'token fake-token'
}
)
}
)
errorServer.listen()
const r = new CommitsAdapter(commitsUrl)
const result = await r.getCommitsFromUrl(commitsUrl)
expect(result).toBe(undefined)
expect(setFailed).toHaveBeenCalledWith('Bad credentials')
errorServer.close()
})
})
9 changes: 6 additions & 3 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

Loading
Loading