Skip to content

chore: Update CI with new actions and change renovate schedule #25

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

Merged
merged 5 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 25 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!--
[For internal use] 
Copy the task id from the bottom of the sidebar and paste it after FTRACK-

Resolves FTRACK-

-->

- [ ] I have added automatic tests where applicable
- [ ] The PR title is suitable as a release note
- [ ] The PR contains a description of what has been changed
- [ ] The description contains manual test instructions

## Changes

<!--
What are you changing and what is the reason? If there are any UI changes, include a screenshot for the changes.
-->

## Test

<!--
How should this be tested? Include any requirements on other repositories or environment.
For bug fixes, include the original reproduction steps.
-->
121 changes: 121 additions & 0 deletions .github/scripts/ftrack-sync.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { v5 as uuid } from "uuid";

if (!process.env.FTRACK_API_KEY || !process.env.PR_JSON) {
console.error(`This script is intended to be run in CI only. To run locally for development, use:
FTRACK_API_KEY="[dev api key]" PR_JSON='{"url":"https://github.com/ftrackhq/frontend/pull/120","body":"Resolves FTRACK-c018c026-3599-11ed-8012-aab5768efa1e"}' node ftrack-sync.mjs
`);
process.exit(1);
}

const UUID_NAMESPACE = "1b671a64-40d5-491e-99b0-da01ff1f3341";

const requestHeaders = {
"ftrack-api-key": process.env.FTRACK_API_KEY,
"Content-Type": "application/json",
"Response-Type": "application/json",
"ftrack-user": "github_bot@ftrack.com",
"ftrack-bulk": "true",
};

function getTaskIdsAndNoteIdsFromBody(body, prUrl) {
const taskIds = Array.from(body.matchAll(/FTRACK-([\w\d-]+)/g)).map(
(match) => match[1]
);
// generate a unique id for each note based on PR.html_url and taskId
const uuids = taskIds.map((taskId) => ({
noteId: uuid(prUrl + taskId, UUID_NAMESPACE),
taskId,
}));

return uuids;
}

async function groupIntoExistingAndNewNoteIds(noteIds) {
const response = await (
await fetch("https://dev.ftrackapp.com/api", {
method: "POST",
headers: requestHeaders,
body: JSON.stringify([
{
action: "query",
expression: `select id, parent_id from Note where id in (${noteIds
.map(({ noteId }) => noteId)
.join(",")})`,
},
]),
})
).json();

try {
const existingIds = response[0].data.map((note) => ({
noteId: note.id,
taskId: note.parent_id,
}));
const newIds = noteIds.filter(
({ noteId }) =>
!existingIds
.map(({ noteId: existingNoteId }) => existingNoteId)
.includes(noteId)
);
return { existingIds, newIds };
} catch (error) {
console.error("Error fetching existing notes - response:", response);
throw error;
}
}

function getNoteRequestBody(action, prUrl, { noteId, taskId }) {
const linkDescription = prUrl.match(/\.com\/(.+)/)[1];

const content = `PR opened: [${linkDescription}](${prUrl})

Last change: ${new Date().toISOString().replace("T", " ").slice(0, -8)} GMT`;

return {
action,
entity_key: noteId,
entity_type: "Note",
entity_data: {
id: noteId,
parent_id: taskId,
content,
parent_type: "TypedContext",
user_id: "76a40852-359d-11ed-8012-aab5768efa1e",
},
};
}

export async function getNotesRequestBody(PR) {
if (!PR.body) return [];
const taskIds = getTaskIdsAndNoteIdsFromBody(PR.body, PR.html_url);
if (taskIds.length === 0) return [];
const { existingIds, newIds } = await groupIntoExistingAndNewNoteIds(taskIds);
return [
...newIds.map(getNoteRequestBody.bind(this, "create", PR.html_url)),
...existingIds.map(getNoteRequestBody.bind(this, "update", PR.html_url)),
];
}

const PR_JSON = JSON.parse(process.env.PR_JSON);
console.log("Input:", PR_JSON);
const notes = await getNotesRequestBody(PR_JSON);

if (notes.length === 0) {
console.log("Couldn't find any notes to update, exiting...");
process.exit(0);
}

console.log("Creating notes:", notes);

try {
const response = await (
await fetch("https://dev.ftrackapp.com/api", {
method: "POST",
headers: requestHeaders,
body: JSON.stringify(notes),
})
).json();
console.log("Response: ", response);
} catch (err) {
console.error(err);
}
33 changes: 33 additions & 0 deletions .github/workflows/check-pr-title.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Check PR title

permissions:
pull-requests: read

on:
pull_request:
types:
- opened
- edited
- synchronize

jobs:
main:
name: Validate PR title
runs-on: ubuntu-latest
steps:
- uses: amannn/action-semantic-pull-request@v4
with:
types: |
feat
fix
docs
style
refactor
perf
test
build
ci
chore
revert
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31 changes: 31 additions & 0 deletions .github/workflows/ftrack-sync.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: ftrack Sync

on:
pull_request:
types:
- opened
- edited
- synchronize

jobs:
main:
name: Sync PR
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: 8BitJonny/gh-get-current-pr@2.1.1
id: PR
with:
sha: ${{ github.event.pull_request.head.sha }}
- name: Use Node.js 18.x
uses: actions/setup-node@v3
with:
node-version: 18.x
- name: node ftrack-sync.mjs
run: |
cd .github/scripts
npm init -y && npm install uuid
node ftrack-sync.mjs
env:
FTRACK_API_KEY: ${{ secrets.FTRACK_API_KEY }}
PR_JSON: ${{ steps.PR.outputs.pr }}
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ coverage/
build/
dist/
.eggs/
renovate.json
13 changes: 10 additions & 3 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": ["config:base"],
"enabledManagers": ["npm"],
"rangeStrategy": "update-lockfile"
"extends": [
"config:base"
],
"enabledManagers": [
"npm"
],
"rangeStrategy": "update-lockfile",
"schedule": [
"every month on the first day of the month"
]
}